|
|
@@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.xuekairui.common.BusinessException;
|
|
|
import com.xuekairui.common.ErrorCode;
|
|
|
import com.xuekairui.user.dto.AdminUserResponse;
|
|
|
-import com.xuekairui.user.dto.UpdateRoleRequest;
|
|
|
import com.xuekairui.user.entity.Admin;
|
|
|
import com.xuekairui.user.entity.User;
|
|
|
import com.xuekairui.user.mapper.AdminMapper;
|
|
|
@@ -17,7 +16,6 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
@@ -49,7 +47,6 @@ public class AdminUserService {
|
|
|
if (nickname != null && !nickname.isBlank()) {
|
|
|
wrapper.like(User::getNickname, nickname);
|
|
|
}
|
|
|
- // 前端用户列表仅查 USER,管理员走 listAdmins
|
|
|
wrapper.orderByDesc(User::getCreateTime);
|
|
|
|
|
|
Page<User> userPage = userMapper.selectPage(page, wrapper);
|
|
|
@@ -129,74 +126,6 @@ public class AdminUserService {
|
|
|
tokenVersionCache.clear();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 修改用户角色(仅超级管理员可调用)
|
|
|
- * 升级为 ADMIN/SUPER_ADMIN → 插入 t_admin;降级为 USER → 删除 t_admin 记录
|
|
|
- */
|
|
|
- @Transactional
|
|
|
- public AdminUserResponse updateUserRole(Long userId, UpdateRoleRequest request, Long operatorId) {
|
|
|
- User user = userMapper.selectById(userId);
|
|
|
- if (user == null) {
|
|
|
- throw new BusinessException(ErrorCode.USER_NOT_FOUND);
|
|
|
- }
|
|
|
-
|
|
|
- String targetRole = request.getRole();
|
|
|
- Admin existingAdmin = adminMapper.selectById(userId);
|
|
|
-
|
|
|
- // 禁止取消最后一个超级管理员
|
|
|
- if (existingAdmin != null && "SUPER_ADMIN".equals(existingAdmin.getRole())
|
|
|
- && !"SUPER_ADMIN".equals(targetRole)) {
|
|
|
- long otherCount = adminMapper.selectCount(
|
|
|
- new LambdaQueryWrapper<Admin>()
|
|
|
- .eq(Admin::getRole, "SUPER_ADMIN")
|
|
|
- .ne(Admin::getId, userId));
|
|
|
- if (otherCount == 0) {
|
|
|
- throw new BusinessException(ErrorCode.BUSINESS_ERROR, "系统中至少保留一个超级管理员");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (Objects.equals(operatorId, userId)
|
|
|
- && existingAdmin != null && "SUPER_ADMIN".equals(existingAdmin.getRole())
|
|
|
- && !"SUPER_ADMIN".equals(targetRole)) {
|
|
|
- throw new BusinessException(ErrorCode.BUSINESS_ERROR, "不能取消自己的超级管理员权限");
|
|
|
- }
|
|
|
-
|
|
|
- if ("ADMIN".equals(targetRole) || "SUPER_ADMIN".equals(targetRole)) {
|
|
|
- // 升级:插入或更新 t_admin
|
|
|
- if (existingAdmin != null) {
|
|
|
- existingAdmin.setRole(targetRole);
|
|
|
- adminMapper.updateById(existingAdmin);
|
|
|
- } else {
|
|
|
- // 如果没有密码,用手机号后6位作为初始密码(BCrypt加密)
|
|
|
- String pwd = user.getPassword();
|
|
|
- if (pwd == null || pwd.isEmpty()) {
|
|
|
- String phone = user.getPhone();
|
|
|
- String rawPwd = phone != null && phone.length() >= 6
|
|
|
- ? phone.substring(phone.length() - 6) : "123456";
|
|
|
- pwd = passwordUtil.encode(rawPwd);
|
|
|
- }
|
|
|
- Admin admin = Admin.builder()
|
|
|
- .id(userId)
|
|
|
- .username(user.getPhone() != null ? user.getPhone() : "admin_" + userId)
|
|
|
- .password(pwd)
|
|
|
- .phone(user.getPhone())
|
|
|
- .role(targetRole)
|
|
|
- .status(1)
|
|
|
- .build();
|
|
|
- adminMapper.insert(admin);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 降级为 USER:删除 t_admin 记录
|
|
|
- if (existingAdmin != null) {
|
|
|
- adminMapper.deleteById(userId);
|
|
|
- tokenVersionCache.clear();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- log.info("操作人{}修改用户{}角色为{}", operatorId, userId, targetRole);
|
|
|
- return toResponse(user);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 强制踢出用户(递增 tokenVersion,所有 Token 立即失效)
|
|
|
*/
|
|
|
@@ -233,7 +162,7 @@ public class AdminUserService {
|
|
|
|
|
|
private AdminUserResponse buildAdminResponse(Admin a) {
|
|
|
return AdminUserResponse.builder()
|
|
|
- .id(a.getId()).phone(a.getPhone()).nickname(a.getUsername())
|
|
|
+ .id(a.getId()).phone(a.getPhone()).userName(a.getUsername())
|
|
|
.role(a.getRole()).status(a.getStatus()).createTime(a.getCreateTime())
|
|
|
.build();
|
|
|
}
|
|
|
@@ -242,7 +171,7 @@ public class AdminUserService {
|
|
|
return AdminUserResponse.builder()
|
|
|
.id(user.getId())
|
|
|
.phone(user.getPhone())
|
|
|
- .nickname(user.getNickname())
|
|
|
+ .userName(user.getNickname())
|
|
|
.role("USER")
|
|
|
.status(user.getStatus())
|
|
|
.createTime(user.getCreateTime())
|