sh-tools 2.0.0 → 2.0.1
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/packages/api/index.js +84 -84
package/package.json
CHANGED
package/packages/api/index.js
CHANGED
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
import XEAjax from 'xe-ajax'
|
|
2
|
-
import Notice from '../components/sh-notification'
|
|
3
|
-
|
|
4
|
-
class HttpRequest {
|
|
5
|
-
constructor(options = {}) {
|
|
6
|
-
this.instance = XEAjax
|
|
7
|
-
this.options = options
|
|
8
|
-
this.setOptions(options)
|
|
9
|
-
this.interceptors()
|
|
10
|
-
}
|
|
11
|
-
setOptions(options) {
|
|
12
|
-
this.instance.setup(options)
|
|
13
|
-
}
|
|
14
|
-
// 继承类重写方法 用于处理个性化业务 预留
|
|
15
|
-
setRequest(request) {}
|
|
16
|
-
setResponse(response) {
|
|
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
|
-
resBody = response.
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
break
|
|
69
|
-
case 401:
|
|
70
|
-
Notice.error({ title: '系统提示 401', content: '未授权,请重新登录' })
|
|
71
|
-
return
|
|
72
|
-
case 504:
|
|
73
|
-
Notice.error({ title: '系统提示 网络超时', content: errorMsg })
|
|
74
|
-
break
|
|
75
|
-
default:
|
|
76
|
-
Notice.error({ title: `系统提示 ${status}`, content: errorMsg })
|
|
77
|
-
break
|
|
78
|
-
}
|
|
79
|
-
this.setResponse(resBody)
|
|
80
|
-
next(
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export default HttpRequest
|
|
1
|
+
import XEAjax from 'xe-ajax'
|
|
2
|
+
import Notice from '../components/sh-notification'
|
|
3
|
+
|
|
4
|
+
class HttpRequest {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.instance = XEAjax
|
|
7
|
+
this.options = options
|
|
8
|
+
this.setOptions(options)
|
|
9
|
+
this.interceptors()
|
|
10
|
+
}
|
|
11
|
+
setOptions(options) {
|
|
12
|
+
this.instance.setup(options)
|
|
13
|
+
}
|
|
14
|
+
// 继承类重写方法 用于处理个性化业务 预留
|
|
15
|
+
setRequest(request) {}
|
|
16
|
+
async setResponse(body, response, status) {
|
|
17
|
+
return { status: 200, body }
|
|
18
|
+
}
|
|
19
|
+
request(options) {
|
|
20
|
+
return this.instance.ajax(options)
|
|
21
|
+
}
|
|
22
|
+
get(url, params, options) {
|
|
23
|
+
return this.instance.get(url, params, options)
|
|
24
|
+
}
|
|
25
|
+
post(url, params, options) {
|
|
26
|
+
return this.instance.post(url, params, options)
|
|
27
|
+
}
|
|
28
|
+
put(url, params, options) {
|
|
29
|
+
return this.instance.put(url, params, options)
|
|
30
|
+
}
|
|
31
|
+
delete(url, params, options) {
|
|
32
|
+
return this.instance.delete(url, params, options)
|
|
33
|
+
}
|
|
34
|
+
patch(url, params, options) {
|
|
35
|
+
return this.instance.patch(url, params, options)
|
|
36
|
+
}
|
|
37
|
+
head(url, params, options) {
|
|
38
|
+
return this.instance.head(url, params, options)
|
|
39
|
+
}
|
|
40
|
+
jsonp(url, params, options) {
|
|
41
|
+
return this.instance.jsonp(url, params, options)
|
|
42
|
+
}
|
|
43
|
+
// 拦截器
|
|
44
|
+
interceptors() {
|
|
45
|
+
// 请求拦截
|
|
46
|
+
this.instance.interceptors.request.use((request, next) => {
|
|
47
|
+
this.setRequest(request)
|
|
48
|
+
next()
|
|
49
|
+
})
|
|
50
|
+
// 响应拦截
|
|
51
|
+
this.instance.interceptors.response.use((response, next) => {
|
|
52
|
+
this.responseNext(response, next)
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
// 错误处理
|
|
56
|
+
async responseNext(response, next) {
|
|
57
|
+
const { status, statusText, headers } = response
|
|
58
|
+
let resBody = null
|
|
59
|
+
try {
|
|
60
|
+
resBody = await response.json()
|
|
61
|
+
} catch (e) {
|
|
62
|
+
resBody = response._response || {}
|
|
63
|
+
}
|
|
64
|
+
let errorMsg = resBody.error || resBody.msg || resBody.message || resBody.result || statusText || '系统错误'
|
|
65
|
+
switch (status) {
|
|
66
|
+
case 0:
|
|
67
|
+
Notice.error({ title: '请求错误', content: errorMsg })
|
|
68
|
+
break
|
|
69
|
+
case 401:
|
|
70
|
+
Notice.error({ title: '系统提示 401', content: '未授权,请重新登录' })
|
|
71
|
+
return
|
|
72
|
+
case 504:
|
|
73
|
+
Notice.error({ title: '系统提示 网络超时', content: errorMsg })
|
|
74
|
+
break
|
|
75
|
+
default:
|
|
76
|
+
Notice.error({ title: `系统提示 ${status}`, content: errorMsg })
|
|
77
|
+
break
|
|
78
|
+
}
|
|
79
|
+
let nextBody = await this.setResponse(resBody, response, status)
|
|
80
|
+
next(nextBody)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default HttpRequest
|