byt-ui 0.1.9 → 0.1.11

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": "byt-ui",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "private": false,
5
5
  "description": "byt组件库",
6
6
  "author": {
@@ -3,9 +3,9 @@ import { Message } from 'element-ui'
3
3
  import { getCookie } from './cookie'
4
4
 
5
5
  class Request {
6
- constructor(config = {}) {
6
+ constructor(config = {}, Vue = null) {
7
7
  const { options = {}, handler = {}} = config;
8
- this.request = null;
8
+ this._Vue = Vue || null;
9
9
  this._errorCode = {
10
10
  '000': '操作太频繁,请勿重复请求',
11
11
  '401': '当前操作没有权限',
@@ -13,8 +13,9 @@ class Request {
13
13
  '404': '接口不存在',
14
14
  '417': '未绑定登录账号,请使用密码登录后绑定',
15
15
  '423': '演示环境不能操作,如需了解联系我们',
16
+ '424': 'token过期,请重新登录',
16
17
  '426': '用户名不存在或密码错误',
17
- '428': '验证码错误,请重新输入',
18
+ '428': 'token过期,请重新登录',
18
19
  '429': '请求过频繁',
19
20
  '479': '演示环境,没有权限操作',
20
21
  'default': '系统未知错误,请反馈给管理员'
@@ -27,6 +28,8 @@ class Request {
27
28
  timeout: 60000,
28
29
  withCredentials: false
29
30
  }, options)
31
+
32
+ this.request = axios.create(this.options)
30
33
  // 注册fetch;
31
34
  this.register()
32
35
  }
@@ -40,9 +43,8 @@ class Request {
40
43
  if (token && !isToken) {
41
44
  config.headers['Authorization'] = `Bearer ${token}`// token
42
45
  }
43
- if (TENANT_ID) {
44
- config.headers['TENANT-ID'] = TENANT_ID // 租户ID
45
- }
46
+ config.headers['TENANT-ID'] = TENANT_ID || this.getQueryString('TENANT-ID') || '' // 租户ID
47
+
46
48
  return config
47
49
  }, error => {
48
50
  return Promise.reject(error)
@@ -56,19 +58,22 @@ class Request {
56
58
  case 200:
57
59
  if (res.data.code === 1) {
58
60
  Message.error(message)
59
- this.handler.error && this.handler.error(res.data);
60
61
  return Promise.reject(res.data)
61
62
  }
62
- this.handler.success && this.handler.success(res.data)
63
+ this.handler.success && this.handler.success(res.data);
63
64
  return res.data
64
65
  case 424:
65
66
  case 428:
66
67
  // 后台定义 424||428 针对令牌过期的特殊响应码
67
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
+ }
68
74
  break;
69
75
  default:
70
76
  Message.error(message);
71
- this.handler.error && this.handler.error(res.data);
72
77
  return Promise.reject(message)
73
78
  }
74
79
  }, error => {
@@ -82,12 +87,20 @@ class Request {
82
87
  Message.error(error.response.data.msg)
83
88
  break;
84
89
  }
85
- this.error && this.error(error.response);
90
+ this.handler.error && this.handler.error(error.response);
86
91
  }
87
92
  return Promise.reject(new Error(error))
88
93
  })
89
94
  }
90
95
 
96
+ getQueryString(name) {
97
+ const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i')
98
+ const r = window.location.search.substr(1).match(reg)
99
+ if (r != null) {
100
+ return decodeURIComponent(r[2])
101
+ }
102
+ return null
103
+ }
91
104
  setMethods() {
92
105
  this._methods.forEach(v => {
93
106
  this.request[v] = ({
@@ -138,8 +151,6 @@ class Request {
138
151
  }
139
152
 
140
153
  register() {
141
- this.request = axios.create(this.options)
142
-
143
154
  // 添加拦截器
144
155
  this.interceptors();
145
156
 
@@ -148,12 +159,14 @@ class Request {
148
159
  }
149
160
  }
150
161
 
151
- export const { request } = new Request()
162
+ export const fetch = (config = {}, Vue) => {
163
+ const { request } = new Request(Object.assign({}, config), Vue)
164
+ return request
165
+ }
152
166
 
153
167
  export default {
154
- install(Vue, options = {}) {
155
- const { request } = new Request(Object.assign({}, options))
156
- Vue.prototype.$http = request
168
+ install(Vue, config = {}) {
169
+ Vue.prototype.$http = fetch(config, Vue)
157
170
  }
158
171
  }
159
172