Forráskód Böngészése

修改异常处理

wwl 4 éve
szülő
commit
23ff58a9fd

+ 10 - 19
hzgzpt-admin/src/main/java/com/miaxis/app/controller/user/UserInfoController.java

@@ -129,11 +129,8 @@ public class UserInfoController extends BaseController{
     @PostMapping("/saveUserInformation")
     @ApiOperation("保存用户报名信息")
     public Response saveUserInformation(UserDto userDto) {
-        try {
-            return userInfoService.saveUserInformation(userDto);
-        }catch (Exception e){
-            return Response.error();
-        }
+
+        return userInfoService.saveUserInformation(userDto);
     }
 
     /**
@@ -141,14 +138,11 @@ public class UserInfoController extends BaseController{
      * @return
      */
     @ApiOperation("注册报名获取短信验证码")
-    @ApiImplicitParams({
-            @ApiImplicitParam(name = "type",value = "login:用户登录验证码、modify:修改手机验证码、signUp:注册报名验证码" ,dataType = "String",required = true),
-            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")
-    })
+    @ApiImplicitParam(name = "phone",value = "手机号码" ,dataType = "String",required = true)
     @PostMapping(value = "/sendSignUpVerificationCode")
-    public Response sendSignUpVerificationCode(String type,String phone){
+    public Response sendSignUpVerificationCode(String phone){
 
-        return userInfoService.sendSignUpVerificationCode(type,phone);
+        return userInfoService.sendSignUpVerificationCode(phone);
     }
 
     /**
@@ -156,16 +150,13 @@ public class UserInfoController extends BaseController{
      * @return
      */
     @ApiOperation("修改手机号获取短信验证码")
-    @ApiImplicitParams({
-            @ApiImplicitParam(name = "type",value = "login:用户登录验证码、modify:修改手机验证码、signUp:注册报名验证码" ,dataType = "String",required = true),
-            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")
-    })
+    @ApiImplicitParam(name = "phone",value = "手机号码" ,dataType = "String",required = true)
     @PostMapping(value = "/sendModifyVerificationCode")
-    public Response sendModifyVerificationCode(String type,String phone){
+    public Response sendModifyVerificationCode(String phone){
         //获取当前用户
         SysUser user = SecurityUtils.getLoginUser().getUser();
 
-        return userInfoService.sendModifyVerificationCode(user,type,phone);
+        return userInfoService.sendModifyVerificationCode(user,phone);
     }
 
 
@@ -179,11 +170,11 @@ public class UserInfoController extends BaseController{
             @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String"),
             @ApiImplicitParam(name = "verificationCode", value = "短信验证码", required = true, dataType = "String")
     })
-    public Response modifyPhoneNumber(String smsCodeType,String phone,String verificationCode){
+    public Response modifyPhoneNumber(String phone,String verificationCode){
         //获取当前用户
         SysUser user = SecurityUtils.getLoginUser().getUser();
         //修改手机号
-        return userInfoService.modifyPhoneNumber(smsCodeType,phone,verificationCode,user);
+        return userInfoService.modifyPhoneNumber(phone,verificationCode,user);
     }
 
     /**

+ 23 - 10
hzgzpt-common/src/main/java/com/miaxis/common/utils/RedisPrefixUtils.java

@@ -1,7 +1,6 @@
 package com.miaxis.common.utils;
 
-import java.util.HashMap;
-import java.util.Map;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 
 /**
  * redis前缀生成工具
@@ -14,19 +13,33 @@ public class RedisPrefixUtils {
     /**
      * 生成短信验证码redis前缀
      * keyPrefix : 验证码缓存前缀
-     * smsKeyCountPrefix :该手机号验证次数缓存前缀
      * @return
      */
-    public static Map<String,String> smsRedisPrefix(String type, String phone){
+    public static String getSmsKeyPrefix(String type, String phone){
 
-        String keyPrefix = "sms:code:"+type+":"+phone;
-        String smsKeyCountPrefix = "sms:code:count:"+type+":"+phone;
+        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(phone)){
+            String keyPrefix = "sms:code:"+type+":"+phone;
+            return keyPrefix;
+        }
+        return null;
+    }
 
-        Map<String, String> map = new HashMap<>();
-        map.put("keyPrefix",keyPrefix);
-        map.put("smsKeyCountPrefix",smsKeyCountPrefix);
+    /**
+     * 生成短信验证码redis前缀
+     *  smsKeyCountPrefix :该手机号验证次数缓存前缀
+     * @param type
+     * @param phone
+     * @return
+     */
+    public static String getSmsKeyCountPrefix(String type, String phone){
 
-        return map;
+        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(phone)){
+            String smsKeyCountPrefix = "sms:code:count:"+type+":"+phone;
+            return smsKeyCountPrefix;
+        }
+        return null;
     }
 
+
+
 }

+ 1 - 1
hzgzpt-service-app/src/main/java/com/miaxis/app/sms/domain/SmsParameter.java

@@ -16,7 +16,7 @@ public class SmsParameter implements Serializable {
     /** 手机号码 */
     private String phone;
 
-    /** 发送短信类型(登录验证、修改手机号验证...) */
+    /** 发送短信类型(login:用户登录验证码、modify:修改手机验证码、signUp:注册报名验证码) */
     private String type;
 
     /** 缓存key前缀 */

+ 15 - 14
hzgzpt-service-app/src/main/java/com/miaxis/app/sms/impl/SmsServiceImpl.java

@@ -2,10 +2,9 @@ package com.miaxis.app.sms.impl;
 
 import com.miaxis.app.sms.ISmsService;
 import com.miaxis.app.sms.domain.SmsParameter;
-import com.miaxis.common.constant.Constants;
 import com.miaxis.common.constant.SmsTemplateConstants;
 import com.miaxis.common.core.domain.Response;
-import com.miaxis.common.core.domain.ResponseEnum;
+import com.miaxis.common.exception.CustomException;
 import com.miaxis.common.sms.Client;
 import com.miaxis.common.utils.RedisPrefixUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -14,7 +13,9 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import java.net.URLEncoder;
-import java.util.*;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -60,25 +61,26 @@ public class SmsServiceImpl implements ISmsService {
                 .filter(str -> str.equals(smsParameter.getType()))
                 .collect(Collectors.toList());
         if (collect.size()==0){
-            return Response.error(500,"参数{type}类型错误");
+            throw new CustomException("参数{type}类型错误!");
         }
 
         //手机号格式验证
         if(smsParameter.getPhone().length() != 11){
-            return Response.error(500,"手机格式错误");
+            throw new CustomException("手机格式错误!");
         } else {
             Pattern pattern = Pattern.compile(MOBILE_REGEX);
             Matcher matcher = pattern.matcher(smsParameter.getPhone());
             if(!matcher.matches()) {
-                return Response.error(500,"手机号格式错误");
+                throw new CustomException("手机号格式错误!");
             }
         }
         try{
 
             //生成redis 验证码key前缀 验证次数key前缀
-            Map<String, String> map = RedisPrefixUtils.smsRedisPrefix(smsParameter.getType(), smsParameter.getPhone());
-            smsParameter.setKeyPrefix(map.get("keyPrefix"));
-            smsParameter.setVerificationCount(map.get("smsKeyCountPrefix"));
+            String smsKeyPrefix = RedisPrefixUtils.getSmsKeyPrefix(smsParameter.getType(), smsParameter.getPhone());
+            String smsKeyCountPrefix = RedisPrefixUtils.getSmsKeyCountPrefix(smsParameter.getType(), smsParameter.getPhone());
+            smsParameter.setKeyPrefix(smsKeyPrefix);
+            smsParameter.setVerificationCount(smsKeyCountPrefix);
 
             if ("login".equals(smsParameter.getType())){
                 smsParameter.setSmsTemplateConstants(SmsTemplateConstants.login);
@@ -144,10 +146,9 @@ public class SmsServiceImpl implements ISmsService {
                 Info = "未知错误";
             }
 
-            return Response.error(500,Info);
+            throw new CustomException(Info);
         } catch (Exception e) {
-            e.printStackTrace();
-            return Response.error(ResponseEnum.ERROR);
+            throw new CustomException(e.getMessage());
         }
     }
 
@@ -164,13 +165,13 @@ public class SmsServiceImpl implements ISmsService {
         if(redisTemplate.hasKey(smsParameter.getVerificationCount())){
             // 验证发送时间
             if (redisTemplate.hasKey(smsParameter.getKeyPrefix())){
-                return Response.error(500,"验证码还未过期,不能重新发送!");
+                throw new CustomException("验证码还未过期,不能重新发送!",500);
             }
 
             // 验证发送次数
             Integer count = Integer.valueOf(redisTemplate.opsForValue().get(smsParameter.getVerificationCount()).toString());
             if(count >= MAX_COUNT) {
-                return Response.error(500,"当前手机号发送验证码次数过多!");
+                throw new CustomException("当前手机号发送验证码次数过多!",500);
             }
         }
         return Response.success();

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

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

+ 3 - 5
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 smsCodeType,String phone, String verificationCode,SysUser user);
+    Response modifyPhoneNumber(String phone, String verificationCode,SysUser user);
 
     /**
      * 保存用户报名信息
@@ -55,20 +55,18 @@ public interface IUserInfoService extends IService<UserInfo>{
 
     /**
      * 注册报名获取短信验证码
-     * @param type
      * @param phone
      * @return
      */
-    Response sendSignUpVerificationCode(String type, String phone);
+    Response sendSignUpVerificationCode(String phone);
 
     /**
      * 修改手机号获取短信验证码
      * @param user
-     * @param type
      * @param phone
      * @return
      */
-    Response sendModifyVerificationCode(SysUser user, String type, String phone);
+    Response sendModifyVerificationCode(SysUser user, String phone);
 
 
     /**

+ 88 - 91
hzgzpt-service-app/src/main/java/com/miaxis/app/user/service/impl/UserInfoServiceImpl.java

@@ -18,6 +18,7 @@ import com.miaxis.common.constant.SmsTemplateConstants;
 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.exception.CustomException;
 import com.miaxis.common.utils.DateUtils;
 import com.miaxis.common.utils.RedisPrefixUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -102,20 +103,21 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public Response modifyPhoneNumber(String smsCodeType,String phone,String verificationCode,SysUser user) {
+    public Response modifyPhoneNumber(String phone,String verificationCode,SysUser user) {
 
         //生成短信缓存前缀
-        Map<String, String> map = RedisPrefixUtils.smsRedisPrefix(smsCodeType,phone);
+        String smsKeyPrefix = RedisPrefixUtils.getSmsKeyPrefix("modify", phone);
+        String smsKeyCountPrefix = RedisPrefixUtils.getSmsKeyCountPrefix("modify", phone);
 
         //查看验证码是否过期
-        if (redisTemplate.hasKey(map.get("smsKeyCountPrefix")) &&
-                !redisTemplate.hasKey(map.get("keyPrefix"))){
-            return Response.error(500,"验证码已过期,请重新验证!");
+        if (redisTemplate.hasKey(smsKeyCountPrefix) &&
+                !redisTemplate.hasKey(smsKeyPrefix)){
+            throw new CustomException("验证码已过期,请重新验证!");
         }
         //比对缓存中的验证码
-        String smsCodeValue = redisTemplate.opsForValue().get(map.get("keyPrefix")).toString();
+        String smsCodeValue = redisTemplate.opsForValue().get(smsKeyPrefix).toString();
         if (!verificationCode.equals(smsCodeValue)){
-            return Response.error(500,"验证码错误,请重新输入");
+            throw new CustomException("验证码错误,请重新输入!");
         }
 
         UserInfo userInfo = new UserInfo();
@@ -135,102 +137,98 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Response saveUserInformation(UserDto userDto) {
-        try {
 
-            //生成短信缓存前缀
-            Map<String, String> map = RedisPrefixUtils.smsRedisPrefix(userDto.getSmsCodeType(), userDto.getMobile());
+        //生成短信缓存前缀
+        String smsKeyPrefix = RedisPrefixUtils.getSmsKeyPrefix("signUp", userDto.getMobile());
+        String smsKeyCountPrefix = RedisPrefixUtils.getSmsKeyCountPrefix("signUp", userDto.getMobile());
 
-            //查看验证码是否过期
-            if (redisTemplate.hasKey(map.get("smsKeyCountPrefix")) &&
-                    !redisTemplate.hasKey(map.get("keyPrefix"))){
-                return Response.error(500,"验证码已过期,请重新验证!");
-            }
-            //比对缓存中的验证码
-            String smsCodeValue = redisTemplate.opsForValue().get(map.get("keyPrefix")).toString();
-            if (!userDto.getVerificationCode().equals(smsCodeValue)){
-                return Response.error(500,"验证码错误,请重新输入");
-            }
+        //查看验证码是否过期
+        if (redisTemplate.hasKey(smsKeyCountPrefix) &&
+                !redisTemplate.hasKey(smsKeyPrefix)){
+            throw new CustomException("验证码已过期,请重新验证!");
+        }
+        //比对缓存中的验证码
+        String smsCodeValue = redisTemplate.opsForValue().get(smsKeyPrefix).toString();
+        if (!userDto.getVerificationCode().equals(smsCodeValue)){
+            throw new CustomException("验证码错误,请重新输入!");
+        }
 
-            UserInfo userInfo = new UserInfo();
-            //用户信息表
-            userInfo.setName(userDto.getName());
-            userInfo.setSex(userDto.getSex());
-            userInfo.setSfzmlx(1l);
-            userInfo.setIdCard(userDto.getIdCard());
-            userInfo.setMobile(userDto.getMobile());
-            userInfo.setInscode(userDto.getInscode());
-            userInfo.setPxcx(userDto.getPxcx());
-            userInfo.setSchoolClassTypeId(userDto.getSchoolClassTypeId());
-            userInfo.setCoachnum(userDto.getCoachnum());
-            userInfo.setStatus(0);
-            userInfoMapper.insert(userInfo);
-
-
-            //用户报名信息表
-            UserRegister userRegister = new UserRegister();
-            userRegister.setUserId(userInfo.getId());
-            userRegister.setInscode(userDto.getInscode());
-            userRegister.setBmcx(userDto.getPxcx());
-            userRegister.setCoachnum(userDto.getCoachnum());
-            userRegister.setClassId(userDto.getSchoolClassTypeId());
-            userRegisterMapper.insert(userRegister);
-
-            //报名缴费信息
-            UserPay userPay = new UserPay();
-            userPay.setUserId(userInfo.getId());
-            userPay.setPrice(userDto.getSchoolClassTypePrice());
-            userPay.setPayType(4l);
-            userPay.setPayStatus(0l);
-            userPayMapper.insert(userPay);
-
-            //用户图片信息
-            List<UserImages> userImagesList = new ArrayList<>();
-            for (int i = 0; i < 3; i++) {
-                UserImages userImages = new UserImages();
-                userImages.setUserId(userInfo.getId());
-                switch(i){
-                    case 0 :
-                        userImages.setFileUrl(userDto.getIdCardFrontUrl());
-                        userImages.setFilePath(userDto.getIdCardFrontPath());
-                        userImages.setImageType(1l);
-                        userImagesList.add(userImages);
-                        break;
-                    case 1 :
-                        userImages.setFileUrl(userDto.getIdCardReverseUrl());
-                        userImages.setFilePath(userDto.getIdCardReversePath());
-                        userImages.setImageType(2l);
-                        userImagesList.add(userImages);
-                        break;
-                    case 2 :
-                        userImages.setFileUrl(userDto.getFacesUrl());
-                        userImages.setFilePath(userDto.getFacesPath());
-                        userImages.setImageType(3l);
-                        userImagesList.add(userImages);
-                        break;
-                    default :
-                        break;
-                }
+        UserInfo userInfo = new UserInfo();
+        //用户信息表
+        userInfo.setName(userDto.getName());
+        userInfo.setSex(userDto.getSex());
+        userInfo.setSfzmlx(1l);
+        userInfo.setIdCard(userDto.getIdCard());
+        userInfo.setMobile(userDto.getMobile());
+        userInfo.setInscode(userDto.getInscode());
+        userInfo.setPxcx(userDto.getPxcx());
+        userInfo.setSchoolClassTypeId(userDto.getSchoolClassTypeId());
+        userInfo.setCoachnum(userDto.getCoachnum());
+        userInfo.setStatus(0);
+        userInfoMapper.insert(userInfo);
+
+
+        //用户报名信息表
+        UserRegister userRegister = new UserRegister();
+        userRegister.setUserId(userInfo.getId());
+        userRegister.setInscode(userDto.getInscode());
+        userRegister.setBmcx(userDto.getPxcx());
+        userRegister.setCoachnum(userDto.getCoachnum());
+        userRegister.setClassId(userDto.getSchoolClassTypeId());
+        userRegisterMapper.insert(userRegister);
+
+        //报名缴费信息
+        UserPay userPay = new UserPay();
+        userPay.setUserId(userInfo.getId());
+        userPay.setPrice(userDto.getSchoolClassTypePrice());
+        userPay.setPayType(4l);
+        userPay.setPayStatus(0l);
+        userPayMapper.insert(userPay);
+
+        //用户图片信息
+        List<UserImages> userImagesList = new ArrayList<>();
+        for (int i = 0; i < 3; i++) {
+            UserImages userImages = new UserImages();
+            userImages.setUserId(userInfo.getId());
+            switch(i){
+                case 0 :
+                    userImages.setFileUrl(userDto.getIdCardFrontUrl());
+                    userImages.setFilePath(userDto.getIdCardFrontPath());
+                    userImages.setImageType(1l);
+                    userImagesList.add(userImages);
+                    break;
+                case 1 :
+                    userImages.setFileUrl(userDto.getIdCardReverseUrl());
+                    userImages.setFilePath(userDto.getIdCardReversePath());
+                    userImages.setImageType(2l);
+                    userImagesList.add(userImages);
+                    break;
+                case 2 :
+                    userImages.setFileUrl(userDto.getFacesUrl());
+                    userImages.setFilePath(userDto.getFacesPath());
+                    userImages.setImageType(3l);
+                    userImagesList.add(userImages);
+                    break;
+                default :
+                    break;
             }
-            userImagesService.saveBatch(userImagesList);
-
-            return Response.success(userInfo);
-        }catch (Exception e){
-            throw new RuntimeException(e);
         }
+        userImagesService.saveBatch(userImagesList);
+
+        return Response.success(userInfo);
 
     }
 
     /**
      * 注册报名获取短信验证码
-     * @param type
      * @param phone
      * @return
      */
     @Override
-    public Response sendSignUpVerificationCode(String type, String phone) {
+    public Response sendSignUpVerificationCode(String phone) {
         SmsParameter smsParameter = new SmsParameter();
         smsParameter.setPhone(phone);
-        smsParameter.setType(type);
+        smsParameter.setType("signUp");
 
         return smsService.sendVerificationCode(smsParameter);
     }
@@ -239,19 +237,18 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
     /**
      * 修改手机号获取短信验证码
      * @param user
-     * @param type
      * @param phone
      * @return
      */
     @Override
-    public Response sendModifyVerificationCode(SysUser user, String type, String phone) {
+    public Response sendModifyVerificationCode(SysUser user, String phone) {
         if (user.getPhonenumber().equals(phone)){
-            return Response.error(500,"该手机号与当前绑定的手机号相同");
+            throw new CustomException("该手机号与当前绑定的手机号相同!");
         }
 
         SmsParameter smsParameter = new SmsParameter();
         smsParameter.setPhone(phone);
-        smsParameter.setType(type);
+        smsParameter.setType("modify");
 
         return smsService.sendVerificationCode(smsParameter);
     }