VrUserController.java
6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.glsx.usedcar.controller;
import com.glsx.usedcar.module.vrsystem.dto.LoginUserDTO;
import com.glsx.usedcar.module.vrsystem.service.VrUserService;
import com.glsx.usedcar.module.vrsystem.util.PasswordStrengthUtil;
import com.glsx.usedcar.module.vrsystem.vo.*;
import com.glsx.usedcar.resp.BaseResp;
import com.glsx.usedcar.util.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.summerframework.core.enums.ErrorCodeEnum;
import org.summerframework.core.exception.BusinessCodeException;
import java.util.Map;
import java.util.Optional;
/**
* VR用户管理接口
*/
@Slf4j
@RestController
@RequestMapping("/rest/system/user")
@RequiredArgsConstructor
@Tag(name = "用户管理接口", description = "用户的增删改查、状态管理、密码重置等操作")
public class VrUserController {
private final VrUserService vrUserService;
@PostMapping("/page")
@Operation(summary = "分页查询用户", description = "支持按手机号、姓名、角色ID、状态筛选")
public BaseResp<Page<VrUserPageRespVO>> pageUsers(@RequestBody VrUserPageQueryVO queryVO) {
return BaseResp.success(vrUserService.pageUsers(queryVO));
}
@GetMapping("/detail")
@Operation(summary = "用户详情", description = "查询用户基本信息及关联的角色、组织信息")
public BaseResp<VrUserDetailRespVO> getUserDetail(
@Parameter(description = "用户ID", required = true, example = "1") @RequestParam Long id) {
return BaseResp.success(vrUserService.getUserDetail(id));
}
@PostMapping("/add")
@Operation(summary = "新增用户", description = "新增用户,默认密码为手机号后6位")
public BaseResp<Void> addUser(@Valid @RequestBody VrUserAddVO addVO) {
vrUserService.addUser(addVO);
return BaseResp.success();
}
@PostMapping("/update/{id}")
@Operation(summary = "修改用户", description = "修改用户基本信息及关联的角色、组织关系")
public BaseResp<Void> updateUser(
@Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long id,
@Valid @RequestBody VrUserUpdateVO updateVO) {
vrUserService.updateUser(id, updateVO);
return BaseResp.success();
}
@PostMapping("/updateStatus")
@Operation(summary = "启用/停用用户", description = "切换用户状态,1=正常 0=停用。禁用用户时会强制退出所有登录设备")
public BaseResp<Void> updateStatus(@Valid @RequestBody VrUserStatusVO statusVO) {
vrUserService.updateStatus(statusVO.getId(), statusVO.getStatus());
return BaseResp.success();
}
@PostMapping("/resetPassword")
@Operation(summary = "重置密码", description = "将用户密码重置为手机号后6位")
public BaseResp<Void> resetPassword(
@Parameter(description = "用户ID", required = true, example = "1") @RequestParam Long id) {
vrUserService.resetPassword(id);
return BaseResp.success();
}
@PostMapping("/updatePassword/{id}")
@Deprecated
@Operation(summary = "修改密码", description = "修改用户登录密码,密码长度6-20位,支持大小写字母、数字及特殊字符", deprecated = true)
public BaseResp<Void> updatePassword(
@Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long id,
@Valid @RequestBody VrUserPasswordVO passwordVO) {
vrUserService.updatePassword(id, passwordVO);
return BaseResp.success();
}
@DeleteMapping("/delete/{id}")
@Operation(summary = "删除用户", description = "逻辑删除用户并清除关联的角色、组织关系")
public BaseResp<Void> deleteUser(
@Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long id) {
vrUserService.deleteUser(id);
return BaseResp.success();
}
@GetMapping("/summary")
@Operation(summary = "用户统计", description = "统计正常和停用用户数量")
public BaseResp<Map<String, Long>> summary() {
return BaseResp.success(vrUserService.summary());
}
/**
* ==================== 当前登录用户接口 ====================
*/
@PostMapping("/changePassword")
@Operation(summary = "修改密码", description = "当前登录用户修改自己的密码,需验证旧密码,成功后强制退出所有登录设备")
public BaseResp<Void> changePassword(@Valid @RequestBody VrUserChangePasswordVO passwordVO) {
// 前端校验:新密码与确认密码一致性
if (!passwordVO.getNewPassword().equals(passwordVO.getConfirmPassword())) {
throw new BusinessCodeException(ErrorCodeEnum.VR_PASSWORD_NOT_SAME);
}
// 获取当前登录用户
LoginUserDTO currentUser = Optional.ofNullable(UserContext.getUser()).orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.LOGIN_ERROR));
vrUserService.changePassword(currentUser.getUserId(), passwordVO);
return BaseResp.success();
}
@GetMapping("/passwordStrength")
@Operation(summary = "密码强度评估", description = "实时评估密码强度等级:弱(红色)/中(橙色)/强(绿色)")
public BaseResp<Map<String, String>> evaluatePasswordStrength(
@Parameter(description = "密码", required = true, example = "Pass123!")
@NotBlank(message = "密码不能为空")
@Size(min = 6, max = 20, message = "密码长度必须在6-20个字符之间")
@RequestParam String password) {
PasswordStrengthUtil.StrengthLevel level = PasswordStrengthUtil.evaluate(password);
return BaseResp.success(Map.of(
"level", level.name(),
"label", level.getLabel(),
"color", level.getColor()
));
}
public static void main(String[] var0) {
BCryptPasswordEncoder var1 = new BCryptPasswordEncoder();
String[] var2 = new String[]{"15770727626"};
for(String var6 : var2) {
String var7 = var6.substring(var6.length() - 6);
System.out.println(var6 + "|" + var7 + "|" + var1.encode(var7));
}
}
}