From 6e2de461579dccbfe6e723bfc69960a25b0066e9 Mon Sep 17 00:00:00 2001 From: Chaos Date: Sat, 29 Nov 2025 09:01:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(user):=20=E9=87=8D=E6=9E=84=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=E4=B8=8E=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改application.yaml中logic-not-delete-value为"null" - 删除RoleController中无用的RoleResponse导入 - 修改RoleController的@GetMapping注解路径为/options - UserController使用构造器注入替代@Autowired并添加@RequiredArgsConstructor - UserController的@RequestMapping从/api/user改为/api/users - UserController的获取用户列表接口路径从/all改为/并增加分页参数说明 - UserController新增根据ID获取用户信息接口GET /{id} - UserController的重置用户密码接口路径从/setUserPassword改为/{userId}/password - 删除UserController中已废弃的更新用户名接口/updateUsername - UserController的获取当前用户信息接口路径从/profile改为/me - UserController的更新用户信息接口路径从/profile改为/me - UserController新增更新用户名接口PUT /me/username并标记为@Deprecated - UserController的设置用户昵称接口路径改为/{userId}/nickname - UserProfileService接口中的findUserWithRoles方法重命名为findUserWithRolesByUsername - UserProfileService接口新增findUserWithRolesById方法定义 - UserProfileServiceImpl实现类中删除无用RoleResponse导入 - UserProfileServiceImpl中findUserWithRoles方法重命名为findUserWithRolesByUsername - UserProfileServiceImpl中实现findUserWithRolesById方法逻辑并增加空值校验 --- .../chaos_api/service/UserProfileService.java | 10 +- .../service/impl/UserProfileServiceImpl.java | 16 +++- .../chaos_api/controller/RoleController.java | 3 +- .../chaos_api/controller/UserController.java | 96 ++++++++++--------- .../src/main/resources/application.yaml | 2 +- 5 files changed, 77 insertions(+), 50 deletions(-) diff --git a/chaos_api_interface/src/main/java/cn/nopj/chaos_api/service/UserProfileService.java b/chaos_api_interface/src/main/java/cn/nopj/chaos_api/service/UserProfileService.java index 4a0a10f..b034a21 100644 --- a/chaos_api_interface/src/main/java/cn/nopj/chaos_api/service/UserProfileService.java +++ b/chaos_api_interface/src/main/java/cn/nopj/chaos_api/service/UserProfileService.java @@ -52,7 +52,7 @@ public interface UserProfileService { * @param username 用户名 * @return 用户信息 */ - UserProfileResponse findUserWithRoles(String username); + UserProfileResponse findUserWithRolesByUsername(String username); /** * 设置用户昵称 @@ -90,4 +90,12 @@ public interface UserProfileService { * @return 所有用户信息 */ IPage getAllUsers(Integer pageNum, Integer pageSize); + + /** + * 根据用户ID查询用户信息 + * + * @param id 用户ID + * @return 用户信息 + */ + UserProfileResponse findUserWithRolesById(Long id); } diff --git a/chaos_api_service/src/main/java/cn/nopj/chaos_api/service/impl/UserProfileServiceImpl.java b/chaos_api_service/src/main/java/cn/nopj/chaos_api/service/impl/UserProfileServiceImpl.java index 1d99e02..2972614 100644 --- a/chaos_api_service/src/main/java/cn/nopj/chaos_api/service/impl/UserProfileServiceImpl.java +++ b/chaos_api_service/src/main/java/cn/nopj/chaos_api/service/impl/UserProfileServiceImpl.java @@ -4,7 +4,6 @@ import cn.nopj.chaos_api.common.constants.ErrorCode; import cn.nopj.chaos_api.common.exceotion.BizException; import cn.nopj.chaos_api.domain.entity.User; import cn.nopj.chaos_api.dto.request.UserProfileUpdateRequest; -import cn.nopj.chaos_api.dto.response.RoleResponse; import cn.nopj.chaos_api.dto.response.UserProfileResponse; import cn.nopj.chaos_api.mapper.UserMapper; import cn.nopj.chaos_api.service.UserProfileService; @@ -81,7 +80,7 @@ public class UserProfileServiceImpl implements UserProfileService { } @Override - public UserProfileResponse findUserWithRoles(String username) { + public UserProfileResponse findUserWithRolesByUsername(String username) { if (username == null || username.isEmpty()){ throw new BizException(ErrorCode.USER_NOT_EXISTS); } @@ -160,8 +159,21 @@ public class UserProfileServiceImpl implements UserProfileService { return userPage.convert(UserProfileResponse::new); } + @Override + public UserProfileResponse findUserWithRolesById(Long id) { + if (id == null){ + throw new BizException(ErrorCode.USER_ID_INVALID); + } + User userWithRolesById = userMapper.findUserWithRolesById(id); + + if (userWithRolesById == null){ + throw new BizException(ErrorCode.USER_NOT_EXISTS); + } + + return new UserProfileResponse(userWithRolesById); + } } diff --git a/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/RoleController.java b/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/RoleController.java index 736f711..0d0a3c8 100644 --- a/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/RoleController.java +++ b/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/RoleController.java @@ -3,7 +3,6 @@ package cn.nopj.chaos_api.controller; import cn.nopj.chaos_api.dto.request.SetUserRoleRequest; import cn.nopj.chaos_api.dto.response.OptionResponse; -import cn.nopj.chaos_api.dto.response.RoleResponse; import cn.nopj.chaos_api.model.ApiResult; import cn.nopj.chaos_api.service.RoleService; import cn.nopj.chaos_api.service.UserRoleService; @@ -34,7 +33,7 @@ public class RoleController { /** * 获取用户角色选项 */ - @GetMapping + @GetMapping("/options") public ApiResult> getUserRole() { return ApiResult.success(roleService.getAllRoles()); diff --git a/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/UserController.java b/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/UserController.java index 7a76a0f..7aebf75 100644 --- a/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/UserController.java +++ b/chaos_api_web/src/main/java/cn/nopj/chaos_api/controller/UserController.java @@ -9,14 +9,12 @@ import cn.nopj.chaos_api.dto.response.UserProfileResponse; import cn.nopj.chaos_api.model.ApiResult; import cn.nopj.chaos_api.service.UserProfileService; import com.baomidou.mybatisplus.core.metadata.IPage; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.List; - /** * 用户管理 * @@ -24,73 +22,83 @@ import java.util.List; */ @Slf4j @RestController -@RequestMapping("/api/user") +@RequestMapping("/api/users") +@RequiredArgsConstructor public class UserController { - @Autowired - private UserProfileService userProfileService; + private final UserProfileService userProfileService; /** - * 获取所有用户信息 - * @return 所有用户信息 + * 获取用户列表 (分页) + * URL: GET /api/users?pageNum=1&pageSize=10 */ @PreAuthorize("hasAuthority('admin')") - @GetMapping("/all") - ApiResult> getAllUsers(@RequestParam(defaultValue = "1" , name = "pageNum") Integer pageNum, @RequestParam(defaultValue = "10", name="pageSize") Integer pageSize){ + @GetMapping + public ApiResult> listUsers( + @RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum, + @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize) { return ApiResult.success(userProfileService.getAllUsers(pageNum, pageSize)); } - /** - * 设置用户密码 - * @param request 设置用户密码请求 - * @return 设置用户密码结果 - */ - @PreAuthorize("hasAuthority('admin')") - @PutMapping("/setUserPassword") - ApiResult setUserPassword(@RequestBody @Validated SetUserPasswordRequest request){ - userProfileService.setUserPassword(request.getUserId(), request.getPassword()); - return ApiResult.success("用户密码修改成功"); + @GetMapping("/{id}") + public ApiResult getUserProfile(@PathVariable Long id) { + return ApiResult.success(userProfileService.findUserWithRolesById(id)); } /** - * 更新用户名 - * @return 更新后用户名 - * @deprecated 废弃 修改用户名之后token会失效 + * 管理员重置指定用户的密码 + * URL: PUT /api/users/{userId}/password */ - @PutMapping("/updateUsername") - ApiResult updateUsername(@RequestAttribute("currentUsername") String username,@RequestBody @Validated UpdateUsernameRequest request){ - userProfileService.updateUsername(username, request.getUsername()); - return ApiResult.success("用户名更新成功"); + @PreAuthorize("hasAuthority('admin')") + @PutMapping("/{userId}/password") + public ApiResult resetUserPassword( + @PathVariable Long userId, + @RequestBody @Validated SetUserPasswordRequest request) { + userProfileService.setUserPassword(userId, request.getPassword()); + return ApiResult.success("用户密码修改成功"); } /** * 获取当前登录用户信息 - * @return 用户信息 + * URL: GET /api/users/me (或 /api/users/profile) */ - @GetMapping("/profile") - ApiResult getCurrentUserProfile(@RequestAttribute("currentUsername") String username){ - return ApiResult.success(userProfileService.findUserWithRoles(username)); + @GetMapping("/me") + public ApiResult getCurrentUserProfile(@RequestAttribute("currentUsername") String username) { + return ApiResult.success(userProfileService.findUserWithRolesByUsername(username)); } /** - * 更新用户自身信息 - * @param username 用户名 - * @param request 用户更新信息 - * @return 用户信息 + * 更新当前用户的基础信息 + * URL: PATCH /api/users/me */ - @PatchMapping("/profile") - ApiResult getUserInfo(@RequestAttribute("currentUsername") String username, @RequestBody @Validated UserProfileUpdateRequest request){ - return ApiResult.success(userProfileService.updateUserProfile(username,request)); + @PatchMapping("/me") + public ApiResult updateMyProfile( + @RequestAttribute("currentUsername") String username, + @RequestBody @Validated UserProfileUpdateRequest request) { + return ApiResult.success(userProfileService.updateUserProfile(username, request)); } - /** - * 设置用户昵称 - * @param request 用户昵称 + * 更新当前用户的用户名 + * URL: PUT /api/users/me/username + * @deprecated 修改用户名会导致 Token 失效 + */ + @Deprecated + @PutMapping("/me/username") + public ApiResult updateMyUsername( + @RequestAttribute("currentUsername") String username, + @RequestBody @Validated UpdateUsernameRequest request) { + userProfileService.updateUsername(username, request.getUsername()); + return ApiResult.success("用户名更新成功"); + } + /** + * 管理员修改用户昵称 + * URL: PUT /api/users/{userId}/nickname */ @PreAuthorize("hasAuthority('admin')") @PutMapping("/{userId}/nickname") - ApiResult setNickname(@PathVariable Long userId, @RequestBody @Validated SetUserNicknameRequest request){ - return ApiResult.success(userProfileService.setUserNickname(userId,request.getNickname())); + public ApiResult setUserNickname( + @PathVariable Long userId, + @RequestBody @Validated SetUserNicknameRequest request) { + return ApiResult.success(userProfileService.setUserNickname(userId, request.getNickname())); } - } diff --git a/chaos_api_web/src/main/resources/application.yaml b/chaos_api_web/src/main/resources/application.yaml index 533450d..3348f06 100644 --- a/chaos_api_web/src/main/resources/application.yaml +++ b/chaos_api_web/src/main/resources/application.yaml @@ -31,7 +31,7 @@ mybatis-plus: global-config: db-config: id-type: auto - logic-not-delete-value: + logic-not-delete-value: "null" logic-delete-value: "NOW()" configuration: