refactor(user): 重构用户相关接口与实现

- 修改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方法逻辑并增加空值校验
This commit is contained in:
Chaos
2025-11-29 09:01:50 +08:00
parent b479304687
commit 6e2de46157
5 changed files with 77 additions and 50 deletions

View File

@@ -52,7 +52,7 @@ public interface UserProfileService {
* @param username 用户名 * @param username 用户名
* @return 用户信息 * @return 用户信息
*/ */
UserProfileResponse findUserWithRoles(String username); UserProfileResponse findUserWithRolesByUsername(String username);
/** /**
* 设置用户昵称 * 设置用户昵称
@@ -90,4 +90,12 @@ public interface UserProfileService {
* @return 所有用户信息 * @return 所有用户信息
*/ */
IPage<UserProfileResponse> getAllUsers(Integer pageNum, Integer pageSize); IPage<UserProfileResponse> getAllUsers(Integer pageNum, Integer pageSize);
/**
* 根据用户ID查询用户信息
*
* @param id 用户ID
* @return 用户信息
*/
UserProfileResponse findUserWithRolesById(Long id);
} }

View File

@@ -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.common.exceotion.BizException;
import cn.nopj.chaos_api.domain.entity.User; import cn.nopj.chaos_api.domain.entity.User;
import cn.nopj.chaos_api.dto.request.UserProfileUpdateRequest; 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.dto.response.UserProfileResponse;
import cn.nopj.chaos_api.mapper.UserMapper; import cn.nopj.chaos_api.mapper.UserMapper;
import cn.nopj.chaos_api.service.UserProfileService; import cn.nopj.chaos_api.service.UserProfileService;
@@ -81,7 +80,7 @@ public class UserProfileServiceImpl implements UserProfileService {
} }
@Override @Override
public UserProfileResponse findUserWithRoles(String username) { public UserProfileResponse findUserWithRolesByUsername(String username) {
if (username == null || username.isEmpty()){ if (username == null || username.isEmpty()){
throw new BizException(ErrorCode.USER_NOT_EXISTS); throw new BizException(ErrorCode.USER_NOT_EXISTS);
} }
@@ -160,8 +159,21 @@ public class UserProfileServiceImpl implements UserProfileService {
return userPage.convert(UserProfileResponse::new); 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);
}
} }

View File

