2022-04-22 15:26:39 +08:00
|
|
|
package net.maku.system.controller;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2023-01-18 00:04:56 +08:00
|
|
|
import jakarta.validation.Valid;
|
2022-04-22 15:26:39 +08:00
|
|
|
import lombok.AllArgsConstructor;
|
2023-01-20 13:47:45 +08:00
|
|
|
import net.maku.framework.common.utils.PageResult;
|
2022-04-22 15:26:39 +08:00
|
|
|
import net.maku.framework.common.utils.Result;
|
2023-05-22 15:59:15 +08:00
|
|
|
import net.maku.framework.operatelog.annotations.OperateLog;
|
|
|
|
import net.maku.framework.operatelog.enums.OperateTypeEnum;
|
2022-04-22 15:26:39 +08:00
|
|
|
import net.maku.framework.security.user.SecurityUser;
|
|
|
|
import net.maku.framework.security.user.UserDetail;
|
|
|
|
import net.maku.system.convert.SysUserConvert;
|
|
|
|
import net.maku.system.entity.SysUserEntity;
|
2022-05-05 13:58:45 +08:00
|
|
|
import net.maku.system.query.SysUserQuery;
|
2024-02-24 12:24:44 +08:00
|
|
|
import net.maku.system.service.SysPostService;
|
2022-04-22 15:26:39 +08:00
|
|
|
import net.maku.system.service.SysUserPostService;
|
|
|
|
import net.maku.system.service.SysUserRoleService;
|
|
|
|
import net.maku.system.service.SysUserService;
|
2024-04-01 16:37:36 +08:00
|
|
|
import net.maku.system.vo.SysUserAvatarVO;
|
2024-02-24 12:24:44 +08:00
|
|
|
import net.maku.system.vo.SysUserBaseVO;
|
2022-05-05 13:58:45 +08:00
|
|
|
import net.maku.system.vo.SysUserPasswordVO;
|
2022-08-28 00:01:17 +08:00
|
|
|
import net.maku.system.vo.SysUserVO;
|
2023-01-18 00:04:56 +08:00
|
|
|
import org.springdoc.core.annotations.ParameterObject;
|
2022-04-23 23:01:54 +08:00
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
2022-04-22 15:26:39 +08:00
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
2022-11-04 16:37:24 +08:00
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2022-04-22 15:26:39 +08:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户管理
|
|
|
|
*
|
|
|
|
* @author 阿沐 babamu@126.com
|
2023-02-21 16:44:04 +08:00
|
|
|
* <a href="https://maku.net">MAKU</a>
|
2022-04-22 15:26:39 +08:00
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("sys/user")
|
|
|
|
@AllArgsConstructor
|
2022-08-28 00:01:17 +08:00
|
|
|
@Tag(name = "用户管理")
|
2022-04-22 15:26:39 +08:00
|
|
|
public class SysUserController {
|
|
|
|
private final SysUserService sysUserService;
|
|
|
|
private final SysUserRoleService sysUserRoleService;
|
|
|
|
private final SysUserPostService sysUserPostService;
|
2024-02-24 12:24:44 +08:00
|
|
|
private final SysPostService sysPostService;
|
2022-04-22 15:26:39 +08:00
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
@GetMapping("page")
|
|
|
|
@Operation(summary = "分页")
|
2022-04-29 22:51:40 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:page')")
|
2023-01-15 20:08:11 +08:00
|
|
|
public Result<PageResult<SysUserVO>> page(@ParameterObject @Valid SysUserQuery query) {
|
2022-04-22 15:26:39 +08:00
|
|
|
PageResult<SysUserVO> page = sysUserService.page(query);
|
|
|
|
|
|
|
|
return Result.ok(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("{id}")
|
|
|
|
@Operation(summary = "信息")
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:info')")
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<SysUserVO> get(@PathVariable("id") Long id) {
|
2022-04-22 15:26:39 +08:00
|
|
|
SysUserEntity entity = sysUserService.getById(id);
|
|
|
|
|
|
|
|
SysUserVO vo = SysUserConvert.INSTANCE.convert(entity);
|
|
|
|
|
|
|
|
// 用户角色列表
|
|
|
|
List<Long> roleIdList = sysUserRoleService.getRoleIdList(id);
|
|
|
|
vo.setRoleIdList(roleIdList);
|
|
|
|
|
|
|
|
// 用户岗位列表
|
|
|
|
List<Long> postIdList = sysUserPostService.getPostIdList(id);
|
|
|
|
vo.setPostIdList(postIdList);
|
|
|
|
|
|
|
|
return Result.ok(vo);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("info")
|
|
|
|
@Operation(summary = "登录用户")
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<SysUserVO> info() {
|
2022-04-22 15:26:39 +08:00
|
|
|
SysUserVO user = SysUserConvert.INSTANCE.convert(SecurityUser.getUser());
|
|
|
|
|
2024-02-24 12:24:44 +08:00
|
|
|
// 用户岗位列表
|
|
|
|
List<Long> postIdList = sysUserPostService.getPostIdList(user.getId());
|
|
|
|
user.setPostIdList(postIdList);
|
|
|
|
|
|
|
|
// 用户岗位名称列表
|
|
|
|
List<String> postNameList = sysPostService.getNameList(postIdList);
|
|
|
|
user.setPostNameList(postNameList);
|
|
|
|
|
2022-04-22 15:26:39 +08:00
|
|
|
return Result.ok(user);
|
|
|
|
}
|
|
|
|
|
2024-02-24 12:24:44 +08:00
|
|
|
@PutMapping("info")
|
|
|
|
@Operation(summary = "修改登录用户信息")
|
|
|
|
@OperateLog(type = OperateTypeEnum.UPDATE)
|
|
|
|
public Result<String> loginInfo(@RequestBody @Valid SysUserBaseVO vo) {
|
|
|
|
sysUserService.updateLoginInfo(vo);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
2024-04-01 16:37:36 +08:00
|
|
|
@PutMapping("avatar")
|
|
|
|
@Operation(summary = "修改登录用户头像")
|
|
|
|
@OperateLog(type = OperateTypeEnum.UPDATE)
|
|
|
|
public Result<String> avatar(@RequestBody SysUserAvatarVO avatar) {
|
|
|
|
sysUserService.updateAvatar(avatar);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:26:39 +08:00
|
|
|
@PutMapping("password")
|
|
|
|
@Operation(summary = "修改密码")
|
2023-05-22 15:59:15 +08:00
|
|
|
@OperateLog(type = OperateTypeEnum.UPDATE)
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<String> password(@RequestBody @Valid SysUserPasswordVO vo) {
|
2022-04-22 15:26:39 +08:00
|
|
|
// 原密码不正确
|
|
|
|
UserDetail user = SecurityUser.getUser();
|
2022-08-28 00:01:17 +08:00
|
|
|
if (!passwordEncoder.matches(vo.getPassword(), user.getPassword())) {
|
2022-04-22 15:26:39 +08:00
|
|
|
return Result.error("原密码不正确");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 修改密码
|
|
|
|
sysUserService.updatePassword(user.getId(), passwordEncoder.encode(vo.getNewPassword()));
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
|
@Operation(summary = "保存")
|
2023-05-22 15:59:15 +08:00
|
|
|
@OperateLog(type = OperateTypeEnum.INSERT)
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:save')")
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<String> save(@RequestBody @Valid SysUserVO vo) {
|
2022-04-22 15:26:39 +08:00
|
|
|
// 新增密码不能为空
|
2022-08-28 00:01:17 +08:00
|
|
|
if (StrUtil.isBlank(vo.getPassword())) {
|
2023-10-07 16:20:33 +08:00
|
|
|
return Result.error("密码不能为空");
|
2022-04-22 15:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 密码加密
|
|
|
|
vo.setPassword(passwordEncoder.encode(vo.getPassword()));
|
|
|
|
|
|
|
|
// 保存
|
|
|
|
sysUserService.save(vo);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PutMapping
|
|
|
|
@Operation(summary = "修改")
|
2023-05-22 15:59:15 +08:00
|
|
|
@OperateLog(type = OperateTypeEnum.UPDATE)
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:update')")
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<String> update(@RequestBody @Valid SysUserVO vo) {
|
2022-04-22 15:26:39 +08:00
|
|
|
// 如果密码不为空,则进行加密处理
|
2022-08-28 00:01:17 +08:00
|
|
|
if (StrUtil.isBlank(vo.getPassword())) {
|
2022-04-22 15:26:39 +08:00
|
|
|
vo.setPassword(null);
|
2022-08-28 00:01:17 +08:00
|
|
|
} else {
|
2022-04-22 15:26:39 +08:00
|
|
|
vo.setPassword(passwordEncoder.encode(vo.getPassword()));
|
|
|
|
}
|
|
|
|
|
|
|
|
sysUserService.update(vo);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@DeleteMapping
|
|
|
|
@Operation(summary = "删除")
|
2023-05-22 15:59:15 +08:00
|
|
|
@OperateLog(type = OperateTypeEnum.DELETE)
|
2022-04-23 23:01:54 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:delete')")
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<String> delete(@RequestBody List<Long> idList) {
|
2022-06-01 20:26:44 +08:00
|
|
|
Long userId = SecurityUser.getUserId();
|
2022-08-28 00:01:17 +08:00
|
|
|
if (idList.contains(userId)) {
|
2022-06-01 20:26:44 +08:00
|
|
|
return Result.error("不能删除当前登录用户");
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:26:39 +08:00
|
|
|
sysUserService.delete(idList);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
2022-11-04 16:37:24 +08:00
|
|
|
|
|
|
|
@PostMapping("import")
|
|
|
|
@Operation(summary = "导入用户")
|
2023-05-22 15:59:15 +08:00
|
|
|
@OperateLog(type = OperateTypeEnum.IMPORT)
|
2022-11-04 16:45:18 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:import')")
|
2022-11-04 16:37:24 +08:00
|
|
|
public Result<String> importExcel(@RequestParam("file") MultipartFile file) {
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
return Result.error("请选择需要上传的文件");
|
|
|
|
}
|
|
|
|
sysUserService.importByExcel(file, passwordEncoder.encode("123456"));
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("export")
|
|
|
|
@Operation(summary = "导出用户")
|
2023-05-22 15:59:15 +08:00
|
|
|
@OperateLog(type = OperateTypeEnum.EXPORT)
|
2022-11-04 17:13:06 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:export')")
|
2022-11-09 17:19:39 +08:00
|
|
|
public void export() {
|
|
|
|
sysUserService.export();
|
2022-11-04 16:37:24 +08:00
|
|
|
}
|
2022-04-22 15:26:39 +08:00
|
|
|
}
|