af-mobile-client-vue3 1.6.13 → 1.6.15
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/src/stores/modules/user.ts +2 -16
- package/src/utils/Storage.ts +124 -124
- package/src/utils/http/index.ts +228 -225
package/package.json
CHANGED
|
@@ -154,20 +154,6 @@ export const useUserStore = defineStore('app-user', () => {
|
|
|
154
154
|
setToken(token)
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
|
-
const setSessionKey = (key: string | undefined, expiresInMinutes: number | null = null) => {
|
|
158
|
-
if (expiresInMinutes === 0) {
|
|
159
|
-
Storage.set('v4-session-key', key, 0)
|
|
160
|
-
}
|
|
161
|
-
else if (expiresInMinutes && expiresInMinutes > 0) {
|
|
162
|
-
const expiresInSeconds = expiresInMinutes * 60
|
|
163
|
-
const BUFFER_SECONDS = 30
|
|
164
|
-
const safeExpire = Math.max(expiresInSeconds - BUFFER_SECONDS, 0)
|
|
165
|
-
Storage.set('v4-session-key', key, safeExpire)
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
localStorage.setItem('v4-session-key', key)
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
157
|
const setUserInfo = (info: any | null) => {
|
|
172
158
|
userState.value.userInfo = info
|
|
173
159
|
userState.value.lastUpdateTime = new Date().getTime()
|
|
@@ -323,7 +309,7 @@ export const useUserStore = defineStore('app-user', () => {
|
|
|
323
309
|
Storage.set('LoginTicket', LoginTicket)
|
|
324
310
|
if (data.session && useSettingStore().getSetting().requestEncrypt) {
|
|
325
311
|
const k = encryptUtil.RSADecrypt(data.session as string)
|
|
326
|
-
|
|
312
|
+
localStorage.setItem('v4-session-key', k)
|
|
327
313
|
secureStorageWrite('v4-session-key', k)
|
|
328
314
|
}
|
|
329
315
|
return Promise.resolve(data)
|
|
@@ -341,7 +327,7 @@ export const useUserStore = defineStore('app-user', () => {
|
|
|
341
327
|
const LoginTicket = crypto.AESEncrypt(JSON.stringify(data), '3KMKqvgwR8ULbR8Z')
|
|
342
328
|
if (data.session && useSettingStore().getSetting().requestEncrypt) {
|
|
343
329
|
const k = encryptUtil.RSADecrypt(data.session as string)
|
|
344
|
-
|
|
330
|
+
localStorage.setItem('v4-session-key', k)
|
|
345
331
|
secureStorageWrite('v4-session-key', k)
|
|
346
332
|
}
|
|
347
333
|
Storage.set('LoginTicket', LoginTicket)
|
package/src/utils/Storage.ts
CHANGED
|
@@ -1,124 +1,124 @@
|
|
|
1
|
-
// 默认缓存期限为7天
|
|
2
|
-
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 创建本地缓存对象
|
|
6
|
-
*/
|
|
7
|
-
export function createStorage({ prefixKey = '', storage = localStorage } = {}) {
|
|
8
|
-
/**
|
|
9
|
-
* 本地缓存类
|
|
10
|
-
* @class Storage
|
|
11
|
-
*/
|
|
12
|
-
const Storage = class {
|
|
13
|
-
private storage = storage
|
|
14
|
-
private prefixKey?: string = prefixKey
|
|
15
|
-
|
|
16
|
-
private getKey(key: string) {
|
|
17
|
-
return `${this.prefixKey}${key}`.toUpperCase()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @description 设置缓存
|
|
22
|
-
* @param {string} key 缓存键
|
|
23
|
-
* @param {*} value 缓存值
|
|
24
|
-
* @param expire
|
|
25
|
-
*/
|
|
26
|
-
set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
|
27
|
-
const stringData = JSON.stringify({
|
|
28
|
-
value,
|
|
29
|
-
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
|
|
30
|
-
})
|
|
31
|
-
this.storage.setItem(this.getKey(key), stringData)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 读取缓存
|
|
36
|
-
* @param {string} key 缓存键
|
|
37
|
-
* @param {*=} def 默认值
|
|
38
|
-
*/
|
|
39
|
-
get(key: string, def: any = null) {
|
|
40
|
-
const item = this.storage.getItem(this.getKey(key))
|
|
41
|
-
if (item) {
|
|
42
|
-
try {
|
|
43
|
-
const data = JSON.parse(item)
|
|
44
|
-
const { value, expire } = data
|
|
45
|
-
// 在有效期内直接返回
|
|
46
|
-
if (expire === null || expire >= Date.now())
|
|
47
|
-
return value
|
|
48
|
-
|
|
49
|
-
this.remove(key)
|
|
50
|
-
}
|
|
51
|
-
catch {
|
|
52
|
-
return def
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return def
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 从缓存删除某项
|
|
60
|
-
* @param {string} key
|
|
61
|
-
*/
|
|
62
|
-
remove(key: string) {
|
|
63
|
-
this.storage.removeItem(this.getKey(key))
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 清空所有缓存
|
|
68
|
-
* @memberOf Cache
|
|
69
|
-
*/
|
|
70
|
-
clear(): void {
|
|
71
|
-
this.storage.clear()
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 设置cookie
|
|
76
|
-
* @param {string} name cookie 名称
|
|
77
|
-
* @param {*} value cookie 值
|
|
78
|
-
* @param {number=} expire 过期时间
|
|
79
|
-
* 如果过期时间为设置,默认关闭浏览器自动删除
|
|
80
|
-
* @example
|
|
81
|
-
*/
|
|
82
|
-
setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
|
83
|
-
document.cookie = `${this.getKey(name)}=${value}; Max-Age=${expire}`
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 根据名字获取cookie值
|
|
88
|
-
* @param name
|
|
89
|
-
*/
|
|
90
|
-
getCookie(name: string): string {
|
|
91
|
-
const cookieArr = document.cookie.split('; ')
|
|
92
|
-
for (let i = 0, length = cookieArr.length; i < length; i++) {
|
|
93
|
-
const kv = cookieArr[i].split('=')
|
|
94
|
-
if (kv[0] === this.getKey(name))
|
|
95
|
-
return kv[1]
|
|
96
|
-
}
|
|
97
|
-
return ''
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 根据名字删除指定的cookie
|
|
102
|
-
* @param {string} key
|
|
103
|
-
*/
|
|
104
|
-
removeCookie(key: string) {
|
|
105
|
-
this.setCookie(key, 1, -1)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 清空cookie,使所有cookie失效
|
|
110
|
-
*/
|
|
111
|
-
clearCookie(): void {
|
|
112
|
-
const keys = document.cookie.match(/[^ =;]+(?==)/g)
|
|
113
|
-
if (keys) {
|
|
114
|
-
for (let i = keys.length; i--;)
|
|
115
|
-
document.cookie = `${keys[i]}=0;expire=${new Date(0).toUTCString()}`
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return new Storage()
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export const storage = createStorage()
|
|
123
|
-
|
|
124
|
-
export default Storage
|
|
1
|
+
// 默认缓存期限为7天
|
|
2
|
+
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 创建本地缓存对象
|
|
6
|
+
*/
|
|
7
|
+
export function createStorage({ prefixKey = '', storage = localStorage } = {}) {
|
|
8
|
+
/**
|
|
9
|
+
* 本地缓存类
|
|
10
|
+
* @class Storage
|
|
11
|
+
*/
|
|
12
|
+
const Storage = class {
|
|
13
|
+
private storage = storage
|
|
14
|
+
private prefixKey?: string = prefixKey
|
|
15
|
+
|
|
16
|
+
private getKey(key: string) {
|
|
17
|
+
return `${this.prefixKey}${key}`.toUpperCase()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @description 设置缓存
|
|
22
|
+
* @param {string} key 缓存键
|
|
23
|
+
* @param {*} value 缓存值
|
|
24
|
+
* @param expire
|
|
25
|
+
*/
|
|
26
|
+
set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
|
27
|
+
const stringData = JSON.stringify({
|
|
28
|
+
value,
|
|
29
|
+
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
|
|
30
|
+
})
|
|
31
|
+
this.storage.setItem(this.getKey(key), stringData)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 读取缓存
|
|
36
|
+
* @param {string} key 缓存键
|
|
37
|
+
* @param {*=} def 默认值
|
|
38
|
+
*/
|
|
39
|
+
get(key: string, def: any = null) {
|
|
40
|
+
const item = this.storage.getItem(this.getKey(key))
|
|
41
|
+
if (item) {
|
|
42
|
+
try {
|
|
43
|
+
const data = JSON.parse(item)
|
|
44
|
+
const { value, expire } = data
|
|
45
|
+
// 在有效期内直接返回
|
|
46
|
+
if (expire === null || expire >= Date.now())
|
|
47
|
+
return value
|
|
48
|
+
|
|
49
|
+
this.remove(key)
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return def
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return def
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 从缓存删除某项
|
|
60
|
+
* @param {string} key
|
|
61
|
+
*/
|
|
62
|
+
remove(key: string) {
|
|
63
|
+
this.storage.removeItem(this.getKey(key))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 清空所有缓存
|
|
68
|
+
* @memberOf Cache
|
|
69
|
+
*/
|
|
70
|
+
clear(): void {
|
|
71
|
+
this.storage.clear()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 设置cookie
|
|
76
|
+
* @param {string} name cookie 名称
|
|
77
|
+
* @param {*} value cookie 值
|
|
78
|
+
* @param {number=} expire 过期时间
|
|
79
|
+
* 如果过期时间为设置,默认关闭浏览器自动删除
|
|
80
|
+
* @example
|
|
81
|
+
*/
|
|
82
|
+
setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
|
83
|
+
document.cookie = `${this.getKey(name)}=${value}; Max-Age=${expire}`
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 根据名字获取cookie值
|
|
88
|
+
* @param name
|
|
89
|
+
*/
|
|
90
|
+
getCookie(name: string): string {
|
|
91
|
+
const cookieArr = document.cookie.split('; ')
|
|
92
|
+
for (let i = 0, length = cookieArr.length; i < length; i++) {
|
|
93
|
+
const kv = cookieArr[i].split('=')
|
|
94
|
+
if (kv[0] === this.getKey(name))
|
|
95
|
+
return kv[1]
|
|
96
|
+
}
|
|
97
|
+
return ''
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 根据名字删除指定的cookie
|
|
102
|
+
* @param {string} key
|
|
103
|
+
*/
|
|
104
|
+
removeCookie(key: string) {
|
|
105
|
+
this.setCookie(key, 1, -1)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 清空cookie,使所有cookie失效
|
|
110
|
+
*/
|
|
111
|
+
clearCookie(): void {
|
|
112
|
+
const keys = document.cookie.match(/[^ =;]+(?==)/g)
|
|
113
|
+
if (keys) {
|
|
114
|
+
for (let i = keys.length; i--;)
|
|
115
|
+
document.cookie = `${keys[i]}=0;expire=${new Date(0).toUTCString()}`
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return new Storage()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const storage = createStorage()
|
|
123
|
+
|
|
124
|
+
export default Storage
|
package/src/utils/http/index.ts
CHANGED
|
@@ -1,225 +1,228 @@
|
|
|
1
|
-
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
2
|
-
import { ContentTypeEnum, ResultEnum } from '@af-mobile-client-vue3/enums/requestEnum'
|
|
3
|
-
import { useUserStore } from '@af-mobile-client-vue3/stores/modules/user'
|
|
4
|
-
import { ACCESS_TOKEN } from '@af-mobile-client-vue3/stores/mutation-type'
|
|
5
|
-
import { encryptUtil } from '@af-mobile-client-vue3/utils/EncryptUtil'
|
|
6
|
-
import axios from 'axios'
|
|
7
|
-
import { showToast } from 'vant'
|
|
8
|
-
|
|
9
|
-
// 默认 axios 实例请求配置
|
|
10
|
-
const configDefault = {
|
|
11
|
-
headers: {
|
|
12
|
-
'Content-Type': ContentTypeEnum.JSON,
|
|
13
|
-
},
|
|
14
|
-
// 请求超时时间
|
|
15
|
-
timeout: 20000,
|
|
16
|
-
// API 请求的默认前缀
|
|
17
|
-
baseURL: import.meta.env.VITE_APP_API_BASE_URL,
|
|
18
|
-
data: {},
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
class Http {
|
|
22
|
-
// 当前实例
|
|
23
|
-
private static axiosInstance: AxiosInstance
|
|
24
|
-
// 请求配置
|
|
25
|
-
private static axiosConfigDefault: AxiosRequestConfig
|
|
26
|
-
|
|
27
|
-
// 请求拦截
|
|
28
|
-
private httpInterceptorsRequest(): void {
|
|
29
|
-
Http.axiosInstance.interceptors.request.use(
|
|
30
|
-
(config) => {
|
|
31
|
-
// 发送请求前,可在此让每个请求携带自定义 token, 请根据实际情况修改
|
|
32
|
-
const savedToken = useUserStore().getToken()
|
|
33
|
-
// 如果 token 存在
|
|
34
|
-
if (savedToken)
|
|
35
|
-
config.headers[ACCESS_TOKEN] = savedToken
|
|
36
|
-
const v4SessionKey = localStorage.getItem('v4-session-key')
|
|
37
|
-
if (['post'].includes(config.method.toLowerCase()) &&
|
|
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
|
-
return
|
|
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
|
-
case
|
|
125
|
-
message = '
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
break
|
|
132
|
-
case
|
|
133
|
-
message =
|
|
134
|
-
break
|
|
135
|
-
case
|
|
136
|
-
message =
|
|
137
|
-
break
|
|
138
|
-
case
|
|
139
|
-
message = '
|
|
140
|
-
break
|
|
141
|
-
case
|
|
142
|
-
message = '
|
|
143
|
-
break
|
|
144
|
-
case
|
|
145
|
-
message = '
|
|
146
|
-
break
|
|
147
|
-
case
|
|
148
|
-
message = '
|
|
149
|
-
break
|
|
150
|
-
case
|
|
151
|
-
message = '
|
|
152
|
-
break
|
|
153
|
-
case
|
|
154
|
-
message = '
|
|
155
|
-
break
|
|
156
|
-
|
|
157
|
-
message = '
|
|
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
|
-
* @param
|
|
204
|
-
* @
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
1
|
+
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
2
|
+
import { ContentTypeEnum, ResultEnum } from '@af-mobile-client-vue3/enums/requestEnum'
|
|
3
|
+
import { useUserStore } from '@af-mobile-client-vue3/stores/modules/user'
|
|
4
|
+
import { ACCESS_TOKEN } from '@af-mobile-client-vue3/stores/mutation-type'
|
|
5
|
+
import { encryptUtil } from '@af-mobile-client-vue3/utils/EncryptUtil'
|
|
6
|
+
import axios from 'axios'
|
|
7
|
+
import { showToast } from 'vant'
|
|
8
|
+
|
|
9
|
+
// 默认 axios 实例请求配置
|
|
10
|
+
const configDefault = {
|
|
11
|
+
headers: {
|
|
12
|
+
'Content-Type': ContentTypeEnum.JSON,
|
|
13
|
+
},
|
|
14
|
+
// 请求超时时间
|
|
15
|
+
timeout: 20000,
|
|
16
|
+
// API 请求的默认前缀
|
|
17
|
+
baseURL: import.meta.env.VITE_APP_API_BASE_URL,
|
|
18
|
+
data: {},
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class Http {
|
|
22
|
+
// 当前实例
|
|
23
|
+
private static axiosInstance: AxiosInstance
|
|
24
|
+
// 请求配置
|
|
25
|
+
private static axiosConfigDefault: AxiosRequestConfig
|
|
26
|
+
|
|
27
|
+
// 请求拦截
|
|
28
|
+
private httpInterceptorsRequest(): void {
|
|
29
|
+
Http.axiosInstance.interceptors.request.use(
|
|
30
|
+
(config) => {
|
|
31
|
+
// 发送请求前,可在此让每个请求携带自定义 token, 请根据实际情况修改
|
|
32
|
+
const savedToken = useUserStore().getToken()
|
|
33
|
+
// 如果 token 存在
|
|
34
|
+
if (savedToken)
|
|
35
|
+
config.headers[ACCESS_TOKEN] = savedToken
|
|
36
|
+
const v4SessionKey = localStorage.getItem('v4-session-key')
|
|
37
|
+
if (['post'].includes(config.method.toLowerCase()) &&
|
|
38
|
+
!config.url.includes('/logic/openapi/') &&
|
|
39
|
+
!config.url.includes('auth/login')
|
|
40
|
+
&& v4SessionKey) {
|
|
41
|
+
if (config.data && !(config.data instanceof FormData)) {
|
|
42
|
+
config.data = {
|
|
43
|
+
encrypted: encryptUtil.AESEncryptCBC(config.data, v4SessionKey),
|
|
44
|
+
}
|
|
45
|
+
config.headers['X-Sec'] = '1'
|
|
46
|
+
config.headers['X-Rand'] = Math.random().toString(36).substr(2, 5)
|
|
47
|
+
config.headers['X-Ts'] = Date.now()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return config
|
|
51
|
+
},
|
|
52
|
+
(error: AxiosError) => {
|
|
53
|
+
showToast({
|
|
54
|
+
message: error.message,
|
|
55
|
+
position: 'bottom',
|
|
56
|
+
})
|
|
57
|
+
return Promise.reject(error)
|
|
58
|
+
},
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 响应拦截
|
|
63
|
+
private httpInterceptorsResponse(): void {
|
|
64
|
+
Http.axiosInstance.interceptors.response.use(
|
|
65
|
+
async (response: AxiosResponse) => {
|
|
66
|
+
// 判断是否需要解密
|
|
67
|
+
if (response.headers && response.headers['x-encrypted'] === '1') {
|
|
68
|
+
const v4SessionKey = localStorage.getItem('v4-session-key')
|
|
69
|
+
if (v4SessionKey && response.data) {
|
|
70
|
+
const decryptedData = encryptUtil.decryptResponse(response?.data, v4SessionKey)
|
|
71
|
+
// 如果解密成功且不等于原数据,说明解密有效
|
|
72
|
+
if (decryptedData !== response.data) {
|
|
73
|
+
// 响应解密成功
|
|
74
|
+
response.data = decryptedData
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const compatible = import.meta.env.VITE_APP_COMPATIBLE
|
|
79
|
+
if (compatible !== 'V4') {
|
|
80
|
+
return response.data
|
|
81
|
+
}
|
|
82
|
+
// 与后端协定的返回字段
|
|
83
|
+
const { code, msg, data } = response.data
|
|
84
|
+
// 临时向v3请求上传服务,因为没有code,未来会改
|
|
85
|
+
if (code === undefined) {
|
|
86
|
+
return response.data
|
|
87
|
+
}
|
|
88
|
+
// 判断请求是否成功
|
|
89
|
+
const isSuccess = code === ResultEnum.SUCCESS
|
|
90
|
+
if (isSuccess) {
|
|
91
|
+
// 兼容 V3 请求没有data的情况
|
|
92
|
+
if (data) {
|
|
93
|
+
return data
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return response.data
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
if (code === 401 && response.config.url?.indexOf('af-auth/logout') === -1) {
|
|
101
|
+
showToast({
|
|
102
|
+
message: '登录态已失效,请重新登录',
|
|
103
|
+
position: 'bottom',
|
|
104
|
+
})
|
|
105
|
+
await useUserStore().logout()
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
showToast({
|
|
109
|
+
message: msg,
|
|
110
|
+
position: 'bottom',
|
|
111
|
+
})
|
|
112
|
+
console.error(`请求失败,返回结果:${msg}`)
|
|
113
|
+
}
|
|
114
|
+
return Promise.reject(response.data)
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
(error: AxiosError) => {
|
|
118
|
+
// 处理 HTTP 网络错误
|
|
119
|
+
let message: string = error.response?.data as string
|
|
120
|
+
if (!message) {
|
|
121
|
+
// HTTP 状态码
|
|
122
|
+
const status = error.response?.status
|
|
123
|
+
switch (status) {
|
|
124
|
+
case 400:
|
|
125
|
+
message = '请求错误'
|
|
126
|
+
break
|
|
127
|
+
case 401:
|
|
128
|
+
message = '登录态已失效,请重新登录'
|
|
129
|
+
// 401状态码处理:自动登出并跳转到登录页
|
|
130
|
+
useUserStore().logout()
|
|
131
|
+
break
|
|
132
|
+
case 403:
|
|
133
|
+
message = '拒绝访问'
|
|
134
|
+
break
|
|
135
|
+
case 404:
|
|
136
|
+
message = `请求地址出错: ${error.response?.config?.url}`
|
|
137
|
+
break
|
|
138
|
+
case 408:
|
|
139
|
+
message = '请求超时'
|
|
140
|
+
break
|
|
141
|
+
case 500:
|
|
142
|
+
message = '服务器内部错误'
|
|
143
|
+
break
|
|
144
|
+
case 501:
|
|
145
|
+
message = '服务未实现'
|
|
146
|
+
break
|
|
147
|
+
case 502:
|
|
148
|
+
message = '网关错误'
|
|
149
|
+
break
|
|
150
|
+
case 503:
|
|
151
|
+
message = '服务不可用'
|
|
152
|
+
break
|
|
153
|
+
case 504:
|
|
154
|
+
message = '网关超时'
|
|
155
|
+
break
|
|
156
|
+
case 505:
|
|
157
|
+
message = 'HTTP版本不受支持'
|
|
158
|
+
break
|
|
159
|
+
default:
|
|
160
|
+
message = '网络连接故障'
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
showToast({
|
|
164
|
+
message,
|
|
165
|
+
position: 'bottom',
|
|
166
|
+
})
|
|
167
|
+
return Promise.reject(error)
|
|
168
|
+
},
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
constructor(config: AxiosRequestConfig) {
|
|
173
|
+
Http.axiosConfigDefault = config
|
|
174
|
+
Http.axiosInstance = axios.create(config)
|
|
175
|
+
this.httpInterceptorsRequest()
|
|
176
|
+
this.httpInterceptorsResponse()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 通用请求函数
|
|
180
|
+
public request<T>(paramConfig: AxiosRequestConfig): Promise<T> {
|
|
181
|
+
const config = { ...Http.axiosConfigDefault, ...paramConfig }
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
Http.axiosInstance
|
|
184
|
+
.request(config)
|
|
185
|
+
.then((response: any) => {
|
|
186
|
+
resolve(response)
|
|
187
|
+
})
|
|
188
|
+
.catch((error) => {
|
|
189
|
+
reject(error)
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const METHOD = {
|
|
196
|
+
GET: 'get',
|
|
197
|
+
POST: 'post',
|
|
198
|
+
PUT: 'put',
|
|
199
|
+
DELETE: 'delete',
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* axios请求
|
|
203
|
+
* @param url 请求地址
|
|
204
|
+
* @param method {METHOD} http method
|
|
205
|
+
* @param params 请求参数
|
|
206
|
+
* @param config
|
|
207
|
+
* @returns {Promise<AxiosResponse<T>>}
|
|
208
|
+
*/
|
|
209
|
+
async function request(url, method, params, config) {
|
|
210
|
+
switch (method) {
|
|
211
|
+
case METHOD.GET:
|
|
212
|
+
return axios.get(url, { params, ...config })
|
|
213
|
+
case METHOD.POST:
|
|
214
|
+
return axios.post(url, params, config)
|
|
215
|
+
case METHOD.PUT:
|
|
216
|
+
return axios.put(url, params, config)
|
|
217
|
+
case METHOD.DELETE:
|
|
218
|
+
return axios.delete(url, { params, ...config })
|
|
219
|
+
default:
|
|
220
|
+
return axios.get(url, { params, ...config })
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export const http = new Http(configDefault)
|
|
225
|
+
export {
|
|
226
|
+
METHOD,
|
|
227
|
+
request,
|
|
228
|
+
}
|