byt-ui 0.1.4 → 0.1.5
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 +3798 -45
- package/lib/byt-ui.umd.js +3798 -45
- package/lib/byt-ui.umd.min.js +6 -6
- package/package.json +1 -1
- package/packages/common/index.js +8 -5
- package/packages/common/modules/cookie.js +13 -5
- package/packages/common/modules/request.js +159 -0
package/package.json
CHANGED
package/packages/common/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Description:
|
|
3
3
|
* @Author: 王国火
|
|
4
4
|
* @Date: 2022-09-19 10:17:14
|
|
5
|
-
* @LastEditTime: 2024-04-
|
|
5
|
+
* @LastEditTime: 2024-04-22 12:56:46
|
|
6
6
|
* @LastEditors: 王国火
|
|
7
7
|
*/
|
|
8
8
|
// 动态引入
|
|
@@ -11,10 +11,13 @@ const requireContext = require.context('./modules', true, /.*\.js/)
|
|
|
11
11
|
requireContext.keys().map(key => {
|
|
12
12
|
const reg = /\w+/
|
|
13
13
|
const k = key.match(reg)[0]
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
const cnt = requireContext(key)
|
|
15
|
+
if (!cnt) return
|
|
16
|
+
const conf = { ...cnt }
|
|
17
|
+
if (conf.default) {
|
|
18
|
+
conmmon[k] = conf.default
|
|
19
|
+
delete (conf.default)
|
|
18
20
|
}
|
|
21
|
+
conmmon = Object.assign(conmmon, cnt)
|
|
19
22
|
})
|
|
20
23
|
export default conmmon
|
|
@@ -2,22 +2,30 @@
|
|
|
2
2
|
* @Description:
|
|
3
3
|
* @Author: 王国火
|
|
4
4
|
* @Date: 2022-10-18 12:37:03
|
|
5
|
-
* @LastEditTime: 2024-04-
|
|
5
|
+
* @LastEditTime: 2024-04-22 14:40:35
|
|
6
6
|
* @LastEditors: 王国火
|
|
7
7
|
*/
|
|
8
8
|
import Cookie from 'js-cookie'
|
|
9
9
|
import website from './website'
|
|
10
10
|
export const getCookie = (key) => {
|
|
11
|
-
const
|
|
12
|
-
return Cookie.get(
|
|
11
|
+
const fullKey = `${website.key}-${key}`;
|
|
12
|
+
return Cookie.get(fullKey, {
|
|
13
|
+
domain: window.location.hostname
|
|
14
|
+
}) || ''
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
export const setCookie = (key, value, expires = 7, path = '/') => {
|
|
16
|
-
|
|
18
|
+
const fullKey = `${website.key}-${key}`;
|
|
19
|
+
return Cookie.set(fullKey, value, {
|
|
20
|
+
expires,
|
|
21
|
+
path,
|
|
22
|
+
domain: window.location.hostname
|
|
23
|
+
})
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
export const removeCookie = (key, path = '/') => {
|
|
20
|
-
|
|
27
|
+
const fullKey = `${website.key}-${key}`;
|
|
28
|
+
return Cookie.remove(fullKey, {
|
|
21
29
|
path,
|
|
22
30
|
domain: window.location.hostname
|
|
23
31
|
})
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import { Message } from 'element-ui'
|
|
3
|
+
import { getCookie } from './cookie'
|
|
4
|
+
|
|
5
|
+
class Request {
|
|
6
|
+
constructor(config = {}) {
|
|
7
|
+
const { options = {}, handler = {}} = config;
|
|
8
|
+
this.request = null;
|
|
9
|
+
this._errorCode = {
|
|
10
|
+
'000': '操作太频繁,请勿重复请求',
|
|
11
|
+
'401': '当前操作没有权限',
|
|
12
|
+
'403': '当前操作没有权限',
|
|
13
|
+
'404': '接口不存在',
|
|
14
|
+
'417': '未绑定登录账号,请使用密码登录后绑定',
|
|
15
|
+
'423': '演示环境不能操作,如需了解联系我们',
|
|
16
|
+
'426': '用户名不存在或密码错误',
|
|
17
|
+
'428': '验证码错误,请重新输入',
|
|
18
|
+
'429': '请求过频繁',
|
|
19
|
+
'479': '演示环境,没有权限操作',
|
|
20
|
+
'default': '系统未知错误,请反馈给管理员'
|
|
21
|
+
}
|
|
22
|
+
this._methods = ['get', 'post', 'delete', 'put']
|
|
23
|
+
this.handler = Object.assign({}, handler);
|
|
24
|
+
this.options = Object.assign({}, {
|
|
25
|
+
baseURL: '/bytserver',
|
|
26
|
+
responseType: 'json',
|
|
27
|
+
timeout: 60000,
|
|
28
|
+
withCredentials: false
|
|
29
|
+
}, options)
|
|
30
|
+
// 注册fetch;
|
|
31
|
+
this.register()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interceptors() {
|
|
35
|
+
// HTTPrequest拦截
|
|
36
|
+
this.request.interceptors.request.use(config => {
|
|
37
|
+
const TENANT_ID = getCookie('tenantId')
|
|
38
|
+
const isToken = (config.headers || {}).isToken === false
|
|
39
|
+
const token = getCookie('access_token')
|
|
40
|
+
if (token && !isToken) {
|
|
41
|
+
config.headers['Authorization'] = `Bearer ${token}`// token
|
|
42
|
+
}
|
|
43
|
+
if (TENANT_ID) {
|
|
44
|
+
config.headers['TENANT-ID'] = TENANT_ID // 租户ID
|
|
45
|
+
}
|
|
46
|
+
return config
|
|
47
|
+
}, error => {
|
|
48
|
+
return Promise.reject(error)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// HTTPresponse拦截
|
|
52
|
+
this.request.interceptors.response.use(res => {
|
|
53
|
+
const status = Number(res.status) || 200
|
|
54
|
+
const message = res.data.msg || this._errorCode[status] || this._errorCode['default']
|
|
55
|
+
switch (status * 1) {
|
|
56
|
+
case 200:
|
|
57
|
+
if (res.data.code === 1) {
|
|
58
|
+
Message.error(message)
|
|
59
|
+
this.handler.error && this.handler.error(res.data);
|
|
60
|
+
return Promise.reject(res.data)
|
|
61
|
+
}
|
|
62
|
+
this.handler.success && this.handler.success(res.data)
|
|
63
|
+
return res.data
|
|
64
|
+
case 424:
|
|
65
|
+
case 428:
|
|
66
|
+
// 后台定义 424||428 针对令牌过期的特殊响应码
|
|
67
|
+
this.handler.expire && this.handler.expire(res.data);
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
Message.error(message);
|
|
71
|
+
this.handler.error && this.handler.error(res.data);
|
|
72
|
+
return Promise.reject(message)
|
|
73
|
+
}
|
|
74
|
+
}, error => {
|
|
75
|
+
if (error.response) {
|
|
76
|
+
const status = error.response.status
|
|
77
|
+
switch (status) {
|
|
78
|
+
case 404:
|
|
79
|
+
Message.error(this._errorCode[status])
|
|
80
|
+
break;
|
|
81
|
+
case 503:
|
|
82
|
+
Message.error(error.response.data.msg)
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
this.error && this.error(error.response);
|
|
86
|
+
}
|
|
87
|
+
return Promise.reject(new Error(error))
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setMethods() {
|
|
92
|
+
this._methods.forEach(v => {
|
|
93
|
+
this.request[v] = ({
|
|
94
|
+
url,
|
|
95
|
+
data = {},
|
|
96
|
+
params = {},
|
|
97
|
+
responseType = 'json',
|
|
98
|
+
headers = {},
|
|
99
|
+
retry = 0
|
|
100
|
+
}) => {
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
this.request({
|
|
103
|
+
url,
|
|
104
|
+
method: v.toUpperCase(),
|
|
105
|
+
data,
|
|
106
|
+
params,
|
|
107
|
+
responseType,
|
|
108
|
+
headers
|
|
109
|
+
}).then(res => {
|
|
110
|
+
if (
|
|
111
|
+
!res.code ||
|
|
112
|
+
res.code === 0 ||
|
|
113
|
+
responseType == 'arraybuffer' ||
|
|
114
|
+
responseType == 'blob'
|
|
115
|
+
) {
|
|
116
|
+
resolve(res)
|
|
117
|
+
} else {
|
|
118
|
+
Message.error(res.msg)
|
|
119
|
+
reject(res)
|
|
120
|
+
}
|
|
121
|
+
}).catch(err => {
|
|
122
|
+
// 重试请求
|
|
123
|
+
reject(err)
|
|
124
|
+
if (retry > 0) {
|
|
125
|
+
this.request[v]({
|
|
126
|
+
url,
|
|
127
|
+
data,
|
|
128
|
+
params,
|
|
129
|
+
responseType,
|
|
130
|
+
headers,
|
|
131
|
+
retry: retry - 1
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
register() {
|
|
141
|
+
this.request = axios.create(this.options)
|
|
142
|
+
|
|
143
|
+
// 添加拦截器
|
|
144
|
+
this.interceptors();
|
|
145
|
+
|
|
146
|
+
// 覆盖自定义方法
|
|
147
|
+
this.setMethods();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const { request } = new Request()
|
|
152
|
+
|
|
153
|
+
export default {
|
|
154
|
+
install(Vue, options = {}) {
|
|
155
|
+
const { request } = new Request(Object.assign({}, options))
|
|
156
|
+
Vue.prototype.$http = request
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|