From cd854d0ee13323eeaa23951315adabc31cee2edd Mon Sep 17 00:00:00 2001 From: niudehua <657563945@qq.com> Date: Sat, 13 Mar 2021 21:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E4=B8=AA=E4=BA=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=AE=BE=E7=BD=AE=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/user/SysUserController.java | 4 +- .../user/SysUserProfileController.java | 42 ++++++++------ .../user/vo/user/SysUserProfileRespVO.java | 5 -- .../vo/user/SysUserProfileUpdateReqVO.java | 27 +++++++-- .../system/service/user/SysUserService.java | 6 +- .../service/user/SysUserServiceImpl.java | 56 ++++++++++--------- src/main/resources/application-dev.yaml | 2 +- src/main/resources/application-local.yaml | 2 +- 8 files changed, 86 insertions(+), 58 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java index 273be98f0..4374ff4d9 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java @@ -40,8 +40,8 @@ public class SysUserController { @Resource private SysDeptService deptService; - @ApiOperation("获得用户分页列表") @GetMapping("/page") + @ApiOperation("获得用户分页列表") @PreAuthorize("@ss.hasPermission('system:user:list')") public CommonResult> pageUsers(@Validated SysUserPageReqVO reqVO) { // 获得用户分页列表 @@ -66,9 +66,9 @@ public class SysUserController { /** * 根据用户编号获取详细信息 */ + @GetMapping("/get") @ApiOperation("获得用户详情") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) - @GetMapping("/get") // @PreAuthorize("@ss.hasPermi('system:user:query')") public CommonResult getInfo(@RequestParam("id") Long id) { return success(SysUserConvert.INSTANCE.convert(userService.getUser(id))); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserProfileController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserProfileController.java index 40a99910f..710617e1f 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserProfileController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserProfileController.java @@ -1,5 +1,6 @@ package cn.iocoder.dashboard.modules.system.controller.user; +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.framework.security.core.LoginUser; import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils; @@ -12,8 +13,10 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; import cn.iocoder.dashboard.modules.system.service.user.SysUserService; +import cn.iocoder.dashboard.util.collection.CollectionUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -24,15 +27,19 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.io.IOException; import java.util.List; -import java.util.stream.Collectors; + +import static cn.iocoder.dashboard.common.pojo.CommonResult.success; +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.FILE_UPLOAD_FAILED; /** * @author niudehua */ -@Api(tags = "用户个人中心") @RestController @RequestMapping("/system/user/profile") +@Api(tags = "用户个人中心") +@Slf4j public class SysUserProfileController { @Resource @@ -47,18 +54,17 @@ public class SysUserProfileController { * * @return 个人信息详情 */ - @ApiOperation("获得登录用户信息") @GetMapping("/get") + @ApiOperation("获得登录用户信息") public CommonResult profile() { LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); // 获取用户信息 - assert loginUser != null; Long userId = loginUser.getId(); SysUserDO user = userService.getUser(userId); SysUserProfileRespVO userProfileRespVO = SysUserConvert.INSTANCE.convert03(user); List userRoles = roleService.listRolesFromCache(permissionService.listUserRoleIs(userId)); - userProfileRespVO.setRoles(userRoles.stream().map(SysUserConvert.INSTANCE::convert).collect(Collectors.toSet())); - return CommonResult.success(userProfileRespVO); + userProfileRespVO.setRoles(CollectionUtils.convertSet(userRoles, SysUserConvert.INSTANCE::convert)); + return success(userProfileRespVO); } /** @@ -68,14 +74,12 @@ public class SysUserProfileController { * @param request HttpServletRequest * @return 修改结果 */ - @ApiOperation("修改用户个人信息") @PostMapping("/update") + @ApiOperation("修改用户个人信息") public CommonResult updateProfile(@RequestBody SysUserProfileUpdateReqVO reqVO, HttpServletRequest request) { - if (userService.updateUserProfile(reqVO) > 0) { - SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request); - return CommonResult.success(true); - } - return CommonResult.success(false); + userService.updateUserProfile(reqVO); + SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request); + return success(true); } /** @@ -84,16 +88,20 @@ public class SysUserProfileController { * @param file 头像文件 * @return 上传结果 */ - @ApiOperation("上传用户个人头像") @PostMapping("/uploadAvatar") + @ApiOperation("上传用户个人头像") public CommonResult uploadAvatar(@RequestParam("avatarFile") MultipartFile file) { if (!file.isEmpty()) { LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); - assert loginUser != null; - if (userService.updateAvatar(loginUser.getId(), file) > 0) { - return CommonResult.success(true); + try { + if (userService.updateAvatar(loginUser.getId(), file.getInputStream()) > 0) { + return success(true); + } + } catch (IOException e) { + log.error("文件上传失败", e); + throw ServiceExceptionUtil.exception(FILE_UPLOAD_FAILED); } } - return CommonResult.success(false); + return success(false); } } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileRespVO.java index a081dea65..39737f00b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileRespVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileRespVO.java @@ -17,11 +17,6 @@ import java.util.Set; @EqualsAndHashCode(callSuper = true) public class SysUserProfileRespVO extends SysUserRespVO { - @ApiModelProperty(value = "旧密码", required = true, example = "123456") - private String oldPassword; - - @ApiModelProperty(value = "新密码", required = true, example = "123456") - private String newPassword; /** * 所属角色 */ diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileUpdateReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileUpdateReqVO.java index d3185b242..cea2ca77c 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserProfileUpdateReqVO.java +++ b/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; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import lombok.EqualsAndHashCode; +import javax.validation.constraints.Email; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; -@ApiModel("用户更新 Request VO") +@ApiModel("用户个人信息更新 Request VO") @Data -@EqualsAndHashCode(callSuper = true) -public class SysUserProfileUpdateReqVO extends SysUserBaseVO { +public class SysUserProfileUpdateReqVO { @ApiModelProperty(value = "用户编号", required = true, example = "1024") @NotNull(message = "用户编号不能为空") private Long id; + @ApiModelProperty(value = "用户昵称", required = true, example = "芋艿") + @Size(max = 30, message = "用户昵称长度不能超过30个字符") + private String nickname; + + @ApiModelProperty(value = "用户邮箱", example = "yudao@iocoder.cn") + @Email(message = "邮箱格式不正确") + @Size(max = 50, message = "邮箱长度不能超过50个字符") + private String email; + + @ApiModelProperty(value = "手机号码", example = "15601691300") + @Size(max = 11, message = "手机号码长度不能超过11个字符") + private String mobile; + + @ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SysSexEnum 枚举类") + private Integer sex; + + @ApiModelProperty(value = "用户头像", example = "http://www.iocoder.cn/xxx.png") + private String avatar; + @ApiModelProperty(value = "旧密码", required = true, example = "123456") private String oldPassword; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java index e8097590f..def5624e9 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java +++ b/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 import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.dashboard.util.collection.CollectionUtils; -import org.springframework.web.multipart.MultipartFile; +import java.io.InputStream; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -115,7 +115,7 @@ public interface SysUserService { * @param reqVO 用户个人信息 * @return 修改结果 */ - int updateUserProfile(SysUserProfileUpdateReqVO reqVO); + void updateUserProfile(SysUserProfileUpdateReqVO reqVO); /** * 删除用户 @@ -156,7 +156,7 @@ public interface SysUserService { * @param avatarFile 头像文件 * @return 更新结果 */ - int updateAvatar(Long id, MultipartFile avatarFile); + int updateAvatar(Long id, InputStream avatarFile); // // /** diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java index c1fc828a0..735121216 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java @@ -2,11 +2,13 @@ package cn.iocoder.dashboard.modules.system.service.user; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.lang.UUID; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.exception.ServiceException; import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.modules.infra.service.file.InfFileService; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO; 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; import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysPostDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.dashboard.modules.system.dal.mysql.user.SysUserMapper; -import cn.iocoder.dashboard.modules.system.service.common.SysFileService; import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService; import cn.iocoder.dashboard.modules.system.service.dept.SysPostService; import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; @@ -28,10 +29,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -66,7 +66,7 @@ public class SysUserServiceImpl implements SysUserService { private PasswordEncoder passwordEncoder; @Resource - private SysFileService fileService; + private InfFileService fileService; // /** // * 根据条件分页查询用户列表 @@ -156,20 +156,22 @@ public class SysUserServiceImpl implements SysUserService { } @Override - public int updateUserProfile(SysUserProfileUpdateReqVO reqVO) { + public void updateUserProfile(SysUserProfileUpdateReqVO reqVO) { // 校验正确性 - this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), - reqVO.getDeptId(), reqVO.getPostIds()); - - SysUserDO updateObj = SysUserConvert.INSTANCE.convert(reqVO); - // 校验旧密码 - if (checkOldPassword(reqVO.getId(), reqVO.getOldPassword(), reqVO.getNewPassword())) { - return userMapper.updateById(updateObj); + this.checkUserExists(reqVO.getId()); + this.checkEmailUnique(reqVO.getId(), reqVO.getEmail()); + this.checkMobileUnique(reqVO.getId(), reqVO.getMobile()); + // 校验填写密码 + String encode = null; + if (this.checkOldPassword(reqVO.getId(), reqVO.getOldPassword(), reqVO.getNewPassword())) { + // 更新密码 + encode = passwordEncoder.encode(reqVO.getNewPassword()); } - - String encode = passwordEncoder.encode(reqVO.getNewPassword()); - updateObj.setPassword(encode); - return userMapper.updateById(updateObj); + SysUserDO updateObj = SysUserConvert.INSTANCE.convert(reqVO); + if (StrUtil.isNotBlank(encode)) { + updateObj.setPassword(encode); + } + userMapper.updateById(updateObj); } @Override @@ -314,9 +316,17 @@ public class SysUserServiceImpl implements SysUserService { }); } + /** + * 校验旧密码、新密码 + * + * @param id 用户 id + * @param oldPassword 旧密码 + * @param newPassword 新密码 + * @return + */ private boolean checkOldPassword(Long id, String oldPassword, String newPassword) { if (id == null || StrUtil.isBlank(oldPassword) || StrUtil.isBlank(newPassword)) { - return true; + return false; } SysUserDO user = userMapper.selectById(id); if (user == null) { @@ -326,7 +336,7 @@ public class SysUserServiceImpl implements SysUserService { if (!passwordEncoder.matches(oldPassword, user.getPassword())) { throw ServiceExceptionUtil.exception(USER_PASSWORD_FAILED); } - return false; + return true; } @Override @@ -368,15 +378,11 @@ public class SysUserServiceImpl implements SysUserService { } @Override - public int updateAvatar(Long id, MultipartFile avatarFile) { + public int updateAvatar(Long id, InputStream avatarFile) { this.checkUserExists(id); // 存储文件 - String avatar = null; - try { - avatar = fileService.createFile(avatarFile.getOriginalFilename(), IoUtil.readBytes(avatarFile.getInputStream())); - } catch (IOException e) { - throw ServiceExceptionUtil.exception(FILE_UPLOAD_FAILED); - } + String avatar; + avatar = fileService.createFile(UUID.fastUUID().toString(), IoUtil.readBytes(avatarFile)); // 更新路径 SysUserDO sysUserDO = new SysUserDO(); sysUserDO.setId(id); diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index c9c40e739..b0a96fb04 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -145,7 +145,7 @@ yudao: swagger: title: 管理后台 description: 提供管理员管理的所有功能 - version: ${yudao.info.base-package} + version: ${yudao.info.version} base-package: ${yudao.info.base-package}.modules captcha: timeout: 5m diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index 500eb7b51..fdb260758 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -145,7 +145,7 @@ yudao: swagger: title: 管理后台 description: 提供管理员管理的所有功能 - version: ${yudao.info.base-package} + version: ${yudao.info.version} base-package: ${yudao.info.base-package}.modules captcha: timeout: 5m