byt-ui 0.1.13 → 0.1.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/lib/byt-ui.common.js +33 -50
- package/lib/byt-ui.umd.js +33 -50
- package/lib/byt-ui.umd.min.js +1 -1
- package/package.json +1 -1
- package/packages/common/modules/request.js +33 -48
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
2
|
import { Message } from 'element-ui'
|
|
3
3
|
import { getCookie } from './cookie'
|
|
4
|
-
|
|
5
4
|
class Request {
|
|
6
5
|
constructor(config = {}, Vue = null) {
|
|
7
6
|
const { options = {}, handler = {}} = config;
|
|
@@ -18,6 +17,7 @@ class Request {
|
|
|
18
17
|
'428': 'token过期,请重新登录',
|
|
19
18
|
'429': '请求过频繁',
|
|
20
19
|
'479': '演示环境,没有权限操作',
|
|
20
|
+
'503': '服务未启动,请联系管理员处理',
|
|
21
21
|
'default': '系统未知错误,请反馈给管理员'
|
|
22
22
|
}
|
|
23
23
|
this._methods = ['get', 'post', 'delete', 'put']
|
|
@@ -26,17 +26,22 @@ class Request {
|
|
|
26
26
|
baseURL: '/bytserver',
|
|
27
27
|
responseType: 'json',
|
|
28
28
|
timeout: 60000,
|
|
29
|
-
|
|
29
|
+
showError: true,
|
|
30
|
+
withCredentials: false,
|
|
31
|
+
validateStatus(status) {
|
|
32
|
+
// >=200或者<=500的统一由成功拦截处理
|
|
33
|
+
return status >= 200 && status <= 500 // 默认的
|
|
34
|
+
}
|
|
30
35
|
}, options)
|
|
31
36
|
|
|
32
|
-
this.
|
|
37
|
+
this._request = axios.create(this.options)
|
|
33
38
|
// 注册fetch;
|
|
34
39
|
this.register()
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
interceptors() {
|
|
38
43
|
// HTTPrequest拦截
|
|
39
|
-
this.
|
|
44
|
+
this._request.interceptors.request.use(config => {
|
|
40
45
|
const TENANT_ID = getCookie('tenantId')
|
|
41
46
|
const isToken = (config.headers || {}).isToken === false
|
|
42
47
|
const token = getCookie('access_token')
|
|
@@ -51,50 +56,30 @@ class Request {
|
|
|
51
56
|
})
|
|
52
57
|
|
|
53
58
|
// HTTPresponse拦截
|
|
54
|
-
this.
|
|
59
|
+
this._request.interceptors.response.use(res => {
|
|
55
60
|
const status = Number(res.status) || 200
|
|
56
61
|
const message = res.data.msg || this._errorCode[status] || this._errorCode['default']
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
case 424:
|
|
66
|
-
case 428:
|
|
67
|
-
// 后台定义 424||428 针对令牌过期的特殊响应码
|
|
68
|
-
this.handler.expire && this.handler.expire(res.data);
|
|
69
|
-
Message.error(message);
|
|
70
|
-
if (window.__POWERED_BY_QIANKUN__ && this._Vue) {
|
|
71
|
-
const { outLogin } = this._Vue.prototype.$appCommon;
|
|
72
|
-
outLogin && outLogin();
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
default:
|
|
76
|
-
Message.error(message);
|
|
77
|
-
return Promise.reject(message)
|
|
62
|
+
if (status == 424 || status == 428) {
|
|
63
|
+
// 后台定义 424||428 针对令牌过期的特殊响应码
|
|
64
|
+
this.handler.expire && this.handler.expire(res.data);
|
|
65
|
+
if (window.__POWERED_BY_QIANKUN__ && this._Vue) {
|
|
66
|
+
const { outLogin } = this._Vue.prototype.$appCommon;
|
|
67
|
+
outLogin && outLogin();
|
|
68
|
+
}
|
|
69
|
+
return Promise.reject(res.data)
|
|
78
70
|
}
|
|
79
|
-
|
|
71
|
+
if (status !== 200 || res.data.code === 1) {
|
|
72
|
+
if (res.config.showError) Message.error(message)
|
|
73
|
+
return Promise.reject(res.data)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.handler.success && this.handler.success(res.data);
|
|
77
|
+
return res.data
|
|
78
|
+
}, (error) => {
|
|
80
79
|
if (error.response) {
|
|
81
|
-
const status = error.response.status
|
|
80
|
+
// const status = error.response.status
|
|
82
81
|
const message = error.response.data.msg;
|
|
83
|
-
|
|
84
|
-
case 424:
|
|
85
|
-
case 428:
|
|
86
|
-
// 后台定义 424||428 针对令牌过期的特殊响应码
|
|
87
|
-
this.handler.expire && this.handler.expire(error.response);
|
|
88
|
-
Message.error(message);
|
|
89
|
-
if (window.__POWERED_BY_QIANKUN__ && this._Vue) {
|
|
90
|
-
const { outLogin } = this._Vue.prototype.$appCommon;
|
|
91
|
-
outLogin && outLogin();
|
|
92
|
-
}
|
|
93
|
-
break;
|
|
94
|
-
default:
|
|
95
|
-
if (error.config.showError)Message.error(message)
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
82
|
+
if (error.config.showError)Message.error(message)
|
|
98
83
|
this.handler.error && this.handler.error(error.response);
|
|
99
84
|
}
|
|
100
85
|
return Promise.reject(new Error(error))
|
|
@@ -111,7 +96,7 @@ class Request {
|
|
|
111
96
|
}
|
|
112
97
|
setMethods() {
|
|
113
98
|
this._methods.forEach(v => {
|
|
114
|
-
this.
|
|
99
|
+
this._request[v] = ({
|
|
115
100
|
url,
|
|
116
101
|
data = {},
|
|
117
102
|
params = {},
|
|
@@ -121,7 +106,7 @@ class Request {
|
|
|
121
106
|
showError
|
|
122
107
|
}) => {
|
|
123
108
|
return new Promise((resolve, reject) => {
|
|
124
|
-
this.
|
|
109
|
+
this._request({
|
|
125
110
|
url,
|
|
126
111
|
method: v.toUpperCase(),
|
|
127
112
|
data,
|
|
@@ -144,7 +129,7 @@ class Request {
|
|
|
144
129
|
// 重试请求
|
|
145
130
|
reject(err)
|
|
146
131
|
if (retry > 0) {
|
|
147
|
-
this.
|
|
132
|
+
this._request[v]({
|
|
148
133
|
url,
|
|
149
134
|
data,
|
|
150
135
|
params,
|
|
@@ -170,8 +155,8 @@ class Request {
|
|
|
170
155
|
}
|
|
171
156
|
|
|
172
157
|
export const fetch = (config = {}, Vue) => {
|
|
173
|
-
const {
|
|
174
|
-
return
|
|
158
|
+
const { _request } = new Request(Object.assign({}, config), Vue)
|
|
159
|
+
return _request
|
|
175
160
|
}
|
|
176
161
|
|
|
177
162
|
export default {
|