Browse Source

阿里云短信验证码

gaodm 6 years ago
parent
commit
01038e9256

+ 0 - 2
user-service/src/main/java/com/diagbot/entity/SwsVerInfo.java

@@ -13,8 +13,6 @@ import java.util.Date;
 @Getter
 @Setter
 public class SwsVerInfo {
-    //用户ID
-    private Long userId;
     //用户电话
     private String mobile;
     //验证码

+ 4 - 4
user-service/src/main/java/com/diagbot/service/SmsVerService.java

@@ -25,13 +25,13 @@ public interface SmsVerService {
 
     /**
      * 获取用户短信验证码信息
-     * @param userId 用户ID
-     * @return 用户信息
+     * @param mobile 用户电话
+     * @return 用户短信验证信息
      */
-    SwsVerInfo getSmsVerification(Long userId);
+    SwsVerInfo getSmsVerification(String mobile);
 
     /**
      * 删除用户短信验证码信息
      */
-    Boolean deleteSmsVerification(Long userId);
+    Boolean deleteSmsVerification(String mobile);
 }

+ 16 - 24
user-service/src/main/java/com/diagbot/service/impl/SmsVerServiceImpl.java

@@ -25,7 +25,7 @@ import java.util.Date;
 /**
  * @Description:
  * 短信验证接口实现
- * 阿里大于验证码发送
+ * 阿里短信验证码发送
  * @author: gaodm
  * @time: 2018/9/4 16:25
  */
@@ -51,9 +51,9 @@ public class SmsVerServiceImpl implements SmsVerService {
         return redisForSms.getValueSerializer().deserialize(b);
     }
 
-    private byte[] getUserSmsKey(Long userId) {
-        String userSmsFormat = "user_sms_%d";
-        return serializeKey(String.format(userSmsFormat, userId));
+    private byte[] getUserSmsKey(String mobile) {
+        String userSmsFormat = "user_sms_%s";
+        return serializeKey(String.format(userSmsFormat, mobile));
     }
 
     public String smsSend(String mobile,String smsTemplateCode){
@@ -104,10 +104,6 @@ public class SmsVerServiceImpl implements SmsVerService {
             throw new CommonException(ErrorCode.PARAM_IS_NULL,
                     "用户短信验证信息不能为空!");
         }
-        if (null == swsVerInfo.getUserId()){
-            throw new CommonException(ErrorCode.PARAM_IS_NULL,
-                    "用户ID不能为空!");
-        }
         if (StringUtil.isBlank(swsVerInfo.getMobile())){
             throw new CommonException(ErrorCode.PARAM_IS_NULL,
                     "用户电话不能为空!");
@@ -119,7 +115,7 @@ public class SmsVerServiceImpl implements SmsVerService {
         swsVerInfo.setCreateTime(DateUtil.now());
         final Date expireDate = DateUtil.addMinutes(DateUtil.now(), 3);
         swsVerInfo.setExpireTime(expireDate);
-        final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
+        final byte[] redis_key = getUserSmsKey(swsVerInfo.getMobile());
         redisForSms.execute(new RedisCallback<Object>() {
             @Override
             public Object doInRedis(RedisConnection connection) throws DataAccessException {
@@ -150,10 +146,6 @@ public class SmsVerServiceImpl implements SmsVerService {
             throw new CommonException(ErrorCode.PARAM_IS_NULL,
                     "用户短信验证信息不能为空!");
         }
-        if (null == swsVerInfo.getUserId()){
-            throw new CommonException(ErrorCode.PARAM_IS_NULL,
-                    "用户ID不能为空!");
-        }
         if (StringUtil.isBlank(swsVerInfo.getMobile())){
             throw new CommonException(ErrorCode.PARAM_IS_NULL,
                     "用户电话不能为空!");
@@ -163,7 +155,7 @@ public class SmsVerServiceImpl implements SmsVerService {
                     "短信验证码不能为空!");
         }
         //从redis中取出
-        final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
+        final byte[] redis_key = getUserSmsKey(swsVerInfo.getMobile());
         SwsVerInfo swsVerInfoRes = (SwsVerInfo) redisForSms.execute(new RedisCallback<SwsVerInfo>() {
             @Override
             public SwsVerInfo doInRedis(RedisConnection connection) throws DataAccessException {
@@ -175,24 +167,24 @@ public class SmsVerServiceImpl implements SmsVerService {
             }
         });
         if (swsVerInfoRes == null) {
-            log.info("用户短信验证信息不存在!用户ID:{}", swsVerInfo.getUserId());
+            log.info("用户短信验证信息不存在!用户电话:{}", swsVerInfo.getMobile());
             throw new CommonException(ErrorCode.NOT_EXISTS,
                     "用户短信验证信息不存在!");
         }
         //判断是否是想要的对象
         if (!swsVerInfoRes.getMobile().equals(swsVerInfo.getMobile())) {
-            log.info("用户电话不一致!用户ID:{},用户电话:{}", swsVerInfo.getUserId(), swsVerInfo.getMobile());
+            log.info("用户电话不一致! 用户电话:{}", swsVerInfo.getMobile());
             throw new CommonException(ErrorCode.PARAM_IS_ERROR,
                     "用户电话不一致!");
         }
         if (!swsVerInfoRes.getCode().equals(swsVerInfo.getCode())) {
-            log.info("用户短信验证码不一致!用户ID:{},验证码:{}", swsVerInfo.getUserId(),swsVerInfo.getCode());
+            log.info("用户短信验证码不一致!用户电话:{},验证码:{}", swsVerInfo.getMobile(),swsVerInfo.getCode());
             throw new CommonException(ErrorCode.PARAM_IS_ERROR,
                     "用户短息验证码不一致!");
         }
         //判断是否过期
         if (!DateUtil.after(swsVerInfoRes.getExpireTime(), DateUtil.now())) {
-            log.info("短息验证码已过期,请重新获取!用户ID:{},过期时间:{}", swsVerInfo.getUserId(), swsVerInfoRes.getExpireTime());
+            log.info("短息验证码已过期,请重新获取!用户电话:{},过期时间:{}", swsVerInfo.getMobile(), swsVerInfoRes.getExpireTime());
             throw new CommonException(ErrorCode.PARAM_IS_ERROR,
                     "短息验证码已过期,请重新获取!");
         }
@@ -201,15 +193,15 @@ public class SmsVerServiceImpl implements SmsVerService {
 
     /**
      * 获取用户短信验证码信息
-     * @param userId
-     * @return
+     * @param mobile 用户电话
+     * @return 用户短信验证码信息
      */
     @Override
-    public SwsVerInfo getSmsVerification(Long userId){
+    public SwsVerInfo getSmsVerification(String mobile){
         return (SwsVerInfo) redisForSms.execute(new RedisCallback<Object>() {
             @Override
             public Object doInRedis(RedisConnection connection) throws DataAccessException {
-                byte[] redis_key = getUserSmsKey(userId);
+                byte[] redis_key = getUserSmsKey(mobile);
                 byte[] bytes = connection.get(redis_key);
                 if (bytes == null) {
                     return null;
@@ -223,8 +215,8 @@ public class SmsVerServiceImpl implements SmsVerService {
      * 删除用户短信验证码信息
      */
     @Override
-    public Boolean deleteSmsVerification(Long userId){
-        final byte[] redis_key = getUserSmsKey(userId);
+    public Boolean deleteSmsVerification(String mobile){
+        final byte[] redis_key = getUserSmsKey(mobile);
         Long l = (Long) redisForSms.execute(new RedisCallback<Long>() {
             @Override
             public Long doInRedis(RedisConnection connection) throws DataAccessException {

+ 2 - 0
user-service/src/main/java/com/diagbot/web/TestController.java

@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+import springfox.documentation.annotations.ApiIgnore;
 
 /**
  * @Description: 测试控制层
@@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
  */
 @RestController
 @Slf4j
+//@ApiIgnore
 @RequestMapping("/test")
 public class TestController {
     @Autowired