LingChuan.Xiang 1 місяць тому
батько
коміт
a99aeb5012

+ 41 - 0
platomix-gmetry-system-api/src/main/java/org/springblade/common/utils/PasswordGeneratorUtil.java

@@ -0,0 +1,41 @@
+package org.springblade.common.utils;
+
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class PasswordGeneratorUtil {
+
+    private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
+    private static final String DIGITS = "0123456789";
+    private static final String SPECIAL_CHARS = "!@#$%^&*()_+[]{}|;:'\",.<>?/~`\\-=";
+
+    private static final int MIN_LENGTH = 10;
+    private static final SecureRandom RANDOM = new SecureRandom();
+
+    public static String generatePassword(int length) {
+        if (length < MIN_LENGTH) {
+            throw new IllegalArgumentException("Password length must be at least " + MIN_LENGTH + " characters.");
+        }
+        List<Character> passwordChars = new ArrayList<>();
+
+        passwordChars.add(UPPERCASE.charAt(RANDOM.nextInt(UPPERCASE.length())));
+        passwordChars.add(LOWERCASE.charAt(RANDOM.nextInt(LOWERCASE.length())));
+        passwordChars.add(DIGITS.charAt(RANDOM.nextInt(DIGITS.length())));
+        passwordChars.add(SPECIAL_CHARS.charAt(RANDOM.nextInt(SPECIAL_CHARS.length())));
+        String allChars = UPPERCASE + LOWERCASE + DIGITS + SPECIAL_CHARS;
+        for (int i = 4; i < length; i++) {
+            passwordChars.add(allChars.charAt(RANDOM.nextInt(allChars.length())));
+        }
+        // Shuffle the characters to ensure randomness
+        Collections.shuffle(passwordChars);
+        // Convert list to string
+        StringBuilder password = new StringBuilder();
+        for (char c : passwordChars) {
+            password.append(c);
+        }
+        return password.toString();
+    }
+}

+ 8 - 2
platomix-gmetry-system-api/src/main/java/org/springblade/modules/system/controller/UserController.java

@@ -31,6 +31,7 @@ import javax.validation.Valid;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import org.springblade.common.excel.utils.ExcelUtil;
 import org.springblade.common.utils.CacheUtil;
+import org.springblade.common.utils.PasswordGeneratorUtil;
 import org.springblade.core.constant.AppConstant;
 import org.springblade.core.constant.BladeConstant;
 import org.springblade.core.constant.RoleConstant;
@@ -269,8 +270,13 @@ public class UserController {
 	@ApiOperation(value = "初始化密码", notes = "传入userId集合")
 	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
 	public R resetPassword(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds) {
-		boolean temp = userService.resetPassword(userIds);
-		return R.status(temp);
+		String newPassword = PasswordGeneratorUtil.generatePassword(12);
+		boolean temp = userService.resetPasswordV2(userIds, newPassword);
+		if(temp){
+			return R.data("重置密码成功,新密码为:"+newPassword);
+		}else{
+			return R.fail("重置密码失败.");
+		}
 	}
 
 	/**

+ 8 - 0
platomix-gmetry-system-api/src/main/java/org/springblade/modules/system/service/IUserService.java

@@ -152,6 +152,14 @@ public interface IUserService extends BaseService<User> {
 	boolean resetPassword(String userIds);
 
 	/**
+	 * 初始化密码
+	 *
+	 * @param userIds
+	 * @return
+	 */
+	boolean resetPasswordV2(String userIds, String newPassword);
+
+	/**
 	 * 修改密码
 	 *
 	 * @param userId

+ 9 - 0
platomix-gmetry-system-api/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java

@@ -329,6 +329,15 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
 		return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds)));
 	}
 
+
+	@Override
+	public boolean resetPasswordV2(String userIds, String newPassword) {
+		User user = new User();
+		user.setPassword(DigestUtil.encrypt(newPassword));
+		user.setUpdateTime(new Date());
+		return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds)));
+	}
+
 	@Override
 	public boolean updatePassword(Long userId, String oldPassword, String newPassword, String newPassword1) {
 		User user = getById(userId);