本文最后更新于 2026年5月21日 晚上
获取用户个人中心信息接口的实现
获取用户个人中心信息接口的需求
获取当前用户id
根据用户id查询用户信息
封装成vo
回填部门名称
代码实现
在 AdminUserController 中
1 2 3 4 5 6 7
| @GetMapping("/profile") @SystemLog(businessName = "获取个人中心信息接口") @ApiOperation(value = "获取个人中心信息", notes = "获取个用户人中心信息", response = UserProfileVo.class) public ResponseResult<UserProfileVo> getUserProfile() { return adminUserService.getUserProfile(); }
|
创建 UserProfileVo
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
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "后台用户个人中心简介对象") public class UserProfileVo {
@ApiModelProperty(value = "用户id", example = "1") private Long id;
@ApiModelProperty(value = "头像URL", example = "https://avatars.githubusercontent.com/u/155459505?v=4") private String avatar;
@ApiModelProperty(value = "创建时间", example = "2026-01-12 20:58:53") private Date createTime;
@ApiModelProperty(value = "部门名称", example = "测试不") private String deptName;
@ApiModelProperty(value = "用户邮箱", example = "2962933152@qq.com") private String email;
@ApiModelProperty(value = "用户性别,(0 男,1 女,2 隐藏)", example = "0") private String sex;
@ApiModelProperty(value = "手机号", example = "18329384753") @JSONField(name = "mobile") private String phone;
@ApiModelProperty(value = "昵称", example = "云梦泽") private String nickname;
@ApiModelProperty(value = "角色名称,多个使用英文逗号(,)分割", example = "系统管理员") private String roleNames;
@ApiModelProperty(value = "用户名", example = "云梦泽") private String username; }
|
在 AdminUserServiceImpl 中
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
| @Override public ResponseResult<UserProfileVo> getUserProfile() { Long userId = SecurityUtils.getUserId();
SysUser user = adminUserService.getById(userId); if (user == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "用户不存在!"); }
UserProfileVo profileVo = BeanCopyUtils.copyBean(user, UserProfileVo.class);
if (StringUtils.hasText(user.getDeptId())) { try { SysDept dept = sysDeptService.getById(Long.valueOf(user.getDeptId())); if (dept != null) { profileVo.setDeptName(dept.getName()); } } catch (NumberFormatException ignored) { profileVo.setDeptName(user.getDeptId()); } }
return ResponseResult.okResult(profileVo); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/