2
2
import Vue from 'vue'
3
3
import axios from 'axios'
4
4
import Config from '@/config'
5
+ import ErrorCode from '@/config/error-code'
5
6
import store from '@/store'
6
7
import { getToken } from '@/lin/utils/token'
7
8
// eslint-disable-next-line import/no-cycle
@@ -21,7 +22,7 @@ const config = {
21
22
// 定义可获得的http响应状态码
22
23
// return true、设置为null或者undefined,promise将resolved,否则将rejected
23
24
validateStatus ( status ) {
24
- return status >= 200 && status < 500
25
+ return status >= 200 && status < 510
25
26
} ,
26
27
}
27
28
@@ -87,7 +88,6 @@ _axios.interceptors.request.use((originConfig) => {
87
88
/* eslint-disable-next-line */
88
89
console . warn ( `其他请求类型: ${ reqConfig . method } , 暂无自动处理` )
89
90
}
90
-
91
91
// step2: auth 处理
92
92
if ( reqConfig . url === 'cms/user/refresh' ) {
93
93
const refreshToken = getToken ( 'refresh_token' )
@@ -110,10 +110,10 @@ _axios.interceptors.request.use((originConfig) => {
110
110
111
111
// Add a response interceptor
112
112
_axios . interceptors . response . use ( async ( res ) => {
113
+ let { error_code, msg } = res . data // eslint-disable-line
113
114
if ( res . status . toString ( ) . charAt ( 0 ) === '2' ) {
114
115
return res . data
115
116
}
116
-
117
117
return new Promise ( async ( resolve , reject ) => {
118
118
// 将本次失败请求保存
119
119
const { params, url, method } = res . config
@@ -122,25 +122,20 @@ _axios.interceptors.response.use(async (res) => {
122
122
url,
123
123
method,
124
124
} )
125
-
126
- // 处理 API 异常
127
- let { error_code, msg } = res . data // eslint-disable-line
128
- if ( msg instanceof Object ) {
129
- let showMsg = ''
130
- Object . getOwnPropertyNames ( msg ) . forEach ( ( key , index ) => {
131
- if ( index === 0 ) {
132
- showMsg = msg [ key ] // 如果是数组,展示第一条
133
- }
134
- } )
135
- msg = showMsg
125
+ // 用户自己try catch
126
+ if ( params . handleError ) {
127
+ reject ( res )
128
+ return
136
129
}
137
- // 如果令牌无效或者是refreshToken相关异常
130
+ // 处理 API 异常
138
131
if ( error_code === 10000 || error_code === 10100 ) {
139
132
setTimeout ( ( ) => {
140
133
store . dispatch ( 'loginOut' )
141
134
const { origin } = window . location
142
135
window . location . href = origin
143
136
} , 1500 )
137
+ resolve ( null )
138
+ return
144
139
}
145
140
// 令牌相关,刷新令牌
146
141
if ( error_code === 10040 || error_code === 10050 ) {
@@ -154,12 +149,20 @@ _axios.interceptors.response.use(async (res) => {
154
149
return
155
150
}
156
151
}
157
-
152
+ const errorArr = Object . entries ( ErrorCode ) . filter ( v => v [ 0 ] === error_code . toString ( ) )
153
+ // 匹配到自定义的错误码
154
+ if ( errorArr . length > 0 ) {
155
+ if ( errorArr [ 0 ] [ 1 ] !== '' ) {
156
+ msg = errorArr [ 0 ] [ 1 ] // eslint-disable-line
157
+ } else {
158
+ msg = ErrorCode [ '777' ]
159
+ }
160
+ }
158
161
Vue . prototype . $message ( {
159
- message : msg || '未知的error_code' ,
162
+ message : msg ,
160
163
type : 'error' ,
161
164
} )
162
- reject ( res . data )
165
+ resolve ( null )
163
166
} )
164
167
} , ( error ) => {
165
168
if ( ! error . response ) {
0 commit comments