lrf 8 hónapja
szülő
commit
39816b0ac2

+ 25 - 0
src/main/java/com/free/controller/LoginController.java

@@ -24,6 +24,7 @@ import com.free.config.ExceptionEnum;
 import com.free.config.ResponseFormat;
 import com.free.dto.LoginDTO;
 import com.free.dto.PasswordUpdateDTO;
+import com.free.dto.ResetPasswordDTO;
 import com.free.entity.system.Admin;
 import com.free.entity.system.Customer;
 import com.free.entity.system.Role;
@@ -33,6 +34,7 @@ import com.free.service.system.MenusService;
 import com.free.service.system.RoleService;
 import com.free.utils.BcryptUtil;
 import com.free.utils.JwtUtil;
+import com.free.utils.Utils;
 
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -138,6 +140,29 @@ public class LoginController {
     return ResponseFormat.success();
   }
 
+  @ApiOperation("重置密码")
+  @PostMapping("/resetPwd")
+  public Object resetPassword(@RequestBody @Valid ResetPasswordDTO data) {
+    String type = data.getType();
+    String password = Utils.randomString(6);
+    String cpwd = BcryptUtil.encryptPassword(password);
+    if (this.adminLoginType.equals(type)) {
+      Admin adminData = new Admin();
+      adminData.setId(data.getId());
+      adminData.setPassword(cpwd);
+      this.adminService.updateById(adminData);
+    } else if (this.csLoginType.equals(type)) {
+      Customer customerData = new Customer();
+      customerData.setId(data.getId());
+      customerData.setPassword(cpwd);
+      this.customerService.updateById(customerData);
+    } else {
+      // 用户类型不对,抛出异常
+      throw new CustomizationException(ExceptionEnum.USER_TYPE_ERROR);
+    }
+    return ResponseFormat.success();
+  }
+
   /**
    * 根据token,查询出用户的权限,目录
    * 

+ 15 - 0
src/main/java/com/free/dto/ResetPasswordDTO.java

@@ -0,0 +1,15 @@
+package com.free.dto;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+import lombok.Data;
+
+@Data
+public class ResetPasswordDTO {
+  @NotNull(message = "缺少用户信息")
+  private Long id;
+
+  @NotBlank(message = "缺少用户类型")
+  private String type;
+}

+ 12 - 0
src/main/java/com/free/utils/Utils.java

@@ -9,10 +9,22 @@ import java.io.Reader;
 import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
 
 import org.springframework.util.ResourceUtils;
 
 public class Utils {
+
+  public static String randomString(int length) {
+    UUID randomUUID = UUID.randomUUID();
+    String str = randomUUID.toString().replaceAll("-", "");
+    if (length >= 32) {
+      length = 32;
+    }
+    str = str.substring(0, length);
+    return str;
+  }
+
   // 对象转Map
   public static Map<String, Object> objectToMap(Object object) {
     Map<String, Object> dataMap = new HashMap<>();