init
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import axios from 'axios'
|
||||
import { clearAdminSession, getAdminToken } from '@/utils/admin-auth'
|
||||
|
||||
export interface ApiEnvelope<T> {
|
||||
code: number
|
||||
msg: string
|
||||
data: T
|
||||
errorCode?: string
|
||||
}
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: '/',
|
||||
timeout: 50_000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
http.interceptors.response.use(
|
||||
(response) => response.data,
|
||||
(error) => {
|
||||
const responseMessage =
|
||||
typeof error?.response?.data?.msg === 'string' ? error.response.data.msg.trim() : ''
|
||||
const fallbackMessage = resolveFallbackHttpMessage(error)
|
||||
const normalizedError = new Error(responseMessage || fallbackMessage) as Error & {
|
||||
errorCode?: string
|
||||
status?: number
|
||||
}
|
||||
|
||||
if (error?.response?.data?.errorCode) {
|
||||
normalizedError.errorCode = String(error.response.data.errorCode)
|
||||
}
|
||||
|
||||
if (typeof error?.response?.status === 'number') {
|
||||
normalizedError.status = error.response.status
|
||||
}
|
||||
|
||||
if (error?.config?.url && String(error.config.url).startsWith('/api/v1/admin')) {
|
||||
const status = Number(error?.response?.status || 0)
|
||||
|
||||
if (status === 401) {
|
||||
clearAdminSession()
|
||||
|
||||
if (window.location.hash.startsWith('#/admin') && !window.location.hash.startsWith('#/admin/login')) {
|
||||
window.location.hash = '#/admin/login'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(normalizedError)
|
||||
},
|
||||
)
|
||||
|
||||
http.interceptors.request.use((config) => {
|
||||
if (String(config.url || '').startsWith('/api/v1/admin')) {
|
||||
const token = getAdminToken()
|
||||
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
|
||||
return config
|
||||
})
|
||||
|
||||
function resolveFallbackHttpMessage(error: unknown) {
|
||||
const message = String((error as { message?: string })?.message ?? '')
|
||||
|
||||
if (message.includes('timeout')) {
|
||||
return '网络超时'
|
||||
}
|
||||
|
||||
if (message === 'Network Error') {
|
||||
return '网络连接错误'
|
||||
}
|
||||
|
||||
if (typeof (error as { response?: { statusText?: string } })?.response?.statusText === 'string') {
|
||||
return String((error as { response: { statusText: string } }).response.statusText).trim()
|
||||
}
|
||||
|
||||
return '接口请求失败'
|
||||
}
|
||||
|
||||
export function apiGet<T>(url: string, params?: Record<string, unknown>) {
|
||||
return http.get<ApiEnvelope<T>>(url, { params }) as unknown as Promise<ApiEnvelope<T>>
|
||||
}
|
||||
|
||||
export function apiGetBlob(url: string, params?: Record<string, unknown>) {
|
||||
return http.get<Blob>(url, {
|
||||
params,
|
||||
responseType: 'blob',
|
||||
}) as unknown as Promise<Blob>
|
||||
}
|
||||
|
||||
export function apiPost<T>(url: string, data?: Record<string, unknown>) {
|
||||
return http.post<ApiEnvelope<T>>(url, data) as unknown as Promise<ApiEnvelope<T>>
|
||||
}
|
||||
|
||||
export function apiDelete<T>(url: string) {
|
||||
return http.delete<ApiEnvelope<T>>(url) as unknown as Promise<ApiEnvelope<T>>
|
||||
}
|
||||
Reference in New Issue
Block a user