mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 00:57:13 +08:00 
			
		
		
		
	refactor: 完善前端 axios 请求响应拦截器
This commit is contained in:
		| @@ -143,13 +143,13 @@ public class GlobalExceptionHandler { | |||||||
|         String errorMsg; |         String errorMsg; | ||||||
|         switch (e.getType()) { |         switch (e.getType()) { | ||||||
|             case NotLoginException.KICK_OUT: |             case NotLoginException.KICK_OUT: | ||||||
|                 errorMsg = "您已被踢下线"; |                 errorMsg = "您已被踢下线。"; | ||||||
|                 break; |                 break; | ||||||
|             case NotLoginException.BE_REPLACED_MESSAGE: |             case NotLoginException.BE_REPLACED_MESSAGE: | ||||||
|                 errorMsg = "您已被顶下线"; |                 errorMsg = "您已被顶下线。"; | ||||||
|                 break; |                 break; | ||||||
|             default: |             default: | ||||||
|                 errorMsg = "登录状态已过期,请重新登录"; |                 errorMsg = "您的登录状态已过期,请重新登录。"; | ||||||
|                 break; |                 break; | ||||||
|         } |         } | ||||||
|         LogContextHolder.setErrorMsg(errorMsg); |         LogContextHolder.setErrorMsg(errorMsg); | ||||||
|   | |||||||
							
								
								
									
										11
									
								
								continew-admin-ui/src/utils/message-error-wrapper.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								continew-admin-ui/src/utils/message-error-wrapper.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | import { Message, MessageReturn } from '@arco-design/web-vue'; | ||||||
|  |  | ||||||
|  | let messageInstance: MessageReturn | null; | ||||||
|  | const messageErrorWrapper = (options: any) => { | ||||||
|  |   if (messageInstance) { | ||||||
|  |     messageInstance.close(); | ||||||
|  |   } | ||||||
|  |   messageInstance = Message.error(options); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | export default messageErrorWrapper; | ||||||
							
								
								
									
										11
									
								
								continew-admin-ui/src/utils/modal-error-wrapper.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								continew-admin-ui/src/utils/modal-error-wrapper.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | import { Modal, ModalReturn } from '@arco-design/web-vue'; | ||||||
|  |  | ||||||
|  | let modalInstance: ModalReturn | null; | ||||||
|  | const modalErrorWrapper = (options: any) => { | ||||||
|  |   if (modalInstance) { | ||||||
|  |     modalInstance.close(); | ||||||
|  |   } | ||||||
|  |   modalInstance = Modal.error(options); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | export default modalErrorWrapper; | ||||||
| @@ -1,7 +1,9 @@ | |||||||
| import axios from 'axios'; | import axios from 'axios'; | ||||||
| import type { AxiosRequestConfig, AxiosResponse } from 'axios'; | import type { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||||||
| import { Message } from '@arco-design/web-vue'; | import { useLoginStore } from '@/store'; | ||||||
| import { getToken } from '@/utils/auth'; | import { getToken } from '@/utils/auth'; | ||||||
|  | import modalErrorWrapper from '@/utils/modal-error-wrapper'; | ||||||
|  | import messageErrorWrapper from '@/utils/message-error-wrapper'; | ||||||
|  |  | ||||||
| // default config | // default config | ||||||
| if (import.meta.env.VITE_API_BASE_URL) { | if (import.meta.env.VITE_API_BASE_URL) { | ||||||
| @@ -9,6 +11,14 @@ if (import.meta.env.VITE_API_BASE_URL) { | |||||||
|   axios.defaults.timeout = 60000; // 1 分钟 |   axios.defaults.timeout = 60000; // 1 分钟 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export interface HttpResponse<T = unknown> { | ||||||
|  |   success: boolean; // 是否成功 | ||||||
|  |   code: number; // 状态码 | ||||||
|  |   msg: string; // 状态信息 | ||||||
|  |   data: T; // 返回数据 | ||||||
|  |   timestamp: string; // 时间戳 | ||||||
|  | } | ||||||
|  |  | ||||||
| // request interceptors | // request interceptors | ||||||
| axios.interceptors.request.use( | axios.interceptors.request.use( | ||||||
|   (config: AxiosRequestConfig) => { |   (config: AxiosRequestConfig) => { | ||||||
| @@ -26,14 +36,6 @@ axios.interceptors.request.use( | |||||||
|   } |   } | ||||||
| ); | ); | ||||||
|  |  | ||||||
| export interface HttpResponse<T = unknown> { |  | ||||||
|   success: boolean; // 是否成功 |  | ||||||
|   code: number; // 状态码 |  | ||||||
|   msg: string; // 状态信息 |  | ||||||
|   data: T; // 返回数据 |  | ||||||
|   timestamp: string; // 时间戳 |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // response interceptors | // response interceptors | ||||||
| axios.interceptors.response.use( | axios.interceptors.response.use( | ||||||
|   (response: AxiosResponse<HttpResponse>) => { |   (response: AxiosResponse<HttpResponse>) => { | ||||||
| @@ -45,28 +47,34 @@ axios.interceptors.response.use( | |||||||
|       return response; |       return response; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // 操作成功则直接返回 |  | ||||||
|     const res = response.data; |     const res = response.data; | ||||||
|     if (res.success) { |     if (res.success) { | ||||||
|       return res; |       return res; | ||||||
|     } |     } | ||||||
|     // 操作失败,弹出错误提示 |     return Promise.reject(new Error(res.msg || '未知错误')); | ||||||
|     Message.error({ |  | ||||||
|       content: res.msg, |  | ||||||
|       duration: 3000, |  | ||||||
|     }); |  | ||||||
|     // |  | ||||||
|     // if (res.code === 401) { |  | ||||||
|     //   // 重定向路由到登录页面 |  | ||||||
|     // } |  | ||||||
|     return Promise.reject(new Error(res.msg)); |  | ||||||
|   }, |   }, | ||||||
|   (error) => { |   (error) => { | ||||||
|     const res = error.response.data; |     const { response } = error; | ||||||
|     Message.error({ |     const res = response.data; | ||||||
|       content: res.msg || '网络错误', |     if ([401].includes(res.code) && response.config.url !== '/auth/user/info') { | ||||||
|       duration: 3000, |       modalErrorWrapper({ | ||||||
|     }); |         title: '确认退出', | ||||||
|  |         content: res.msg, | ||||||
|  |         maskClosable: false, | ||||||
|  |         escToClose: false, | ||||||
|  |         okText: '重新登录', | ||||||
|  |         async onOk() { | ||||||
|  |           const userStore = useLoginStore(); | ||||||
|  |           await userStore.logout(); | ||||||
|  |           window.location.reload(); | ||||||
|  |         }, | ||||||
|  |       }); | ||||||
|  |     } else { | ||||||
|  |       messageErrorWrapper({ | ||||||
|  |         content: res.msg || '网络错误', | ||||||
|  |         duration: 5 * 1000, | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|     return Promise.reject(error); |     return Promise.reject(error); | ||||||
|   } |   } | ||||||
| ); | ); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user