Kaynağa Gözat

密码登录

hurixing 1 yıl önce
ebeveyn
işleme
ea48b50fe9

+ 15 - 1
hcp-app/src/main/java/com/yingyangfly/app/controller/LoginController.java

@@ -34,6 +34,13 @@ public class LoginController {
         return ResultResponse.success(appUserService.login(dto));
     }
 
+    @TraceLog
+    @PostMapping("/reset/password")
+    public ResultResponse resetPassWord(String username,String newPass,String code) {
+        return ResultResponse.success(appUserService.resetPassWordApp(username,newPass,code));
+    }
+
+
     @Log(title = "手机短信登录",operatorType = OperatorType.MOBILE)
     @ApiOperation("手机短信登录")
     @PostMapping("/loginMsg")
@@ -45,13 +52,20 @@ public class LoginController {
         return appUserService.loginMsg(checkCode,mobile);
     }
 
-    @ApiOperation("手机短信换取")
+    @ApiOperation("手机短信换取登录")
     @PostMapping("/getCheckCode")
     @TraceLog
     public ResultResponse getCheckCode(String mobile){
         return appUserService.getCheckCode(mobile);
     }
 
+    @ApiOperation("手机短信换取重置密码")
+    @PostMapping("/getChangePasswordCode")
+    @TraceLog
+    public ResultResponse getChangePasswordCode(String mobile) {
+        return appUserService.getChangePasswordCode(mobile);
+    }
+
     @Log(title = "退出登录",operatorType = OperatorType.MOBILE)
     @ApiOperation("退出")
     @PostMapping("logout")

+ 1 - 0
hcp-app/src/main/resources/application.yml

@@ -37,3 +37,4 @@ security:
       - /game_voice/**
       - /app/learn/package/get/list
       - /app/pay/white/list/createPreOrder
+      - /app/getChangePasswordCode

+ 2 - 1
hcp-core/src/main/java/com/yingyangfly/core/enums/MsgTemplateEnums.java

@@ -9,7 +9,8 @@ public enum MsgTemplateEnums {
     /**
      * 登录验证码
      */
-    GET_CHECK_CODE("CHECK_CODE", "SMS_470580061");
+    GET_CHECK_CODE("CHECK_CODE", "SMS_470580061"),
+    CHANGE_PASSWORD_CODE("CHANGE_PASSWORD_CODE","SMS_483175131");
     private final String name;
     private final String tempalteCode;
 

+ 120 - 5
hcp-core/src/main/java/com/yingyangfly/core/service/impl/AppUserService.java

@@ -25,10 +25,7 @@ import com.yingyangfly.core.mapper.*;
 import com.yingyangfly.core.recommend.RecommendFacade;
 import com.yingyangfly.core.security.util.TokenUtil;
 import com.yingyangfly.core.service.*;
-import com.yingyangfly.core.util.AmountUtils;
-import com.yingyangfly.core.util.EntityConverter;
-import com.yingyangfly.core.util.Sm4Util;
-import com.yingyangfly.core.util.SmgUtil;
+import com.yingyangfly.core.util.*;
 import com.yingyangfly.core.vo.WxPayJsApiPreVO;
 import com.yingyangfly.redis.client.RedisClient;
 import lombok.extern.slf4j.Slf4j;
@@ -157,10 +154,36 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
     @Transactional(rollbackFor = Exception.class)
     public String login(LoginDto dto) {
         AppCurrentLoginUser userDetails = (AppCurrentLoginUser) loadUserByUsername(dto.getLoginName());
+        if (ObjectUtils.isNull(userDetails.getOrderEndTime())){
+            throw new BadCredentialsException("请充值后在进行登录");
+        }
+        if (LocalDate.parse(userDetails.getOrderEndTime()).isBefore(LocalDate.now())){
+            throw new BadCredentialsException("你的套餐已到期,请充值后登录!");
+        }
+
+        String errorNumKey = "app:error:num:"+dto.getLoginName();
+        String errorNumRedis = redisClient.get(errorNumKey, "");
+        if (ObjectUtils.isNotNull(errorNumRedis)){
+            if (Integer.parseInt(errorNumRedis) >= 3){
+                throw new BadCredentialsException("密码错误了3次,账户锁定10分钟");
+            }
+        }
+        dto.setPassWord(CryptoUtil.decrypt(dto.getPassWord()));
         if (!bCryptPasswordEncoder.matches(dto.getPassWord(), userDetails.getPassword())) {
+            Integer errorNum = 0;
+            if (ObjectUtils.isNotNull(errorNumRedis)){
+                errorNum = Integer.parseInt(errorNumRedis);
+            }
+            errorNum++;
+            redisClient.set(errorNumKey,String.valueOf(errorNum),600);
             throw new BadCredentialsException("密码不正确");
         }
         String token = tokenUtil.generateToken(userDetails, dto.getRememberMe(),"app");
+        String tokenRedis = redisClient.get(String.format("%s%s", "token:app:", dto.getLoginName()),"");
+        if (StringUtils.isNotBlank(tokenRedis)){
+            redisClient.del(String.format("%s%s", "token:app:", dto.getLoginName()));
+        }
+        redisClient.set(String.format("%s%s", "token:app:", dto.getLoginName()),token);
         //记录登录历史
         LoginRecord loginRecord = new LoginRecord();
         loginRecord.setCreateTime(new Date());
@@ -174,6 +197,65 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
         return token;
     }
 
