Sfoglia il codice sorgente

API 发送短信配置配置为用户信息

limingming 1 anno fa
parent
commit
74d4362a70

+ 11 - 11
rouyi-api/src/main/java/com/info666/infraredRemote/controller/SmsController.java

@@ -5,6 +5,7 @@ import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.ip.IPUtils;
 import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.common.core.sms.SendMessage;
+import com.ruoyi.common.utils.RedisKeys;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.domain.UserInfo;
 import com.ruoyi.system.domain.vo.SmsResponseVo;
@@ -19,7 +20,6 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.servlet.http.HttpServletRequest;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
-import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 
@@ -29,13 +29,14 @@ public class SmsController {
 
     @Autowired
     private RedisCache redisCache;
+
     @Autowired
     private RedisTemplate redisTemplate;
 
 
     @PostMapping("/send")
     public R<SmsResponseVo> sendSms(@RequestBody UserInfo param, HttpServletRequest request) {
-        if (StringUtils.isEmpty(param.getUserPhone()) || Pattern.matches("^1[3-9]\\d{9}$\n",param.getUserPhone())){
+        if (StringUtils.isEmpty(param.getUserPhone()) || Pattern.matches("^1[3-9]\\d{9}$\n", param.getUserPhone())) {
             return R.fail("手机号格式不正确");
         }
 
@@ -44,9 +45,9 @@ public class SmsController {
         String ip = IPUtils.getIpAddr(request);
         DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
         String strDate = dtf.format(LocalDate.now());
-        String ipKey = strDate + "_" + ip;
-        String phoneIconKey = strDate + "_"+"icon" + param.getUserPhone();
-        String phone = "phone_"+param.getUserPhone();
+        String ipKey = RedisKeys.INCR_IP + strDate + "_" + ip;
+        String phoneIconKey = RedisKeys.INCR_PHONE + strDate + "_" + param.getUserPhone();
+        String phone = RedisKeys.SMS_PHONE_TIME + param.getUserPhone();
         //一分钟发送一次
         if (redisTemplate.hasKey(phone)) {
             //存在
@@ -59,7 +60,7 @@ public class SmsController {
         //手机号
         if (redisTemplate.hasKey(phoneIconKey)) {
             //存在
-            Long increment = redisTemplate.opsForValue().increment(phoneIconKey, 1);
+            Integer increment = redisCache.getCacheObject(phoneIconKey);
             if (increment > 10) {
                 return R.fail("该手机号发送次数过多,已禁止发送!");
             }
@@ -71,7 +72,7 @@ public class SmsController {
         //ip
         if (redisTemplate.hasKey(ipKey)) {
             //存在
-            Long increment = redisTemplate.opsForValue().increment(ipKey, 1);
+            Integer increment = redisCache.getCacheObject(ipKey);
             if (increment > 100) {
                 return R.fail("发送次数过多,已禁止发送!");
             }
@@ -88,13 +89,12 @@ public class SmsController {
             SendSmsResponse sendSmsResponse = SendMessage.sendSms(param.getUserPhone(), code);
 
             if (sendSmsResponse.getMessage().equals("OK")) {
-                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-                redisCache.setCacheObject(uuid, code, 5, TimeUnit.MINUTES);
+                redisCache.setCacheObject(RedisKeys.SMS_PHONE_CODE + param.getUserPhone(), code, 5, TimeUnit.MINUTES);
                 smsResponse.setCode(sendSmsResponse.getCode());
                 smsResponse.setMessage(sendSmsResponse.getMessage());
-                smsResponse.setKeyCode(uuid);
+                smsResponse.setKeyCode(RedisKeys.SMS_PHONE_CODE + param.getUserPhone());
 
-            }else {
+            } else {
                 return R.fail(sendSmsResponse.getMessage());
             }
         } catch (Exception e) {

+ 8 - 4
ruoyi-common/src/main/java/com/ruoyi/common/core/sms/SendMessage.java

@@ -23,8 +23,11 @@ public class SendMessage {
     static final String domain = "dysmsapi.aliyuncs.com";
 
     // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
-    static final String accessKeyId = "LTAI5tKvxXUQ61e5PSm3mzQ1";
-    static final String accessKeySecret = "WEzhPLHWqEINBbiLn168g2nP1H8Cje";
+//    static final String accessKeyId = "LTAI5tKvxXUQ61e5PSm3mzQ1";
+    static final String accessKeyId = "LTAI4GEnYjNkDo3PiCLwmcsS";
+
+//    static final String accessKeySecret = "WEzhPLHWqEINBbiLn168g2nP1H8Cje";
+    static final String accessKeySecret = "PVJvmoubjj3BTRViAxPpcFZvEyAeOj";
 
     public static SendSmsResponse sendSms(String phone, String code) throws ClientException {
 
@@ -42,9 +45,10 @@ public class SendMessage {
         //必填:待发送手机号
         request.setPhoneNumbers(phone);
         //必填:短信签名-可在短信控制台中找到
-        request.setSignName("阿里云短信测试");
+        request.setSignName("菊辉电子");
         //必填:短信模板-可在短信控制台中找到
-        request.setTemplateCode("SMS_154950909");
+//        request.setTemplateCode("SMS_154950909");
+        request.setTemplateCode("SMS_283430418");
         //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
         request.setTemplateParam("{\"code\":\"" + code + "\"}");
 

+ 39 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisKeys.java

@@ -0,0 +1,39 @@
+package com.ruoyi.common.utils;
+
+/**
+ * Redis所有Keys
+ *
+ * @Author LIMING
+ * @Time 2023-08-03
+ */
+public class RedisKeys {
+    /**
+     * 短信验证码,图形验证码类
+     */
+    public static final String SMS_PHONE_CODE = "SMS_PHONE_CODE:";
+    public static final String SMS_PHONE_TIME = "SMS_PHONE_TIME:";
+    public static final String IMAGE_CODE_NUM = "IMAGE_CODE_NUM:";
+    public static final String INCR_PHONE = "INCR_PHONE:";
+    public static final String INCR_IP = "INCR_IP:";
+
+    /**
+     * 短信
+     * 暂时被锁的手机号和ip
+     */
+    public static final String LOCK_SMS_PHONE = "LOCK_SMS_PHONE:";
+    public static final String LOCK_SMS_IP = "LOCK_SMS_IP:";
+
+
+    /**
+     * 登录次数记录
+     */
+    public static final String INCR_LOGIN_USER = "INCR_LOGIN_USER:";
+    public static final String INCR_LOGIN_IP = "INCR_LOGIN_IP:";
+    /**
+     * 登录
+     * 暂时被锁的手机号和ip
+     */
+    public static final String LOCK_LOGIN_USER = "LOCK_LOGIN_USER:";
+    public static final String LOCK_LOGIN_IP = "LOCK_LOGIN_IP:";
+
+}