// 角色管理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
|
}
|
}
|
`,
|
|
// 根据名称模糊查询角色
|
GET_ROLES_BY_NAME: `
|
query GetRolesByName($name: String!) {
|
rolesByName(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 response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: ROLE_QUERIES.GET_ROLES
|
})
|
})
|
const result = await response.json()
|
if (result.errors) {
|
throw new Error(result.errors[0].message)
|
}
|
return result.data?.roles || []
|
} catch (error: any) {
|
throw new Error(error.message || '获取角色列表失败')
|
}
|
},
|
|
// 获取所有启用状态的角色
|
async getActiveRoles(): Promise<Role[]> {
|
try {
|
const response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: ROLE_QUERIES.GET_ACTIVE_ROLES
|
})
|
})
|
const result = await response.json()
|
if (result.errors) {
|
throw new Error(result.errors[0].message)
|
}
|
return result.data?.activeRoles || []
|
} catch (error: any) {
|
throw new Error(error.message || '获取启用角色列表失败')
|
}
|
},
|
|
// 根据ID获取角色详情
|
async getRoleById(id: string): Promise<Role | null> {
|
try {
|
const response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: ROLE_QUERIES.GET_ROLE,
|
variables: { id }
|
})
|
})
|
const result = await response.json()
|
if (result.errors) {
|
throw new Error(result.errors[0].message)
|
}
|
return result.data?.role || null
|
} catch (error: any) {
|
throw new Error(error.message || '获取角色详情失败')
|
}
|
},
|
|
// 根据角色代码获取角色
|
async getRoleByCode(code: string): Promise<Role | null> {
|
try {
|
const response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: ROLE_QUERIES.GET_ROLE_BY_CODE,
|
variables: { code }
|
})
|
})
|
const result = await response.json()
|
if (result.errors) {
|
throw new Error(result.errors[0].message)
|
}
|
return result.data?.roleByCode || null
|
} catch (error: any) {
|
throw new Error(error.message || '根据代码获取角色失败')
|
}
|
},
|
|
// 根据状态获取角色
|
async getRolesByState(state: number): Promise<Role[]> {
|
try {
|
const response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: ROLE_QUERIES.GET_ROLES_BY_STATE,
|
variables: { state }
|
})
|
})
|
const result = await response.json()
|
if (result.errors) {
|
throw new Error(result.errors[0].message)
|
}
|
return result.data?.rolesByState || []
|
} catch (error: any) {
|
throw new Error(error.message || '根据状态获取角色失败')
|
}
|
},
|
|
// 根据名称模糊查询角色
|
async searchRolesByName(name: string): Promise<Role[]> {
|
try {
|
const response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: ROLE_QUERIES.GET_ROLES_BY_NAME,
|
variables: { name }
|
})
|
})
|
const result = await response.json()
|
if (result.errors) {
|
throw new Error(result.errors[0].message)
|
}
|
return result.data?.rolesByName || []
|
} catch (error: any) {
|
throw new Error(error.message || '根据名称搜索角色失败')
|
}
|
}
|
}
|