增加 个人信息设置 功能
parent
4e5430b5c0
commit
0181baa72b
@ -1,92 +1,99 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.user;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||
import cn.iocoder.dashboard.framework.security.core.LoginUser;
|
||||
import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileRespVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert;
|
||||
import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO;
|
||||
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 io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author niudehua
|
||||
*/
|
||||
@Api(tags = "用户个人中心")
|
||||
@RestController
|
||||
@RequestMapping("/system/user/profile")
|
||||
public class SysUserProfileController {
|
||||
|
||||
// /**
|
||||
// * 个人信息
|
||||
// */
|
||||
// @GetMapping
|
||||
// public AjaxResult profile()
|
||||
// {
|
||||
// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
// SysUser user = loginUser.getUser();
|
||||
// AjaxResult ajax = AjaxResult.success(user);
|
||||
// ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
|
||||
// ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
|
||||
// return ajax;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 修改用户
|
||||
// */
|
||||
// @Log(title = "个人信息", businessType = BusinessType.UPDATE)
|
||||
// @PutMapping
|
||||
// public AjaxResult updateProfile(@RequestBody SysUser user)
|
||||
// {
|
||||
// if (userService.updateUserProfile(user) > 0)
|
||||
// {
|
||||
// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
// // 更新缓存用户信息
|
||||
// loginUser.getUser().setNickName(user.getNickName());
|
||||
// loginUser.getUser().setPhonenumber(user.getPhonenumber());
|
||||
// loginUser.getUser().setEmail(user.getEmail());
|
||||
// loginUser.getUser().setSex(user.getSex());
|
||||
// tokenService.setLoginUser(loginUser);
|
||||
// return AjaxResult.success();
|
||||
// }
|
||||
// return AjaxResult.error("修改个人信息异常,请联系管理员");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 重置密码
|
||||
// */
|
||||
// @Log(title = "个人信息", businessType = BusinessType.UPDATE)
|
||||
// @PutMapping("/updatePwd")
|
||||
// public AjaxResult updatePwd(String oldPassword, String newPassword)
|
||||
// {
|
||||
// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
// String userName = loginUser.getUsername();
|
||||
// String password = loginUser.getPassword();
|
||||
// if (!SecurityUtils.matchesPassword(oldPassword, password))
|
||||
// {
|
||||
// return AjaxResult.error("修改密码失败,旧密码错误");
|
||||
// }
|
||||
// if (SecurityUtils.matchesPassword(newPassword, password))
|
||||
// {
|
||||
// return AjaxResult.error("新密码不能与旧密码相同");
|
||||
// }
|
||||
// if (userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)) > 0)
|
||||
// {
|
||||
// // 更新缓存用户密码
|
||||
// loginUser.getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
|
||||
// tokenService.setLoginUser(loginUser);
|
||||
// return AjaxResult.success();
|
||||
// }
|
||||
// return AjaxResult.error("修改密码异常,请联系管理员");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 头像上传
|
||||
// */
|
||||
// @Log(title = "用户头像", businessType = BusinessType.UPDATE)
|
||||
// @PostMapping("/avatar")
|
||||
// public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
|
||||
// {
|
||||
// if (!file.isEmpty())
|
||||
// {
|
||||
// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
// String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
|
||||
// if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
|
||||
// {
|
||||
// AjaxResult ajax = AjaxResult.success();
|
||||
// ajax.put("imgUrl", avatar);
|
||||
// // 更新缓存用户头像
|
||||
// loginUser.getUser().setAvatar(avatar);
|
||||
// tokenService.setLoginUser(loginUser);
|
||||
// return ajax;
|
||||
// }
|
||||
// }
|
||||
// return AjaxResult.error("上传图片异常,请联系管理员");
|
||||
// }
|
||||
@Resource
|
||||
private SysUserService userService;
|
||||
@Resource
|
||||
private SysPermissionService permissionService;
|
||||
@Resource
|
||||
private SysRoleService roleService;
|
||||
|
||||
/**
|
||||
* 个人信息
|
||||
*
|
||||
* @return 个人信息详情
|
||||
*/
|
||||
@ApiOperation("获得登录用户信息")
|
||||
@GetMapping("/get")
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改个人信息
|
||||
*
|
||||
* @param reqVO 个人信息更新 reqVO
|
||||
* @param request HttpServletRequest
|
||||
* @return 修改结果
|
||||
*/
|
||||
@ApiOperation("修改用户个人信息")
|
||||
@PostMapping("/update")
|
||||
public CommonResult<Boolean> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传用户个人头像
|
||||
*
|
||||
* @param file 头像文件
|
||||
* @return 上传结果
|
||||
*/
|
||||
@ApiOperation("上传用户个人头像")
|
||||
@PostMapping("/uploadAvatar")
|
||||
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);
|
||||
}
|
||||
}
|
||||
return CommonResult.success(false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.user.vo.user;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
@ApiModel("用户个人中心信息 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@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;
|
||||
/**
|
||||
* 所属角色
|
||||
*/
|
||||
@ApiModelProperty(value = "所属角色", required = true, example = "123456")
|
||||
private Set<Role> roles;
|
||||
|
||||
@ApiModel("角色")
|
||||
@Data
|
||||
public static class Role {
|
||||
|
||||
@ApiModelProperty(value = "角色编号", required = true, example = "1")
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "角色名称", required = true, example = "普通角色")
|
||||
private String name;
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
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.NotNull;
|
||||
|
||||
@ApiModel("用户更新 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SysUserProfileUpdateReqVO extends SysUserBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "用户编号", required = true, example = "1024")
|
||||
@NotNull(message = "用户编号不能为空")
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "旧密码", required = true, example = "123456")
|
||||
private String oldPassword;
|
||||
|
||||
@ApiModelProperty(value = "新密码", required = true, example = "654321")
|
||||
private String newPassword;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue