From 7ac385ed8cb65746b9f1f3d8994f03b3d9de7ac1 Mon Sep 17 00:00:00 2001
From: xusd <330628789@qq.com>
Date: Tue, 01 Jul 2025 22:46:24 +0800
Subject: [PATCH] fix:优化登录

---
 dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtAccountService.java |   36 +++++++++++++++++++++++++++++-------
 1 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtAccountService.java b/dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtAccountService.java
index bc1888a..6fd2eb4 100644
--- a/dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtAccountService.java
+++ b/dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtAccountService.java
@@ -29,10 +29,7 @@
 
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * @author liyj
@@ -201,7 +198,7 @@
     public Boolean checkCredential(String cipher, CtAccount ctAccount) {
         String credentialMd5 = DigestUtils.md5DigestAsHex(cipher.getBytes());
         log.info("xsd:{}", credentialMd5);
-        if (StringUtils.equals(credentialMd5, ctAccount.getCipher())) {
+        if (StringUtils.equals(cipher, ctAccount.getCipher())) {
             return true;
         } else {
 //            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
@@ -235,9 +232,9 @@
             // 判断账号是否存在
             CtAccount ctAccount = this.getByAccAndType(ctAccountLoginDTO.getAcc(), UserBaseConsts.ACC_TYPE_1);
             if (ObjectUtils.isNotEmpty(ctAccount)) {
+                Date nowDate = new Date();
                 // 判断账号是否被锁定
                 if (ObjectUtils.isNotEmpty(ctAccount.getLimitTime())) {
-                    Date nowDate = new Date();
                     if (nowDate.before(ctAccount.getLimitTime())) {
                         SimpleDateFormat sdf = new SimpleDateFormat("HH点mm分");
                         return ReturnFailUtils.getRepInfo("账号已锁定,请于" + sdf.format(ctAccount.getLimitTime()) + "后重试,或者请联系管理员解锁!");
@@ -245,6 +242,13 @@
                 }
                 // 判断密码是否正确
                 if (checkCredential(ctAccountLoginDTO.getCipher(), ctAccount)) {
+                    // 密码正确:重置错误次数
+                    UpdateWrapper<CtAccount> resetPwdErr = new UpdateWrapper<>();
+                    resetPwdErr.set("pwd_error_count", 0)
+                            .set("limit_time", null)
+                            .set("login_time", DateUtils.getNowDate())
+                            .eq("id", ctAccount.getId());
+                    this.update(resetPwdErr);
                     //返回结果
                     UserLoginDTO userLoginDTO = new UserLoginDTO();
                     CtUser ctUser = ctUserService.getById(ctAccount.getUserId());
@@ -307,7 +311,25 @@
                     this.update(accountUpdateWrapper);
                     return ReturnSucUtils.getRepInfo(userLoginDTO);
                 } else {
-                    return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!");
+                    // 密码错误:增加错误次数,必要时冻结
+                    int currentErrCount = ctAccount.getPwdErrorCount() == null ? 0 : ctAccount.getPwdErrorCount();
+                    currentErrCount++;
+
+                    UpdateWrapper<CtAccount> updateWrapper = new UpdateWrapper<>();
+                    updateWrapper.set("pwd_error_count", currentErrCount).eq("id", ctAccount.getId());
+
+                    if (currentErrCount >= 5) {
+                        // 冻结5分钟
+                        Calendar cal = Calendar.getInstance();
+                        cal.setTime(nowDate);
+                        cal.add(Calendar.MINUTE, 5);
+                        updateWrapper.set("limit_time", cal.getTime());
+                        this.update(updateWrapper);
+                        return ReturnFailUtils.getRepInfo("密码错误已达5次,账号已锁定5分钟,请稍后再试!");
+                    } else {
+                        this.update(updateWrapper);
+                        return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!(" + currentErrCount + "/5)");
+                    }
                 }
             } else {
                 return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!");

--
Gitblit v1.8.0