lyx 3 долоо хоног өмнө
parent
commit
a8b213b71e

+ 26 - 0
alien-store/src/main/java/shop/alien/store/config/NacosConfig.java

@@ -0,0 +1,26 @@
+package shop.alien.store.config;
+
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * @author ssk
+ * @version 1.0
+ * @date 2025/11/6 13:39
+ */
+@Data
+@Component
+@RefreshScope
+public class NacosConfig {
+
+    /**
+     * 测试手机号
+     */
+    @Value("${ali.sms.testPhone}")
+    private List<String> testPhone;
+
+}

+ 35 - 5
alien-store/src/main/java/shop/alien/store/controller/AliController.java

@@ -138,17 +138,47 @@ public class AliController {
 
     @ApiOperation("发送短信")
     @ApiOperationSupport(order = 4)
-    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "appType", value = "端区分(0:用户, 1:商家)", dataType = "Integer", paramType = "query", required = true, defaultValue = "0"),
+            @ApiImplicitParam(name = "businessType", value = "业务类型 (0:登录, 1:修改密码, 2:注册, 3:修改手机号, 4:注销店铺, 5:注销账号, 6:忘记密码)", dataType = "Integer", paramType = "query", required = true, defaultValue = "0")
+    })
     @GetMapping("/sendSms")
-    public R sendSms(String phone) {
-        Integer code = aliSmsConfig.sendSms(phone);
-        log.info("AliController.sendSms?phone={}&code={}", phone, code);
+    public R sendSms(
+            @RequestParam("phone") String phone,
+            @RequestParam("appType") Integer appType,
+            @RequestParam("businessType") Integer businessType
+    ) {
+        Integer code = aliSmsConfig.sendSms(phone, appType, businessType);
+        log.info("AliController.sendSms?phone={}&code={}&businessType={}", phone, code, businessType);
         if (code != null) {
-            return R.data(code);
+            return R.data("短信发送成功");
         }
         return R.fail("短信发送失败");
     }
 
+    @ApiOperation("校验短信验证码")
+    @ApiOperationSupport(order = 4)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "appType", value = "端区分(0:用户, 1:商家)", dataType = "Integer", paramType = "query", required = true, defaultValue = "0"),
+            @ApiImplicitParam(name = "businessType", value = "业务类型 (0:登录, 1:修改密码, 2:注册, 3:修改手机号, 4:注销店铺, 5:注销账号, 6:忘记密码)", dataType = "Integer", paramType = "query", required = true, defaultValue = "0"),
+            @ApiImplicitParam(name = "code", value = "验证码", dataType = "Integer", paramType = "query", required = true)
+    })
+    @GetMapping("/checkSmsCode")
+    public R checkSmsCode(
+            @RequestParam("phone") String phone,
+            @RequestParam("appType") Integer appType,
+            @RequestParam("businessType") Integer businessType,
+            @RequestParam("code") Integer code
+    ) {
+        log.info("AliController.checkSmsCode?phone={}&appType={}&businessType={}&code={}", phone, appType, businessType, code);
+        if (aliSmsConfig.checkSmsCode(phone, appType, businessType, code)) {
+            return R.success("验证码校验成功");
+        }
+        return R.fail("验证码校验失败");
+    }
+
     @ApiOperation("银行卡核验")
     @ApiOperationSupport(order = 5)
     @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = true),

+ 127 - 11
alien-store/src/main/java/shop/alien/store/util/ali/AliSms.java

@@ -10,11 +10,9 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import shop.alien.store.config.BaseRedisService;
+import shop.alien.store.config.NacosConfig;
 import shop.alien.util.common.RandomCreateUtil;
 
-import java.util.Arrays;
-import java.util.List;
-
 /**
  * 阿里云验证码配置
  *
@@ -28,6 +26,8 @@ import java.util.List;
 public class AliSms {
     private final BaseRedisService baseRedisService;
 
+    private final NacosConfig nacosConfig;
+
     @Value("${ali.sms.accessKeyId}")
     private String accessKeyId;
 
@@ -43,21 +43,62 @@ public class AliSms {
     @Value("${ali.sms.templateCode}")
     private String templateCode;
 
+    @Value("${ali.sms.codeTimeOut}")
+    private Long codeTimeOut;
+
     /**
      * 发送验证码
      *
-     * @param phone 手机号
+     * @param phone        手机号
+     * @param appType      端区分(0:用户, 1:商家)
+     * @param businessType 业务类型 (0:登录, 1:修改密码, 2:注册, 3:修改手机号, 4:注销店铺, 5:注销账号, 6:忘记密码)
      * @return 验证码
      */
