@@ -3,17 +3,17 @@ import axios from 'axios'
33import { localStorage } from '@/utils/local-storage'
44import { STORAGE_TOKEN_KEY } from '@/stores/mutation-type'
55
6- // 这里是用于设定请求后端时,所用的 Token KEY
7- // 可以根据自己的需要修改,常见的如 Access-Token,Authorization
8- // 需要注意的是,请尽量保证使用中横线 `-` 来作为分隔符,
9- // 避免被 nginx 等负载均衡器丢弃了自定义的请求头
6+ // This is the Token KEY used to set the request backend.
7+ // You can modify it according to your own needs, such as Access-Token,Authorization
8+ // It should be noted that please try to use the horizontal line `-` as the separator
9+ // Avoid discarding custom request headers by load balancers such as nginx
1010export const REQUEST_TOKEN_KEY = 'Access-Token'
1111
12- // 创建 axios 实例
12+ // Create an axios instance
1313const request = axios . create ( {
14- // API 请求的默认前缀
14+ // API Default prefix for requests
1515 baseURL : import . meta. env . VITE_APP_API_BASE_URL ,
16- timeout : 6000 , // 请求超时时间
16+ timeout : 6000 , // Request timeout
1717} )
1818
1919export type RequestError = AxiosError < {
@@ -22,28 +22,28 @@ export type RequestError = AxiosError<{
2222 errorMessage ?: string
2323} >
2424
25- // 异常拦截处理器
25+ // Abnormal interception processor
2626function errorHandler ( error : RequestError ) : Promise < any > {
2727 if ( error . response ) {
2828 const { data = { } , status, statusText } = error . response
29- // 403 无权限
29+ // 403 No permission
3030 if ( status === 403 )
3131 Snackbar ( { type : 'warning' , content : ( data && data . message ) || statusText } )
3232
33- // 401 未登录/未授权
33+ // 401 Not logged in/Unauthorized
3434 if ( status === 401 && data . result && data . result . isLogin )
3535 Snackbar ( { type : 'warning' , content : 'Authorization verification failed' } )
36- // 如果你需要直接跳转登录页面
36+ // If you need to jump directly to the login page
3737 // location.replace(loginRoutePath)
3838 }
3939 return Promise . reject ( error )
4040}
4141
42- // 请求拦截器
42+ // Request interceptor
4343function requestHandler ( config : InternalAxiosRequestConfig ) : InternalAxiosRequestConfig | Promise < InternalAxiosRequestConfig > {
4444 const savedToken = localStorage . get ( STORAGE_TOKEN_KEY )
45- // 如果 token 存在
46- // 让每个请求携带自定义 token, 请根据实际情况修改
45+ // If the token exists
46+ // Let each request carry a custom token, please modify it according to the actual situation.
4747 if ( savedToken )
4848 config . headers [ REQUEST_TOKEN_KEY ] = savedToken
4949
@@ -53,7 +53,7 @@ function requestHandler(config: InternalAxiosRequestConfig): InternalAxiosReques
5353// Add a request interceptor
5454request . interceptors . request . use ( requestHandler , errorHandler )
5555
56- // 响应拦截器
56+ // Response interceptor
5757function responseHandler ( response : { data : any } ) {
5858 return response . data
5959}
0 commit comments