// 角色管理API
|
|
import { API_CONFIG, graphqlRequest } from '@/config/api'
|
|
// 角色相关的 GraphQL 查询
|
export const ROLE_QUERIES = {
|
// 获取所有角色
|
GET_ROLES: `
|
query GetRoles {
|
roles {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 获取所有启用状态的角色
|
GET_ACTIVE_ROLES: `
|
query GetActiveRoles {
|
activeRoles {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 根据ID获取角色详情
|
GET_ROLE: `
|
query GetRole($id: Long!) {
|
role(id: $id) {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 根据角色代码获取角色
|
GET_ROLE_BY_CODE: `
|
query GetRoleByCode($code: String!) {
|
roleByCode(code: $code) {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 根据状态获取角色
|
GET_ROLES_BY_STATE: `
|
query GetRolesByState($state: Int!) {
|
rolesByState(state: $state) {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 根据名称模糊查询角色
|
SEARCH_ROLES_BY_NAME: `
|
query SearchRolesByName($name: String!) {
|
searchRolesByName(name: $name) {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}
|
`
|
}
|
|
// 类型定义
|
export interface Role {
|
id: string
|
code: string
|
name: string
|
description?: string
|
state: number
|
createTime?: string
|
updateTime?: string
|
}
|
|
// API 函数
|
export const roleApi = {
|
// 获取所有角色
|
async getRoles(): Promise<Role[]> {
|
try {
|
const data = await graphqlRequest(ROLE_QUERIES.GET_ROLES)
|
return data?.roles || []
|
} catch (error: any) {
|
throw new Error(error.message || '获取角色列表失败')
|
}
|
},
|
|
// 获取激活状态的角色
|
async getActiveRoles(): Promise<Role[]> {
|
try {
|
const data = await graphqlRequest(ROLE_QUERIES.GET_ACTIVE_ROLES)
|
return data?.activeRoles || []
|
} catch (error: any) {
|
throw new Error(error.message || '获取激活角色列表失败')
|
}
|
},
|
|
// 根据ID获取角色
|
async getRoleById(id: string): Promise<Role | null> {
|
try {
|
const data = await graphqlRequest(ROLE_QUERIES.GET_ROLE, { id })
|
return data?.role || null
|
} catch (error: any) {
|
throw new Error(error.message || '获取角色详情失败')
|
}
|
},
|
|
// 根据代码获取角色
|
async getRoleByCode(code: string): Promise<Role | null> {
|
try {
|
const data = await graphqlRequest(ROLE_QUERIES.GET_ROLE_BY_CODE, { code })
|
return data?.roleByCode || null
|
} catch (error: any) {
|
throw new Error(error.message || '获取角色详情失败')
|
}
|
},
|
|
// 根据状态获取角色
|
async getRolesByState(state: number): Promise<Role[]> {
|
try {
|
const data = await graphqlRequest(ROLE_QUERIES.GET_ROLES_BY_STATE, { state })
|
return data?.rolesByState || []
|
} catch (error: any) {
|
throw new Error(error.message || '获取角色列表失败')
|
}
|
},
|
|
// 根据名称搜索角色
|
async searchRolesByName(name: string): Promise<Role[]> {
|
try {
|
const data = await graphqlRequest(ROLE_QUERIES.SEARCH_ROLES_BY_NAME, { name })
|
return data?.searchRolesByName || []
|
} catch (error: any) {
|
throw new Error(error.message || '搜索角色失败')
|
}
|
}
|
}
|