|
|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|