From 3244b550596e0330031b3f4547356927df83b0ad Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期一, 19 五月 2025 11:48:35 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/CodeController.java |   99 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 84 insertions(+), 15 deletions(-)

diff --git a/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/CodeController.java b/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/CodeController.java
index 0eaf607..1fd839d 100644
--- a/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/CodeController.java
+++ b/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/CodeController.java
@@ -1,5 +1,6 @@
 package com.ruoyi.chargingPile.controller;
 
+import com.ruoyi.account.api.dto.SendMessageDTO;
 import com.ruoyi.account.api.feignClient.AppUserClient;
 import com.ruoyi.account.api.model.TAppUser;
 import com.ruoyi.chargingPile.api.model.TChargingPile;
@@ -10,19 +11,27 @@
 import com.ruoyi.common.core.utils.MsgUtil;
 import com.ruoyi.common.core.web.domain.AjaxResult;
 import com.ruoyi.common.redis.service.RedisService;
+import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
 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;
 
+@RestController
+@RequestMapping("/code")
 public class CodeController {
 
     @Autowired
     private RedisService redisService;
-    @Autowired
+    @Resource
     private AppUserClient appUserClient;
     @Autowired
     private TChargingPileNotificationService chargingPileNotificationService;
@@ -34,19 +43,49 @@
      * @return 结果
      */
     @ApiOperation(value = "获取验证码",notes = "获取验证码",tags = {"更换手机号获取验证码"})
-    @GetMapping("getCode")
-    public AjaxResult getCode(@RequestParam("phone") String phone)
-    {
+    @GetMapping("/getCode")
+    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);
-        String reqStr = MsgUtil.codeMsg(phone, code);
-        String result = HttpUtils.post(MsgConstants.SEND_URL, reqStr);
+        SendMessageDTO sendMessageDTO = new SendMessageDTO();
+        sendMessageDTO.setPhone(phone);
+        sendMessageDTO.setCode(code);
+        sendMessageDTO.setType(1);
+        String result = appUserClient.sendMessage(sendMessageDTO).getData();
+//        String reqStr = MsgUtil.codeMsg(phone, code);
+//        String result = HttpUtils.post(MsgConstants.SEND_URL, reqStr);
         // 记录短信发送
-        chargingPileNotificationService.saveData(1,null,null,phone,"验证码:"+code+",用于更换手机号。请勿转发。");
+        chargingPileNotificationService.saveData(1,-1,-1,phone,"验证码:"+code+",用于更换手机号。请勿转发。");
         return AjaxResult.success(result);
     }
 
@@ -56,16 +95,46 @@
      * @param phone 手机号
      * @return 结果
      */
-    @ApiOperation(value = "获取验证码",notes = "获取验证码",tags = {"申请建桩"})
-    @GetMapping("getApplyCode")
-    public AjaxResult getApplyCode(@RequestParam("phone") String phone)
-    {
+    @ApiOperation(value = "获取验证码",notes = "获取验证码",tags = {"申请建桩获取验证码"})
+    @GetMapping("/getApplyCode")
+    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);
-        String reqStr = MsgUtil.applyCodeMsg(phone, code);
-        String result = HttpUtils.post(MsgConstants.SEND_URL, reqStr);
+        SendMessageDTO sendMessageDTO = new SendMessageDTO();
+        sendMessageDTO.setPhone(phone);
+        sendMessageDTO.setCode(code);
+        sendMessageDTO.setType(2);
+        String result = appUserClient.sendMessage(sendMessageDTO).getData();
+//        String reqStr = MsgUtil.applyCodeMsg(phone, code);
+//        String result = HttpUtils.post(MsgConstants.SEND_URL, reqStr);
         // 记录短信发送
-        chargingPileNotificationService.saveData(1,null,null,phone,"验证码:"+code+",用于申请建桩。请勿转发。");
+        chargingPileNotificationService.saveData(1,-1,-1,phone,"验证码:"+code+",用于申请建桩。请勿转发。");
         return AjaxResult.success(result);
     }
 

--
Gitblit v1.7.1