lrj
1 天以前 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 角色管理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 || '搜索角色失败')
    }
  }
}