37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
import axios from 'axios'
|
|
import type { BaseResponse } from '@/types'
|
|
|
|
const publicPlRequest = axios.create({
|
|
baseURL: import.meta.env.VITE_API_URL || '/api',
|
|
timeout: 60000
|
|
})
|
|
|
|
publicPlRequest.interceptors.response.use(response => {
|
|
const payload = response.data as BaseResponse<unknown>
|
|
if (payload && typeof payload.code === 'number' && payload.code !== 200) {
|
|
return Promise.reject(new Error(payload.message || '请求失败'))
|
|
}
|
|
return response
|
|
})
|
|
|
|
export async function publicPlGet<T>(url: string): Promise<T> {
|
|
const res = await publicPlRequest.get<BaseResponse<T>>(url)
|
|
return (res.data?.data ?? res.data) as T
|
|
}
|
|
|
|
export async function publicPlPost<T>(url: string, data?: unknown): Promise<T> {
|
|
const res = await publicPlRequest.post<BaseResponse<T>>(url, data)
|
|
return (res.data?.data ?? res.data) as T
|
|
}
|
|
|
|
export async function publicPlUpload<T>(url: string, file: File): Promise<T> {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
const res = await publicPlRequest.post<BaseResponse<T>>(url, formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' }
|
|
})
|
|
return (res.data?.data ?? res.data) as T
|
|
}
|
|
|
|
export default publicPlRequest
|