This commit is contained in:
@@ -6,6 +6,45 @@ import type { RequestOptions, ErrorMessageMode } from '@/types/api'
|
||||
import { normalizeApiError } from '@/utils/business/apiError'
|
||||
import { syncCompatibleIdFields } from '@/utils/business/id'
|
||||
|
||||
const normalizeRfc3339Time = (value: unknown, isEndTime: boolean): unknown => {
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString().replace(/\.\d{3}(?=Z$)/, '')
|
||||
}
|
||||
if (typeof value !== 'string' || !value) return value
|
||||
|
||||
const dateOnlyMatch = value.match(/^(\d{4}-\d{2}-\d{2})$/)
|
||||
if (dateOnlyMatch) {
|
||||
return `${dateOnlyMatch[1]}T${isEndTime ? '23:59:59' : '00:00:00'}+08:00`
|
||||
}
|
||||
|
||||
const localDateTimeMatch = value.match(/^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2})(?::(\d{2}))?$/)
|
||||
if (localDateTimeMatch) {
|
||||
return `${localDateTimeMatch[1]}T${localDateTimeMatch[2]}:${localDateTimeMatch[3] || '00'}+08:00`
|
||||
}
|
||||
|
||||
return value.replace(/\.\d{3}(?=Z$|[+-]\d{2}:\d{2}$)/, '')
|
||||
}
|
||||
|
||||
const normalizeTimeQueryParams = (params: unknown) => {
|
||||
if (
|
||||
!params ||
|
||||
typeof params !== 'object' ||
|
||||
Array.isArray(params) ||
|
||||
params instanceof URLSearchParams
|
||||
) {
|
||||
return params
|
||||
}
|
||||
|
||||
const queryParams = params as Record<string, unknown>
|
||||
if (!('start_time' in queryParams) && !('end_time' in queryParams)) return params
|
||||
|
||||
return {
|
||||
...queryParams,
|
||||
start_time: normalizeRfc3339Time(queryParams.start_time, false),
|
||||
end_time: normalizeRfc3339Time(queryParams.end_time, true)
|
||||
}
|
||||
}
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
timeout: 15000, // 请求超时时间(毫秒)
|
||||
baseURL: import.meta.env.DEV ? '' : import.meta.env.VITE_API_URL, // 开发服务器使用代理,其他模式使用完整URL
|
||||
@@ -43,6 +82,7 @@ const axiosInstance = axios.create({
|
||||
// 请求拦截器
|
||||
axiosInstance.interceptors.request.use(
|
||||
(request: InternalAxiosRequestConfig) => {
|
||||
request.params = normalizeTimeQueryParams(request.params)
|
||||
const { accessToken } = useUserStore()
|
||||
|
||||
// 如果 token 存在,则设置请求头
|
||||
|
||||
Reference in New Issue
Block a user