package com.ruoyi.web.controller.common;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SendSmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.common.constant.CommonConstants.LOGIN_USER_SMS;


/**
 * @Author: tjf
 * @Date: 2024/03/05 17:15
 * @Describe:
 */
@RestController
@RequestMapping("/sendSms")
public class SendSmsController {
    @Autowired
    private RedisCache redisCache;

    /**
     * 发送登录短信接口
     * @return
     */
    @PostMapping("/sendLoginSms")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    public AjaxResult sendLoginSms(@RequestBody LoginBody user) {
        String code = SendSmsUtils.getCode(4);
        String username = user.getUsername();
        redisCache.setCacheObject(LOGIN_USER_SMS + username, code);
        String msg = SendSmsUtils.sendPassword(code, username);
        System.out.println(username+"登录短信发送回复："+msg);
        return AjaxResult.success();
    }
}