69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
// 导入Axios
|
|
import type { AxiosInstance, AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
|
|
import axios from 'axios' // pnpm add axios
|
|
import { config } from '@/assets/config';
|
|
|
|
const apiUrl = config.apiBaseUrl;
|
|
|
|
console.log('API Base URL:', apiUrl);
|
|
// 创建axios实例
|
|
const request: AxiosInstance = axios.create({
|
|
//baseURL: 'http://24.47.182.209/Handler',
|
|
//baseURL: 'http://24.47.177.195/Handler',
|
|
// baseURL: 'http://172.16.1.254:10013/Handler',
|
|
|
|
baseURL: apiUrl,
|
|
timeout: 60000,
|
|
headers: {
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
}
|
|
})
|
|
|
|
// // 拦截器request
|
|
// request.interceptors.request.use(
|
|
// (config) => {
|
|
// const token =
|
|
// 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJpYXQiOjE2ODcxNjAyNDB9.3KWLh4m6JJ3ZXFaC0ZUpF4VfMDLlaTnfkOI7bpXBv4s'
|
|
// // localStorage.getItem('token') || '9201591ba0eb36c8abaea2854274f5082'
|
|
// // config.headers.Authorization = token
|
|
// config.headers['token'] = token
|
|
// return config
|
|
// },
|
|
// (error: AxiosError) => {
|
|
// return Promise.reject(error)
|
|
// }
|
|
// )
|
|
// // 拦截器response
|
|
// request.interceptors.response.use(
|
|
// (response: AxiosResponse) => {
|
|
// return response.data
|
|
// },
|
|
// (error: AxiosError) => {
|
|
// return Promise.reject(error)
|
|
// }
|
|
// )
|
|
|
|
// 导出useAxios
|
|
const get = <T = any>(url: string, params: object = {}, option: AxiosRequestConfig = {}): Promise<T> => {
|
|
return request.get(url, { params, ...option })
|
|
}
|
|
|
|
const del = <T = any>(url: string, params: object = {}, option: AxiosRequestConfig = {}): Promise<T> => {
|
|
return request.delete(url, { params, ...option })
|
|
}
|
|
|
|
const post = <T = any>(url: string, data: object = {}, option: AxiosRequestConfig = {}): Promise<T> => {
|
|
return request.post(url, data, option)
|
|
}
|
|
|
|
const put = <T = any>(url: string, data: object = {}, option: AxiosRequestConfig = {}): Promise<T> => {
|
|
return request.put(url, data, option)
|
|
}
|
|
|
|
export default {
|
|
get,
|
|
del,
|
|
post,
|
|
put
|
|
}
|