mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 02:39:19 +08:00
feat: 超管账户切换
This commit is contained in:
+9
@@ -1,6 +1,7 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -54,6 +55,14 @@ public class AuthController {
|
||||
return ApiResponse.success(loginVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "切换账户", description = "超级管理员切换到指定用户账户,角色保持为super")
|
||||
@PostMapping("/switch/{userId}")
|
||||
public ApiResponse<LoginVO> switchAccount(
|
||||
@Parameter(description = "目标用户ID") @PathVariable Long userId) {
|
||||
LoginVO loginVO = userService.switchAccount(userId);
|
||||
return ApiResponse.success(loginVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户权限码", description = "获取当前登录用户的权限码列表")
|
||||
@GetMapping("/codes")
|
||||
public ApiResponse<List<String>> getAccessCodes() {
|
||||
|
||||
+13
-2
@@ -5,9 +5,12 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.ChangePasswordDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UpdateUserInfoDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UserFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.UserMenuService;
|
||||
@@ -67,8 +70,16 @@ public class UserController {
|
||||
|
||||
@Operation(summary = "获取用户菜单", description = "根据用户角色获取对应的菜单,用于前端动态路由")
|
||||
@GetMapping("/menu")
|
||||
public ApiResponse<List<UserMenuVO>> getUserRoutes() {
|
||||
List<UserMenuVO> userMenuVOS = userMenuService.getUserRoutes();
|
||||
public ApiResponse<List<UserMenuVO>> getUserMenus() {
|
||||
List<UserMenuVO> userMenuVOS = userMenuService.getUserMenus();
|
||||
return ApiResponse.success(userMenuVOS);
|
||||
}
|
||||
|
||||
@Operation(summary = "分页查询用户列表", description = "分页查询用户列表,支持多种过滤条件")
|
||||
@PostMapping("/page")
|
||||
public ApiResponse<PageResultVO<UserVO>> getUserPage(
|
||||
@Valid @RequestBody PageQueryDTO<UserFilterDTO> pageQuery) {
|
||||
PageResultVO<UserVO> pageResult = userService.getUserPage(pageQuery);
|
||||
return ApiResponse.success(pageResult);
|
||||
}
|
||||
}
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.model.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户分页查询过滤DTO
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户分页查询过滤DTO")
|
||||
public class UserFilterDTO {
|
||||
|
||||
/**
|
||||
* 用户名称(模糊查询)
|
||||
*/
|
||||
@Schema(description = "用户名称(模糊查询)", example = "张")
|
||||
private String realName;
|
||||
|
||||
/**
|
||||
* 登录账号(模糊查询)
|
||||
*/
|
||||
@Schema(description = "登录账号(模糊查询)", example = "admin")
|
||||
private String loginCode;
|
||||
|
||||
/**
|
||||
* 手机号码(精确查询)
|
||||
*/
|
||||
@Schema(description = "手机号码(精确查询)", example = "13800138000")
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "user", allowableValues = { "user", "admin", "super" })
|
||||
private String role;
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
*/
|
||||
@Schema(description = "公司ID", example = "1")
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 部门ID
|
||||
*/
|
||||
@Schema(description = "部门ID", example = "1")
|
||||
private Long departmentId;
|
||||
|
||||
/**
|
||||
* 岗位ID
|
||||
*/
|
||||
@Schema(description = "岗位ID", example = "1")
|
||||
private Long positionId;
|
||||
|
||||
/**
|
||||
* 状态查询
|
||||
*/
|
||||
@Schema(description = "用户状态", example = "0", allowableValues = { "0", "1", "2", "3" })
|
||||
private Integer status;
|
||||
}
|
||||
+1
-1
@@ -15,5 +15,5 @@ public interface UserMenuService {
|
||||
*
|
||||
* @return 路由列表
|
||||
*/
|
||||
List<UserMenuVO> getUserRoutes();
|
||||
List<UserMenuVO> getUserMenus();
|
||||
}
|
||||
|
||||
+19
@@ -2,11 +2,14 @@ package top.xyquan.ecomdmbbackend.modules.sys.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.ChangePasswordDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.LoginDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.RefreshTokenDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.RegisterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UpdateUserInfoDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UserFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.LoginVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserVO;
|
||||
@@ -77,4 +80,20 @@ public interface UserService extends IService<UserPO> {
|
||||
* @return 新的登录响应
|
||||
*/
|
||||
LoginVO refreshToken(RefreshTokenDTO refreshTokenDTO);
|
||||
|
||||
/**
|
||||
* 切换账户
|
||||
*
|
||||
* @param userId 目标用户ID
|
||||
* @return 登录响应
|
||||
*/
|
||||
LoginVO switchAccount(Long userId);
|
||||
|
||||
/**
|
||||
* 分页查询用户列表
|
||||
*
|
||||
* @param pageQuery 分页查询条件
|
||||
* @return 分页结果
|
||||
*/
|
||||
PageResultVO<UserVO> getUserPage(PageQueryDTO<UserFilterDTO> pageQuery);
|
||||
}
|
||||
|
||||
+28
-9
@@ -3,11 +3,13 @@ package top.xyquan.ecomdmbbackend.modules.sys.service.impl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.CompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.PositionCompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.MenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.UserMenuService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.UserService;
|
||||
import top.xyquan.ecomdmbbackend.utils.SecurityUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -23,24 +25,41 @@ public class UserMenuServiceImpl implements UserMenuService {
|
||||
private final MenuService menuService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
private final PositionCompanyMenuService positionCompanyMenuService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final UserService userService;
|
||||
|
||||
public UserMenuServiceImpl(MenuService menuService,
|
||||
CompanyMenuService companyMenuService,
|
||||
PositionCompanyMenuService positionCompanyMenuService,
|
||||
CurrentUserContext currentUserContext) {
|
||||
UserService userService) {
|
||||
this.menuService = menuService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
this.positionCompanyMenuService = positionCompanyMenuService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserMenuVO> getUserRoutes() {
|
||||
// 获取当前用户信息
|
||||
String userRole = currentUserContext.getCurrentUserRole();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long positionId = currentUserContext.getCurrentUserPositionId();
|
||||
public List<UserMenuVO> getUserMenus() {
|
||||
// 从Security上下文获取用户ID
|
||||
Long userId = SecurityUtils.getCurrentUserId();
|
||||
if (userId == null) {
|
||||
throw new RuntimeException("用户未登录");
|
||||
}
|
||||
|
||||
// 从数据库查询用户信息
|
||||
UserPO user = userService.getById(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
|
||||
// 检查用户状态
|
||||
if (user.getStatus() != 0) {
|
||||
throw new RuntimeException("用户状态异常");
|
||||
}
|
||||
|
||||
// 获取用户角色和其他信息
|
||||
String userRole = user.getRole();
|
||||
Long companyId = user.getCompanyId();
|
||||
Long positionId = user.getPositionId();
|
||||
|
||||
List<MenuVO> menus;
|
||||
|
||||
|
||||
+114
-8
@@ -8,11 +8,14 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.mapper.UserMapper;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.ChangePasswordDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.LoginDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.RefreshTokenDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.RegisterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UpdateUserInfoDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UserFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.LoginVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserVO;
|
||||
@@ -26,6 +29,8 @@ import top.xyquan.ecomdmbbackend.utils.RefreshTokenPayload;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 系统用户服务实现类
|
||||
@@ -263,14 +268,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
throw new BusinessException("刷新令牌已过期");
|
||||
}
|
||||
|
||||
// 从刷新令牌中获取用户ID
|
||||
String userId = jwtUtils.getUserIdFromToken(refreshTokenDTO.getRefreshToken());
|
||||
if (userId == null) {
|
||||
// 从刷新令牌中获取负载信息
|
||||
RefreshTokenPayload refreshPayload = jwtUtils.getRefreshPayloadFromToken(refreshTokenDTO.getRefreshToken());
|
||||
if (refreshPayload.getUserId() == null) {
|
||||
throw new BusinessException("刷新令牌格式错误");
|
||||
}
|
||||
|
||||
// 查询用户信息
|
||||
UserPO user = this.getById(Long.valueOf(userId));
|
||||
UserPO user = this.getById(Long.valueOf(refreshPayload.getUserId()));
|
||||
if (user == null) {
|
||||
throw new BusinessException("用户不存在");
|
||||
}
|
||||
@@ -280,27 +285,128 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
throw new BusinessException("用户状态异常,无法刷新令牌");
|
||||
}
|
||||
|
||||
// 生成新的登录响应
|
||||
return generateLoginResponse(user);
|
||||
// 生成新的登录响应,继承刷新token中的角色
|
||||
return generateLoginResponseWithRole(user, refreshPayload.getRole());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginVO switchAccount(Long userId) {
|
||||
// 查询目标用户信息
|
||||
UserPO targetUser = this.getById(userId);
|
||||
if (targetUser == null) {
|
||||
throw new BusinessException("目标用户不存在");
|
||||
}
|
||||
|
||||
// 检查目标用户状态
|
||||
if (targetUser.getStatus() != 0) {
|
||||
throw new BusinessException("目标用户状态异常,无法切换");
|
||||
}
|
||||
|
||||
// 生成登录响应,角色保持为super
|
||||
return generateLoginResponseWithRole(targetUser, "super");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResultVO<UserVO> getUserPage(PageQueryDTO<UserFilterDTO> pageQuery) {
|
||||
// 构建查询条件
|
||||
LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
UserFilterDTO filter = pageQuery.getFilters();
|
||||
if (filter != null) {
|
||||
// 用户名称模糊查询
|
||||
if (StringUtils.hasText(filter.getRealName())) {
|
||||
queryWrapper.like(UserPO::getRealName, filter.getRealName());
|
||||
}
|
||||
|
||||
// 登录账号模糊查询
|
||||
if (StringUtils.hasText(filter.getLoginCode())) {
|
||||
queryWrapper.like(UserPO::getLoginCode, filter.getLoginCode());
|
||||
}
|
||||
|
||||
// 手机号码精确查询
|
||||
if (StringUtils.hasText(filter.getPhone())) {
|
||||
queryWrapper.eq(UserPO::getPhone, filter.getPhone());
|
||||
}
|
||||
|
||||
// 用户角色查询
|
||||
if (StringUtils.hasText(filter.getRole())) {
|
||||
queryWrapper.eq(UserPO::getRole, filter.getRole());
|
||||
}
|
||||
|
||||
// 公司ID查询
|
||||
if (filter.getCompanyId() != null) {
|
||||
queryWrapper.eq(UserPO::getCompanyId, filter.getCompanyId());
|
||||
}
|
||||
|
||||
// 部门ID查询
|
||||
if (filter.getDepartmentId() != null) {
|
||||
queryWrapper.eq(UserPO::getDepartmentId, filter.getDepartmentId());
|
||||
}
|
||||
|
||||
// 岗位ID查询
|
||||
if (filter.getPositionId() != null) {
|
||||
queryWrapper.eq(UserPO::getPositionId, filter.getPositionId());
|
||||
}
|
||||
|
||||
// 状态查询
|
||||
if (filter.getStatus() != null) {
|
||||
queryWrapper.eq(UserPO::getStatus, filter.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
// 按创建时间倒序排列
|
||||
queryWrapper.orderByDesc(UserPO::getCreateTime);
|
||||
|
||||
// 执行分页查询
|
||||
com.baomidou.mybatisplus.extension.plugins.pagination.Page<UserPO> page =
|
||||
new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(
|
||||
pageQuery.getPage(), pageQuery.getPageSize());
|
||||
|
||||
com.baomidou.mybatisplus.extension.plugins.pagination.Page<UserPO> result = this.page(page, queryWrapper);
|
||||
|
||||
// 转换为VO
|
||||
List<UserVO> userVOList = result.getRecords().stream()
|
||||
.map(this::convertToUserVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建分页结果
|
||||
return PageResultVO.create(userVOList, result.getTotal(), pageQuery.getPage(), pageQuery.getPageSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为UserVO
|
||||
*/
|
||||
private UserVO convertToUserVO(UserPO user) {
|
||||
UserVO userVO = new UserVO();
|
||||
BeanUtils.copyProperties(user, userVO);
|
||||
return userVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成登录响应
|
||||
*/
|
||||
private LoginVO generateLoginResponse(UserPO user) {
|
||||
return generateLoginResponseWithRole(user, user.getRole());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成登录响应(指定角色)
|
||||
*/
|
||||
private LoginVO generateLoginResponseWithRole(UserPO user, String role) {
|
||||
LoginVO loginVO = new LoginVO();
|
||||
|
||||
// 生成访问令牌负载信息(包含完整用户信息)
|
||||
AccessTokenPayload accessPayload = AccessTokenPayload.builder()
|
||||
.userId(user.getId().toString())
|
||||
.companyId(user.getCompanyId())
|
||||
.role(user.getRole())
|
||||
.role(role != null ? role : user.getRole())
|
||||
.positionId(user.getPositionId())
|
||||
.build();
|
||||
|
||||
// 生成刷新令牌负载信息(只包含用户ID)
|
||||
// 生成刷新令牌负载信息(包含用户ID和角色)
|
||||
RefreshTokenPayload refreshPayload = RefreshTokenPayload.builder()
|
||||
.userId(user.getId().toString())
|
||||
.role(role != null ? role : user.getRole())
|
||||
.build();
|
||||
|
||||
// 生成访问令牌和刷新令牌
|
||||
|
||||
@@ -72,6 +72,7 @@ public class JwtUtils {
|
||||
|
||||
return Jwts.builder()
|
||||
.setSubject(payload.getUserId())
|
||||
.claim("role", payload.getRole())
|
||||
.claim("tokenType", payload.getTokenType())
|
||||
.setIssuedAt(now)
|
||||
.setExpiration(expiryDate)
|
||||
@@ -111,6 +112,27 @@ public class JwtUtils {
|
||||
return generateToken(payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从刷新令牌中获取负载信息
|
||||
*
|
||||
* @param token 刷新令牌
|
||||
* @return 刷新令牌负载信息
|
||||
*/
|
||||
public RefreshTokenPayload getRefreshPayloadFromToken(String token) {
|
||||
initKey();
|
||||
Claims claims = Jwts.parserBuilder()
|
||||
.setSigningKey(key)
|
||||
.build()
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
|
||||
return RefreshTokenPayload.builder()
|
||||
.userId(claims.getSubject())
|
||||
.role(getStringFromClaims(claims, "role"))
|
||||
.tokenType(getStringFromClaims(claims, "tokenType"))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从JWT令牌中获取完整的负载信息
|
||||
*
|
||||
|
||||
+5
@@ -20,6 +20,11 @@ public class RefreshTokenPayload {
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
private String role;
|
||||
|
||||
/**
|
||||
* 令牌类型
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user