feat: 添加用户管理员身份设置功能
- 数据库添加is_admin字段和索引 - User实体类添加isAdmin字段 - 后端添加setAdmin API(UserController和UserService) - 前端用户管理界面添加管理员标识列 - 添加设置/取消管理员按钮及功能 - 用户管理表格显示管理员标识(红色标签)
This commit is contained in:
parent
3f57f6d3de
commit
e0a9402efd
@ -134,5 +134,16 @@ public class UserController {
|
||||
log.info("获取设计师列表及工作量: deptId={}", deptId);
|
||||
return Result.success(userService.getDesignersWithWorkload(deptId));
|
||||
}
|
||||
|
||||
@Operation(summary = "设置/取消管理员", description = "设置或取消用户的管理员身份")
|
||||
@PutMapping("/{id}/admin")
|
||||
public Result<Boolean> setAdmin(
|
||||
@PathVariable Long id,
|
||||
@RequestBody Map<String, Integer> params
|
||||
) {
|
||||
Integer isAdmin = params.get("isAdmin");
|
||||
log.info("设置用户管理员身份: userId={}, isAdmin={}", id, isAdmin);
|
||||
return Result.success(userService.setAdmin(id, isAdmin));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -75,6 +75,11 @@ public class User implements Serializable {
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 是否管理员:0-否 1-是
|
||||
*/
|
||||
private Integer isAdmin;
|
||||
|
||||
/**
|
||||
* 用户积分
|
||||
*/
|
||||
|
||||
@ -1262,4 +1262,27 @@ public class UserService {
|
||||
log.info("查询到{}个设计师", result.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置/取消管理员
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean setAdmin(Long userId, Integer isAdmin) {
|
||||
log.info("设置用户管理员身份: userId={}, isAdmin={}", userId, isAdmin);
|
||||
|
||||
User user = getUserById(userId);
|
||||
if (user == null) {
|
||||
throw new BusinessException("用户不存在");
|
||||
}
|
||||
|
||||
// 更新管理员标识
|
||||
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
|
||||
updateWrapper.eq(User::getId, userId);
|
||||
updateWrapper.set(User::getIsAdmin, isAdmin != null ? isAdmin : 0);
|
||||
|
||||
boolean result = userMapper.update(null, updateWrapper) > 0;
|
||||
log.info("设置管理员身份结果: userId={}, isAdmin={}, result={}", userId, isAdmin, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
11
database/add_is_admin_field.sql
Normal file
11
database/add_is_admin_field.sql
Normal file
@ -0,0 +1,11 @@
|
||||
-- 为 sys_user 表添加 is_admin 字段
|
||||
-- 作者:系统
|
||||
-- 日期:2026-01-03
|
||||
-- 说明:添加管理员标识字段,用于标识用户是否具有管理员权限
|
||||
|
||||
ALTER TABLE `sys_user`
|
||||
ADD COLUMN `is_admin` TINYINT DEFAULT 0 COMMENT '是否管理员:0-否 1-是' AFTER `status`;
|
||||
|
||||
-- 更新索引以提高查询性能
|
||||
CREATE INDEX `idx_is_admin` ON `sys_user` (`is_admin`);
|
||||
|
||||
@ -10,6 +10,7 @@ export interface User {
|
||||
deptId?: number;
|
||||
deptName?: string;
|
||||
status?: number;
|
||||
isAdmin?: number; // 是否管理员:0-否 1-是
|
||||
avatar?: string;
|
||||
createTime?: string;
|
||||
updateTime?: string;
|
||||
@ -161,3 +162,11 @@ export const getDesignersWithWorkload = async (deptId?: number): Promise<Designe
|
||||
return response.data || [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置/取消管理员
|
||||
*/
|
||||
export const setAdmin = async (id: number, isAdmin: number): Promise<boolean> => {
|
||||
const response = await request.put(`/users/${id}/admin`, { isAdmin });
|
||||
return response.data;
|
||||
};
|
||||
|
||||
|
||||
@ -348,6 +348,14 @@
|
||||
<a-tag v-else color="orange" size="small">未分配</a-tag>
|
||||
</template>
|
||||
|
||||
<template v-else-if="column.key === 'isAdmin'">
|
||||
<a-tag v-if="record.isAdmin === 1" color="red" size="small">
|
||||
<Icon name="star" size="xs" style="margin-right: 4px;" />
|
||||
管理员
|
||||
</a-tag>
|
||||
<span v-else style="color: #999;">—</span>
|
||||
</template>
|
||||
|
||||
<template v-else-if="column.key === 'status'">
|
||||
<span :class="['status-badge', record.status === 1 ? 'active' : 'inactive']">
|
||||
{{ record.status === 1 ? '启用' : '禁用' }}
|
||||
@ -364,6 +372,15 @@
|
||||
<Icon name="shuffle" size="sm" style="margin-right: 4px;" />
|
||||
调整部门
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
:title="record.isAdmin === 1 ? '确定取消该用户的管理员身份?' : '确定设置该用户为管理员?'"
|
||||
@confirm="toggleUserAdmin(record)"
|
||||
>
|
||||
<a-button type="text" size="small" :type="record.isAdmin === 1 ? 'default' : 'primary'">
|
||||
<Icon :name="record.isAdmin === 1 ? 'lock' : 'star'" size="sm" style="margin-right: 4px;" />
|
||||
{{ record.isAdmin === 1 ? '取消管理员' : '设为管理员' }}
|
||||
</a-button>
|
||||
</a-popconfirm>
|
||||
<a-popconfirm title="确定删除该用户?" @confirm="deleteUserFromCard(record)">
|
||||
<a-button type="text" size="small" danger>
|
||||
<Icon name="trash" size="sm" style="margin-right: 4px;" />
|
||||
@ -1605,9 +1622,10 @@ const userManagementColumns = [
|
||||
{ title: 'ID', dataIndex: 'id', key: 'id', width: 80 },
|
||||
{ title: '用户信息', key: 'userInfo', width: 280 },
|
||||
{ title: '所属部门', key: 'department', width: 180 },
|
||||
{ title: '管理员', key: 'isAdmin', width: 100 },
|
||||
{ title: '状态', key: 'status', width: 100 },
|
||||
{ title: '创建时间', key: 'createTime', width: 160 },
|
||||
{ title: '操作', key: 'actions', width: 200, fixed: 'right' }
|
||||
{ title: '操作', key: 'actions', width: 250, fixed: 'right' }
|
||||
]
|
||||
|
||||
// 所有用户的扁平列表(用于表格展示)
|
||||
@ -2452,6 +2470,18 @@ const deleteUserFromCard = async (user: User) => {
|
||||
}
|
||||
}
|
||||
|
||||
const toggleUserAdmin = async (user: User) => {
|
||||
try {
|
||||
const newAdminStatus = user.isAdmin === 1 ? 0 : 1
|
||||
await userApi.setAdmin(user.id!, newAdminStatus)
|
||||
message.success(newAdminStatus === 1 ? '已设置为管理员' : '已取消管理员身份')
|
||||
await loadDepartmentUsers()
|
||||
} catch (error) {
|
||||
console.error('设置管理员失败:', error)
|
||||
message.error('设置管理员失败')
|
||||
}
|
||||
}
|
||||
|
||||
const goToUserManagement = () => {
|
||||
router.push('/users')
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user