vue2-client 1.7.16 → 1.7.18

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.
@@ -1,24 +1,24 @@
1
- import { METHOD, request } from '@vue2-client/utils/request'
2
-
3
- /**
4
- * GET请求
5
- * @param url 请求地址
6
- * @param parameter 路径参数
7
- * @returns {Promise<AxiosResponse<T>>}
8
- */
9
- function get (url, parameter) {
10
- return request(url, METHOD.GET, parameter)
11
- }
12
-
13
- /**
14
- * POST请求
15
- * @param url 请求地址
16
- * @param parameter 请求参数
17
- * @param config
18
- * @returns {Promise<AxiosResponse<T>>}
19
- */
20
- function post (url, parameter, config = {}) {
21
- return request(url, METHOD.POST, parameter, config)
22
- }
23
-
24
- export { get, post }
1
+ import { METHOD, request } from '@vue2-client/utils/request'
2
+
3
+ /**
4
+ * GET请求
5
+ * @param url 请求地址
6
+ * @param parameter 路径参数
7
+ * @returns {Promise<AxiosResponse<T>>}
8
+ */
9
+ function get (url, parameter) {
10
+ return request(url, METHOD.GET, parameter)
11
+ }
12
+
13
+ /**
14
+ * POST请求
15
+ * @param url 请求地址
16
+ * @param parameter 请求参数
17
+ * @param config
18
+ * @returns {Promise<AxiosResponse<T>>}
19
+ */
20
+ function post (url, parameter, config = {}) {
21
+ return request(url, METHOD.POST, parameter, config)
22
+ }
23
+
24
+ export { get, post }
@@ -1,311 +1,311 @@
1
- import axios from 'axios'
2
- import Cookie from 'js-cookie'
3
- import { ACCESS_TOKEN, SYSTEM_VERSION, V4_ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
4
- import notification from 'ant-design-vue/es/notification'
5
- import errorCode from '@vue2-client/utils/errorCode'
6
- import qs from 'qs'
7
- import { logout } from '@vue2-client/services/user'
8
-
9
- // 是否显示重新登录
10
- let isReloginShow
11
-
12
- axios.defaults.timeout = 20000
13
- axios.defaults.withCredentials = true
14
-
15
- // 认证类型
16
- const AUTH_TYPE = {
17
- BEARER: 'Bearer',
18
- BASIC: 'basic',
19
- AUTH1: 'auth1',
20
- AUTH2: 'auth2'
21
- }
22
-
23
- // http method
24
- const METHOD = {
25
- GET: 'get',
26
- POST: 'post',
27
- PUT: 'put',
28
- DELETE: 'delete'
29
- }
30
-
31
- /**
32
- * axios请求
33
- * @param url 请求地址
34
- * @param method {METHOD} http method
35
- * @param params 请求参数
36
- * @param config
37
- * @returns {Promise<AxiosResponse<T>>}
38
- */
39
- async function request (url, method, params, config) {
40
- switch (method) {
41
- case METHOD.GET:
42
- return axios.get(url, { params, ...config })
43
- case METHOD.POST:
44
- return axios.post(url, params, config)
45
- case METHOD.PUT:
46
- return axios.put(url, params, config)
47
- case METHOD.DELETE:
48
- return axios.delete(url, config)
49
- default:
50
- return axios.get(url, { params, ...config })
51
- }
52
- }
53
-
54
- /**
55
- * 设置认证信息
56
- * @param auth {Object}
57
- * @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
58
- */
59
- function setAuthorization (auth, authType = AUTH_TYPE.BEARER) {
60
- switch (authType) {
61
- case AUTH_TYPE.BEARER:
62
- Cookie.set(V4_ACCESS_TOKEN, 'Bearer ' + auth.token, { expires: auth.expireAt })
63
- break
64
- case AUTH_TYPE.BASIC:
65
- case AUTH_TYPE.AUTH1:
66
- case AUTH_TYPE.AUTH2:
67
- default:
68
- break
69
- }
70
- }
71
-
72
- /**
73
- * 设置系统版本
74
- * @param value 系统版本
75
- */
76
- export function setSystemVersion (value) {
77
- Cookie.set(SYSTEM_VERSION, value)
78
- }
79
-
80
- /**
81
- * 获取系统版本
82
- */
83
- export function getSystemVersion () {
84
- return Cookie.get(SYSTEM_VERSION)
85
- }
86
-
87
- /**
88
- * 移出认证信息
89
- * @param authType {AUTH_TYPE} 认证类型
90
- */
91
- function removeAuthorization (authType = AUTH_TYPE.BEARER) {
92
- switch (authType) {
93
- case AUTH_TYPE.BEARER:
94
- Cookie.remove(V4_ACCESS_TOKEN)
95
- break
96
- case AUTH_TYPE.BASIC:
97
- case AUTH_TYPE.AUTH1:
98
- case AUTH_TYPE.AUTH2:
99
- default:
100
- break
101
- }
102
- }
103
-
104
- /**
105
- * 检查认证信息
106
- * @param authType
107
- * @returns {boolean}
108
- */
109
- function checkAuthorization (authType = AUTH_TYPE.BEARER) {
110
- switch (authType) {
111
- case AUTH_TYPE.BEARER:
112
- if (Cookie.get(V4_ACCESS_TOKEN)) {
113
- return true
114
- }
115
- break
116
- case AUTH_TYPE.BASIC:
117
- case AUTH_TYPE.AUTH1:
118
- case AUTH_TYPE.AUTH2:
119
- default:
120
- break
121
- }
122
- return false
123
- }
124
-
125
- /**
126
- * 加载 axios 拦截器
127
- * @param interceptors
128
- * @param options
129
- */
130
- function loadInterceptors () {
131
- // 加载请求拦截器
132
- axios.interceptors.request.use(config => {
133
- const token = localStorage.getItem(ACCESS_TOKEN)
134
- // 如果 token 存在
135
- // 让每个请求携带自定义 token 请根据实际情况自行修改
136
- if (token) {
137
- if (config.url !== '/auth/login') {
138
- // 判断是否为V4环境
139
- const compatible = getSystemVersion()
140
- if (compatible === 'V4') {
141
- // V4 环境则添加 V4请求头
142
- config.headers[V4_ACCESS_TOKEN] = token
143
- } else {
144
- config.headers[ACCESS_TOKEN] = token
145
- }
146
- }
147
- }
148
- if (!config.headers['Content-Type']) {
149
- config.headers['Content-Type'] = 'application/json;charset=UTF-8'
150
- }
151
- // 处理params参数
152
- if (config.params) {
153
- const url = config.url + '?' + qs.stringify(config.params, { indices: false })
154
- config.params = {}
155
- config.url = url
156
- }
157
- return config
158
- }, errorHandler)
159
- // 加载响应拦截器
160
- axios.interceptors.response.use((res) => {
161
- // 判断是否为V4环境,不为compatible赋初始值
162
- // 其有可能是undefined未定义,或第一次使用本系统LocalStorage在初始化,获得的值为null
163
- const compatible = getSystemVersion()
164
- if (compatible === 'V4' || (res.data.code && res.data.hasOwnProperty('msg'))) {
165
- // 请求rul
166
- const requestUrl = res.config.url
167
- if (res.data.data) {
168
- res.data = res.data.data
169
- }
170
- // 未设置状态码则默认成功状态
171
- const code = res.data.code || 200
172
- // 获取错误信息
173
- const msg = errorCode[code] || res.data.msg || errorCode['default']
174
- // 二进制数据则直接返回
175
- if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
176
- return res.data
177
- }
178
- if (code === 401) {
179
- if (!isReloginShow) {
180
- isReloginShow = true
181
- notification.open({
182
- message: '系统提示',
183
- description: '登录状态已过期,请重新登录',
184
- btn: h => {
185
- return h(
186
- 'a-button',
187
- {
188
- props: {
189
- type: 'primary',
190
- size: 'small'
191
- },
192
- on: {
193
- click: () => {
194
- logout().then(() => {
195
- isReloginShow = false
196
- location.href = '/login'
197
- })
198
- }
199
- }
200
- },
201
- '确认'
202
- )
203
- },
204
- duration: 0,
205
- onClose: () => {
206
- isReloginShow = false
207
- }
208
- })
209
- }
210
- } else if (code === 500) {
211
- if (requestUrl !== '/login') {
212
- notification.error({
213
- message: '失败',
214
- description: msg
215
- })
216
- }
217
- } else if (code === 601) {
218
- notification.warn({
219
- message: '警告',
220
- description: msg
221
- })
222
- } else if (code !== 200) {
223
- notification.error({
224
- message: '失败',
225
- description: msg
226
- })
227
- } else {
228
- return res.data
229
- }
230
- return Promise.reject(msg)
231
- } else {
232
- return res.data
233
- }
234
- }, errorHandler)
235
- }
236
-
237
- // 异常拦截处理器
238
- const errorHandler = (error) => {
239
- if (error.response) {
240
- const data = error.response.data
241
- // 从 localstorage 获取 token
242
- const token = localStorage.getItem(ACCESS_TOKEN)
243
- if (error.response.status === 403 || (data && data.code === 403)) {
244
- notification.error({
245
- message: '禁止访问',
246
- description: data
247
- })
248
- } else if (error.response.status === 401 || (data && data.code === 401)) {
249
- notification.error({
250
- message: '鉴权失败',
251
- description: data
252
- })
253
- if (token) {
254
- logout().then(() => {
255
- setTimeout(() => {
256
- window.location.reload()
257
- }, 1500)
258
- })
259
- }
260
- } else if (error.response.status === 500 || (data && data.code === 500)) {
261
- notification.error({
262
- message: '系统异常',
263
- description: data
264
- })
265
- } else if (error.response.status === 601 || (data && data.code === 601)) {
266
- notification.warn({
267
- message: '系统警告',
268
- description: data
269
- })
270
- } else {
271
- notification.error({
272
- message: '网络异常',
273
- description: data
274
- })
275
- }
276
- }
277
- return Promise.reject(error)
278
- }
279
-
280
- /**
281
- * 解析 url 中的参数
282
- * @param url
283
- * @returns {Object}
284
- */
285
- function parseUrlParams (url) {
286
- const params = {}
287
- if (!url || url === '' || typeof url !== 'string') {
288
- return params
289
- }
290
- const paramsStr = url.split('?')[1]
291
- if (!paramsStr) {
292
- return params
293
- }
294
- const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
295
- for (let i = 0; i < paramsArr.length / 2; i++) {
296
- const value = paramsArr[i * 2 + 1]
297
- params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
298
- }
299
- return params
300
- }
301
-
302
- export {
303
- METHOD,
304
- AUTH_TYPE,
305
- request,
306
- setAuthorization,
307
- removeAuthorization,
308
- checkAuthorization,
309
- loadInterceptors,
310
- parseUrlParams
311
- }
1
+ import axios from 'axios'
2
+ import Cookie from 'js-cookie'
3
+ import { ACCESS_TOKEN, SYSTEM_VERSION, V4_ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
4
+ import notification from 'ant-design-vue/es/notification'
5
+ import errorCode from '@vue2-client/utils/errorCode'
6
+ import qs from 'qs'
7
+ import { logout } from '@vue2-client/services/user'
8
+
9
+ // 是否显示重新登录
10
+ let isReloginShow
11
+
12
+ axios.defaults.timeout = 20000
13
+ axios.defaults.withCredentials = true
14
+
15
+ // 认证类型
16
+ const AUTH_TYPE = {
17
+ BEARER: 'Bearer',
18
+ BASIC: 'basic',
19
+ AUTH1: 'auth1',
20
+ AUTH2: 'auth2'
21
+ }
22
+
23
+ // http method
24
+ const METHOD = {
25
+ GET: 'get',
26
+ POST: 'post',
27
+ PUT: 'put',
28
+ DELETE: 'delete'
29
+ }
30
+
31
+ /**
32
+ * axios请求
33
+ * @param url 请求地址
34
+ * @param method {METHOD} http method
35
+ * @param params 请求参数
36
+ * @param config
37
+ * @returns {Promise<AxiosResponse<T>>}
38
+ */
39
+ async function request (url, method, params, config) {
40
+ switch (method) {
41
+ case METHOD.GET:
42
+ return axios.get(url, { params, ...config })
43
+ case METHOD.POST:
44
+ return axios.post(url, params, config)
45
+ case METHOD.PUT:
46
+ return axios.put(url, params, config)
47
+ case METHOD.DELETE:
48
+ return axios.delete(url, config)
49
+ default:
50
+ return axios.get(url, { params, ...config })
51
+ }
52
+ }
53
+
54
+ /**
55
+ * 设置认证信息
56
+ * @param auth {Object}
57
+ * @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
58
+ */
59
+ function setAuthorization (auth, authType = AUTH_TYPE.BEARER) {
60
+ switch (authType) {
61
+ case AUTH_TYPE.BEARER:
62
+ Cookie.set(V4_ACCESS_TOKEN, 'Bearer ' + auth.token, { expires: auth.expireAt })
63
+ break
64
+ case AUTH_TYPE.BASIC:
65
+ case AUTH_TYPE.AUTH1:
66
+ case AUTH_TYPE.AUTH2:
67
+ default:
68
+ break
69
+ }
70
+ }
71
+
72
+ /**
73
+ * 设置系统版本
74
+ * @param value 系统版本
75
+ */
76
+ export function setSystemVersion (value) {
77
+ Cookie.set(SYSTEM_VERSION, value)
78
+ }
79
+
80
+ /**
81
+ * 获取系统版本
82
+ */
83
+ export function getSystemVersion () {
84
+ return Cookie.get(SYSTEM_VERSION)
85
+ }
86
+
87
+ /**
88
+ * 移出认证信息
89
+ * @param authType {AUTH_TYPE} 认证类型
90
+ */
91
+ function removeAuthorization (authType = AUTH_TYPE.BEARER) {
92
+ switch (authType) {
93
+ case AUTH_TYPE.BEARER:
94
+ Cookie.remove(V4_ACCESS_TOKEN)
95
+ break
96
+ case AUTH_TYPE.BASIC:
97
+ case AUTH_TYPE.AUTH1:
98
+ case AUTH_TYPE.AUTH2:
99
+ default:
100
+ break
101
+ }
102
+ }
103
+
104
+ /**
105
+ * 检查认证信息
106
+ * @param authType
107
+ * @returns {boolean}
108
+ */
109
+ function checkAuthorization (authType = AUTH_TYPE.BEARER) {
110
+ switch (authType) {
111
+ case AUTH_TYPE.BEARER:
112
+ if (Cookie.get(V4_ACCESS_TOKEN)) {
113
+ return true
114
+ }
115
+ break
116
+ case AUTH_TYPE.BASIC:
117
+ case AUTH_TYPE.AUTH1:
118
+ case AUTH_TYPE.AUTH2:
119
+ default:
120
+ break
121
+ }
122
+ return false
123
+ }
124
+
125
+ /**
126
+ * 加载 axios 拦截器
127
+ * @param interceptors
128
+ * @param options
129
+ */
130
+ function loadInterceptors () {
131
+ // 加载请求拦截器
132
+ axios.interceptors.request.use(config => {
133
+ const token = localStorage.getItem(ACCESS_TOKEN)
134
+ // 如果 token 存在
135
+ // 让每个请求携带自定义 token 请根据实际情况自行修改
136
+ if (token) {
137
+ if (config.url !== '/auth/login') {
138
+ // 判断是否为V4环境
139
+ const compatible = getSystemVersion()
140
+ if (compatible === 'V4') {
141
+ // V4 环境则添加 V4请求头
142
+ config.headers[V4_ACCESS_TOKEN] = token
143
+ } else {
144
+ config.headers[ACCESS_TOKEN] = token
145
+ }
146
+ }
147
+ }
148
+ if (!config.headers['Content-Type']) {
149
+ config.headers['Content-Type'] = 'application/json;charset=UTF-8'
150
+ }
151
+ // 处理params参数
152
+ if (config.params) {
153
+ const url = config.url + '?' + qs.stringify(config.params, { indices: false })
154
+ config.params = {}
155
+ config.url = url
156
+ }
157
+ return config
158
+ }, errorHandler)
159
+ // 加载响应拦截器
160
+ axios.interceptors.response.use((res) => {
161
+ // 判断是否为V4环境,不为compatible赋初始值
162
+ // 其有可能是undefined未定义,或第一次使用本系统LocalStorage在初始化,获得的值为null
163
+ const compatible = getSystemVersion()
164
+ if (compatible === 'V4' || (res.data.code && res.data.hasOwnProperty('msg'))) {
165
+ // 请求rul
166
+ const requestUrl = res.config.url
167
+ if (res.data.data) {
168
+ res.data = res.data.data
169
+ }
170
+ // 未设置状态码则默认成功状态
171
+ const code = res.data.code || 200
172
+ // 获取错误信息
173
+ const msg = errorCode[code] || res.data.msg || errorCode['default']
174
+ // 二进制数据则直接返回
175
+ if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
176
+ return res.data
177
+ }
178
+ if (code === 401) {
179
+ if (!isReloginShow) {
180
+ isReloginShow = true
181
+ notification.open({
182
+ message: '系统提示',
183
+ description: '登录状态已过期,请重新登录',
184
+ btn: h => {
185
+ return h(
186
+ 'a-button',
187
+ {
188
+ props: {
189
+ type: 'primary',
190
+ size: 'small'
191
+ },
192
+ on: {
193
+ click: () => {
194
+ logout().then(() => {
195
+ isReloginShow = false
196
+ location.href = '/login'
197
+ })
198
+ }
199
+ }
200
+ },
201
+ '确认'
202
+ )
203
+ },
204
+ duration: 0,
205
+ onClose: () => {
206
+ isReloginShow = false
207
+ }
208
+ })
209
+ }
210
+ } else if (code === 500) {
211
+ if (requestUrl !== '/login') {
212
+ notification.error({
213
+ message: '失败',
214
+ description: msg
215
+ })
216
+ }
217
+ } else if (code === 601) {
218
+ notification.warn({
219
+ message: '警告',
220
+ description: msg
221
+ })
222
+ } else if (code !== 200) {
223
+ notification.error({
224
+ message: '失败',
225
+ description: msg
226
+ })
227
+ } else {
228
+ return res.data
229
+ }
230
+ return Promise.reject(msg)
231
+ } else {
232
+ return res.data
233
+ }
234
+ }, errorHandler)
235
+ }
236
+
237
+ // 异常拦截处理器
238
+ const errorHandler = (error) => {
239
+ if (error.response) {
240
+ const data = error.response.data
241
+ // 从 localstorage 获取 token
242
+ const token = localStorage.getItem(ACCESS_TOKEN)
243
+ if (error.response.status === 403 || (data && data.code === 403)) {
244
+ notification.error({
245
+ message: '禁止访问',
246
+ description: data
247
+ })
248
+ } else if (error.response.status === 401 || (data && data.code === 401)) {
249
+ notification.error({
250
+ message: '鉴权失败',
251
+ description: data
252
+ })
253
+ if (token) {
254
+ logout().then(() => {
255
+ setTimeout(() => {
256
+ window.location.reload()
257
+ }, 1500)
258
+ })
259
+ }
260
+ } else if (error.response.status === 500 || (data && data.code === 500)) {
261
+ notification.error({
262
+ message: '系统异常',
263
+ description: data
264
+ })
265
+ } else if (error.response.status === 601 || (data && data.code === 601)) {
266
+ notification.warn({
267
+ message: '系统警告',
268
+ description: data
269
+ })
270
+ } else {
271
+ notification.error({
272
+ message: '网络异常',
273
+ description: data
274
+ })
275
+ }
276
+ }
277
+ return Promise.reject(error)
278
+ }
279
+
280
+ /**
281
+ * 解析 url 中的参数
282
+ * @param url
283
+ * @returns {Object}
284
+ */
285
+ function parseUrlParams (url) {
286
+ const params = {}
287
+ if (!url || url === '' || typeof url !== 'string') {
288
+ return params
289
+ }
290
+ const paramsStr = url.split('?')[1]
291
+ if (!paramsStr) {
292
+ return params
293
+ }
294
+ const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
295
+ for (let i = 0; i < paramsArr.length / 2; i++) {
296
+ const value = paramsArr[i * 2 + 1]
297
+ params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
298
+ }
299
+ return params
300
+ }
301
+
302
+ export {
303
+ METHOD,
304
+ AUTH_TYPE,
305
+ request,
306
+ setAuthorization,
307
+ removeAuthorization,
308
+ checkAuthorization,
309
+ loadInterceptors,
310
+ parseUrlParams
311
+ }