无关风月
2025-03-21 6903c41af2520b23809ca90e9a211e738de2c224
ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TUserServiceImpl.java
@@ -10,7 +10,9 @@
import com.ruoyi.study.dto.AppUserQuery;
import com.ruoyi.study.mapper.TUserMapper;
import com.ruoyi.study.service.ITUserService;
import com.ruoyi.study.utils.HWSendSms;
import com.ruoyi.study.vo.AppUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -18,6 +20,8 @@
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * <p>
@@ -40,12 +44,24 @@
        return this.baseMapper.listAll(query);
    }
    @Autowired
    private HWSendSms hwSendSms;
    @Override
    public Boolean phoneCode(String phone) {
    public Boolean phoneCode(String phone) throws Exception {
        // 生成随机 6位数字 验证码
        String phoneCode = RandomUtil.randomNumbers(6);
        // todo 手机验证码暂时 123456
        phoneCode = "123456";
        String regex = Constants.PHONE;
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        // 创建Matcher对象
        Matcher matcher = pattern.matcher(phone);
        // 判断是否匹配
        if (!matcher.matches()) {
            throw new GlobalException("手机号不合法!");
        }
        hwSendSms.sendSms(phoneCode, phone);
        // 判断redis中是否存在手机验证码
        Object phoneCodeRedis = redisTemplate.opsForValue().get(RedisConstants.PHONE_CODE + phone);
        if (phoneCodeRedis == null) {
@@ -66,7 +82,7 @@
         * key为 --> phone_code:手机号码 (phone_code表示该业务为 验证码登录)
         * value为 --> 随机验证码:时间戳 (时间戳用于计算是否超过1分钟的重发时间)
         */
        redisTemplate.opsForValue().set(RedisConstants.PHONE_CODE + phone, phoneCode + ":" + System.currentTimeMillis(), 3, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(RedisConstants.PHONE_CODE + phone, phoneCode + ":" + System.currentTimeMillis(), 5, TimeUnit.MINUTES);
        String sendMessage = "验证码发送成功,您的验证码为:" + phoneCode + ",该验证码三分钟内有效,请及时完成登陆";
        // todo 发送此消息
        System.out.println(sendMessage);
@@ -86,4 +102,9 @@
        return null != vipEndTime && System.currentTimeMillis() <= vipEndTime.getTime();
    }
    @Override
    public void updateOne(Integer id, Date vipEndTime,Date vipPayTime) {
        this.baseMapper.updateOne(id,vipPayTime,vipPayTime);
    }
}