ソースを参照

短信服务修改

wwl 4 年 前
コミット
f90741fde0

+ 3 - 2
hzgzpt-admin/src/main/java/com/miaxis/app/controller/user/UserInfoController.java

@@ -177,14 +177,15 @@ public class UserInfoController extends BaseController{
     @PostMapping("/modifyPhoneNumber")
     @ApiOperation("修改用户手机号")
     @ApiImplicitParams({
+            @ApiImplicitParam(name = "smsCodeType,", value = "login:用户登录验证码、modify:修改手机验证码、signUp:注册报名验证码", required = true, dataType = "String"),
             @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String"),
             @ApiImplicitParam(name = "verificationCode", value = "短信验证码", required = true, dataType = "String")
     })
-    public Response modifyPhoneNumber(String phone,String verificationCode){
+    public Response modifyPhoneNumber(String smsCodeType,String phone,String verificationCode){
         //获取当前用户
         SysUser user = SecurityUtils.getLoginUser().getUser();
         //修改手机号
-        return userInfoService.modifyPhoneNumber(phone,verificationCode,user);
+        return userInfoService.modifyPhoneNumber(smsCodeType,phone,verificationCode,user);
     }
 
     /**

+ 1 - 1
hzgzpt-common/src/main/java/com/miaxis/common/annotation/Log.java

@@ -29,7 +29,7 @@ public @interface Log
     /**
      * 操作人类别
      */
-    public OperatorTypeEnum operatorType() default OperatorTypeEnum.MANAGE;
+    public OperatorTypeEnum operatorType() default OperatorTypeEnum.MOBILE;
 
     /**
      * 是否保存请求的参数

+ 0 - 17
hzgzpt-common/src/main/java/com/miaxis/common/constant/Constants.java

@@ -57,15 +57,6 @@ public class Constants
      */
     public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
 
-    /**
-     * 修改手机号验证码 redis key
-     */
-    public static final String SMS_MODIFY_CODE = "sms:modify:code:";
-    /**
-     * 修改手机号验证码次数 redis key
-     */
-    public static final String SMS_MODIFY_CODE_COUNT = "sms:modify:code:count:";
-
     /**
      * 登录短信验证码 redis key
      */
@@ -75,14 +66,6 @@ public class Constants
      */
     public static final String SMS_LOGIN_CODE_COUNT = "sms:login:code:count:";
 
-    /**
-     * 报名手机号验证码 redis key
-     */
-    public static final String SMS_SIGNUP_CODE = "sms:signUp:code:";
-    /**
-     * 报名手机号验证码次数 redis key
-     */
-    public static final String SMS_SIGNUP_CODE_COUNT = "sms:signUp:code:count:";
 
     /**
      * 登录用户 redis key

+ 32 - 0
hzgzpt-common/src/main/java/com/miaxis/common/utils/RedisPrefixUtils.java

@@ -0,0 +1,32 @@
+package com.miaxis.common.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * redis前缀生成工具
+ * @Author wwl
+ * @Date 2020/12/29
+ * @Version 1.0
+ */
+public class RedisPrefixUtils {
+
+    /**
+     * 生成短信验证码redis前缀
+     * keyPrefix : 验证码缓存前缀
+     * smsKeyCountPrefix :该手机号验证次数缓存前缀
+     * @return
+     */
+    public static Map<String,String> smsRedisPrefix(String type, String phone){
+
+        String keyPrefix = "sms:code:"+type+":"+phone;
+        String smsKeyCountPrefix = "sms:code:count:"+type+":"+phone;
+
+        Map<String, String> map = new HashMap<>();
+        map.put("keyPrefix",keyPrefix);
+        map.put("smsKeyCountPrefix",smsKeyCountPrefix);
+
+        return map;
+    }
+
+}

+ 55 - 4
hzgzpt-framework/src/main/java/com/miaxis/framework/aspectj/LogAspect.java

@@ -14,21 +14,22 @@ import com.miaxis.framework.manager.factory.AsyncFactory;
 import com.miaxis.framework.web.service.TokenService;
 import com.miaxis.system.domain.SysOperLog;
 import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.Signature;
-import org.aspectj.lang.annotation.AfterReturning;
-import org.aspectj.lang.annotation.AfterThrowing;
-import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.annotation.*;
 import org.aspectj.lang.reflect.MethodSignature;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.servlet.HandlerMapping;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.Method;
+import java.util.Enumeration;
 import java.util.Map;
 
 /**
@@ -215,4 +216,54 @@ public class LogAspect
     {
         return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse;
     }
+
+    /**
+     * 定义切点
+     */
+    @Pointcut("execution(public * com.miaxis.*.controller..*(..))")
+    public void cutController() {
+    }
+
+    /**
+     * 环绕通知
+     * ProceedingJoinPoint:连接点对象
+     */
+    @Around("cutController()")
+    public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
+        long startTime = System.currentTimeMillis();
+        // 定义返回对象
+        Object resultData = null;
+
+        //获取方法名
+        String strMethodName = point.getSignature().getName();
+        //获取类名
+        String strClassName = point.getTarget().getClass().getName();
+        //获取连接点方法运行时的入参列表
+        Object[] params = point.getArgs();
+        StringBuffer bfParams = new StringBuffer();
+        Enumeration<String> paraNames = null;
+        HttpServletRequest request = null;
+        if (params != null && params.length > 0) {
+            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+            paraNames = request.getParameterNames();
+            String key;
+            String value;
+            while (paraNames.hasMoreElements()) {
+                key = paraNames.nextElement();
+                value = request.getParameter(key);
+                bfParams.append(key).append("=").append(value).append("&");
+            }
+            if (StringUtils.isBlank(bfParams)) {
+                bfParams.append(request.getQueryString());
+            }
+        }
+
+        log.info("=========>请求["+strMethodName+"]接口开始,类名:{},参数:{}", strClassName,bfParams.toString());
+        resultData = point.proceed(params);
+        long endTime = System.currentTimeMillis();
+        log.info("======>请求["+strMethodName+"]接口结束,耗时:{}ms", (endTime - startTime));
+        return resultData;
+    }
+
+
 }

