无关风月
2024-12-11 4d7a208f388e42e7dd83dab0e38eadfa0847de1c
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/CodeController.java
@@ -19,6 +19,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@@ -28,7 +31,7 @@
    @Autowired
    private RedisService redisService;
    @Autowired
    @Resource
    private AppUserClient appUserClient;
    @Autowired
    private TChargingPileNotificationService chargingPileNotificationService;
@@ -41,11 +44,36 @@
     */
    @ApiOperation(value = "获取验证码",notes = "获取验证码",tags = {"更换手机号获取验证码"})
    @GetMapping("/getCode")
    public AjaxResult getCode(@RequestParam("phone") String phone)
    {
    public AjaxResult getCode(@RequestParam("phone") String phone) {
        String regex = "^1[3-9]\\d{9}$";
        if(!phone.matches(regex)){
            return AjaxResult.error("无效的手机号");
        }
        Map<String, Object> cacheMap = redisService.getCacheMap("sms_code_" + phone);
        if(null == cacheMap || cacheMap.size() == 0){
            cacheMap = new HashMap<>();
            cacheMap.put("timestamp", System.currentTimeMillis());
            cacheMap.put("time", 1);
            redisService.setCacheMap("sms_code_" + phone, cacheMap, 300);
        }else{
            Integer time = (Integer) cacheMap.get("time");
            Long timestamp = (Long) cacheMap.get("timestamp");
            if(System.currentTimeMillis() - timestamp > 60000){
                cacheMap.put("timestamp", System.currentTimeMillis());
                cacheMap.put("time", 1);
                redisService.setCacheMap("sms_code_" + phone, cacheMap, 300);
            } else if(System.currentTimeMillis() - timestamp <= 60000 && time < 3){
                time++;
                cacheMap.put("time", time);
                redisService.setCacheMap("sms_code_" + phone, cacheMap, 300);
            }else if(System.currentTimeMillis() - timestamp < 60000 && time >= 3){
                return AjaxResult.error("请求太频繁,请稍后再试!");
            }
        }
        TAppUser appUser = appUserClient.selectByPhone(phone).getData();
        if (Objects.nonNull(appUser)){
            return AjaxResult.error("该手机号已绑定账号");
            return AjaxResult.error();
        }
        String code = MsgUtil.createCode();
        redisService.setCacheObject(phone+ Constants.UPDATE_PHONE,code,5L, TimeUnit.MINUTES);
@@ -69,8 +97,33 @@
     */
    @ApiOperation(value = "获取验证码",notes = "获取验证码",tags = {"申请建桩获取验证码"})
    @GetMapping("/getApplyCode")
    public AjaxResult getApplyCode(@RequestParam("phone") String phone)
    {
    public AjaxResult getApplyCode(@RequestParam("phone") String phone) {
        String regex = "^1[3-9]\\d{9}$";
        if(!phone.matches(regex)){
            return AjaxResult.error("无效的手机号");
        }
        Map<String, Object> cacheMap = redisService.getCacheMap("sms_code_" + phone);
        if(null == cacheMap || cacheMap.size() == 0){
            cacheMap = new HashMap<>();
            cacheMap.put("timestamp", System.currentTimeMillis());
            cacheMap.put("time", 1);
            redisService.setCacheMap("sms_code_" + phone, cacheMap, 300);
        }else{
            Integer time = (Integer) cacheMap.get("time");
            Long timestamp = (Long) cacheMap.get("timestamp");
            if(System.currentTimeMillis() - timestamp > 60000){
                cacheMap.put("timestamp", System.currentTimeMillis());
                cacheMap.put("time", 1);
                redisService.setCacheMap("sms_code_" + phone, cacheMap, 300);
            } else if(System.currentTimeMillis() - timestamp <= 60000 && time < 3){
                time++;
                cacheMap.put("time", time);
                redisService.setCacheMap("sms_code_" + phone, cacheMap, 300);
            }else if(System.currentTimeMillis() - timestamp < 60000 && time >= 3){
                return AjaxResult.error("请求太频繁,请稍后再试!");
            }
        }
        String code = MsgUtil.createCode();
        redisService.setCacheObject(phone+ Constants.APPLY_CHARGING,code,5L, TimeUnit.MINUTES);
        SendMessageDTO sendMessageDTO = new SendMessageDTO();