-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathApiComm.js
393 lines (390 loc) · 11.5 KB
/
ApiComm.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import {buildRouteParams, deepEqual, findMenuByPath} from '@/libs/tools'
const ApiComm = {
$confirm: undefined,
$notify: undefined,
$i18n: undefined,
$http: undefined,
$store: undefined,
$router: undefined,
$loading: undefined,
install: function (app, options) {
app.config.globalProperties.$api = this
this.$confirm = options.confirm
this.$notify = options.notify
this.$i18n = options.i18n
this.$http = options.http
this.$store = options.store
this.$router = options.router
this.$loading = options.loading
// 请求拦截器
this.$http.interceptors.request.use(config => {
this.$loading.start()
if (this.$store.state.app.user.token) {
config.headers.Authorization = `${this.$store.state.app.user.tokenType} ${this.$store.state.app.user.token}`
}
return config
}, error => {
this.$loading.done()
this.errorProcess(error)
return Promise.reject(error)
})
// 响应拦截器
this.$http.interceptors.response.use(data => {
this.$loading.done()
return data
}, error => {
this.$loading.done()
if (error.response) {
switch (error.response.status) {
case 400: // 业务错误
if (!error.config.headers.Process400 || error.config.headers.Process400 !== 'false') {
// 默认处理方式
this.errorProcess(error)
return
}
break
case 401: // token 失效
if (!error.config.headers.Process401 || error.config.headers.Process401 !== 'false') {
// 默认处理方式
this.redirectLogin()
return
}
break
case 403: // 权限不足
error.response.data.errorDescription = this.$i18n.global.t('messages.failed403')
if (!error.config.headers.Process403 || error.config.headers.Process403 !== 'false') {
// 默认处理方式
this.errorProcess(error)
return
}
break
case 404: // 找不到资源
error.response.data.errorDescription = this.$i18n.global.t('messages.failed404')
if (!error.config.headers.Process404) {
// 默认处理方式
this.redirectE404()
return
} else {
if (error.config.headers.Process404 !== 'false') {
// 默认处理方式
this.redirectE404()
return
} else {
if (error.config.headers.Process404Page) {
if (error.config.headers.Process404Page === 'true') {
this.redirectE404()
return
} else {
this.errorProcess(error)
return
}
}
}
}
break
case 500: // 系统内部异常
let errorMsg = 'Internal System Error'
if (error.response.data) {
if (typeof error.response.data === 'string') {
errorMsg = String(error.response.data)
} else {
if (error.response.data.errorDescription) {
errorMsg = error.response.data.errorDescription
} else {
errorMsg = error.response.data.message
}
}
}
this.redirectE500(errorMsg)
return
}
}
return Promise.reject(error)
})
},
errorProcess(error, title) {
let errorMessage = ''
if (error.response) {
if (typeof error.response.data === 'string') {
errorMessage = String(error.response.data)
} else {
if (error.response.data.errorDescription) {
errorMessage = error.response.data.errorDescription
} else {
errorMessage = error.response.data.message
}
}
} else {
errorMessage = error
}
this.$notify.error({
title: title || this.$i18n.global.t('messages.requestFailed'),
message: errorMessage
})
},
redirectHome() {
this.turnToPage(this.$store.state.app.appInfo.homePath,
undefined, undefined, false, true)
},
redirectLogin(asyncFunc, isHoldTagNavList = false) {
this.turnToPage({
name: 'login'
}, (callBackFunc) => {
const doLogOut = () => {
this.$store.commit('LOGIN_OUT')
if (!isHoldTagNavList) {
this.$store.commit('SET_TAG_NAV_LIST', [])
}
}
if (asyncFunc && typeof asyncFunc === 'function') {
asyncFunc(() => {
doLogOut()
callBackFunc(true)
})
} else {
doLogOut()
callBackFunc(true)
}
}, undefined, false, true)
},
redirectE404() {
this.turnToPage({
name: 'E404',
params: {
redirect: this.$router.currentRoute.value.fullPath
}
})
},
redirectE500(errorMsg) {
this.turnToPage({
name: 'E500',
params: {
msg: errorMsg,
redirect: this.$router.currentRoute.value.fullPath
}
})
},
gotoPersonalInformation() {
this.turnToPage({
name: 'personalInformation'
})
},
gotoLogFile() {
this.turnToPage({
name: 'logFile'
})
},
gotoRouteConfig() {
this.turnToPage({
name: 'routeConfig'
})
},
gotoRouteLog() {
this.turnToPage({
name: 'routeLog'
})
},
gotoDeploy() {
this.turnToPage({
name: 'deploy'
})
},
/**
* 页面跳转
* @param obj 跳转参数 stirng | route
* @param asyncFunc 跳转前执行的函数, asyncFunc(callBackFunc);
* 参数为一个函数,callBackFunc函数内容就是具体执行页面跳转;
* callBackFunc函数接收一个参数pass,当pass为true时执行页面跳转,当pass不为true时什么事都不做
* @param pageName 页面名称
* @param closeMore 是否关闭多个页面标签
* @param replace 是否是replace方式跳转
*/
turnToPage(obj, asyncFunc, pageName, closeMore = false, replace = false) {
let query = {}
let pathOrName = ''
if (typeof obj === 'string') { // string
pathOrName = obj
} else { // route
if (obj.path) {
pathOrName = obj.path
} else if (obj.name) {
pathOrName = obj.name
} else {
return
}
query = obj.query || {}
}
let targetMenu, currMenu
currMenu = findMenuByPath(this.$router.currentRoute.value.fullPath, this.$store.state.app.user.menuList)
if (pathOrName.startsWith('/') || pathOrName.toLowerCase().startsWith('http')) {
// 直接路由跳转 或 外部链接跳转
if (!closeMore) {
if (this.$router.currentRoute.value.fullPath === pathOrName) {
return
}
}
targetMenu = findMenuByPath(pathOrName, this.$store.state.app.user.menuList)
} else {
// 路由名称跳转
if (!closeMore) {
if (this.$router.currentRoute.value.name === pathOrName) {
let currentQuery = this.$router.currentRoute.value.query || {}
if (deepEqual(currentQuery, query)) {
return
}
}
}
}
let dataLose = false
if (!closeMore) {
if (this.$router.currentRoute.value.meta) {
dataLose = this.$router.currentRoute.value.meta.withInput && this.$router.currentRoute.value.meta.notCache
}
} else {
dataLose = true
}
const process = (lazyTime) => {
if (asyncFunc && typeof asyncFunc === 'function') {
const doAsyncFunc = () => {
asyncFunc((pass) => {
if (pass) {
if (dataLose) {
if (!closeMore) {
this.removeThisTag()
}
}
ApiComm.routeSwitch(obj, targetMenu, replace)
}
})
}
if (lazyTime > 0) {
setTimeout(() => {
doAsyncFunc()
}, lazyTime)
} else {
doAsyncFunc()
}
} else {
if (dataLose) {
if (!closeMore) {
this.removeThisTag()
}
}
ApiComm.routeSwitch(obj, targetMenu, replace)
}
}
if ((!targetMenu || targetMenu.openType !== 1) && dataLose) {
if (pathOrName === '/404' || pathOrName === '/500' || pathOrName === 'E404' || pathOrName === 'E500') {
process(0)
} else {
this.$confirm(this.getPageTitle(pageName, currMenu) + ' ' +
this.$i18n.global.t('messages.leavePage'), this.$i18n.global.t('dialog.confirm'), {
type: 'warning'
}).then(() => {
process(350)
}).catch(() => {
})
}
} else {
process(0)
}
},
/**
* 执行跳转
* @param obj 跳转参数 stirng | route
* @param menu 目标菜单对象
* @param replace 是否是replace方式跳转
*/
routeSwitch(obj, menu, replace) {
let {pathOrName, params, query} = {pathOrName: '', query: {}, params: {}}
if (typeof obj === 'string') { // string
pathOrName = obj
} else { // route
if (obj.path) {
pathOrName = obj.path
} else if (obj.name) {
pathOrName = obj.name
} else {
return
}
query = obj.query || {}
params = obj.params || {}
}
let option = {}
if (pathOrName.startsWith('/') || pathOrName.toLowerCase().startsWith('http')) {
// path
let path = pathOrName
if (menu) {
if (menu.openType === 1) {
// 打开新页面
window.open(path)
return
}
}
let routeQuery = query
if (pathOrName.indexOf('?') > 0) {
pathOrName.substring(pathOrName.indexOf('?') + 1).split('&').forEach((keyValue) => {
if (keyValue.indexOf('=') > 0) {
const map = keyValue.split('=')
routeQuery[map[0]] = map[1]
}
})
}
option = {
path: path,
query: routeQuery
}
if (this.$router.currentRoute.value.fullPath === path) {
return
}
} else {
// name
option = {
name: pathOrName,
params: buildRouteParams(params),
query: query
}
if (this.$router.currentRoute.value.name === pathOrName) {
let currentQuery = this.$router.currentRoute.value.query || {}
if (deepEqual(currentQuery, query)) {
return
}
}
}
if (replace) {
this.$router.replace(option)
} else {
this.$router.push(option)
}
},
getPageTitle(pageName, pageMenu) {
let pageTitle = pageName
if (!pageTitle) {
if (pageMenu) {
pageTitle = pageMenu.name
} else {
if (this.$router.currentRoute.value.meta.title) {
pageTitle = this.$i18n.global.t(this.$router.currentRoute.value.meta.title)
}
}
}
return pageTitle
},
removeThisTag() {
const res = this.$store.state.app.tagNavList.filter(item => item.path !== this.$router.currentRoute.value.fullPath)
this.$store.commit('SET_TAG_NAV_LIST', res)
},
closeThisTagImmediately() {
const currIndex = this.$store.state.app.tagNavList.findIndex(item => item.path === this.$router.currentRoute.value.fullPath)
let nextPath = this.$store.state.app.appInfo.homePath
if (currIndex === this.$store.state.app.tagNavList.length - 1) {
nextPath = this.$store.state.app.tagNavList[this.$store.state.app.tagNavList.length - 2].path
} else {
nextPath = this.$store.state.app.tagNavList[currIndex + 1].path
}
this.removeThisTag()
this.routeSwitch(nextPath)
},
request: {}
}
export default ApiComm