@@ -3,7 +3,6 @@ package cn.nopj.chaos_api.controller;
import cn.nopj.chaos_api.dto.request.SetUserRoleRequest; import cn.nopj.chaos_api.dto.request.SetUserRoleRequest;
import cn.nopj.chaos_api.dto.response.OptionResponse; 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.model.ApiResult;
import cn.nopj.chaos_api.service.RoleService; import cn.nopj.chaos_api.service.RoleService;
import cn.nopj.chaos_api.service.UserRoleService; import cn.nopj.chaos_api.service.UserRoleService;
@@ -34,7 +33,7 @@ public class RoleController {
/** /**
* 获取用户角色选项 * 获取用户角色选项
*/ */
@GetMapping @GetMapping("/options")
public ApiResult<List<OptionResponse>> getUserRole() { public ApiResult<List<OptionResponse>> getUserRole() {
return ApiResult.success(roleService.getAllRoles()); return ApiResult.success(roleService.getAllRoles());

View File

@@ -9,14 +9,12 @@ import cn.nopj.chaos_api.dto.response.UserProfileResponse;
import cn.nopj.chaos_api.model.ApiResult; import cn.nopj.chaos_api.model.ApiResult;
import cn.nopj.chaos_api.service.UserProfileService; import cn.nopj.chaos_api.service.UserProfileService;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
/** /**
* 用户管理 * 用户管理
* *
@@ -24,73 +22,83 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/api/user") @RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController { public class UserController {
@Autowired
private UserProfileService userProfileService;
private final UserProfileService userProfileService;
/** /**
* 获取所有用户信息 * 获取用户列表 (分页)
* @return 所有用户信息 * URL: GET /api/users?pageNum=1&pageSize=10
*/ */
@PreAuthorize("hasAuthority('admin')") @PreAuthorize("hasAuthority('admin')")
@GetMapping("/all") @GetMapping
ApiResult<IPage<UserProfileResponse>> getAllUsers(@RequestParam(defaultValue = "1" , name = "pageNum") Integer pageNum, @RequestParam(defaultValue = "10", name="pageSize") Integer pageSize){ public ApiResult<IPage<UserProfileResponse>> listUsers(
@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
@RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize) {
return ApiResult.success(userProfileService.getAllUsers(pageNum, pageSize)); return ApiResult.success(userProfileService.getAllUsers(pageNum, pageSize));
} }
/** @GetMapping("/{id}")
* 设置用户密码 public ApiResult<UserProfileResponse> getUserProfile(@PathVariable Long id) {
* @param request 设置用户密码请求 return ApiResult.success(userProfileService.findUserWithRolesById(id));
* @return 设置用户密码结果
*/
@PreAuthorize("hasAuthority('admin')")
@PutMapping("/setUserPassword")
ApiResult<String> setUserPassword(@RequestBody @Validated SetUserPasswordRequest request){
userProfileService.setUserPassword(request.getUserId(), request.getPassword());
return ApiResult.success("用户密码修改成功");
} }
/** /**
* 更新用户名 * 管理员重置指定用户的密码
* @return 更新后用户名 * URL: PUT /api/users/{userId}/password
* @deprecated 废弃 修改用户名之后token会失效
*/ */
@PutMapping("/updateUsername") @PreAuthorize("hasAuthority('admin')")
ApiResult<String> updateUsername(@RequestAttribute("currentUsername") String username,@RequestBody @Validated UpdateUsernameRequest request){ @PutMapping("/{userId}/password")
userProfileService.updateUsername(username, request.getUsername()); public ApiResult<String> resetUserPassword(
return ApiResult.success("用户名更新成功"); @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") @GetMapping("/me")
ApiResult<UserProfileResponse> getCurrentUserProfile(@RequestAttribute("currentUsername") String username){ public ApiResult<UserProfileResponse> getCurrentUserProfile(@RequestAttribute("currentUsername") String username) {
return ApiResult.success(userProfileService.findUserWithRoles(username)); return ApiResult.success(userProfileService.findUserWithRolesByUsername(username));
} }
/** /**
* 更新用户自身信息 * 更新当前用户的基础信息
* @param username 用户名 * URL: PATCH /api/users/me
* @param request 用户更新信息
* @return 用户信息
*/ */
@PatchMapping("/profile") @PatchMapping("/me")
ApiResult<UserProfileResponse> getUserInfo(@RequestAttribute("currentUsername") String username, @RequestBody @Validated UserProfileUpdateRequest request){ public ApiResult<UserProfileResponse> updateMyProfile(
@RequestAttribute("currentUsername") String username,
@RequestBody @Validated UserProfileUpdateRequest request) {
return ApiResult.success(userProfileService.updateUserProfile(username, request)); return ApiResult.success(userProfileService.updateUserProfile(username, request));
} }
/** /**
* 设置用户昵称 * 更新当前用户的用户名
* @param request 用户昵称 * URL: PUT /api/users/me/username
* @deprecated 修改用户名会导致 Token 失效
*/
@Deprecated
@PutMapping("/me/username")
public ApiResult<String> 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')") @PreAuthorize("hasAuthority('admin')")
@PutMapping("/{userId}/nickname") @PutMapping("/{userId}/nickname")
ApiResult<UserProfileResponse> setNickname(@PathVariable Long userId, @RequestBody @Validated SetUserNicknameRequest request){ public ApiResult<UserProfileResponse> setUserNickname(
@PathVariable Long userId,
@RequestBody @Validated SetUserNicknameRequest request) {
return ApiResult.success(userProfileService.setUserNickname(userId, request.getNickname())); return ApiResult.success(userProfileService.setUserNickname(userId, request.getNickname()));
} }
} }

View File

@@ -31,7 +31,7 @@ mybatis-plus:
global-config: global-config:
db-config: db-config:
id-type: auto id-type: auto
logic-not-delete-value: logic-not-delete-value: "null"
logic-delete-value: "NOW()" logic-delete-value: "NOW()"
configuration: configuration: