Commit cd854d0ee13323eeaa23951315adabc31cee2edd
1 parent
e4bda22b
增加 个人信息设置 功能
Showing
8 changed files
with
85 additions
and
57 deletions
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java
| ... | ... | @@ -40,8 +40,8 @@ public class SysUserController { |
| 40 | 40 | @Resource |
| 41 | 41 | private SysDeptService deptService; |
| 42 | 42 | |
| 43 | - @ApiOperation("获得用户分页列表") | |
| 44 | 43 | @GetMapping("/page") |
| 44 | + @ApiOperation("获得用户分页列表") | |
| 45 | 45 | @PreAuthorize("@ss.hasPermission('system:user:list')") |
| 46 | 46 | public CommonResult<PageResult<SysUserPageItemRespVO>> pageUsers(@Validated SysUserPageReqVO reqVO) { |
| 47 | 47 | // 获得用户分页列表 |
| ... | ... | @@ -66,9 +66,9 @@ public class SysUserController { |
| 66 | 66 | /** |
| 67 | 67 | * 根据用户编号获取详细信息 |
| 68 | 68 | */ |
| 69 | + @GetMapping("/get") | |
| 69 | 70 | @ApiOperation("获得用户详情") |
| 70 | 71 | @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) |
| 71 | - @GetMapping("/get") | |
| 72 | 72 | // @PreAuthorize("@ss.hasPermi('system:user:query')") |
| 73 | 73 | public CommonResult<SysUserRespVO> getInfo(@RequestParam("id") Long id) { |
| 74 | 74 | return success(SysUserConvert.INSTANCE.convert(userService.getUser(id))); | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserProfileController.java
| 1 | 1 | package cn.iocoder.dashboard.modules.system.controller.user; |
| 2 | 2 | |
| 3 | +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; | |
| 3 | 4 | import cn.iocoder.dashboard.common.pojo.CommonResult; |
| 4 | 5 | import cn.iocoder.dashboard.framework.security.core.LoginUser; |
| 5 | 6 | import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils; |
| ... | ... | @@ -12,8 +13,10 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; |
| 12 | 13 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| 13 | 14 | import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; |
| 14 | 15 | import cn.iocoder.dashboard.modules.system.service.user.SysUserService; |
| 16 | +import cn.iocoder.dashboard.util.collection.CollectionUtils; | |
| 15 | 17 | import io.swagger.annotations.Api; |
| 16 | 18 | import io.swagger.annotations.ApiOperation; |
| 19 | +import lombok.extern.slf4j.Slf4j; | |
| 17 | 20 | import org.springframework.web.bind.annotation.GetMapping; |
| 18 | 21 | import org.springframework.web.bind.annotation.PostMapping; |
| 19 | 22 | import org.springframework.web.bind.annotation.RequestBody; |
| ... | ... | @@ -24,15 +27,19 @@ import org.springframework.web.multipart.MultipartFile; |
| 24 | 27 | |
| 25 | 28 | import javax.annotation.Resource; |
| 26 | 29 | import javax.servlet.http.HttpServletRequest; |
| 30 | +import java.io.IOException; | |
| 27 | 31 | import java.util.List; |
| 28 | -import java.util.stream.Collectors; | |
| 32 | + | |
| 33 | +import static cn.iocoder.dashboard.common.pojo.CommonResult.success; | |
| 34 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.FILE_UPLOAD_FAILED; | |
| 29 | 35 | |
| 30 | 36 | /** |
| 31 | 37 | * @author niudehua |
| 32 | 38 | */ |
| 33 | -@Api(tags = "用户个人中心") | |
| 34 | 39 | @RestController |
| 35 | 40 | @RequestMapping("/system/user/profile") |
| 41 | +@Api(tags = "用户个人中心") | |
| 42 | +@Slf4j | |
| 36 | 43 | public class SysUserProfileController { |
| 37 | 44 | |
| 38 | 45 | @Resource |
| ... | ... | @@ -47,18 +54,17 @@ public class SysUserProfileController { |
| 47 | 54 | * |
| 48 | 55 | * @return 个人信息详情 |
| 49 | 56 | */ |
| 50 | - @ApiOperation("获得登录用户信息") | |
| 51 | 57 | @GetMapping("/get") |
| 58 | + @ApiOperation("获得登录用户信息") | |
| 52 | 59 | public CommonResult<SysUserProfileRespVO> profile() { |
| 53 | 60 | LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); |
| 54 | 61 | // 获取用户信息 |
| 55 | - assert loginUser != null; | |
| 56 | 62 | Long userId = loginUser.getId(); |
| 57 | 63 | SysUserDO user = userService.getUser(userId); |
| 58 | 64 | SysUserProfileRespVO userProfileRespVO = SysUserConvert.INSTANCE.convert03(user); |
| 59 | 65 | List<SysRoleDO> userRoles = roleService.listRolesFromCache(permissionService.listUserRoleIs(userId)); |
| 60 | - userProfileRespVO.setRoles(userRoles.stream().map(SysUserConvert.INSTANCE::convert).collect(Collectors.toSet())); | |
| 61 | - return CommonResult.success(userProfileRespVO); | |
| 66 | + userProfileRespVO.setRoles(CollectionUtils.convertSet(userRoles, SysUserConvert.INSTANCE::convert)); | |
| 67 | + return success(userProfileRespVO); | |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | /** |
| ... | ... | @@ -68,14 +74,12 @@ public class SysUserProfileController { |
| 68 | 74 | * @param request HttpServletRequest |
| 69 | 75 | * @return 修改结果 |
| 70 | 76 | */ |
| 71 | - @ApiOperation("修改用户个人信息") | |
| 72 | 77 | @PostMapping("/update") |
| 78 | + @ApiOperation("修改用户个人信息") | |
| 73 | 79 | public CommonResult<Boolean> updateProfile(@RequestBody SysUserProfileUpdateReqVO reqVO, HttpServletRequest request) { |
| 74 | - if (userService.updateUserProfile(reqVO) > 0) { | |
| 75 | - SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request); | |
| 76 | - return CommonResult.success(true); | |
| 77 | - } | |
| 78 | - return CommonResult.success(false); | |
| 80 | + userService.updateUserProfile(reqVO); | |
| 81 | + SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request); | |
| 82 | + return success(true); | |
| 79 | 83 | } |
| 80 | 84 | |
| 81 | 85 | /** |
| ... | ... | @@ -84,16 +88,20 @@ public class SysUserProfileController { |
| 84 | 88 | * @param file 头像文件 |
| 85 | 89 | * @return 上传结果 |
| 86 | 90 | */ |
| 87 | - @ApiOperation("上传用户个人头像") | |
| 88 | 91 | @PostMapping("/uploadAvatar") |
| 92 | + @ApiOperation("上传用户个人头像") | |
| 89 | 93 | public CommonResult<Boolean> uploadAvatar(@RequestParam("avatarFile") MultipartFile file) { |
| 90 | 94 | if (!file.isEmpty()) { |
| 91 | 95 | LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); |
| 92 | - assert loginUser != null; | |
| 93 | - if (userService.updateAvatar(loginUser.getId(), file) > 0) { | |
| 94 | - return CommonResult.success(true); | |
| 96 | + try { | |
| 97 | + if (userService.updateAvatar(loginUser.getId(), file.getInputStream()) > 0) { | |
| 98 | + return success(true); | |
| 99 | + } | |
| 100 | + } catch (IOException e) { | |
| 101 | + log.error("文件上传失败", e); | |
| 102 | + throw ServiceExceptionUtil.exception(FILE_UPLOAD_FAILED); | |
| 95 | 103 | } |
| 96 | 104 | } |
| 97 | - return CommonResult.success(false); | |
| 105 | + return success(false); | |
| 98 | 106 | } |
| 99 | 107 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileRespVO.java
| ... | ... | @@ -17,11 +17,6 @@ import java.util.Set; |
| 17 | 17 | @EqualsAndHashCode(callSuper = true) |
| 18 | 18 | public class SysUserProfileRespVO extends SysUserRespVO { |
| 19 | 19 | |
| 20 | - @ApiModelProperty(value = "旧密码", required = true, example = "123456") | |
| 21 | - private String oldPassword; | |
| 22 | - | |
| 23 | - @ApiModelProperty(value = "新密码", required = true, example = "123456") | |
| 24 | - private String newPassword; | |
| 25 | 20 | /** |
| 26 | 21 | * 所属角色 |
| 27 | 22 | */ | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileUpdateReqVO.java
| ... | ... | @@ -3,19 +3,38 @@ package cn.iocoder.dashboard.modules.system.controller.user.vo.user; |
| 3 | 3 | import io.swagger.annotations.ApiModel; |
| 4 | 4 | import io.swagger.annotations.ApiModelProperty; |
| 5 | 5 | import lombok.Data; |
| 6 | -import lombok.EqualsAndHashCode; | |
| 7 | 6 | |
| 7 | +import javax.validation.constraints.Email; | |
| 8 | 8 | import javax.validation.constraints.NotNull; |
| 9 | +import javax.validation.constraints.Size; | |
| 9 | 10 | |
| 10 | -@ApiModel("用户更新 Request VO") | |
| 11 | +@ApiModel("用户个人信息更新 Request VO") | |
| 11 | 12 | @Data |
| 12 | -@EqualsAndHashCode(callSuper = true) | |
| 13 | -public class SysUserProfileUpdateReqVO extends SysUserBaseVO { | |
| 13 | +public class SysUserProfileUpdateReqVO { | |
| 14 | 14 | |
| 15 | 15 | @ApiModelProperty(value = "用户编号", required = true, example = "1024") |
| 16 | 16 | @NotNull(message = "用户编号不能为空") |
| 17 | 17 | private Long id; |
| 18 | 18 | |
| 19 | + @ApiModelProperty(value = "用户昵称", required = true, example = "芋艿") | |
| 20 | + @Size(max = 30, message = "用户昵称长度不能超过30个字符") | |
| 21 | + private String nickname; | |
| 22 | + | |
| 23 | + @ApiModelProperty(value = "用户邮箱", example = "yudao@iocoder.cn") | |
| 24 | + @Email(message = "邮箱格式不正确") | |
| 25 | + @Size(max = 50, message = "邮箱长度不能超过50个字符") | |
| 26 | + private String email; | |
| 27 | + | |
| 28 | + @ApiModelProperty(value = "手机号码", example = "15601691300") | |
| 29 | + @Size(max = 11, message = "手机号码长度不能超过11个字符") | |
| 30 | + private String mobile; | |
| 31 | + | |
| 32 | + @ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SysSexEnum 枚举类") | |
| 33 | + private Integer sex; | |
| 34 | + | |
| 35 | + @ApiModelProperty(value = "用户头像", example = "http://www.iocoder.cn/xxx.png") | |
| 36 | + private String avatar; | |
| 37 | + | |
| 19 | 38 | @ApiModelProperty(value = "旧密码", required = true, example = "123456") |
| 20 | 39 | private String oldPassword; |
| 21 | 40 | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java
| ... | ... | @@ -11,8 +11,8 @@ import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfil |
| 11 | 11 | import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; |
| 12 | 12 | import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; |
| 13 | 13 | import cn.iocoder.dashboard.util.collection.CollectionUtils; |
| 14 | -import org.springframework.web.multipart.MultipartFile; | |
| 15 | 14 | |
| 15 | +import java.io.InputStream; | |
| 16 | 16 | import java.util.Collection; |
| 17 | 17 | import java.util.HashMap; |
| 18 | 18 | import java.util.List; |
| ... | ... | @@ -115,7 +115,7 @@ public interface SysUserService { |
| 115 | 115 | * @param reqVO 用户个人信息 |
| 116 | 116 | * @return 修改结果 |
| 117 | 117 | */ |
| 118 | - int updateUserProfile(SysUserProfileUpdateReqVO reqVO); | |
| 118 | + void updateUserProfile(SysUserProfileUpdateReqVO reqVO); | |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * 删除用户 |
| ... | ... | @@ -156,7 +156,7 @@ public interface SysUserService { |
| 156 | 156 | * @param avatarFile 头像文件 |
| 157 | 157 | * @return 更新结果 |
| 158 | 158 | */ |
| 159 | - int updateAvatar(Long id, MultipartFile avatarFile); | |
| 159 | + int updateAvatar(Long id, InputStream avatarFile); | |
| 160 | 160 | |
| 161 | 161 | // |
| 162 | 162 | // /** | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java
| ... | ... | @@ -2,11 +2,13 @@ package cn.iocoder.dashboard.modules.system.service.user; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.io.IoUtil; |
| 5 | +import cn.hutool.core.lang.UUID; | |
| 5 | 6 | import cn.hutool.core.util.StrUtil; |
| 6 | 7 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 7 | 8 | import cn.iocoder.dashboard.common.exception.ServiceException; |
| 8 | 9 | import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; |
| 9 | 10 | import cn.iocoder.dashboard.common.pojo.PageResult; |
| 11 | +import cn.iocoder.dashboard.modules.infra.service.file.InfFileService; | |
| 10 | 12 | import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; |
| 11 | 13 | import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO; |
| 12 | 14 | import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO; |
| ... | ... | @@ -19,7 +21,6 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO; |
| 19 | 21 | import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysPostDO; |
| 20 | 22 | import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; |
| 21 | 23 | import cn.iocoder.dashboard.modules.system.dal.mysql.user.SysUserMapper; |
| 22 | -import cn.iocoder.dashboard.modules.system.service.common.SysFileService; | |
| 23 | 24 | import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService; |
| 24 | 25 | import cn.iocoder.dashboard.modules.system.service.dept.SysPostService; |
| 25 | 26 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| ... | ... | @@ -28,10 +29,9 @@ import lombok.extern.slf4j.Slf4j; |
| 28 | 29 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 29 | 30 | import org.springframework.stereotype.Service; |
| 30 | 31 | import org.springframework.transaction.annotation.Transactional; |
| 31 | -import org.springframework.web.multipart.MultipartFile; | |
| 32 | 32 | |
| 33 | 33 | import javax.annotation.Resource; |
| 34 | -import java.io.IOException; | |
| 34 | +import java.io.InputStream; | |
| 35 | 35 | import java.util.ArrayList; |
| 36 | 36 | import java.util.Collection; |
| 37 | 37 | import java.util.Collections; |
| ... | ... | @@ -66,7 +66,7 @@ public class SysUserServiceImpl implements SysUserService { |
| 66 | 66 | private PasswordEncoder passwordEncoder; |
| 67 | 67 | |
| 68 | 68 | @Resource |
| 69 | - private SysFileService fileService; | |
| 69 | + private InfFileService fileService; | |
| 70 | 70 | |
| 71 | 71 | // /** |
| 72 | 72 | // * 根据条件分页查询用户列表 |
| ... | ... | @@ -156,20 +156,22 @@ public class SysUserServiceImpl implements SysUserService { |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | @Override |
| 159 | - public int updateUserProfile(SysUserProfileUpdateReqVO reqVO) { | |
| 159 | + public void updateUserProfile(SysUserProfileUpdateReqVO reqVO) { | |
| 160 | 160 | // 校验正确性 |
| 161 | - this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), | |
| 162 | - reqVO.getDeptId(), reqVO.getPostIds()); | |
| 163 | - | |
| 161 | + this.checkUserExists(reqVO.getId()); | |
| 162 | + this.checkEmailUnique(reqVO.getId(), reqVO.getEmail()); | |
| 163 | + this.checkMobileUnique(reqVO.getId(), reqVO.getMobile()); | |
| 164 | + // 校验填写密码 | |
| 165 | + String encode = null; | |
| 166 | + if (this.checkOldPassword(reqVO.getId(), reqVO.getOldPassword(), reqVO.getNewPassword())) { | |
| 167 | + // 更新密码 | |
| 168 | + encode = passwordEncoder.encode(reqVO.getNewPassword()); | |
| 169 | + } | |
| 164 | 170 | SysUserDO updateObj = SysUserConvert.INSTANCE.convert(reqVO); |
| 165 | - // 校验旧密码 | |
| 166 | - if (checkOldPassword(reqVO.getId(), reqVO.getOldPassword(), reqVO.getNewPassword())) { | |
| 167 | - return userMapper.updateById(updateObj); | |
| 171 | + if (StrUtil.isNotBlank(encode)) { | |
| 172 | + updateObj.setPassword(encode); | |
| 168 | 173 | } |
| 169 | - | |
| 170 | - String encode = passwordEncoder.encode(reqVO.getNewPassword()); | |
| 171 | - updateObj.setPassword(encode); | |
| 172 | - return userMapper.updateById(updateObj); | |
| 174 | + userMapper.updateById(updateObj); | |
| 173 | 175 | } |
| 174 | 176 | |
| 175 | 177 | @Override |
| ... | ... | @@ -314,9 +316,17 @@ public class SysUserServiceImpl implements SysUserService { |
| 314 | 316 | }); |
| 315 | 317 | } |
| 316 | 318 | |
| 319 | + /** | |
| 320 | + * 校验旧密码、新密码 | |
| 321 | + * | |
| 322 | + * @param id 用户 id | |
| 323 | + * @param oldPassword 旧密码 | |
| 324 | + * @param newPassword 新密码 | |
| 325 | + * @return | |
| 326 | + */ | |
| 317 | 327 | private boolean checkOldPassword(Long id, String oldPassword, String newPassword) { |
| 318 | 328 | if (id == null || StrUtil.isBlank(oldPassword) || StrUtil.isBlank(newPassword)) { |
| 319 | - return true; | |
| 329 | + return false; | |
| 320 | 330 | } |
| 321 | 331 | SysUserDO user = userMapper.selectById(id); |
| 322 | 332 | if (user == null) { |
| ... | ... | @@ -326,7 +336,7 @@ public class SysUserServiceImpl implements SysUserService { |
| 326 | 336 | if (!passwordEncoder.matches(oldPassword, user.getPassword())) { |
| 327 | 337 | throw ServiceExceptionUtil.exception(USER_PASSWORD_FAILED); |
| 328 | 338 | } |
| 329 | - return false; | |
| 339 | + return true; | |
| 330 | 340 | } |
| 331 | 341 | |
| 332 | 342 | @Override |
| ... | ... | @@ -368,15 +378,11 @@ public class SysUserServiceImpl implements SysUserService { |
| 368 | 378 | } |
| 369 | 379 | |
| 370 | 380 | @Override |
| 371 | - public int updateAvatar(Long id, MultipartFile avatarFile) { | |
| 381 | + public int updateAvatar(Long id, InputStream avatarFile) { | |
| 372 | 382 | this.checkUserExists(id); |
| 373 | 383 | // 存储文件 |
| 374 | - String avatar = null; | |
| 375 | - try { | |
| 376 | - avatar = fileService.createFile(avatarFile.getOriginalFilename(), IoUtil.readBytes(avatarFile.getInputStream())); | |
| 377 | - } catch (IOException e) { | |
| 378 | - throw ServiceExceptionUtil.exception(FILE_UPLOAD_FAILED); | |
| 379 | - } | |
| 384 | + String avatar; | |
| 385 | + avatar = fileService.createFile(UUID.fastUUID().toString(), IoUtil.readBytes(avatarFile)); | |
| 380 | 386 | // 更新路径 |
| 381 | 387 | SysUserDO sysUserDO = new SysUserDO(); |
| 382 | 388 | sysUserDO.setId(id); | ... | ... |
src/main/resources/application-dev.yaml
src/main/resources/application-local.yaml