设置 事务 rollback 属性

增加 个人信息设置 功能

增加 个人信息设置 功能
This commit is contained in:
niudehua 2021-03-13 21:07:04 +08:00
parent e4bda22b73
commit 61c24c0aa7
14 changed files with 98 additions and 111 deletions

View File

@ -41,7 +41,7 @@ public class InfJobServiceImpl implements InfJobService {
private SchedulerManager schedulerManager;
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public Long createJob(InfJobCreateReqVO createReqVO) throws SchedulerException {
validateCronExpression(createReqVO.getCronExpression());
// 校验唯一性
@ -66,7 +66,7 @@ public class InfJobServiceImpl implements InfJobService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void updateJob(InfJobUpdateReqVO updateReqVO) throws SchedulerException {
validateCronExpression(updateReqVO.getCronExpression());
// 校验存在
@ -86,7 +86,7 @@ public class InfJobServiceImpl implements InfJobService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void updateJobStatus(Long id, Integer status) throws SchedulerException {
// 校验 status
if (!containsAny(status, InfJobStatusEnum.NORMAL.getStatus(), InfJobStatusEnum.STOP.getStatus())) {
@ -120,7 +120,7 @@ public class InfJobServiceImpl implements InfJobService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void deleteJob(Long id) throws SchedulerException {
// 校验存在
InfJobDO job = this.validateJobExists(id);

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,7 +1,7 @@
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;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserProfileUpdateReqVO;
@ -12,8 +12,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 +26,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_IS_EMPTY;
/**
* @author niudehua
*/
@Api(tags = "用户个人中心")
@RestController
@RequestMapping("/system/user/profile")
@Api(tags = "用户个人中心")
@Slf4j
public class SysUserProfileController {
@Resource
@ -42,58 +48,33 @@ public class SysUserProfileController {
@Resource
private SysRoleService roleService;
/**
* 个人信息
*
* @return 个人信息详情
*/
@ApiOperation("获得登录用户信息")
@GetMapping("/get")
@ApiOperation("获得登录用户信息")
public CommonResult<SysUserProfileRespVO> profile() {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
// 获取用户信息
assert loginUser != null;
Long userId = loginUser.getId();
Long userId = SecurityFrameworkUtils.getLoginUserId();
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);
}
/**
* 修改个人信息
*
* @param reqVO 个人信息更新 reqVO
* @param request HttpServletRequest
* @return 修改结果
*/
@ApiOperation("修改用户个人信息")
@PostMapping("/update")
@ApiOperation("修改用户个人信息")
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);
userService.updateUserProfile(reqVO);
SecurityFrameworkUtils.setLoginUser(SysAuthConvert.INSTANCE.convert(reqVO), request);
return success(true);
}
/**
* 上传用户个人头像
*
* @param file 头像文件
* @return 上传结果
*/
@PostMapping("/upload-avatar")
@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);
}
public CommonResult<Boolean> uploadAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {
if (file.isEmpty()) {
throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
}
return CommonResult.success(false);
userService.updateAvatar(SecurityFrameworkUtils.getLoginUserId(), file.getInputStream());
return success(true);
}
}

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

@ -76,5 +76,6 @@ public interface SysErrorCodeConstants {
// ========== 文件 1002009000 ==========
ErrorCode FILE_PATH_EXISTS = new ErrorCode(1002009001, "文件路径已经存在");
ErrorCode FILE_UPLOAD_FAILED = new ErrorCode(1002009002, "文件上传失败");
ErrorCode FILE_IS_EMPTY= new ErrorCode(1002009003, "文件为空");
}

View File

@ -206,7 +206,7 @@ public class SysMenuServiceImpl implements SysMenuService {
*
* @param menuId 菜单编号
*/
@Transactional
@Transactional(rollbackFor = Exception.class)
public void deleteMenu(Long menuId) {
// 校验更新的菜单是否存在
if (menuMapper.selectById(menuId) == null) {

View File

@ -176,7 +176,7 @@ public class SysPermissionServiceImpl implements SysPermissionService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void assignRoleMenu(Long roleId, Set<Long> menuIds) {
// 获得角色拥有菜单编号
Set<Long> dbMenuIds = CollectionUtils.convertSet(roleMenuMapper.selectListByRoleId(roleId),

View File

@ -174,7 +174,7 @@ public class SysRoleServiceImpl implements SysRoleService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void deleteRole(Long id) {
// 校验是否可以更新
this.checkUpdateRole(id);

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);
/**
* 删除用户
@ -154,9 +154,8 @@ public interface SysUserService {
*
* @param id 用户 id
* @param avatarFile 头像文件
* @return 更新结果
*/
int updateAvatar(Long id, MultipartFile avatarFile);
void 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.util.IdUtil;
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,20 +66,7 @@ public class SysUserServiceImpl implements SysUserService {
private PasswordEncoder passwordEncoder;
@Resource
private SysFileService fileService;
// /**
// * 根据条件分页查询用户列表
// *
// * @param user 用户信息
// * @return 用户信息集合信息
// */
// @Override
// @DataScope(deptAlias = "d", userAlias = "u")
// public List<SysUser> selectUserList(SysUser user)
// {
// return userMapper.selectUserList(user);
// }
private InfFileService fileService;
@Override
public SysUserDO getUserByUserName(String username) {
@ -156,20 +143,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 +303,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,11 +323,11 @@ public class SysUserServiceImpl implements SysUserService {
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
throw ServiceExceptionUtil.exception(USER_PASSWORD_FAILED);
}
return false;
return true;
}
@Override
@Transactional // 添加事务异常则回滚所有导入
@Transactional(rollbackFor = Exception.class) // 添加事务异常则回滚所有导入
public SysUserImportRespVO importUsers(List<SysUserImportExcelVO> importUsers, boolean isUpdateSupport) {
if (CollUtil.isEmpty(importUsers)) {
throw ServiceExceptionUtil.exception(USER_IMPORT_LIST_IS_EMPTY);
@ -368,20 +365,15 @@ public class SysUserServiceImpl implements SysUserService {
}
@Override
public int updateAvatar(Long id, MultipartFile avatarFile) {
public void 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 = fileService.createFile(IdUtil.fastUUID(), IoUtil.readBytes(avatarFile));
// 更新路径
SysUserDO sysUserDO = new SysUserDO();
sysUserDO.setId(id);
sysUserDO.setAvatar(avatar);
return userMapper.updateById(sysUserDO);
userMapper.updateById(sysUserDO);
}
}

View File

@ -109,7 +109,7 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public List<Long> createCodegenListFromDB(List<String> tableNames) {
List<Long> ids = new ArrayList<>(tableNames.size());
// 遍历添加虽然效率会低一点但是没必要做成完全批量因为不会这么大量
@ -118,7 +118,7 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void updateCodegen(ToolCodegenUpdateReqVO updateReqVO) {
// 校验是否已经存在
if (codegenTableMapper.selectById(updateReqVO.getTable().getId()) == null) {
@ -134,7 +134,7 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void syncCodegenFromDB(Long tableId) {
// 校验是否已经存在
ToolCodegenTableDO table = codegenTableMapper.selectById(tableId);
@ -149,7 +149,7 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void syncCodegenFromSQL(Long tableId, String sql) {
// 校验是否已经存在
ToolCodegenTableDO table = codegenTableMapper.selectById(tableId);
@ -201,7 +201,7 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public void deleteCodegen(Long tableId) {
// 校验是否已经存在
if (codegenTableMapper.selectById(tableId) == null) {

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