haiwei-ui 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/packages/utils/http copy.js +355 -0
- package/packages/utils/http.js +70 -20
package/package.json
CHANGED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import qs from 'qs'
|
|
3
|
+
import dayjs from 'dayjs'
|
|
4
|
+
import token from './token'
|
|
5
|
+
import { Message, MessageBox } from 'element-ui'
|
|
6
|
+
import { store } from '../store'
|
|
7
|
+
import { router } from '../router'
|
|
8
|
+
|
|
9
|
+
axios.defaults.headers.post['Content-Type'] = 'application/json'
|
|
10
|
+
axios.defaults.headers.put['Content-Type'] = 'application/json'
|
|
11
|
+
|
|
12
|
+
function Http() {
|
|
13
|
+
this.axios = axios
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 序列化参数为Url形式
|
|
17
|
+
Http.prototype.stringify = params => {
|
|
18
|
+
return qs.stringify(params, {
|
|
19
|
+
allowDots: true
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
Http.prototype.post = (url, params, config) => {
|
|
24
|
+
return axios.post(url, params, config)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Http.prototype.get = (url, params, config) => {
|
|
28
|
+
const config_ = Object.assign({}, config, {
|
|
29
|
+
// 参数
|
|
30
|
+
params,
|
|
31
|
+
// 修改参数序列化方法
|
|
32
|
+
paramsSerializer: p => {
|
|
33
|
+
// 使用逗号分隔参数
|
|
34
|
+
return qs.stringify(p, {
|
|
35
|
+
allowDots: true
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
return axios.get(url, config_)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Http.prototype.delete = (url, params, config) => {
|
|
43
|
+
const config_ = Object.assign({}, config, {
|
|
44
|
+
// 参数
|
|
45
|
+
params,
|
|
46
|
+
// 修改参数序列化方法
|
|
47
|
+
paramsSerializer: p => {
|
|
48
|
+
// 使用逗号分隔参数
|
|
49
|
+
return qs.stringify(p, {
|
|
50
|
+
allowDots: true
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
return axios.delete(url, config_)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Http.prototype.put = (url, params, config) => {
|
|
58
|
+
return axios.put(url, params, config)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
Http.prototype.download = (url, params, config) => {
|
|
62
|
+
const config_ = Object.assign({ responseType: 'blob' }, config, {
|
|
63
|
+
// 参数
|
|
64
|
+
params,
|
|
65
|
+
// 修改参数序列化方法
|
|
66
|
+
paramsSerializer: p => {
|
|
67
|
+
// 使用逗号分隔参数
|
|
68
|
+
return qs.stringify(p, {
|
|
69
|
+
allowDots: true
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
return axios.get(url, config_)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
Http.prototype.preview = (url, params, config) => {
|
|
77
|
+
const config_ = Object.assign({ responseType: 'blob', headers: { preview: true } }, config, {
|
|
78
|
+
// 参数
|
|
79
|
+
params,
|
|
80
|
+
// 修改参数序列化方法
|
|
81
|
+
paramsSerializer: p => {
|
|
82
|
+
// 使用逗号分隔参数
|
|
83
|
+
return qs.stringify(p, {
|
|
84
|
+
allowDots: true
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
return axios.get(url, config_)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
Http.prototype.export = (url, params, config) => {
|
|
92
|
+
return axios.post(url, params, Object.assign({ responseType: 'blob' }, config))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 通用CRUD接口地址
|
|
96
|
+
Http.prototype.crud = root => {
|
|
97
|
+
if (!root.endsWith('/')) {
|
|
98
|
+
root += '/'
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
query(params) {
|
|
102
|
+
return $http.get(`${root}query`, params)
|
|
103
|
+
},
|
|
104
|
+
add(params) {
|
|
105
|
+
return $http.post(`${root}add`, params)
|
|
106
|
+
},
|
|
107
|
+
remove(id) {
|
|
108
|
+
return $http.delete(`${root}delete`, { id })
|
|
109
|
+
},
|
|
110
|
+
edit(id) {
|
|
111
|
+
return $http.get(`${root}edit`, { id })
|
|
112
|
+
},
|
|
113
|
+
update(params) {
|
|
114
|
+
return $http.post(`${root}update`, params)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 设为全局属性$http
|
|
120
|
+
if (!window.$http) window.$http = new Http()
|
|
121
|
+
|
|
122
|
+
// 消息提醒显示时长(ms)
|
|
123
|
+
const messageDuration = 1500
|
|
124
|
+
//是否显示了账户在其他地方登录的提示框
|
|
125
|
+
let showLoginOnOtherPlaces = false
|
|
126
|
+
|
|
127
|
+
//处理文件下载请求
|
|
128
|
+
const handleDownload = response => {
|
|
129
|
+
//如果返回的是application/json,则表示返回的是json,没有要下载的问题,可能是逻辑处理失败
|
|
130
|
+
if (response.data.type === 'application/json') {
|
|
131
|
+
var reader = new FileReader()
|
|
132
|
+
reader.onload = e => {
|
|
133
|
+
var data = JSON.parse(e.target.result)
|
|
134
|
+
if (data.code === 1) {
|
|
135
|
+
return data.data
|
|
136
|
+
} else {
|
|
137
|
+
Message.error({
|
|
138
|
+
message: data.msg,
|
|
139
|
+
showClose: true,
|
|
140
|
+
duration: messageDuration
|
|
141
|
+
})
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
reader.readAsText(response.data)
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const url = window.URL.createObjectURL(response.data)
|
|
150
|
+
|
|
151
|
+
// 如果是预览直接返回
|
|
152
|
+
if (response.config.headers['preview']) return url
|
|
153
|
+
|
|
154
|
+
let fileName = ''
|
|
155
|
+
// 如果响应头包含'content-disposition'属性,则从该属性中获取文件名称
|
|
156
|
+
let cd = response.headers['content-disposition']
|
|
157
|
+
if (cd) {
|
|
158
|
+
fileName = decodeURI(cd.split("''")[1])
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//如果文件名不存在,则使用时间戳
|
|
162
|
+
if (!fileName) {
|
|
163
|
+
fileName = dayjs().format('YYYYMMDDHHMMSSS')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
//通过模拟a标签点击事件下载文件
|
|
167
|
+
const link = document.createElement('a')
|
|
168
|
+
link.href = url
|
|
169
|
+
link.setAttribute('download', fileName)
|
|
170
|
+
document.body.appendChild(link)
|
|
171
|
+
link.click()
|
|
172
|
+
document.body.removeChild(link)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
//刷新令牌
|
|
176
|
+
const refreshToken = () => {
|
|
177
|
+
let t = token.get()
|
|
178
|
+
if (t && t.refreshToken) {
|
|
179
|
+
return store.state.app.system.actions.refreshToken(t.refreshToken)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
Promise.reject('refresh token error')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 初始化
|
|
186
|
+
export default config => {
|
|
187
|
+
// 接口根路径
|
|
188
|
+
axios.defaults.baseURL = config.baseUrl
|
|
189
|
+
|
|
190
|
+
// 拦截请求
|
|
191
|
+
axios.interceptors.request.use(
|
|
192
|
+
config => {
|
|
193
|
+
let t = token.get()
|
|
194
|
+
if (t && t.accessToken) {
|
|
195
|
+
config.headers.Authorization = 'Bearer ' + t.accessToken
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 开发环境下打印请求日志
|
|
199
|
+
if (process.env.NODE_ENV === 'development') {
|
|
200
|
+
console.group(`[API请求] ${config.method.toUpperCase()} ${config.url}`)
|
|
201
|
+
console.log('请求配置:', {
|
|
202
|
+
方法: config.method,
|
|
203
|
+
URL: config.url,
|
|
204
|
+
参数: config.params,
|
|
205
|
+
数据: config.data,
|
|
206
|
+
头信息: config.headers
|
|
207
|
+
})
|
|
208
|
+
console.groupEnd()
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return config
|
|
212
|
+
},
|
|
213
|
+
function(error) {
|
|
214
|
+
// 开发环境下打印请求错误日志
|
|
215
|
+
if (process.env.NODE_ENV === 'development') {
|
|
216
|
+
console.error('[请求拦截器错误]:', error)
|
|
217
|
+
}
|
|
218
|
+
return Promise.reject(error)
|
|
219
|
+
}
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
// 响应前拦截器
|
|
223
|
+
axios.interceptors.response.use(
|
|
224
|
+
response => {
|
|
225
|
+
const { config } = response
|
|
226
|
+
|
|
227
|
+
// 开发环境下打印响应成功日志
|
|
228
|
+
if (process.env.NODE_ENV === 'development') {
|
|
229
|
+
console.group(`[API响应] ${config.method.toUpperCase()} ${config.url}`)
|
|
230
|
+
console.log('响应信息:', {
|
|
231
|
+
状态码: response.status,
|
|
232
|
+
状态文本: response.statusText,
|
|
233
|
+
数据: response.data,
|
|
234
|
+
头信息: response.headers
|
|
235
|
+
})
|
|
236
|
+
console.groupEnd()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// 文件下载/预览
|
|
240
|
+
if (config.responseType && config.responseType === 'blob') {
|
|
241
|
+
return handleDownload(response)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (response.data.code === 1) {
|
|
245
|
+
return response.data.data
|
|
246
|
+
} else if (response.data.code === 0 && !config.noErrorMsg) {
|
|
247
|
+
Message.error({
|
|
248
|
+
message: response.data.msg,
|
|
249
|
+
showClose: true,
|
|
250
|
+
duration: messageDuration
|
|
251
|
+
})
|
|
252
|
+
return Promise.reject(response.data.msg)
|
|
253
|
+
} else {
|
|
254
|
+
return response.data
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
error => {
|
|
258
|
+
// 开发环境下打印响应错误日志
|
|
259
|
+
if (process.env.NODE_ENV === 'development') {
|
|
260
|
+
const method = error.config && error.config.method ? error.config.method.toUpperCase() : 'UNKNOWN'
|
|
261
|
+
const url = error.config && error.config.url ? error.config.url : 'unknown'
|
|
262
|
+
const status = error.response && error.response.status
|
|
263
|
+
const responseData = error.response && error.response.data
|
|
264
|
+
|
|
265
|
+
console.group(`[API错误] ${method} ${url}`)
|
|
266
|
+
console.log('错误信息:', {
|
|
267
|
+
状态码: status,
|
|
268
|
+
错误消息: error.message,
|
|
269
|
+
响应数据: responseData,
|
|
270
|
+
请求配置: error.config
|
|
271
|
+
})
|
|
272
|
+
console.groupEnd()
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
let currentRoute = router.currentRoute
|
|
276
|
+
let redirect = currentRoute.name !== 'login' ? currentRoute.fullPath : '/' // 跳转页面
|
|
277
|
+
if (error && error.response) {
|
|
278
|
+
switch (error.response.status) {
|
|
279
|
+
case 401:
|
|
280
|
+
return refreshToken()
|
|
281
|
+
.then(data => {
|
|
282
|
+
//重新初始化令牌
|
|
283
|
+
store.commit('app/token/init', data)
|
|
284
|
+
//重新发一起一次上次的的请求
|
|
285
|
+
error.config.headers.Authorization = 'Bearer ' + data.accessToken
|
|
286
|
+
return axios.request(error.config)
|
|
287
|
+
})
|
|
288
|
+
.catch(() => {
|
|
289
|
+
// 如果刷新失败,需要删除token并跳转到登录页面
|
|
290
|
+
token.remove()
|
|
291
|
+
router.push({
|
|
292
|
+
name: 'login',
|
|
293
|
+
query: {
|
|
294
|
+
redirect
|
|
295
|
+
}
|
|
296
|
+
})
|
|
297
|
+
})
|
|
298
|
+
case 403:
|
|
299
|
+
store.dispatch(
|
|
300
|
+
'app/page/close',
|
|
301
|
+
{
|
|
302
|
+
fullPath: currentRoute.path,
|
|
303
|
+
router: router,
|
|
304
|
+
to: {
|
|
305
|
+
name: 'error403'
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
{ root: true }
|
|
309
|
+
)
|
|
310
|
+
break
|
|
311
|
+
case 622:
|
|
312
|
+
//单账户登录功能
|
|
313
|
+
if (!showLoginOnOtherPlaces) {
|
|
314
|
+
showLoginOnOtherPlaces = true
|
|
315
|
+
MessageBox.confirm('账户已在别处登录, 请重新登录~', '提示', {
|
|
316
|
+
confirmButtonText: '确定',
|
|
317
|
+
type: 'warning',
|
|
318
|
+
showCancelButton: false,
|
|
319
|
+
callback() {
|
|
320
|
+
// 删除token
|
|
321
|
+
token.remove()
|
|
322
|
+
router.push({
|
|
323
|
+
name: 'login',
|
|
324
|
+
query: {
|
|
325
|
+
redirect
|
|
326
|
+
}
|
|
327
|
+
})
|
|
328
|
+
showLoginOnOtherPlaces = false
|
|
329
|
+
}
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
break
|
|
333
|
+
default:
|
|
334
|
+
console.error(error.response.data.msg)
|
|
335
|
+
Message.error({
|
|
336
|
+
message: '系统异常,请联系管理员~',
|
|
337
|
+
duration: messageDuration
|
|
338
|
+
})
|
|
339
|
+
break
|
|
340
|
+
}
|
|
341
|
+
} else {
|
|
342
|
+
if (currentRoute.name === 'login') {
|
|
343
|
+
Message.error({
|
|
344
|
+
message: '无法连接网络~',
|
|
345
|
+
duration: messageDuration
|
|
346
|
+
})
|
|
347
|
+
} else {
|
|
348
|
+
token.remove()
|
|
349
|
+
router.push({ name: 'login', query: { redirect } })
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return Promise.reject(error)
|
|
353
|
+
}
|
|
354
|
+
)
|
|
355
|
+
}
|
package/packages/utils/http.js
CHANGED
|
@@ -58,6 +58,52 @@ Http.prototype.put = (url, params, config) => {
|
|
|
58
58
|
return axios.put(url, params, config)
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 通用文件上传接口
|
|
63
|
+
* @param {string} url - 上传接口URL
|
|
64
|
+
* @param {File|Blob} file - 要上传的文件对象
|
|
65
|
+
* @param {string} [fieldName='file'] - 文件字段名称,默认为'file'
|
|
66
|
+
* @param {Object} [formData={}] - 额外的表单数据
|
|
67
|
+
* @param {Object} [config={}] - 额外的axios配置
|
|
68
|
+
* @returns {Promise} 返回Promise对象
|
|
69
|
+
* @example
|
|
70
|
+
* // 基本使用
|
|
71
|
+
* $http.upload('/api/upload', file)
|
|
72
|
+
*
|
|
73
|
+
* // 自定义字段名和额外参数
|
|
74
|
+
* $http.upload('/api/upload', file, 'avatar', { userId: 123 })
|
|
75
|
+
*
|
|
76
|
+
* // 完整配置
|
|
77
|
+
* $http.upload('/api/upload', file, 'document', { category: 'pdf' }, {
|
|
78
|
+
* onUploadProgress: progressEvent => {
|
|
79
|
+
* const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
|
|
80
|
+
* console.log(`上传进度: ${percentCompleted}%`)
|
|
81
|
+
* }
|
|
82
|
+
* })
|
|
83
|
+
*/
|
|
84
|
+
Http.prototype.upload = (url, file, fieldName = 'file', formData = {}, config = {}) => {
|
|
85
|
+
// 创建FormData对象
|
|
86
|
+
const data = new FormData()
|
|
87
|
+
|
|
88
|
+
// 添加文件
|
|
89
|
+
data.append(fieldName, file)
|
|
90
|
+
|
|
91
|
+
// 添加额外的表单数据
|
|
92
|
+
Object.keys(formData).forEach(key => {
|
|
93
|
+
data.append(key, formData[key])
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// 合并配置,设置Content-Type为multipart/form-data
|
|
97
|
+
const config_ = Object.assign({}, config, {
|
|
98
|
+
headers: {
|
|
99
|
+
'Content-Type': 'multipart/form-data',
|
|
100
|
+
...config.headers
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
return axios.post(url, data, config_)
|
|
105
|
+
}
|
|
106
|
+
|
|
61
107
|
Http.prototype.download = (url, params, config) => {
|
|
62
108
|
const config_ = Object.assign({ responseType: 'blob' }, config, {
|
|
63
109
|
// 参数
|
|
@@ -74,17 +120,21 @@ Http.prototype.download = (url, params, config) => {
|
|
|
74
120
|
}
|
|
75
121
|
|
|
76
122
|
Http.prototype.preview = (url, params, config) => {
|
|
77
|
-
const config_ = Object.assign(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
123
|
+
const config_ = Object.assign(
|
|
124
|
+
{ responseType: 'blob', headers: { preview: true } },
|
|
125
|
+
config,
|
|
126
|
+
{
|
|
127
|
+
// 参数
|
|
128
|
+
params,
|
|
129
|
+
// 修改参数序列化方法
|
|
130
|
+
paramsSerializer: p => {
|
|
131
|
+
// 使用逗号分隔参数
|
|
132
|
+
return qs.stringify(p, {
|
|
133
|
+
allowDots: true
|
|
134
|
+
})
|
|
135
|
+
}
|
|
86
136
|
}
|
|
87
|
-
|
|
137
|
+
)
|
|
88
138
|
return axios.get(url, config_)
|
|
89
139
|
}
|
|
90
140
|
|
|
@@ -121,12 +171,12 @@ if (!window.$http) window.$http = new Http()
|
|
|
121
171
|
|
|
122
172
|
// 消息提醒显示时长(ms)
|
|
123
173
|
const messageDuration = 1500
|
|
124
|
-
|
|
174
|
+
// 是否显示了账户在其他地方登录的提示框
|
|
125
175
|
let showLoginOnOtherPlaces = false
|
|
126
176
|
|
|
127
|
-
|
|
177
|
+
// 处理文件下载请求
|
|
128
178
|
const handleDownload = response => {
|
|
129
|
-
|
|
179
|
+
// 如果返回的是application/json,则表示返回的是json,没有要下载的问题,可能是逻辑处理失败
|
|
130
180
|
if (response.data.type === 'application/json') {
|
|
131
181
|
var reader = new FileReader()
|
|
132
182
|
reader.onload = e => {
|
|
@@ -158,12 +208,12 @@ const handleDownload = response => {
|
|
|
158
208
|
fileName = decodeURI(cd.split("''")[1])
|
|
159
209
|
}
|
|
160
210
|
|
|
161
|
-
|
|
211
|
+
// 如果文件名不存在,则使用时间戳
|
|
162
212
|
if (!fileName) {
|
|
163
213
|
fileName = dayjs().format('YYYYMMDDHHMMSSS')
|
|
164
214
|
}
|
|
165
215
|
|
|
166
|
-
|
|
216
|
+
// 通过模拟a标签点击事件下载文件
|
|
167
217
|
const link = document.createElement('a')
|
|
168
218
|
link.href = url
|
|
169
219
|
link.setAttribute('download', fileName)
|
|
@@ -172,7 +222,7 @@ const handleDownload = response => {
|
|
|
172
222
|
document.body.removeChild(link)
|
|
173
223
|
}
|
|
174
224
|
|
|
175
|
-
|
|
225
|
+
// 刷新令牌
|
|
176
226
|
const refreshToken = () => {
|
|
177
227
|
let t = token.get()
|
|
178
228
|
if (t && t.refreshToken) {
|
|
@@ -279,9 +329,9 @@ export default config => {
|
|
|
279
329
|
case 401:
|
|
280
330
|
return refreshToken()
|
|
281
331
|
.then(data => {
|
|
282
|
-
|
|
332
|
+
// 重新初始化令牌
|
|
283
333
|
store.commit('app/token/init', data)
|
|
284
|
-
|
|
334
|
+
// 重新发一起一次上次的的请求
|
|
285
335
|
error.config.headers.Authorization = 'Bearer ' + data.accessToken
|
|
286
336
|
return axios.request(error.config)
|
|
287
337
|
})
|
|
@@ -309,7 +359,7 @@ export default config => {
|
|
|
309
359
|
)
|
|
310
360
|
break
|
|
311
361
|
case 622:
|
|
312
|
-
|
|
362
|
+
// 单账户登录功能
|
|
313
363
|
if (!showLoginOnOtherPlaces) {
|
|
314
364
|
showLoginOnOtherPlaces = true
|
|
315
365
|
MessageBox.confirm('账户已在别处登录, 请重新登录~', '提示', {
|
|
@@ -352,4 +402,4 @@ export default config => {
|
|
|
352
402
|
return Promise.reject(error)
|
|
353
403
|
}
|
|
354
404
|
)
|
|
355
|
-
}
|
|
405
|
+
}
|