@@ -22,6 +22,15 @@ const config = {
2222 } ,
2323}
2424
25+ /**
26+ * 错误码是否是refresh相关
27+ * @param { number } code 错误码
28+ */
29+ function refreshTokenException ( code ) {
30+ const codes = [ 10000 , 10042 , 10050 , 10052 ]
31+ return codes . includes ( code )
32+ }
33+
2534// 创建请求实例
2635const _axios = axios . create ( config )
2736
@@ -34,30 +43,22 @@ _axios.interceptors.request.use(
3443
3544 // step1: 容错处理
3645 if ( ! reqConfig . url ) {
37- /* eslint-disable-next-line */
3846 console . error ( 'request need url' )
3947 throw new Error ( {
4048 source : 'axiosInterceptors' ,
4149 message : 'request need url' ,
4250 } )
4351 }
4452
45- if ( ! reqConfig . method ) {
46- // 默认使用 get 请求
47- reqConfig . method = 'get'
48- }
49- // 大小写容错
50- reqConfig . method = reqConfig . method . toLowerCase ( )
53+ reqConfig . method = reqConfig . method . toLowerCase ( ) // 大小写容错
5154
5255 // 参数容错
5356 if ( reqConfig . method === 'get' ) {
5457 if ( ! reqConfig . params ) {
55- // 防止字段用错
5658 reqConfig . params = reqConfig . data || { }
5759 }
5860 } else if ( reqConfig . method === 'post' ) {
5961 if ( ! reqConfig . data ) {
60- // 防止字段用错
6162 reqConfig . data = reqConfig . params || { }
6263 }
6364
@@ -80,31 +81,24 @@ _axios.interceptors.request.use(
8081 } )
8182 reqConfig . data = formData
8283 }
83- } else {
84- // TODO: 其他类型请求数据格式处理
85- /* eslint-disable-next-line */
86- console . warn ( `其他请求类型: ${ reqConfig . method } , 暂无自动处理` )
8784 }
85+
8886 // step2: permission 处理
8987 if ( reqConfig . url === 'cms/user/refresh' ) {
9088 const refreshToken = getToken ( 'refresh_token' )
9189 if ( refreshToken ) {
92- // eslint-disable-next-line no-param-reassign
9390 reqConfig . headers . Authorization = refreshToken
9491 }
9592 } else {
96- // 有access_token
9793 const accessToken = getToken ( 'access_token' )
9894 if ( accessToken ) {
99- // eslint-disable-next-line no-param-reassign
10095 reqConfig . headers . Authorization = accessToken
10196 }
10297 }
98+
10399 return reqConfig
104100 } ,
105- error => {
106- Promise . reject ( error )
107- } ,
101+ error => Promise . reject ( error ) ,
108102)
109103
110104// Add a response interceptor
@@ -120,16 +114,16 @@ _axios.interceptors.response.use(
120114 const { url } = res . config
121115
122116 // refresh_token 异常,直接登出
123- if ( code === 10000 || code === 10100 ) {
117+ if ( refreshTokenException ( code ) ) {
124118 setTimeout ( ( ) => {
125119 store . dispatch ( 'loginOut' )
126120 const { origin } = window . location
127121 window . location . href = origin
128122 } , 1500 )
129123 return resolve ( null )
130124 }
131- // 令牌相关 ,刷新令牌
132- if ( code === 10040 || code === 10041 || code === 10050 || code === 10051 ) {
125+ // assessToken相关 ,刷新令牌
126+ if ( code === 10041 || code === 10051 ) {
133127 const cache = { }
134128 if ( cache . url !== url ) {
135129 cache . url = url
@@ -140,11 +134,13 @@ _axios.interceptors.response.use(
140134 return resolve ( result )
141135 }
142136 }
143- // 第一种情况:默认直接提示后端返回的异常信息;特殊情况:如果本次请求添加了 handleError: true,用户自己try catch,框架不做处理
137+ // 弹出信息提示的第一种情况:直接提示后端返回的异常信息(框架默认为此配置);
138+ // 特殊情况:如果本次请求添加了 handleError: true,用户自行通过 try catch 处理,框架不做额外处理
144139 if ( res . config . handleError ) {
145140 return reject ( res )
146141 }
147- // 第二种情况:采用前端自己的一套异常提示信息;特殊情况:如果本次请求添加了 showBackend: true, 弹出后端返回错误信息。
142+ // 弹出信息提示的第二种情况:采用前端自己定义的一套异常提示信息(需自行在配置项开启);
143+ // 特殊情况:如果本次请求添加了 showBackend: true, 弹出后端返回错误信息。
148144 if ( Config . useFrontEndErrorMsg && ! res . config . showBackend ) {
149145 // 弹出前端自定义错误信息
150146 const errorArr = Object . entries ( ErrorCode ) . filter ( v => v [ 0 ] === code . toString ( ) )
0 commit comments