增加 个人信息设置 功能

pull/2/head
niudehua 2021-03-13 21:07:04 +08:00
parent e4bda22b73
commit cd854d0ee1
8 changed files with 86 additions and 58 deletions

View File

@ -40,8 +40,8 @@ public class SysUserController {
@Resource
private SysDeptService deptService;
@ApiOperation("获得用户分页列表")
@GetMapping("/page")
@ApiOperation("获得用户分页列表")
@PreAuthorize("@ss.hasPermission('system:user:list')")
public CommonResult<PageResult<SysUserPageItemRespVO>> 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<SysUserRespVO> getInfo(@RequestParam("id") Long id) {
return success(SysUserConvert.INSTANCE.convert(userService.getUser(id)));

View File

@ -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<SysUserProfileRespVO> profile() {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
// 获取用户信息
assert loginUser != null;
Long userId = loginUser.getId();
SysUserDO user = userService.getUser(userId);
SysUserProfileRespVO userProfileRespVO = SysUserConvert.INSTANCE.convert03(user);
List<SysRoleDO> 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<Boolean> updateProfile(@RequestBody SysUserProfileUpdateReqVO reqVO, HttpServletRequest request) {
if (userService.updateUserProfile(reqVO) > 0) {
userService.updateUserProfile(reqVO);
SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request);
return CommonResult.success(true);
}
return CommonResult.success(false);
return success(true);
}
/**
@ -84,16 +88,20 @@ public class SysUserProfileController {
* @param file
* @return
*/
@ApiOperation("上传用户个人头像")
@PostMapping("/uploadAvatar")
@ApiOperation("上传用户个人头像")
public CommonResult<Boolean> 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);
}
}

View File

@ -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;
/**
*
*/

View File

@ -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;

View File

@ -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);
//
// /**

View File

@ -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());
SysUserDO updateObj = SysUserConvert.INSTANCE.convert(reqVO);
if (StrUtil.isNotBlank(encode)) {
updateObj.setPassword(encode);
return userMapper.updateById(updateObj);
}
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);

View File

@ -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

View File

@ -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