role.ts
3.61 KB
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
import axios from 'axios'
// API基础配置
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8083'
// 创建axios实例
const api = axios.create({
baseURL: API_BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
})
console.log('角色API实例创建,baseURL:', API_BASE_URL)
// 请求拦截器 - 添加token
api.interceptors.request.use(
(config) => {
console.log('角色API请求:', {
url: config.url,
baseURL: config.baseURL,
method: config.method,
params: config.params,
fullURL: `${config.baseURL}${config.url}`
})
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器
api.interceptors.response.use(
(response) => {
return response.data
},
(error) => {
console.error('API请求错误:', error)
return Promise.reject(error)
}
)
// 菜单接口类型定义
export interface Menu {
menuId: number
parentId?: number
menuName: string
menuType: string
menuTypeText: string
path?: string
icon?: string
sort: number
status: number
statusText: string
perms?: string
createTime: string
updateTime: string
children?: Menu[]
}
// 角色接口类型定义
export interface Role {
roleId: number
roleCode: string
roleName: string
status: number
statusText: string
remark: string
createTime: string
updateTime: string
menuIds?: number[]
menus?: Menu[]
}
export interface RoleSearchParams {
roleCode?: string
roleName?: string
status?: number
pageNum?: number
pageSize?: number
}
export interface ApiResponse<T = any> {
code: number
message: string
data: T
}
export interface PageResponse<T> {
records: T[]
total: number
size: number
current: number
orders: any[]
optimizeCountSql: boolean
searchCount: boolean
maxLimit: any
countId: any
pages: number
}
// 角色API接口
export const roleApi = {
// 获取角色列表
getRoleList: (params?: RoleSearchParams): Promise<ApiResponse<PageResponse<Role>>> => {
return api.get('/api/system/role/list', { params })
},
// 获取所有角色(不分页,用于下拉选择)
getAllRoles: (): Promise<ApiResponse<Role[]>> => {
return api.get('/api/system/role/list', {
params: { status: 1, pageSize: 9999 }
})
},
// 根据ID获取角色详情
getRoleById: (roleId: number): Promise<ApiResponse<Role>> => {
return api.get(`/api/system/role/${roleId}`)
},
// 创建角色
createRole: (roleData: any): Promise<ApiResponse<any>> => {
return api.post('/api/system/role/add', roleData)
},
// 更新角色
updateRole: (roleData: any): Promise<ApiResponse<any>> => {
return api.post('/api/system/role/edit', roleData)
},
// 删除角色(单个或批量)
deleteRole: (roleIds: number[]): Promise<ApiResponse<any>> => {
return api.delete(`/api/system/role/${roleIds.join(',')}`)
},
// 更新角色状态
updateRoleStatus: (roleId: number, status: number): Promise<ApiResponse<any>> => {
return api.post('/api/system/role/changeStatus', {
roleId: roleId,
status: status
})
},
// 获取菜单树列表
getMenuTree: (): Promise<ApiResponse<Menu[]>> => {
return api.get('/api/system/menu/tree')
},
// 测试角色列表接口参数格式
testRoleListParams: (): RoleSearchParams => {
return {
roleCode: "",
roleName: "",
status: 1, // 默认获取有效角色
pageNum: 1,
pageSize: 9999 // 获取全部有效角色
}
}
}