178 lines
4.9 KiB
TypeScript
178 lines
4.9 KiB
TypeScript
import type { Role } from "./types"
|
|
import { mockResponse, type PaginatedResponse, type PaginationParams } from "./client"
|
|
|
|
// 模拟角色数据
|
|
const mockRoles: Role[] = [
|
|
{
|
|
id: "1",
|
|
name: "超级管理员",
|
|
description: "拥有系统所有权限",
|
|
permissions: ["all"],
|
|
userCount: 1,
|
|
createdAt: "2023-01-01",
|
|
updatedAt: "2023-01-01",
|
|
},
|
|
{
|
|
id: "2",
|
|
name: "内容管理员",
|
|
description: "管理系统内容,包括服装、道具、歌曲等",
|
|
permissions: ["content_view", "content_edit", "content_delete"],
|
|
userCount: 3,
|
|
createdAt: "2023-01-15",
|
|
updatedAt: "2023-03-20",
|
|
},
|
|
{
|
|
id: "3",
|
|
name: "AI模型管理员",
|
|
description: "管理AI模型的训练、部署和监控",
|
|
permissions: ["ai_model_view", "ai_model_edit", "ai_model_deploy"],
|
|
userCount: 2,
|
|
createdAt: "2023-02-10",
|
|
updatedAt: "2023-04-15",
|
|
},
|
|
{
|
|
id: "4",
|
|
name: "卡牌管理员",
|
|
description: "管理卡牌系统,包括服装卡、道具卡等",
|
|
permissions: ["card_view", "card_edit", "card_delete", "card_print"],
|
|
userCount: 4,
|
|
createdAt: "2023-03-05",
|
|
updatedAt: "2023-05-10",
|
|
},
|
|
{
|
|
id: "5",
|
|
name: "查看者",
|
|
description: "只有查看权限,无法编辑或删除内容",
|
|
permissions: ["dashboard_view", "content_view"],
|
|
userCount: 8,
|
|
createdAt: "2023-01-20",
|
|
updatedAt: "2023-01-20",
|
|
},
|
|
]
|
|
|
|
// 获取角色列表
|
|
export const getRoles = async (params?: PaginationParams): Promise<PaginatedResponse<Role>> => {
|
|
let filteredRoles = [...mockRoles]
|
|
|
|
// 搜索过滤
|
|
if (params?.search) {
|
|
const search = params.search.toLowerCase()
|
|
filteredRoles = filteredRoles.filter(
|
|
(role) =>
|
|
role.name.toLowerCase().includes(search) ||
|
|
(role.description && role.description.toLowerCase().includes(search)),
|
|
)
|
|
}
|
|
|
|
// 排序
|
|
if (params?.sortBy) {
|
|
filteredRoles.sort((a: any, b: any) => {
|
|
const aValue = a[params.sortBy!]
|
|
const bValue = b[params.sortBy!]
|
|
|
|
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
return params.sortOrder === "desc" ? bValue.localeCompare(aValue) : aValue.localeCompare(bValue)
|
|
}
|
|
|
|
return params.sortOrder === "desc" ? bValue - aValue : aValue - bValue
|
|
})
|
|
}
|
|
|
|
// 分页
|
|
const page = params?.page || 1
|
|
const pageSize = params?.pageSize || 10
|
|
const startIndex = (page - 1) * pageSize
|
|
const paginatedRoles = filteredRoles.slice(startIndex, startIndex + pageSize)
|
|
|
|
return mockResponse({
|
|
items: paginatedRoles,
|
|
total: filteredRoles.length,
|
|
page,
|
|
pageSize,
|
|
totalPages: Math.ceil(filteredRoles.length / pageSize),
|
|
})
|
|
}
|
|
|
|
// 获取单个角色
|
|
export const getRole = async (id: string): Promise<Role> => {
|
|
const role = mockRoles.find((role) => role.id === id)
|
|
|
|
if (!role) {
|
|
return mockResponse({} as Role, "角色不存在")
|
|
}
|
|
|
|
return mockResponse(role)
|
|
}
|
|
|
|
// 创建角色
|
|
export const createRole = async (roleData: Partial<Role>): Promise<Role> => {
|
|
// 检查角色名是否已存在
|
|
if (mockRoles.some((role) => role.name === roleData.name)) {
|
|
return mockResponse({} as Role, "该角色名已存在")
|
|
}
|
|
|
|
const newRole: Role = {
|
|
id: String(mockRoles.length + 1),
|
|
name: roleData.name || "",
|
|
description: roleData.description,
|
|
permissions: roleData.permissions || [],
|
|
userCount: 0,
|
|
createdAt: new Date().toISOString().split("T")[0],
|
|
updatedAt: new Date().toISOString().split("T")[0],
|
|
}
|
|
|
|
mockRoles.push(newRole)
|
|
|
|
return mockResponse(newRole)
|
|
}
|
|
|
|
// 更新角色
|
|
export const updateRole = async (id: string, roleData: Partial<Role>): Promise<Role> => {
|
|
const roleIndex = mockRoles.findIndex((role) => role.id === id)
|
|
|
|
if (roleIndex === -1) {
|
|
return mockResponse({} as Role, "角色不存在")
|
|
}
|
|
|
|
// 检查角色名是否已被其他角色使用
|
|
if (roleData.name && roleData.name !== mockRoles[roleIndex].name) {
|
|
const nameExists = mockRoles.some((role) => role.name === roleData.name && role.id !== id)
|
|
if (nameExists) {
|
|
return mockResponse({} as Role, "该角色名已被其他角色使用")
|
|
}
|
|
}
|
|
|
|
const updatedRole = {
|
|
...mockRoles[roleIndex],
|
|
...roleData,
|
|
updatedAt: new Date().toISOString().split("T")[0],
|
|
}
|
|
|
|
mockRoles[roleIndex] = updatedRole
|
|
|
|
return mockResponse(updatedRole)
|
|
}
|
|
|
|
// 删除角色
|
|
export const deleteRole = async (id: string): Promise<boolean> => {
|
|
const roleIndex = mockRoles.findIndex((role) => role.id === id)
|
|
|
|
if (roleIndex === -1) {
|
|
return mockResponse(false, "角色不存在")
|
|
}
|
|
|
|
// 超级管理员不能删除
|
|
if (mockRoles[roleIndex].name === "超级管理员") {
|
|
return mockResponse(false, "不能删除超级管理员角色")
|
|
}
|
|
|
|
// 有用户使用的角色不能删除
|
|
if (mockRoles[roleIndex].userCount && mockRoles[roleIndex].userCount > 0) {
|
|
return mockResponse(false, "该角色下有用户,不能删除")
|
|
}
|
|
|
|
mockRoles.splice(roleIndex, 1)
|
|
|
|
return mockResponse(true)
|
|
}
|