@@ -6,6 +6,22 @@ import { useBasicStore } from '@/store/basic'
66const service = axios . create ( )
77let loadingInstance = null //loading实例
88let tempReqUrlSave = ''
9+ let authorTipDoor = true
10+
11+ const noAuthDill = ( ) => {
12+ authorTipDoor = false
13+ ElMessageBox . confirm ( '请重新登录' , {
14+ confirmButtonText : '重新登录' ,
15+ closeOnClickModal : false ,
16+ showCancelButton : false ,
17+ showClose : false ,
18+ type : 'warning'
19+ } ) . then ( ( ) => {
20+ useBasicStore ( ) . resetStateAndToLogin ( )
21+ authorTipDoor = true
22+ } )
23+ }
24+
925//请求前拦截
1026service . interceptors . request . use (
1127 ( req ) => {
@@ -18,10 +34,11 @@ service.interceptors.request.use(
1834 cancel
1935 } )
2036 } )
37+
2138 //设置token到header
22- req . headers [ 'AUTHORIZE_TOKEN ' ] = token
39+ if ( token ) req . headers [ 'Authorization ' ] = token
2340 //如果req.method给get 请求参数设置为 ?name=xxx
24- if ( 'get' . includes ( req . method ?. toLowerCase ( ) ) ) req . params = req . data
41+ if ( 'get' . includes ( req . method ?. toLowerCase ( ) ) && ! req . params ) req . params = req . data
2542
2643 //req loading
2744 // @ts -ignore
@@ -31,7 +48,7 @@ service.interceptors.request.use(
3148 fullscreen : true ,
3249 // spinner: 'CircleCheck',
3350 text : '数据载入中...' ,
34- background : 'rgba(0, 0, 0, 0.3 )'
51+ background : 'rgba(0, 0, 0, 0.1 )'
3552 } )
3653 }
3754 return req
@@ -44,44 +61,52 @@ service.interceptors.request.use(
4461//请求后拦截
4562service . interceptors . response . use (
4663 ( res ) => {
47- useBasicStore ( ) . remotePromiseArrByReqUrl ( tempReqUrlSave ) //从请求集合中移除
64+ //取消请求
65+ useBasicStore ( ) . remotePromiseArrByReqUrl ( tempReqUrlSave )
4866 if ( loadingInstance ) {
4967 loadingInstance && loadingInstance . close ( )
5068 }
5169 //download file
52- if ( [ 'application/zip' , 'zip' , 'blob' , 'arraybuffer' ] . includes ( res . headers [ 'content-type' ] ) ) {
53- return res
70+ const noAuthCode = '401,403'
71+ if ( res . data ?. size ) {
72+ new Response ( res . data ) . text ( ) . then ( ( stringData ) => {
73+ const parseJson = JSON . parse ( stringData )
74+ if ( noAuthCode . includes ( parseJson . code ) ) {
75+ noAuthDill ( )
76+ return
77+ } else {
78+ return res
79+ }
80+ } )
5481 }
5582 const { code, msg } = res . data
5683 const successCode = '0,200,20000'
57- const noAuthCode = '401,403'
5884 if ( successCode . includes ( code ) ) {
5985 return res . data
6086 } else {
61- if ( noAuthCode . includes ( code ) ) {
62- ElMessageBox . confirm ( '请重新登录' , {
63- confirmButtonText : '重新登录' ,
64- closeOnClickModal : false ,
65- showCancelButton : false ,
66- showClose : false ,
67- type : 'warning'
68- } ) . then ( ( ) => {
69- useBasicStore ( ) . resetStateAndToLogin ( )
70- } )
71- }
72- // @ts -ignore
73- if ( ! res . config ?. isNotTipErrorMsg ) {
74- ElMessage . error ( {
75- message : msg ,
76- duration : 2 * 1000
77- } )
87+ //authorTipDoor 防止多个请求 多次alter
88+ if ( authorTipDoor ) {
89+ if ( noAuthCode . includes ( code ) ) {
90+ noAuthDill ( )
91+ } else {
92+ // @ts -ignore
93+ if ( ! res . config ?. isNotTipErrorMsg ) {
94+ ElMessage . error ( {
95+ message : msg ,
96+ duration : 2 * 1000
97+ } )
98+ } else {
99+ return res
100+ }
101+ return Promise . reject ( msg )
102+ }
78103 }
79- return Promise . reject ( msg )
80104 }
81105 } ,
82106 //响应报错
83107 ( err ) => {
84- useBasicStore ( ) . remotePromiseArrByReqUrl ( tempReqUrlSave ) //从请求集合中移除
108+ //取消请求
109+ useBasicStore ( ) . remotePromiseArrByReqUrl ( tempReqUrlSave )
85110 if ( loadingInstance ) {
86111 loadingInstance && loadingInstance . close ( )
87112 }
0 commit comments