hjl
2024-07-02 242725f795b4cca830421c07f714a3ec36af0add
ruoyi-service/ruoyi-admin/src/main/java/com/ruoyi/admin/service/impl/SendSmsServiceImpl.java
@@ -2,6 +2,7 @@
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.admin.service.SendSmsService;
import com.ruoyi.admin.utils.SendSmsUtil;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.RedisConstants;
import com.ruoyi.common.core.exception.GlobalException;
@@ -21,23 +22,28 @@
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private SendSmsUtil sendSmsUtil;
    @Override
    public String workerSettleSms(String phone) {
        return sendPhoneCode(phone, RedisConstants.WORKER_SETTLE_KEY, Constants.FIVE, TimeUnit.MINUTES);
        return sendPhoneCode(phone, RedisConstants.WORKER_SETTLE_KEY,
                sendSmsUtil.applyId);
    }
    @Override
    public String workerLoginSms(String phone) {
        return sendPhoneCode(phone, RedisConstants.WORKER_APPLY_KEY, Constants.FIVE, TimeUnit.MINUTES);
        return sendPhoneCode(phone, RedisConstants.WORKER_APPLY_KEY,
                sendSmsUtil.loginId);
    }
    @Override
    public String userLoginSms(String phone) {
        return sendPhoneCode(phone, RedisConstants.USER_LOGIN_PHONE_CODE, Constants.FIVE, TimeUnit.MINUTES);
        return sendPhoneCode(phone, RedisConstants.USER_LOGIN_PHONE_CODE,
                sendSmsUtil.loginId);
    }
    private String sendPhoneCode(String phone, String redisKey, Integer timeout, TimeUnit timeUnit) {
    private String sendPhoneCode(String phone, String redisKey, String templateId) {
        // 生成随机 6位数字 验证码
        String phoneCode = RandomUtil.randomNumbers(6);
        // 判断redis中是否存在手机验证码
@@ -60,10 +66,13 @@
         * key为 --> phone_code:手机号码 (phone_code表示该业务为 验证码登录)
         * value为 --> 随机验证码:时间戳 (时间戳用于计算是否超过1分钟的重发时间)
         */
        redisTemplate.opsForValue().set(redisKey + phone, phoneCode + ":" + System.currentTimeMillis(), timeout, timeUnit);
        redisTemplate.opsForValue().set(redisKey + phone, phoneCode + ":" + System.currentTimeMillis(),
                Constants.FIVE, TimeUnit.MINUTES);
        String sendMessage = "验证码发送成功,您的验证码为:" + phoneCode + ",该验证码三分钟内有效,请及时完成登陆";
        // todo 发送此消息
        System.out.println(sendMessage);
        String[] param = {phoneCode};
        // 发送验证码
        sendSmsUtil.sendSms(new SendSmsUtil.SendSmsRequest(phone, templateId, param));
        return phoneCode;
    }