feat: first commit
This commit is contained in:
commit
8a85cf5b78
64 files changed
+7832
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import axios, { type AxiosInstance } from 'axios'
|
||||
import type { ErrorResponse } from '@/model/base'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { useAppStore } from '@/stores/app'
|
||||
|
||||
const client: AxiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_PREFIX,
|
||||
timeout: 1000
|
||||
})
|
||||
|
||||
const handleError = (errorResponse: ErrorResponse) => {
|
||||
MessagePlugin.error(errorResponse.message)
|
||||
}
|
||||
client.interceptors.response.use(
|
||||
(response) => {
|
||||
if (response.data.code) {
|
||||
handleError(response.data)
|
||||
return Promise.reject(response.data)
|
||||
}
|
||||
return response
|
||||
},
|
||||
(error) => {
|
||||
const errorResponse = error.response.data as ErrorResponse
|
||||
handleError(errorResponse)
|
||||
return Promise.reject(errorResponse)
|
||||
}
|
||||
)
|
||||
|
||||
client.interceptors.request.use((config) => {
|
||||
const appStore = useAppStore()
|
||||
config.headers.set('Authorization', 'Bearer ' + appStore.token)
|
||||
return config
|
||||
})
|
||||
|
||||
export default {
|
||||
get: async <T>(url: string, params?: any) => {
|
||||
const response = await client.get<T>(url, {
|
||||
params
|
||||
})
|
||||
return response.data as T
|
||||
},
|
||||
post: async <T>(url: string, data: Record<string, any>) => {
|
||||
const response = await client.post<T>(url, data)
|
||||
return response.data as T
|
||||
},
|
||||
delete: async (url: string) => {
|
||||
await client.delete(url)
|
||||
},
|
||||
put: async <T>(url: string, data: Record<string, any> = {}) => {
|
||||
const response = await client.put<T>(url, data)
|
||||
return response.data as T
|
||||
},
|
||||
// 增加文件上传的方法
|
||||
uploadFile: async <T>(url: string, file: File, data?: Record<string, any>) => {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
// 如果有其他参数,添加到 FormData 中
|
||||
if (data) {
|
||||
Object.keys(data).forEach((key) => {
|
||||
formData.append(key, data[key])
|
||||
})
|
||||
}
|
||||
|
||||
const response = await client.post<T>(url, formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
})
|
||||
|
||||
return response.data as T
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user