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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sh-tools",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "基于xe-ajax和xe-utils二次封装",
5
5
  "main": "packages/index.js",
6
6
  "scripts": {
@@ -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
- request(options) {
18
- return this.instance.ajax(options)
19
- }
20
- get(url, params, options) {
21
- return this.instance.get(url, params, options)
22
- }
23
- post(url, params, options) {
24
- return this.instance.post(url, params, options)
25
- }
26
- put(url, params, options) {
27
- return this.instance.put(url, params, options)
28
- }
29
- delete(url, params, options) {
30
- return this.instance.delete(url, params, options)
31
- }
32
- patch(url, params, options) {
33
- return this.instance.patch(url, params, options)
34
- }
35
- head(url, params, options) {
36
- return this.instance.head(url, params, options)
37
- }
38
- jsonp(url, params, options) {
39
- return this.instance.jsonp(url, params, options)
40
- }
41
- // 拦截器
42
- interceptors() {
43
- // 请求拦截
44
- this.instance.interceptors.request.use((request, next) => {
45
- this.setRequest(request)
46
- next()
47
- })
48
- // 响应拦截
49
- this.instance.interceptors.response.use((response, next) => {
50
- this.responseNext(response, next)
51
- })
52
- }
53
- // 错误处理
54
- async responseNext(response, next) {
55
- const { status, statusText, headers } = response
56
- let resBody = null
57
- try {
58
- resBody = await response.json()
59
- } catch (e) {
60
- resBody = response.__response || {}
61
- }
62
- let errorMsg = resBody.error || resBody.msg || resBody.message || resBody.result || statusText || '系统错误'
63
- switch (status) {
64
- case 0:
65
- Notice.error({ title: '请求错误', content: errorMsg })
66
- break
67
- case 200:
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({ status: 200, body: resBody })
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