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;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import net.maku.framework.common.page.PageResult;
|
|
|
|
import net.maku.framework.common.utils.Result;
|
|
|
|
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;
|
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;
|
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;
|
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 javax.validation.Valid;
|
|
|
|
import java.util.List;
|
2022-11-04 16:37:24 +08:00
|
|
|
import java.util.Map;
|
2022-04-22 15:26:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户管理
|
|
|
|
*
|
|
|
|
* @author 阿沐 babamu@126.com
|
|
|
|
*/
|
|
|
|
@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;
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
@GetMapping("page")
|
|
|
|
@Operation(summary = "分页")
|
2022-04-29 22:51:40 +08:00
|
|
|
@PreAuthorize("hasAuthority('sys:user:page')")
|
2022-08-28 00:01:17 +08:00
|
|
|
public Result<PageResult<SysUserVO>> page(@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());
|
|
|
|
|
|
|
|
return Result.ok(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PutMapping("password")
|
|
|
|
@Operation(summary = "修改密码")
|
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 = "保存")
|
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())) {
|
2022-04-22 15:26:39 +08:00
|
|
|
Result.error("密码不能为空");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 密码加密
|
|
|
|
vo.setPassword(passwordEncoder.encode(vo.getPassword()));
|
|
|
|
|
|
|
|
// 保存
|
|
|
|
sysUserService.save(vo);
|
|
|
|
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PutMapping
|
|
|
|
@Operation(summary = "修改")
|
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 = "删除")
|
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 = "导入用户")
|
|
|
|
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 = "导出用户")
|
|
|
|
public Result<Map<String, String>> export() {
|
|
|
|
Map<String, String> map = sysUserService.export();
|
|
|
|
|
|
|
|
return Result.ok(map);
|
|
|
|
}
|
2022-04-22 15:26:39 +08:00
|
|
|
}
|