-    public Integer sendSms(String phone) {
-        log.info("AliSmsConfig.sendSms?phone={}", phone);
+    public Integer sendSms(String phone, Integer appType, Integer businessType) {
+        log.info("AliSmsConfig.sendSms?phone={}&appType={}&businessType={}", phone, appType, businessType);
         try {
+            String appTypeStr = appType == 0 ? "user" : "store";
+            String businessTypeStr;
+            switch (businessType) {
+                case 0:
+                    //登录
+                    businessTypeStr = "login";
+                    break;
+                case 1:
+                    //修改密码
+                    businessTypeStr = "modify_password";
+                    break;
+                case 2:
+                    //注册
+                    businessTypeStr = "register";
+                    break;
+                case 3:
+                    //修改手机号
+                    businessTypeStr = "modify_phone";
+                    break;
+                case 4:
+                    //注销店铺
+                    businessTypeStr = "cancel_store";
+                    break;
+                case 5:
+                    //注销账号
+                    businessTypeStr = "cancel_account";
+                    break;
+                case 6:
+                    //忘记密码
+                    businessTypeStr = "forget_password";
+                    break;
+                case 7:
+                    //忘记支付密码
+                    businessTypeStr = "forget_pay_password";
+                    break;
+                default:
+                    businessTypeStr = "login";
+            }
             // -----------------测试用手机号--------------------------------------------------------------------------------------------
-            List<String> phoneList = Arrays.asList("19999990001", "19999990002", "19999990003", "19999990004", "19999990005", "19999990006", "19999990007", "19999990008", "19999990009", "19999990010",
-                    "16666660001", "16666660002", "16666660003", "16666660004", "16666660005", "16666660006", "16666660007", "16666660008", "16666660009", "16666660010");
-            if (phoneList.contains(phone)) {
+            if (nacosConfig.getTestPhone().contains(phone)) {
                 // 验证码发送成功,将验证码保存到redis中 设置60秒过期
-                baseRedisService.setString("verification_"+phone,"123456",Long.valueOf(60));
+                baseRedisService.setString("verification_" + appTypeStr + "_" + businessTypeStr + "_" + phone, "123456", codeTimeOut);
                 return 123456;
             }
             // -----------------测试用手机号--------------------------------------------------------------------------------------------
@@ -87,7 +128,7 @@ public class AliSms {
                 return null;
             }
             // 验证码发送成功,将验证码保存到redis中 设置60秒过期
-            baseRedisService.setString("verification_"+phone,code.toString(),Long.valueOf(60));
+            baseRedisService.setString("verification_" + appTypeStr + "_" + businessTypeStr + "_" + phone, code.toString(), codeTimeOut);
             return code;
         } catch (Exception e) {
             log.error("AliSmsConfig.sendSms ERROR Msg={}", e.getMessage());
@@ -95,4 +136,79 @@ public class AliSms {
         }
     }
 
+
+    /**
+     * 校验短信验证码
+     *
+     * @param phone        手机号
+     * @param appType      端区分(0:用户, 1:商家)
+     * @param businessType 业务类型 (0:登录, 1:修改密码, 2:注册, 3:修改手机号, 4:注销店铺, 5:注销账号, 6:忘记密码)
+     * @param code         用户输入的验证码
+     * @return 校验结果 true-校验成功 false-校验失败
+     */
+    public boolean checkSmsCode(String phone, Integer appType, Integer businessType, Integer code) {
+        log.info("AliSms.checkSmsCode?phone={}&appType={}&businessType={}&code={}", phone, appType, businessType, code);
+        try {
+            // 构建Redis key,与sendSms方法中的key格式保持一致
+            String appTypeStr = appType == 0 ? "user" : "store";
+            String businessTypeStr;
+            switch (businessType) {
+                case 0:
+                    businessTypeStr = "login";
+                    break;
+                case 1:
+                    businessTypeStr = "modify_password";
+                    break;
+                case 2:
+                    businessTypeStr = "register";
+                    break;
+                case 3:
+                    businessTypeStr = "modify_phone";
+                    break;
+                case 4:
+                    businessTypeStr = "cancel_store";
+                    break;
+                case 5:
+                    businessTypeStr = "cancel_account";
+                    break;
+                case 6:
+                    businessTypeStr = "forget_password";
+                    break;
+                case 7:
+                    businessTypeStr = "forget_pay_password";
+                    break;
+                default:
+                    businessTypeStr = "login";
+            }
+
+            String verifyKey = "verification_" + appTypeStr + "_" + businessTypeStr + "_" + phone;
+
+            // 从Redis中获取验证码
+            String cacheCode = baseRedisService.getString(verifyKey);
+
+            // 验证码不存在或已过期
+            if (cacheCode == null || cacheCode.trim().isEmpty()) {
+                log.warn("验证码不存在或已过期,phone={}, appType={}, businessType={}", phone, appType, businessType);
+                return false;
+            }
+
+            // 比较验证码(去除空格)
+            boolean isValid = cacheCode.trim().equals(String.valueOf(code).trim());
+
+            if (isValid) {
+                // 验证成功,删除验证码(防止重复使用)
+                baseRedisService.delete(verifyKey);
+                log.info("验证码校验成功,phone={}, appType={}, businessType={}", phone, appType, businessType);
+            } else {
+                log.warn("验证码校验失败,phone={}, appType={}, businessType={}, 期望值={}, 实际值={}",
+                        phone, appType, businessType, cacheCode, code);
+            }
+
+            return isValid;
+        } catch (Exception e) {
+            log.error("AliSms.checkSmsCode ERROR Msg={}", e.getMessage(), e);
+            return false;
+        }
+    }
+
 }