| | |
| | | // 角色管理API |
| | | |
| | | import { API_CONFIG, graphqlRequest } from '@/config/api' |
| | | |
| | | // 角色相关的 GraphQL 查询 |
| | | export const ROLE_QUERIES = { |
| | | // 获取所有角色 |
| | |
| | | `, |
| | | |
| | | // 根据名称模糊查询角色 |
| | | GET_ROLES_BY_NAME: ` |
| | | query GetRolesByName($name: String!) { |
| | | rolesByName(name: $name) { |
| | | SEARCH_ROLES_BY_NAME: ` |
| | | query SearchRolesByName($name: String!) { |
| | | searchRolesByName(name: $name) { |
| | | id |
| | | code |
| | | name |
| | |
| | | // 获取所有角色 |
| | | 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 || [] |
| | | 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 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 || [] |
| | | const data = await graphqlRequest(ROLE_QUERIES.GET_ACTIVE_ROLES) |
| | | return data?.activeRoles || [] |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '获取启用角色列表失败') |
| | | throw new Error(error.message || '获取激活角色列表失败') |
| | | } |
| | | }, |
| | | |
| | | // 根据ID获取角色详情 |
| | | // 根据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 |
| | | 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 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 |
| | | const data = await graphqlRequest(ROLE_QUERIES.GET_ROLE_BY_CODE, { code }) |
| | | return data?.roleByCode || null |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '根据代码获取角色失败') |
| | | 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 || [] |
| | | const data = await graphqlRequest(ROLE_QUERIES.GET_ROLES_BY_STATE, { state }) |
| | | return data?.rolesByState || [] |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '根据状态获取角色失败') |
| | | 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 || [] |
| | | const data = await graphqlRequest(ROLE_QUERIES.SEARCH_ROLES_BY_NAME, { name }) |
| | | return data?.searchRolesByName || [] |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '根据名称搜索角色失败') |
| | | throw new Error(error.message || '搜索角色失败') |
| | | } |
| | | } |
| | | } |