优化工作-1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import axios, { type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
|
||||
import axios, { type AxiosResponse, type InternalAxiosRequestConfig, type AxiosError } from 'axios'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { showToast } from 'vant'
|
||||
import { apiMonitor } from './monitor'
|
||||
|
||||
function showError(message: string) {
|
||||
const isMobile = window.location.pathname.startsWith('/m')
|
||||
@@ -14,6 +15,72 @@ function showError(message: string) {
|
||||
declare module 'axios' {
|
||||
export interface AxiosRequestConfig {
|
||||
silent?: boolean
|
||||
skipErrorHandler?: boolean
|
||||
showLoading?: boolean
|
||||
_startTime?: number
|
||||
}
|
||||
}
|
||||
|
||||
// 开发环境日志记录
|
||||
function logRequest(config: InternalAxiosRequestConfig) {
|
||||
if (import.meta.env.DEV) {
|
||||
console.log(`[API Request] ${config.method?.toUpperCase()} ${config.url}`, {
|
||||
params: config.params,
|
||||
data: config.data,
|
||||
headers: config.headers,
|
||||
})
|
||||
}
|
||||
|
||||
// 记录请求开始时间
|
||||
config._startTime = Date.now()
|
||||
}
|
||||
|
||||
function logResponse(response: AxiosResponse) {
|
||||
if (import.meta.env.DEV) {
|
||||
const duration = response.config._startTime
|
||||
? Date.now() - response.config._startTime
|
||||
: 0
|
||||
console.log(`[API Response] ${response.config.method?.toUpperCase()} ${response.config.url} (${duration}ms)`, {
|
||||
status: response.status,
|
||||
data: response.data,
|
||||
})
|
||||
}
|
||||
|
||||
// 记录性能指标
|
||||
if (response.config._startTime) {
|
||||
apiMonitor.record({
|
||||
url: response.config.url || '',
|
||||
method: response.config.method?.toUpperCase() || 'GET',
|
||||
duration: Date.now() - response.config._startTime,
|
||||
status: response.status,
|
||||
timestamp: Date.now(),
|
||||
success: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function logError(error: AxiosError) {
|
||||
if (import.meta.env.DEV) {
|
||||
const duration = error.config?._startTime
|
||||
? Date.now() - error.config._startTime
|
||||
: 0
|
||||
console.error(`[API Error] ${error.config?.method?.toUpperCase()} ${error.config?.url} (${duration}ms)`, {
|
||||
status: error.response?.status,
|
||||
message: error.message,
|
||||
data: error.response?.data,
|
||||
})
|
||||
}
|
||||
|
||||
// 记录错误指标
|
||||
if (error.config?._startTime) {
|
||||
apiMonitor.record({
|
||||
url: error.config.url || '',
|
||||
method: error.config.method?.toUpperCase() || 'GET',
|
||||
duration: Date.now() - error.config._startTime,
|
||||
status: error.response?.status || 0,
|
||||
timestamp: Date.now(),
|
||||
success: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +96,10 @@ import type { ApiResponse } from '@/shared/types/types'
|
||||
|
||||
export const apiClient = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 10000,
|
||||
timeout: 30000, // 增加到 30 秒,避免大文件上传超时
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
export async function unwrapData<T>(request: Promise<AxiosResponse<ApiResponse<T>>>) {
|
||||
@@ -50,6 +120,8 @@ type RefreshState = {
|
||||
type RetriableRequestConfig = InternalAxiosRequestConfig & {
|
||||
_retry?: boolean
|
||||
silent?: boolean
|
||||
skipErrorHandler?: boolean
|
||||
showLoading?: boolean
|
||||
}
|
||||
|
||||
const refreshStates: Record<AuthScope, RefreshState> = {
|
||||
@@ -115,15 +187,30 @@ apiClient.interceptors.request.use((config) => {
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
// 记录请求日志
|
||||
logRequest(config)
|
||||
|
||||
return config
|
||||
}, (error) => {
|
||||
logError(error)
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
(response) => {
|
||||
// 记录响应日志
|
||||
logResponse(response)
|
||||
return response
|
||||
},
|
||||
async (error) => {
|
||||
// 记录错误日志
|
||||
logError(error)
|
||||
|
||||
const originalRequest = error.config as RetriableRequestConfig | undefined
|
||||
if (!originalRequest || error?.response?.status !== 401 || originalRequest._retry) {
|
||||
if (originalRequest && !originalRequest.silent) {
|
||||
// 统一错误处理,支持 skipErrorHandler 跳过
|
||||
if (originalRequest && !originalRequest.silent && !originalRequest.skipErrorHandler) {
|
||||
const msg = error.response?.data?.message || error.message || '网络连接异常,请稍后重试'
|
||||
showError(msg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user