163 lines
6.7 KiB
Java
163 lines
6.7 KiB
Java
package com.yj.earth.business.controller;
|
|
|
|
import cn.dev33.satoken.stp.SaTokenInfo;
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import cn.hutool.crypto.digest.BCrypt;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.yj.earth.annotation.CheckAuth;
|
|
import com.yj.earth.annotation.EncryptResponse;
|
|
import com.yj.earth.annotation.ExcludeField;
|
|
import com.yj.earth.annotation.RoleAccess;
|
|
import com.yj.earth.business.domain.Role;
|
|
import com.yj.earth.business.domain.User;
|
|
import com.yj.earth.business.service.RoleService;
|
|
import com.yj.earth.dto.relation.UserBindOrUnBindRoleDto;
|
|
import com.yj.earth.dto.user.*;
|
|
import com.yj.earth.business.service.UserService;
|
|
import com.yj.earth.common.util.ApiResponse;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Tag(name = "用户数据管理")
|
|
@RestController
|
|
@RequestMapping("/user")
|
|
public class UserController {
|
|
@Resource
|
|
private UserService userService;
|
|
@Resource
|
|
private RoleService roleService;
|
|
|
|
@Operation(summary = "新增用户")
|
|
@PostMapping("/add")
|
|
@RoleAccess(roleNames = "管理员")
|
|
public ApiResponse save(@RequestBody AddUserDto addUserDto) {
|
|
User user = new User();
|
|
BeanUtils.copyProperties(addUserDto, user);
|
|
if (userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername())) != null) {
|
|
return ApiResponse.failure("用户已存在");
|
|
}
|
|
String password = user.getPassword();
|
|
user.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
|
|
userService.save(user);
|
|
return ApiResponse.success(null);
|
|
}
|
|
|
|
@Operation(summary = "更新信息")
|
|
@PostMapping("/update")
|
|
public ApiResponse update(@RequestBody UpdateUserDto updateUserDto) {
|
|
User user = new User();
|
|
BeanUtils.copyProperties(updateUserDto, user);
|
|
userService.updateById(user);
|
|
return ApiResponse.success(null);
|
|
}
|
|
|
|
@Operation(summary = "更新密码")
|
|
@PostMapping("/updatePassword")
|
|
public ApiResponse updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) {
|
|
User user = userService.getById(updatePasswordDto.getId());
|
|
if (user == null) {
|
|
return ApiResponse.failure("用户不存在");
|
|
}
|
|
if (!BCrypt.checkpw(updatePasswordDto.getOldPassword(), user.getPassword())) {
|
|
return ApiResponse.failure("旧密码错误");
|
|
}
|
|
user.setPassword(BCrypt.hashpw(updatePasswordDto.getNewPassword(), BCrypt.gensalt()));
|
|
userService.updateById(user);
|
|
return ApiResponse.success(null);
|
|
}
|
|
|
|
@Operation(summary = "用户详情")
|
|
@GetMapping("/getById")
|
|
public ApiResponse get(@Parameter(description = "用户ID") String id) {
|
|
return ApiResponse.success(userService.getById(id));
|
|
}
|
|
|
|
@Operation(summary = "用户列表")
|
|
@GetMapping("/list")
|
|
@RoleAccess(roleNames = "管理员")
|
|
public ApiResponse list(@Parameter(description = "分页数量") Integer pageNum,
|
|
@Parameter(description = "分页大小") Integer pageSize,
|
|
@Parameter(description = "搜索字段") String searchKey,
|
|
@Parameter(description = "角色ID") String roleId,
|
|
@Parameter(description = "用户状态") Integer status) {
|
|
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
|
// 根据用户名或者昵称进行模糊搜索
|
|
if (StringUtils.isNotBlank(searchKey)) {
|
|
wrapper.nested(q -> q.like(User::getUsername, searchKey).or().like(User::getNickname, searchKey));
|
|
}
|
|
// 根据角色ID等值搜索
|
|
if (StringUtils.isNotBlank(roleId)) {
|
|
wrapper.eq(User::getRoleId, roleId);
|
|
}
|
|
// 角色状态处理
|
|
if (status != null) {
|
|
wrapper.eq(User::getStatus, status);
|
|
}
|
|
Page<User> userPage = userService.page(new Page<>(pageNum, pageSize), wrapper);
|
|
return ApiResponse.success(userPage);
|
|
}
|
|
|
|
@Operation(summary = "设置角色")
|
|
@PostMapping("/userBindOrUnBindRole")
|
|
@RoleAccess(roleNames = "管理员")
|
|
public ApiResponse userBindOrUnBindRole(@RequestBody UserBindOrUnBindRoleDto userBindOrUnBindRoleDto) {
|
|
userService.lambdaUpdate().set(User::getRoleId, userBindOrUnBindRoleDto.getRoleId()).eq(User::getId, userBindOrUnBindRoleDto.getUserId()).update();
|
|
return ApiResponse.success(null);
|
|
}
|
|
|
|
@Operation(summary = "用户登录")
|
|
@PostMapping("/login")
|
|
public ApiResponse login(@RequestBody UserLoginDto userLoginDto) {
|
|
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userLoginDto.getUsername()));
|
|
if (user == null) {
|
|
return ApiResponse.failure("用户名不存在");
|
|
}
|
|
if (!BCrypt.checkpw(userLoginDto.getPassword(), user.getPassword())) {
|
|
return ApiResponse.failure("密码错误");
|
|
}
|
|
StpUtil.login(user.getId());
|
|
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
|
|
return ApiResponse.success(Map.of("header", tokenInfo.getTokenName(), "token", tokenInfo.getTokenValue()));
|
|
}
|
|
|
|
@Operation(summary = "用户登出")
|
|
@PostMapping("/logout")
|
|
public ApiResponse logout() {
|
|
StpUtil.logout();
|
|
return ApiResponse.success(null);
|
|
}
|
|
|
|
@Operation(summary = "获取当前用户信息")
|
|
@GetMapping("/getCurrentUserInfo")
|
|
public ApiResponse getCurrentUserInfo() {
|
|
return ApiResponse.success(userService.getById(StpUtil.getLoginIdAsString()));
|
|
}
|
|
|
|
@Operation(summary = "启用禁用用户数统计")
|
|
@GetMapping("/getUserStatusCount")
|
|
public ApiResponse getUserStatusCount() {
|
|
// 查询状态为1的用户数
|
|
long useUserCount = userService.count(new LambdaQueryWrapper<User>().eq(User::getStatus, 1));
|
|
// 查询状态为0的用户数
|
|
long bindUserCount = userService.count(new LambdaQueryWrapper<User>().eq(User::getStatus, 0));
|
|
return ApiResponse.success(Map.of("useUserCount", useUserCount, "bindUserCount", bindUserCount));
|
|
}
|
|
|
|
@Operation(summary = "删除用户")
|
|
@PostMapping("/deletes")
|
|
@RoleAccess(roleNames = "管理员")
|
|
public ApiResponse deletes(@Parameter(description = "用户ID列表") @RequestBody List<String> ids) {
|
|
userService.removeByIds(ids);
|
|
return ApiResponse.success(null);
|
|
}
|
|
}
|