+    /**
+     * app 忘记密码重置
+     * @param username
+     * @param newPass
+     * @param code
+     * @return
+     */
+    public Boolean resetPassWordApp(String username,String newPass,String code) {
+        // 判断验证码是否正确
+        String redisKey = "hcp:changePass:mobile:" + username;
+        String checkCodeRedis = redisClient.get(redisKey, "");
+        if (StringUtils.isEmpty(checkCodeRedis)) {
+            throw new BadCredentialsException("验证码已经过期");
+        }
+        if (!checkCodeRedis.equals(code)) {
+            throw new BadCredentialsException("验证码输入错误");
+        }
+        String encrypt = Sm4Util.encrypt(username);
+        AppUser appUser = selectByMobile(encrypt, "0");
+        if (appUser == null){
+            throw new ServiceException("手机号:" + username + " 不存在");
+        }
+        AppUser appUserUpdate = new AppUser();
+        appUserUpdate.setId(appUser.getId());
+        String pwd = CryptoUtil.decrypt(newPass);
+        appUserUpdate.setPwd(bCryptPasswordEncoder.encode(pwd));
+        int i = appUserMapper.updateById(appUserUpdate);
+        // 清除缓存
+        cacheUserService.delAppUser(username);
+        if (i>0) {
+            return true;
+        }else {
+            return false;
+        }
+    }
+
+    /**
+     * 后台重置密码
+     * @param appUser
+     * @return
+     */
+    public Boolean resetPassWordSys(AppUser appUser) {
+        AppUser appUserUpdate = new AppUser();
+        appUserUpdate.setId(appUser.getId());
+        if (ObjectUtils.isNotNull(appUser.getPwd())) {
+            appUser.setPwd(CryptoUtil.decrypt(appUser.getPwd()));
+        }
+        appUserUpdate.setPwd(bCryptPasswordEncoder.encode(appUser.getPwd()));
+        int i = appUserMapper.updateById(appUserUpdate);
+        if (i>0) {
+            AppUser appUser1 = appUserMapper.selectById(appUser.getId());
+            // 清除缓存
+            cacheUserService.delAppUser(Sm4Util.decrypt(appUser1.getMobile()));
+            return true;
+        }else {
+            return false;
+        }
+    }
+
     /**
      * 退出
      */
@@ -634,7 +716,7 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
         Boolean isSuccess = true;
         if (isSuccess) {
             // 防机器
-            redisClient.set("hcp:sms:mobile:"+mobile,random,60);
+            redisClient.set("hcp:sms:mobile:"+mobile,random,120);
             //存入redis
             redisClient.set("hcp:mobile:" + mobile, random, 300);
             return ResultResponse.success(true);
@@ -644,6 +726,39 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
 
     }
 
+
+    public ResultResponse getChangePasswordCode(String mobile) {
+        String redisKey = "hcp:sms:changePass:mobile:" + mobile;
+        String checkCodeRedis = redisClient.get(redisKey, "");
+        if (!StringUtils.isEmpty(checkCodeRedis)) {
+            return ResultResponse.fail("请不要频繁发送验证码");
+        }
+        String encrypt = Sm4Util.encrypt(mobile);
+        AppUser appUser = this.selectByMobile(encrypt,"0");
+        if (appUser == null) {
+            return ResultResponse.fail("患者不存在");
+        }
+        if (ObjectUtils.isNull(appUser.getOrderEndTime())){
+            return ResultResponse.success(false);
+        }
+        if (LocalDate.parse(appUser.getOrderEndTime()).isBefore(LocalDate.now())){
+            return ResultResponse.success(false);
+        }
+        Random rand = new Random();
+        int randomNumber = rand.nextInt(1000000);
+        String random = String.format("%06d", randomNumber);
+        Boolean isSuccess = SmgUtil.sendCheckCode(mobile, MsgTemplateEnums.GET_CHECK_CODE.getTempalteCode(), random);
+        if (isSuccess) {
+            // 防机器
+            redisClient.set("hcp:sms:changePass:mobile:"+mobile,random,120);
+            //存入redis
+            redisClient.set("hcp:changePass:mobile:" + mobile, random, 300);
+            return ResultResponse.success(true);
+        } else {
+            return ResultResponse.fail("请不要频繁发送验证码");
+        }
+    }
+
     public List<GameDto> findByGameNameAndUserId(String gameName,String playClass,Long userId,String status) {
 
         List<GameDto> gameDtoList = new ArrayList<>();

+ 6 - 0
hcp-platform/src/main/java/com/yingyangfly/platform/controller/PatientController.java

@@ -194,5 +194,11 @@ public class PatientController {
         return ResultResponse.success();
     }
 
+    @PostMapping("/reset/password")
+    @TraceLog
+    public ResultResponse resetPasswordSys(@RequestBody AppUser appUser) {
+        return ResultResponse.success(appUserService.resetPassWordSys(appUser));
+    }
+
 
 }