feat(frontend): Align admin and user API integration
This commit is contained in:
@@ -2,6 +2,7 @@ import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse,
|
||||
import { useUserStore } from '@/stores/user.ts'
|
||||
import { showToast } from 'vant'
|
||||
import router from '@/router'
|
||||
import { mockRequest, isMockEnabled } from '@/mock'
|
||||
|
||||
// API 基础配置
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api'
|
||||
@@ -92,25 +93,74 @@ service.interceptors.response.use(
|
||||
}
|
||||
)
|
||||
|
||||
// Mock 模式下的请求处理
|
||||
async function mockFetch<T>(method: string, url: string, data?: any, params?: any): Promise<T> {
|
||||
const result = await mockRequest(url, {
|
||||
method,
|
||||
body: data,
|
||||
params
|
||||
})
|
||||
|
||||
if (result.code === 200) {
|
||||
return result.data
|
||||
}
|
||||
|
||||
if (result.code === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
router.push('/login')
|
||||
showToast('登录已过期,请重新登录')
|
||||
throw new Error('Token expired')
|
||||
}
|
||||
|
||||
showToast(result.message || '请求失败')
|
||||
throw new Error(result.message || '请求失败')
|
||||
}
|
||||
|
||||
// 封装请求方法
|
||||
const request = {
|
||||
get<T = any>(url: string, params?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
async get<T = any>(url: string, params?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
// Mock 模式
|
||||
if (isMockEnabled()) {
|
||||
return mockFetch<T>('GET', url, undefined, params)
|
||||
}
|
||||
|
||||
return service.get(url, { params, ...config })
|
||||
},
|
||||
|
||||
post<T = any>(url: string, data?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
async post<T = any>(url: string, data?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
// Mock 模式
|
||||
if (isMockEnabled()) {
|
||||
return mockFetch<T>('POST', url, data)
|
||||
}
|
||||
|
||||
return service.post(url, data, config)
|
||||
},
|
||||
|
||||
put<T = any>(url: string, data?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
async put<T = any>(url: string, data?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
// Mock 模式
|
||||
if (isMockEnabled()) {
|
||||
return mockFetch<T>('PUT', url, data)
|
||||
}
|
||||
|
||||
return service.put(url, data, config)
|
||||
},
|
||||
|
||||
delete<T = any>(url: string, params?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
async delete<T = any>(url: string, params?: object, config?: AxiosRequestConfig): Promise<T> {
|
||||
// Mock 模式
|
||||
if (isMockEnabled()) {
|
||||
return mockFetch<T>('DELETE', url, undefined, params)
|
||||
}
|
||||
|
||||
return service.delete(url, { params, ...config })
|
||||
},
|
||||
|
||||
upload<T = any>(url: string, file: File, onProgress?: (percent: number) => void): Promise<T> {
|
||||
async upload<T = any>(url: string, file: File, onProgress?: (percent: number) => void): Promise<T> {
|
||||
// Mock 模式
|
||||
if (isMockEnabled()) {
|
||||
return mockFetch<T>('POST', url, { file })
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user