chendayu 2 rokov pred
rodič
commit
5893ad3e13

+ 12 - 0
PCS/src/main/java/cn/cslg/permission/common/GlobalException.java

@@ -3,7 +3,9 @@ package cn.cslg.permission.common;
 import cn.cslg.permission.common.core.exception.PermissionException;
 import cn.cslg.permission.common.utils.Response;
 import cn.cslg.permission.common.utils.ResponseEnum;
+import cn.cslg.permission.exception.XiaoShiException;
 import cn.dev33.satoken.exception.NotLoginException;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 
@@ -13,6 +15,7 @@ import javax.servlet.http.HttpServletResponse;
 /**
  * 全局异常处理
  */
+@Slf4j
 @RestControllerAdvice // 可指定包前缀,比如:(basePackages = "com.pj.admin")
 public class GlobalException {
 
@@ -29,4 +32,13 @@ public class GlobalException {
             return Response.error(ResponseEnum.SYSTEM_ERROR);
         }
     }
+
+    //小世异常
+    @ExceptionHandler
+    public Response handlerXiaoShiException(XiaoShiException e) {
+        log.info("全局异常处理机制捕获到XiaoShiException,异常信息提示为:{}", e.getMessage());
+        return Response.fail(e.getMessage());
+
+    }
+
 }

+ 8 - 0
PCS/src/main/java/cn/cslg/permission/common/utils/Response.java

@@ -44,6 +44,14 @@ public class Response {
         return JsonUtils.objectToJson(response);
     }
 
+    public static Response fail(Object data) {
+        Response response = new Response();
+        response.setCode(ResponseEnum.ERROR.getCode());
+        response.setMessage(data + "");
+        response.setData(data);
+        return response;
+    }
+
     public static String error(String message) {
         Response response = new Response();
         response.setCode(ResponseEnum.ERROR.getCode());

+ 12 - 0
PCS/src/main/java/cn/cslg/permission/controller/LoginController.java

@@ -9,6 +9,7 @@ import cn.dev33.satoken.stp.StpUtil;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.web.bind.annotation.*;
 
@@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
  * @description 登录 Controller 层
  */
 
+@Slf4j
 @Tag(name = "登录管理")
 @RestController
 @RequestMapping(Constants.PERMISSION_API + "/admin")
@@ -57,4 +59,14 @@ public class LoginController {
     public String reSetPassword(String userId) {
         return loginService.reSetPassword(userId);
     }
+
+    @Operation(summary = "登陆前重置密码")
+    @GetMapping("/reSetPasswordBeforeLogin")
+    public String reSetPasswordBeforeLogin(String personnelUserName) {
+        log.info("开始处理【登录前重置密码】的请求,请求参数为:{}", personnelUserName);
+
+        loginService.reSetPasswordBeforeLogin(personnelUserName);
+        return Response.success();
+    }
+
 }

+ 41 - 0
PCS/src/main/java/cn/cslg/permission/service/LoginService.java

@@ -20,6 +20,7 @@ import cn.hutool.crypto.SecureUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
@@ -32,6 +33,7 @@ import java.util.concurrent.TimeUnit;
  * @description 登录 Service 层
  */
 
+@Slf4j
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
@@ -217,4 +219,43 @@ public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
         return Response.success(true);
     }
 
+    /**
+     * 登录前重置密码
+     *
+     * @param personnelUserName 人员账号
+     */
+    public void reSetPasswordBeforeLogin(String personnelUserName) {
+        log.info("开始处理【登录前重置密码】的业务,参数为:{}", personnelUserName);
+
+        //检查账号是否存在,若不存在则返回提示"账号不存在,请检查账号是否输入正确"
+        Personnel personnel = this.getOne(new LambdaQueryWrapper<Personnel>().eq(Personnel::getPersonnelUserName, personnelUserName).last("LIMIT 1"));
+        if (personnel == null) {
+            ThrowException.throwXiaoShiException("该账号不存在或已被禁用,请检查账号是否输入正确");
+            return;
+        }
+
+        //检查邮箱是否存在,若没有邮箱则返回提示"该账号未绑定邮箱无法重置密码,您可在登陆后进行密码重置"
+        String personnelEmail = personnel.getPersonnelEmail();
+        if (personnelEmail == null || personnelEmail.trim().equals("")) {
+            ThrowException.throwXiaoShiException("该账号未绑定邮箱无法重置密码,您可在登陆后进行密码重置");
+        }
+
+        //重置密码,发送邮件通知
+        int tem = (int) ((Math.random() * 9 + 1) * 100000);
+        String newPassword = String.valueOf(tem);
+        personnel.setPersonnelPassword(SecureUtil.md5(newPassword));
+        if (personnel.updateById()) {
+            Map<String, Object> map = new HashMap<>();
+            map.put("title", "密码重置通知");
+            map.put("template", "mail/email.html");
+            map.put("value1", newPassword);
+            map.put("img", "/logo.png");
+            map.put("email", personnelEmail);
+            map.put("value2", personnel.getPersonnelName());
+            mailUtils.sendEmailMessage(map);
+        }
+
+
+    }
+
 }