user.ts
4.44 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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'
}
})
// 请求拦截器 - 添加token
api.interceptors.request.use(
(config) => {
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
},
(error) => {
if (error.response?.status === 401) {
// token过期,跳转到登录页
localStorage.removeItem('token')
localStorage.removeItem('userInfo')
window.location.href = '/'
}
return Promise.reject(error)
}
)
// 用户数据类型
export interface User {
userId: number
username: string
realName: string
phone: string
email: string
status: number
statusText: string
remark: string
createBy: string
createTime: string
updateBy: string
updateTime: string
lastLoginTime: string | null
lastLoginIp: string | null
roles: Role[]
}
// 角色类型
export interface Role {
roleId: number
roleCode: string
roleName: string
status: number
statusText: string
remark: string
createBy: string
createTime: string
updateBy: string
updateTime: string
menus: any
}
// 搜索参数类型
export interface UserSearchParams {
username?: string
phone?: string
status?: number
startTime?: string
endTime?: string
page?: number
pageSize?: number
}
// 用户表单类型
export interface UserForm {
userId?: number
username: string
realName: string
phone: string
email: string
status: number
remark: string
roleIds: number[]
password?: string
}
// API响应类型
export interface ApiResponse<T> {
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 userApi = {
// 获取用户列表
getUserList: async (params: UserSearchParams = {}) => {
try {
const response = await api.get<ApiResponse<PageResponse<User>>>('/api/system/user/list', { params })
return response.data
} catch (error) {
console.error('获取用户列表失败:', error)
throw error
}
},
// 获取用户详情
getUserById: async (id: number) => {
try {
const response = await api.get<ApiResponse<User>>(`/api/system/user/${id}`)
return response.data
} catch (error) {
console.error('获取用户详情失败:', error)
throw error
}
},
// 创建用户
createUser: async (userData: UserForm) => {
try {
const response = await api.post<ApiResponse<User>>('/api/system/user/add', userData)
return response.data
} catch (error) {
console.error('创建用户失败:', error)
throw error
}
},
// 更新用户
updateUser: async (id: number, userData: UserForm) => {
try {
const response = await api.post<ApiResponse<User>>('/api/system/user/edit', userData)
return response.data
} catch (error) {
console.error('更新用户失败:', error)
throw error
}
},
// 删除用户(单个或批量)
deleteUser: async (id: number) => {
try {
const response = await api.delete<ApiResponse<void>>(`/api/system/user/${id}`)
return response.data
} catch (error) {
console.error('删除用户失败:', error)
throw error
}
},
// 批量删除用户
batchDeleteUsers: async (ids: number[]) => {
try {
const response = await api.delete<ApiResponse<void>>(`/api/system/user/${ids.join(',')}`)
return response.data
} catch (error) {
console.error('批量删除用户失败:', error)
throw error
}
},
// 更新用户状态
updateUserStatus: async (id: number, status: number) => {
try {
const response = await api.post<ApiResponse<void>>('/api/system/user/changeStatus', {
userId: id,
status: status
})
return response.data
} catch (error) {
console.error('更新用户状态失败:', error)
throw error
}
},
}
export default userApi