+ 1 - 0
hzgzpt-framework/src/main/java/com/miaxis/framework/manager/AsyncManager.java

@@ -38,6 +38,7 @@ public class AsyncManager
 
     /**
      * 执行任务
+     * 创建并执行启用的一次性操作,在给定的延迟时间之后执行
      * 
      * @param task 任务
      */

+ 28 - 12
hzgzpt-service-app/src/main/java/com/miaxis/app/sms/impl/SmsServiceImpl.java

@@ -7,6 +7,7 @@ import com.miaxis.common.constant.SmsTemplateConstants;
 import com.miaxis.common.core.domain.Response;
 import com.miaxis.common.core.domain.ResponseEnum;
 import com.miaxis.common.sms.Client;
+import com.miaxis.common.utils.RedisPrefixUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -31,7 +32,7 @@ public class SmsServiceImpl implements ISmsService {
     private final String MOBILE_REGEX = "^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";//手机号码验证
     private final Integer SMS_MAX_COUNT = 500 ;//每天系统发送最大次数(预留)
     private final Integer MAX_COUNT = 5 ;//每个手机号验证发送最大次数
-    private final long SEND_INTERVAL = 2 ;//发送间隔时间
+    private final long SEND_INTERVAL = 3 ;//发送间隔时间
 
 
     @Value("${sms_user_name}")
@@ -74,7 +75,22 @@ public class SmsServiceImpl implements ISmsService {
         }
         try{
 
-            //验证手机号
+            //生成redis 验证码key前缀 验证次数key前缀
+            Map<String, String> map = RedisPrefixUtils.smsRedisPrefix(smsParameter.getType(), smsParameter.getPhone());
+            smsParameter.setKeyPrefix(map.get("keyPrefix"));
+            smsParameter.setVerificationCount(map.get("smsKeyCountPrefix"));
+
+            if ("login".equals(smsParameter.getType())){
+                smsParameter.setSmsTemplateConstants(SmsTemplateConstants.login);
+            }
+            if ("signUp".equals(smsParameter.getType())){
+                smsParameter.setSmsTemplateConstants(SmsTemplateConstants.signUp);
+            }
+            if ("modify".equals(smsParameter.getType())){
+                smsParameter.setSmsTemplateConstants(SmsTemplateConstants.modify);
+            }
+
+            //验证缓存信息
             Response resultJson = validateMobile(smsParameter);
             if("500".equals(resultJson.getCode().toString())) {
                 return resultJson;
@@ -145,14 +161,14 @@ public class SmsServiceImpl implements ISmsService {
      */
     private Response validateMobile(SmsParameter smsParameter){
 
-        if(redisTemplate.hasKey(smsParameter.getVerificationCount() + smsParameter.getPhone())){
+        if(redisTemplate.hasKey(smsParameter.getVerificationCount())){
             // 验证发送时间
-            if (redisTemplate.hasKey(smsParameter.getKeyPrefix() + smsParameter.getPhone())){
+            if (redisTemplate.hasKey(smsParameter.getKeyPrefix())){
                 return Response.error(500,"验证码还未过期,不能重新发送!");
             }
 
             // 验证发送次数
-            Integer count = Integer.valueOf(redisTemplate.opsForValue().get(smsParameter.getVerificationCount() + smsParameter.getPhone()).toString());
+            Integer count = Integer.valueOf(redisTemplate.opsForValue().get(smsParameter.getVerificationCount()).toString());
             if(count >= MAX_COUNT) {
                 return Response.error(500,"当前手机号发送验证码次数过多!");
             }
@@ -169,25 +185,25 @@ public class SmsServiceImpl implements ISmsService {
     private void writeCache(String code,SmsParameter smsParameter){
 
         //该手机号首次获取验证码
-        if(!redisTemplate.hasKey(smsParameter.getKeyPrefix() + smsParameter.getPhone()) && !redisTemplate.hasKey(smsParameter.getVerificationCount() + smsParameter.getPhone())){
+        if(!redisTemplate.hasKey(smsParameter.getKeyPrefix()) && !redisTemplate.hasKey(smsParameter.getVerificationCount())){
             synchronized(this){
                 //设置该手机号验证码 两分钟过期
-                redisTemplate.opsForValue().set(smsParameter.getKeyPrefix() + smsParameter.getPhone() , code, SEND_INTERVAL, TimeUnit.MINUTES);
+                redisTemplate.opsForValue().set(smsParameter.getKeyPrefix() , code, SEND_INTERVAL, TimeUnit.MINUTES);
                 //设置该手机号验证次数 一天过期
-                redisTemplate.opsForValue().set(smsParameter.getVerificationCount() + smsParameter.getPhone() , "1",1,TimeUnit.DAYS);
+                redisTemplate.opsForValue().set(smsParameter.getVerificationCount() , "1",1,TimeUnit.DAYS);
             }
         }
 
         //多次获取验证码
-        if (redisTemplate.hasKey(smsParameter.getVerificationCount() + smsParameter.getPhone()) && !redisTemplate.hasKey(smsParameter.getKeyPrefix() + smsParameter.getPhone())){
+        if (redisTemplate.hasKey(smsParameter.getVerificationCount()) && !redisTemplate.hasKey(smsParameter.getKeyPrefix())){
             //判断该手机验证次数
-            Integer count = Integer.valueOf(redisTemplate.opsForValue().get(smsParameter.getVerificationCount() + smsParameter.getPhone()).toString());
+            Integer count = Integer.valueOf(redisTemplate.opsForValue().get(smsParameter.getVerificationCount()).toString());
             if (count < MAX_COUNT){
                 synchronized(this){
                     //该手机号验证次数+1 一天过期
-                    redisTemplate.opsForValue().set(smsParameter.getVerificationCount() + smsParameter.getPhone(),String.valueOf(count+1),1,TimeUnit.DAYS);
+                    redisTemplate.opsForValue().set(smsParameter.getVerificationCount() ,String.valueOf(count+1),1,TimeUnit.DAYS);
                     //设置该手机号验证码 两分钟过期
-                    redisTemplate.opsForValue().set(smsParameter.getKeyPrefix() + smsParameter.getPhone() , code, SEND_INTERVAL, TimeUnit.MINUTES);
+                    redisTemplate.opsForValue().set(smsParameter.getKeyPrefix() , code, SEND_INTERVAL, TimeUnit.MINUTES);
                 }
             }
         }

+ 3 - 0
hzgzpt-service-app/src/main/java/com/miaxis/app/user/dto/UserDto.java

@@ -71,6 +71,9 @@ public class UserDto extends BaseBusinessEntity {
     @ApiModelProperty(value = "人像图片路径")
     private String facesPath;
 
+    @ApiModelProperty(value = "获取短信验证码类型")
+    private String smsCodeType;
+
     @ApiModelProperty(value = "短信验证码")
     private String verificationCode;
 }

+ 1 - 1
hzgzpt-service-app/src/main/java/com/miaxis/app/user/service/IUserInfoService.java

@@ -44,7 +44,7 @@ public interface IUserInfoService extends IService<UserInfo>{
      * @param verificationCode
      * @return
      */
-    Response modifyPhoneNumber(String phone, String verificationCode,SysUser user);
+    Response modifyPhoneNumber(String smsCodeType,String phone, String verificationCode,SysUser user);
 
     /**
      * 保存用户报名信息

+ 16 - 13
hzgzpt-service-app/src/main/java/com/miaxis/app/user/service/impl/UserInfoServiceImpl.java

@@ -19,6 +19,7 @@ import com.miaxis.common.core.domain.Response;
 import com.miaxis.common.core.domain.entity.SysUser;
 import com.miaxis.common.core.domain.entity.UserInfo;
 import com.miaxis.common.utils.DateUtils;
+import com.miaxis.common.utils.RedisPrefixUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -27,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 用户信息Service业务层处理
@@ -100,14 +102,18 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public Response modifyPhoneNumber(String phone,String verificationCode,SysUser user) {
+    public Response modifyPhoneNumber(String smsCodeType,String phone,String verificationCode,SysUser user) {
+
+        //生成短信缓存前缀
+        Map<String, String> map = RedisPrefixUtils.smsRedisPrefix(smsCodeType,phone);
+
         //查看验证码是否过期
-        if (redisTemplate.hasKey(Constants.SMS_MODIFY_CODE_COUNT + phone) &&
-                !redisTemplate.hasKey(Constants.SMS_MODIFY_CODE + phone)){
+        if (redisTemplate.hasKey(map.get("smsKeyCountPrefix")) &&
+                !redisTemplate.hasKey(map.get("keyPrefix"))){
             return Response.error(500,"验证码已过期,请重新验证!");
         }
         //比对缓存中的验证码
-        String smsCodeValue = redisTemplate.opsForValue().get(Constants.SMS_MODIFY_CODE + phone).toString();
+        String smsCodeValue = redisTemplate.opsForValue().get(map.get("keyPrefix")).toString();
         if (!verificationCode.equals(smsCodeValue)){
             return Response.error(500,"验证码错误,请重新输入");
         }
@@ -131,13 +137,16 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
     public Response saveUserInformation(UserDto userDto) {
         try {
 
+            //生成短信缓存前缀
+            Map<String, String> map = RedisPrefixUtils.smsRedisPrefix(userDto.getSmsCodeType(), userDto.getMobile());
+
             //查看验证码是否过期
-            if (redisTemplate.hasKey(Constants.SMS_SIGNUP_CODE_COUNT + userDto.getMobile()) &&
-                    !redisTemplate.hasKey(Constants.SMS_SIGNUP_CODE + userDto.getMobile())){
+            if (redisTemplate.hasKey(map.get("smsKeyCountPrefix")) &&
+                    !redisTemplate.hasKey(map.get("keyPrefix"))){
                 return Response.error(500,"验证码已过期,请重新验证!");
             }
             //比对缓存中的验证码
-            String smsCodeValue = redisTemplate.opsForValue().get(Constants.SMS_SIGNUP_CODE + userDto.getMobile()).toString();
+            String smsCodeValue = redisTemplate.opsForValue().get(map.get("keyPrefix")).toString();
             if (!userDto.getVerificationCode().equals(smsCodeValue)){
                 return Response.error(500,"验证码错误,请重新输入");
             }
@@ -222,9 +231,6 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
         SmsParameter smsParameter = new SmsParameter();
         smsParameter.setPhone(phone);
         smsParameter.setType(type);
-        smsParameter.setKeyPrefix(Constants.SMS_SIGNUP_CODE);
-        smsParameter.setVerificationCount(Constants.SMS_SIGNUP_CODE_COUNT);
-        smsParameter.setSmsTemplateConstants(SmsTemplateConstants.signUp);
 
         return smsService.sendVerificationCode(smsParameter);
     }
@@ -246,9 +252,6 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
         SmsParameter smsParameter = new SmsParameter();
         smsParameter.setPhone(phone);
         smsParameter.setType(type);
-        smsParameter.setKeyPrefix(Constants.SMS_MODIFY_CODE);
-        smsParameter.setVerificationCount(Constants.SMS_MODIFY_CODE_COUNT);
-        smsParameter.setSmsTemplateConstants(SmsTemplateConstants.modify);
 
         return smsService.sendVerificationCode(smsParameter);
     }