vue2-client 1.0.0 → 1.1.0
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/README.md +10 -2
- package/package.json +1 -1
- package/src/base-client/plugins/GetLoginInfoService.js +1 -2
- package/src/bootstrap.js +27 -25
- package/src/index.js +7 -8
- package/src/main.js +9 -1
- package/src/router/index.js +27 -31
- package/src/store/index.js +5 -8
- package/src/utils/request.js +197 -197
- package/webpack.config.js +12 -0
package/README.md
CHANGED
|
@@ -7,9 +7,17 @@
|
|
|
7
7
|
```javascript
|
|
8
8
|
import Vue from 'vue'
|
|
9
9
|
import App from './App.vue'
|
|
10
|
-
import
|
|
10
|
+
import Router from 'vue-router'
|
|
11
|
+
import Vuex from 'vuex'
|
|
12
|
+
import '@/mock'
|
|
13
|
+
import { routerOptions, modules, i18n, message, bootstrap } from 'vue2-client/index'
|
|
14
|
+
|
|
15
|
+
Vue.use(Router)
|
|
16
|
+
Vue.use(Vuex)
|
|
17
|
+
// 创建router store
|
|
18
|
+
const store = new Vuex.Store({ modules })
|
|
19
|
+
const router = new Router(routerOptions)
|
|
11
20
|
|
|
12
|
-
// router, store 可在执行bootstrap函数前自行合并
|
|
13
21
|
bootstrap({ router, store, i18n, message })
|
|
14
22
|
new Vue({
|
|
15
23
|
router,
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import Vue from 'vue'
|
|
2
2
|
import { SEARCH } from '@/services/api'
|
|
3
3
|
import { post } from '@/services/api/restTools'
|
|
4
|
-
import store from '@/store'
|
|
5
4
|
|
|
6
5
|
const loginGen = async function (response) {
|
|
7
6
|
Vue.$login.f = response
|
|
@@ -11,7 +10,7 @@ const loginGen = async function (response) {
|
|
|
11
10
|
jwt: Vue.$login.f.id,
|
|
12
11
|
r: Vue.$login.r
|
|
13
12
|
}
|
|
14
|
-
store.commit('account/setLogin', login)
|
|
13
|
+
Vue.$store.commit('account/setLogin', login)
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
/**
|
package/src/bootstrap.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
12
|
-
* @param i18n 应用的
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
import Vue from 'vue'
|
|
2
|
+
import { loadRoutes, loadGuards, setAppOptions } from '@/utils/routerUtil'
|
|
3
|
+
import { loadInterceptors } from '@/utils/request'
|
|
4
|
+
import guards from '@/router/guards'
|
|
5
|
+
import interceptors from '@/utils/axios-interceptors'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 启动引导方法
|
|
9
|
+
* 应用启动时需要执行的操作放在这里
|
|
10
|
+
* @param router 应用的路由实例
|
|
11
|
+
* @param store 应用的 vuex.store 实例
|
|
12
|
+
* @param i18n 应用的 vue-i18n 实例
|
|
13
|
+
* @param i18n 应用的 message 实例
|
|
14
|
+
*/
|
|
15
|
+
function bootstrap ({ router, store, i18n, message }) {
|
|
16
|
+
Vue.$store = store
|
|
17
|
+
// 设置应用配置
|
|
18
|
+
setAppOptions({ router, store, i18n })
|
|
19
|
+
// 加载 axios 拦截器
|
|
20
|
+
loadInterceptors(interceptors, { router, store, i18n, message })
|
|
21
|
+
// 加载路由
|
|
22
|
+
loadRoutes()
|
|
23
|
+
// 加载路由守卫
|
|
24
|
+
loadGuards(guards, { router, store, i18n, message })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default bootstrap
|
package/src/index.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import Vue from 'vue'
|
|
2
2
|
import { initRouter } from './router'
|
|
3
3
|
import { initI18n } from './utils/i18n'
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import Viser from 'viser-vue'
|
|
7
|
-
|
|
4
|
+
import bootstrap from './bootstrap'
|
|
5
|
+
import { modules } from './store'
|
|
8
6
|
import './theme/index.less'
|
|
9
7
|
import './base-client/all'
|
|
10
8
|
import './utils/filter'
|
|
11
|
-
import bootstrap from './bootstrap'
|
|
12
9
|
import 'animate.css/source/animate.css'
|
|
13
10
|
import 'moment/locale/zh-cn'
|
|
11
|
+
import Antd from 'ant-design-vue'
|
|
12
|
+
import Viser from 'viser-vue'
|
|
14
13
|
|
|
15
|
-
const
|
|
14
|
+
const routerOptions = initRouter(modules.setting.state.asyncRoutes)
|
|
16
15
|
const i18n = initI18n('CN', 'US')
|
|
17
16
|
|
|
18
17
|
Vue.use(Antd)
|
|
@@ -21,8 +20,8 @@ Vue.config.productionTip = false
|
|
|
21
20
|
const message = Vue.prototype.$message
|
|
22
21
|
|
|
23
22
|
export {
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
routerOptions,
|
|
24
|
+
modules,
|
|
26
25
|
i18n,
|
|
27
26
|
message,
|
|
28
27
|
bootstrap
|
package/src/main.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import Vue from 'vue'
|
|
2
2
|
import App from './App.vue'
|
|
3
|
+
import Router from 'vue-router'
|
|
4
|
+
import Vuex from 'vuex'
|
|
3
5
|
import '@/mock'
|
|
4
|
-
import {
|
|
6
|
+
import { routerOptions, modules, i18n, message, bootstrap } from './index'
|
|
7
|
+
|
|
8
|
+
Vue.use(Router)
|
|
9
|
+
Vue.use(Vuex)
|
|
10
|
+
// 创建router store
|
|
11
|
+
const store = new Vuex.Store({ modules })
|
|
12
|
+
const router = new Router(routerOptions)
|
|
5
13
|
|
|
6
14
|
bootstrap({ router, store, i18n, message })
|
|
7
15
|
new Vue({
|
package/src/router/index.js
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
formatRoutes(options.routes)
|
|
29
|
-
return new Router(options)
|
|
30
|
-
}
|
|
31
|
-
export { loginIgnore, initRouter }
|
|
1
|
+
import { formatRoutes } from '@/utils/routerUtil'
|
|
2
|
+
|
|
3
|
+
// 不需要登录拦截的路由配置
|
|
4
|
+
const loginIgnore = {
|
|
5
|
+
names: ['404', '403'], // 根据路由名称匹配
|
|
6
|
+
paths: ['/login'], // 根据路由fullPath匹配
|
|
7
|
+
/**
|
|
8
|
+
* 判断路由是否包含在该配置中
|
|
9
|
+
* @param route vue-router 的 route 对象
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
12
|
+
includes (route) {
|
|
13
|
+
return this.names.includes(route.name) || this.paths.includes(route.path)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 初始化路由实例
|
|
19
|
+
* @param isAsync 是否异步路由模式
|
|
20
|
+
* @returns {RouterOptions}
|
|
21
|
+
*/
|
|
22
|
+
function initRouter (isAsync) {
|
|
23
|
+
const options = isAsync ? require('./async/config.async').default : require('./config').default
|
|
24
|
+
formatRoutes(options.routes)
|
|
25
|
+
return options
|
|
26
|
+
}
|
|
27
|
+
export { loginIgnore, initRouter }
|
package/src/store/index.js
CHANGED
package/src/utils/request.js
CHANGED
|
@@ -1,197 +1,197 @@
|
|
|
1
|
-
import axios from 'axios'
|
|
2
|
-
import Cookie from 'js-cookie'
|
|
3
|
-
import
|
|
4
|
-
import { ACCESS_TOKEN } from '@/store/mutation-types'
|
|
5
|
-
import notification from 'ant-design-vue/es/notification'
|
|
6
|
-
|
|
7
|
-
// 跨域认证信息 header 名
|
|
8
|
-
const xsrfHeaderName = 'Authorization'
|
|
9
|
-
|
|
10
|
-
axios.defaults.timeout = 5000
|
|
11
|
-
axios.defaults.withCredentials = true
|
|
12
|
-
axios.defaults.xsrfHeaderName = xsrfHeaderName
|
|
13
|
-
axios.defaults.xsrfCookieName = xsrfHeaderName
|
|
14
|
-
|
|
15
|
-
// 认证类型
|
|
16
|
-
const AUTH_TYPE = {
|
|
17
|
-
BEARER: 'Bearer',
|
|
18
|
-
BASIC: 'basic',
|
|
19
|
-
AUTH1: 'auth1',
|
|
20
|
-
AUTH2: 'auth2'
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// http method
|
|
24
|
-
const METHOD = {
|
|
25
|
-
GET: 'get',
|
|
26
|
-
POST: 'post'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* axios请求
|
|
31
|
-
* @param url 请求地址
|
|
32
|
-
* @param method {METHOD} http method
|
|
33
|
-
* @param params 请求参数
|
|
34
|
-
* @returns {Promise<AxiosResponse<T>>}
|
|
35
|
-
*/
|
|
36
|
-
async function request (url, method, params, config) {
|
|
37
|
-
switch (method) {
|
|
38
|
-
case METHOD.GET:
|
|
39
|
-
return axios.get(url, { params, ...config })
|
|
40
|
-
case METHOD.POST:
|
|
41
|
-
return axios.post(url, params, config)
|
|
42
|
-
default:
|
|
43
|
-
return axios.get(url, { params, ...config })
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 设置认证信息
|
|
49
|
-
* @param auth {Object}
|
|
50
|
-
* @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
|
|
51
|
-
*/
|
|
52
|
-
function setAuthorization (auth, authType = AUTH_TYPE.BEARER) {
|
|
53
|
-
switch (authType) {
|
|
54
|
-
case AUTH_TYPE.BEARER:
|
|
55
|
-
Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, { expires: auth.expireAt })
|
|
56
|
-
break
|
|
57
|
-
case AUTH_TYPE.BASIC:
|
|
58
|
-
case AUTH_TYPE.AUTH1:
|
|
59
|
-
case AUTH_TYPE.AUTH2:
|
|
60
|
-
default:
|
|
61
|
-
break
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 移出认证信息
|
|
67
|
-
* @param authType {AUTH_TYPE} 认证类型
|
|
68
|
-
*/
|
|
69
|
-
function removeAuthorization (authType = AUTH_TYPE.BEARER) {
|
|
70
|
-
switch (authType) {
|
|
71
|
-
case AUTH_TYPE.BEARER:
|
|
72
|
-
Cookie.remove(xsrfHeaderName)
|
|
73
|
-
break
|
|
74
|
-
case AUTH_TYPE.BASIC:
|
|
75
|
-
case AUTH_TYPE.AUTH1:
|
|
76
|
-
case AUTH_TYPE.AUTH2:
|
|
77
|
-
default:
|
|
78
|
-
break
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 检查认证信息
|
|
84
|
-
* @param authType
|
|
85
|
-
* @returns {boolean}
|
|
86
|
-
*/
|
|
87
|
-
function checkAuthorization (authType = AUTH_TYPE.BEARER) {
|
|
88
|
-
switch (authType) {
|
|
89
|
-
case AUTH_TYPE.BEARER:
|
|
90
|
-
if (Cookie.get(xsrfHeaderName)) {
|
|
91
|
-
return true
|
|
92
|
-
}
|
|
93
|
-
break
|
|
94
|
-
case AUTH_TYPE.BASIC:
|
|
95
|
-
case AUTH_TYPE.AUTH1:
|
|
96
|
-
case AUTH_TYPE.AUTH2:
|
|
97
|
-
default:
|
|
98
|
-
break
|
|
99
|
-
}
|
|
100
|
-
return false
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 加载 axios 拦截器
|
|
105
|
-
* @param interceptors
|
|
106
|
-
* @param options
|
|
107
|
-
*/
|
|
108
|
-
function loadInterceptors () {
|
|
109
|
-
// 加载请求拦截器
|
|
110
|
-
axios.interceptors.request.use(config => {
|
|
111
|
-
const token = localStorage.getItem(ACCESS_TOKEN)
|
|
112
|
-
// 如果 token 存在
|
|
113
|
-
// 让每个请求携带自定义 token 请根据实际情况自行修改
|
|
114
|
-
if (token) {
|
|
115
|
-
config.headers['Access-Token'] = token
|
|
116
|
-
}
|
|
117
|
-
if (!config.headers['Content-Type']) {
|
|
118
|
-
config.headers['Content-Type'] = 'application/json;charset=UTF-8'
|
|
119
|
-
}
|
|
120
|
-
return config
|
|
121
|
-
}, errorHandler)
|
|
122
|
-
// 加载响应拦截器
|
|
123
|
-
axios.interceptors.response.use((response) => {
|
|
124
|
-
return response.data
|
|
125
|
-
}, errorHandler)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// 异常拦截处理器
|
|
129
|
-
const errorHandler = (error) => {
|
|
130
|
-
if (error.response) {
|
|
131
|
-
const data = error.response.data
|
|
132
|
-
// 从 localstorage 获取 token
|
|
133
|
-
const token = localStorage.getItem(ACCESS_TOKEN)
|
|
134
|
-
if (error.response.status === 403) {
|
|
135
|
-
notification.error({
|
|
136
|
-
message: '禁止访问',
|
|
137
|
-
description: data
|
|
138
|
-
})
|
|
139
|
-
} else if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
|
|
140
|
-
notification.error({
|
|
141
|
-
message: '鉴权失败',
|
|
142
|
-
description: data
|
|
143
|
-
})
|
|
144
|
-
if (token) {
|
|
145
|
-
store.dispatch('Logout').then(() => {
|
|
146
|
-
setTimeout(() => {
|
|
147
|
-
window.location.reload()
|
|
148
|
-
}, 1500)
|
|
149
|
-
})
|
|
150
|
-
}
|
|
151
|
-
} else if (error.response.status === 500) {
|
|
152
|
-
notification.error({
|
|
153
|
-
message: '系统异常',
|
|
154
|
-
description: data
|
|
155
|
-
})
|
|
156
|
-
} else {
|
|
157
|
-
notification.error({
|
|
158
|
-
message: '数据异常',
|
|
159
|
-
description: data
|
|
160
|
-
})
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return Promise.reject(error)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 解析 url 中的参数
|
|
168
|
-
* @param url
|
|
169
|
-
* @returns {Object}
|
|
170
|
-
*/
|
|
171
|
-
function parseUrlParams (url) {
|
|
172
|
-
const params = {}
|
|
173
|
-
if (!url || url === '' || typeof url !== 'string') {
|
|
174
|
-
return params
|
|
175
|
-
}
|
|
176
|
-
const paramsStr = url.split('?')[1]
|
|
177
|
-
if (!paramsStr) {
|
|
178
|
-
return params
|
|
179
|
-
}
|
|
180
|
-
const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
|
|
181
|
-
for (let i = 0; i < paramsArr.length / 2; i++) {
|
|
182
|
-
const value = paramsArr[i * 2 + 1]
|
|
183
|
-
params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
|
|
184
|
-
}
|
|
185
|
-
return params
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export {
|
|
189
|
-
METHOD,
|
|
190
|
-
AUTH_TYPE,
|
|
191
|
-
request,
|
|
192
|
-
setAuthorization,
|
|
193
|
-
removeAuthorization,
|
|
194
|
-
checkAuthorization,
|
|
195
|
-
loadInterceptors,
|
|
196
|
-
parseUrlParams
|
|
197
|
-
}
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import Cookie from 'js-cookie'
|
|
3
|
+
import Vue from 'vue'
|
|
4
|
+
import { ACCESS_TOKEN } from '@/store/mutation-types'
|
|
5
|
+
import notification from 'ant-design-vue/es/notification'
|
|
6
|
+
|
|
7
|
+
// 跨域认证信息 header 名
|
|
8
|
+
const xsrfHeaderName = 'Authorization'
|
|
9
|
+
|
|
10
|
+
axios.defaults.timeout = 5000
|
|
11
|
+
axios.defaults.withCredentials = true
|
|
12
|
+
axios.defaults.xsrfHeaderName = xsrfHeaderName
|
|
13
|
+
axios.defaults.xsrfCookieName = xsrfHeaderName
|
|
14
|
+
|
|
15
|
+
// 认证类型
|
|
16
|
+
const AUTH_TYPE = {
|
|
17
|
+
BEARER: 'Bearer',
|
|
18
|
+
BASIC: 'basic',
|
|
19
|
+
AUTH1: 'auth1',
|
|
20
|
+
AUTH2: 'auth2'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// http method
|
|
24
|
+
const METHOD = {
|
|
25
|
+
GET: 'get',
|
|
26
|
+
POST: 'post'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* axios请求
|
|
31
|
+
* @param url 请求地址
|
|
32
|
+
* @param method {METHOD} http method
|
|
33
|
+
* @param params 请求参数
|
|
34
|
+
* @returns {Promise<AxiosResponse<T>>}
|
|
35
|
+
*/
|
|
36
|
+
async function request (url, method, params, config) {
|
|
37
|
+
switch (method) {
|
|
38
|
+
case METHOD.GET:
|
|
39
|
+
return axios.get(url, { params, ...config })
|
|
40
|
+
case METHOD.POST:
|
|
41
|
+
return axios.post(url, params, config)
|
|
42
|
+
default:
|
|
43
|
+
return axios.get(url, { params, ...config })
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 设置认证信息
|
|
49
|
+
* @param auth {Object}
|
|
50
|
+
* @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
|
|
51
|
+
*/
|
|
52
|
+
function setAuthorization (auth, authType = AUTH_TYPE.BEARER) {
|
|
53
|
+
switch (authType) {
|
|
54
|
+
case AUTH_TYPE.BEARER:
|
|
55
|
+
Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, { expires: auth.expireAt })
|
|
56
|
+
break
|
|
57
|
+
case AUTH_TYPE.BASIC:
|
|
58
|
+
case AUTH_TYPE.AUTH1:
|
|
59
|
+
case AUTH_TYPE.AUTH2:
|
|
60
|
+
default:
|
|
61
|
+
break
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 移出认证信息
|
|
67
|
+
* @param authType {AUTH_TYPE} 认证类型
|
|
68
|
+
*/
|
|
69
|
+
function removeAuthorization (authType = AUTH_TYPE.BEARER) {
|
|
70
|
+
switch (authType) {
|
|
71
|
+
case AUTH_TYPE.BEARER:
|
|
72
|
+
Cookie.remove(xsrfHeaderName)
|
|
73
|
+
break
|
|
74
|
+
case AUTH_TYPE.BASIC:
|
|
75
|
+
case AUTH_TYPE.AUTH1:
|
|
76
|
+
case AUTH_TYPE.AUTH2:
|
|
77
|
+
default:
|
|
78
|
+
break
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 检查认证信息
|
|
84
|
+
* @param authType
|
|
85
|
+
* @returns {boolean}
|
|
86
|
+
*/
|
|
87
|
+
function checkAuthorization (authType = AUTH_TYPE.BEARER) {
|
|
88
|
+
switch (authType) {
|
|
89
|
+
case AUTH_TYPE.BEARER:
|
|
90
|
+
if (Cookie.get(xsrfHeaderName)) {
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
break
|
|
94
|
+
case AUTH_TYPE.BASIC:
|
|
95
|
+
case AUTH_TYPE.AUTH1:
|
|
96
|
+
case AUTH_TYPE.AUTH2:
|
|
97
|
+
default:
|
|
98
|
+
break
|
|
99
|
+
}
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 加载 axios 拦截器
|
|
105
|
+
* @param interceptors
|
|
106
|
+
* @param options
|
|
107
|
+
*/
|
|
108
|
+
function loadInterceptors () {
|
|
109
|
+
// 加载请求拦截器
|
|
110
|
+
axios.interceptors.request.use(config => {
|
|
111
|
+
const token = localStorage.getItem(ACCESS_TOKEN)
|
|
112
|
+
// 如果 token 存在
|
|
113
|
+
// 让每个请求携带自定义 token 请根据实际情况自行修改
|
|
114
|
+
if (token) {
|
|
115
|
+
config.headers['Access-Token'] = token
|
|
116
|
+
}
|
|
117
|
+
if (!config.headers['Content-Type']) {
|
|
118
|
+
config.headers['Content-Type'] = 'application/json;charset=UTF-8'
|
|
119
|
+
}
|
|
120
|
+
return config
|
|
121
|
+
}, errorHandler)
|
|
122
|
+
// 加载响应拦截器
|
|
123
|
+
axios.interceptors.response.use((response) => {
|
|
124
|
+
return response.data
|
|
125
|
+
}, errorHandler)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 异常拦截处理器
|
|
129
|
+
const errorHandler = (error) => {
|
|
130
|
+
if (error.response) {
|
|
131
|
+
const data = error.response.data
|
|
132
|
+
// 从 localstorage 获取 token
|
|
133
|
+
const token = localStorage.getItem(ACCESS_TOKEN)
|
|
134
|
+
if (error.response.status === 403) {
|
|
135
|
+
notification.error({
|
|
136
|
+
message: '禁止访问',
|
|
137
|
+
description: data
|
|
138
|
+
})
|
|
139
|
+
} else if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
|
|
140
|
+
notification.error({
|
|
141
|
+
message: '鉴权失败',
|
|
142
|
+
description: data
|
|
143
|
+
})
|
|
144
|
+
if (token) {
|
|
145
|
+
Vue.$store.dispatch('Logout').then(() => {
|
|
146
|
+
setTimeout(() => {
|
|
147
|
+
window.location.reload()
|
|
148
|
+
}, 1500)
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
} else if (error.response.status === 500) {
|
|
152
|
+
notification.error({
|
|
153
|
+
message: '系统异常',
|
|
154
|
+
description: data
|
|
155
|
+
})
|
|
156
|
+
} else {
|
|
157
|
+
notification.error({
|
|
158
|
+
message: '数据异常',
|
|
159
|
+
description: data
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return Promise.reject(error)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 解析 url 中的参数
|
|
168
|
+
* @param url
|
|
169
|
+
* @returns {Object}
|
|
170
|
+
*/
|
|
171
|
+
function parseUrlParams (url) {
|
|
172
|
+
const params = {}
|
|
173
|
+
if (!url || url === '' || typeof url !== 'string') {
|
|
174
|
+
return params
|
|
175
|
+
}
|
|
176
|
+
const paramsStr = url.split('?')[1]
|
|
177
|
+
if (!paramsStr) {
|
|
178
|
+
return params
|
|
179
|
+
}
|
|
180
|
+
const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
|
|
181
|
+
for (let i = 0; i < paramsArr.length / 2; i++) {
|
|
182
|
+
const value = paramsArr[i * 2 + 1]
|
|
183
|
+
params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
|
|
184
|
+
}
|
|
185
|
+
return params
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export {
|
|
189
|
+
METHOD,
|
|
190
|
+
AUTH_TYPE,
|
|
191
|
+
request,
|
|
192
|
+
setAuthorization,
|
|
193
|
+
removeAuthorization,
|
|
194
|
+
checkAuthorization,
|
|
195
|
+
loadInterceptors,
|
|
196
|
+
parseUrlParams
|
|
197
|
+
}
|