update
This commit is contained in:
@@ -2,8 +2,6 @@ import { api } from '@/utils/request'
|
|||||||
import type { PaginatedData, OrderItemFilters } from '@/types/api'
|
import type { PaginatedData, OrderItemFilters } from '@/types/api'
|
||||||
import type { OrderItemRecord } from '@/stores/order'
|
import type { OrderItemRecord } from '@/stores/order'
|
||||||
|
|
||||||
export type { OrderItemRecord }
|
|
||||||
|
|
||||||
/** 名称映射用的查找表 */
|
/** 名称映射用的查找表 */
|
||||||
interface LookupItem {
|
interface LookupItem {
|
||||||
id: number
|
id: number
|
||||||
|
|||||||
@@ -13,7 +13,10 @@ interface RequestOptions extends RequestInit {
|
|||||||
timeout?: number
|
timeout?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || ''
|
||||||
|
|
||||||
async function request<T = unknown>(url: string, options: RequestOptions = {}): Promise<T> {
|
async function request<T = unknown>(url: string, options: RequestOptions = {}): Promise<T> {
|
||||||
|
url = `${API_BASE_URL}${url}`
|
||||||
const token = tokenGetter?.() ?? localStorage.getItem('access_token')
|
const token = tokenGetter?.() ?? localStorage.getItem('access_token')
|
||||||
|
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
@@ -50,15 +53,18 @@ async function request<T = unknown>(url: string, options: RequestOptions = {}):
|
|||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 401 未授权:保留当前路径作为 redirect 参数
|
// 401 未授权:跳过登录/注册接口(由页面自行处理错误提示),其余接口重定向到登录页
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
localStorage.removeItem('access_token')
|
const isAuthEndpoint = /\/api\/v1\/(login|register)$/.test(url)
|
||||||
localStorage.removeItem('refresh_token')
|
if (!isAuthEndpoint) {
|
||||||
localStorage.removeItem('user')
|
localStorage.removeItem('access_token')
|
||||||
const redirect = encodeURIComponent(
|
localStorage.removeItem('refresh_token')
|
||||||
window.location.pathname + window.location.search,
|
localStorage.removeItem('user')
|
||||||
)
|
const redirect = encodeURIComponent(
|
||||||
window.location.href = `/login?redirect=${redirect}`
|
window.location.pathname + window.location.search,
|
||||||
|
)
|
||||||
|
window.location.href = `/login?redirect=${redirect}`
|
||||||
|
}
|
||||||
throw new ApiError('登录已过期,请重新登录', 401)
|
throw new ApiError('登录已过期,请重新登录', 401)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,4 +64,12 @@ export default defineConfig({
|
|||||||
'@': fileURLToPath(new URL('./src', import.meta.url))
|
'@': fileURLToPath(new URL('./src', import.meta.url))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
server: {
|
||||||
|
proxy: {
|
||||||
|
'/api': {
|
||||||
|
target: 'http://127.0.0.1:9501',
|
||||||
|
changeOrigin: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
-1
@@ -1 +0,0 @@
|
|||||||
{"version":"4.1.0","results":[[":frontend/src/pages/failed-messages/__tests__/index.spec.ts",{"duration":0,"failed":true}]]}
|
|
||||||
Reference in New Issue
Block a user