53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import axios from 'axios'
|
|
import { BizError } from '@/types/core';
|
|
import {getToken} from '@/service/user-store'
|
|
|
|
const axiosService = axios.create({
|
|
baseURL: '/api/v2',
|
|
timeout: 5000,
|
|
headers: {
|
|
'Authorization': getToken()
|
|
}
|
|
})
|
|
|
|
|
|
export function request<T>(options: RequestOption) {
|
|
return new Promise<T>((resolve, reject) => {
|
|
const { url, method, data, baseURL, getOriginResult } = options;
|
|
|
|
axiosService.request<APIResponse<T>>({
|
|
url,
|
|
method: method || 'get',
|
|
data,
|
|
baseURL,
|
|
}).then(res => {
|
|
if (res.status != 200) {
|
|
reject(new BizError("Service Internal Exception,Please Try Later!", res.status))
|
|
return;
|
|
}
|
|
if (getOriginResult) {
|
|
resolve(res.data as unknown as T)
|
|
return;
|
|
}
|
|
// const
|
|
const { code, message, data } = res.data
|
|
if (code == 0) {
|
|
resolve(data as unknown as T)
|
|
} else {
|
|
reject(new BizError(message, code, data))
|
|
}
|
|
}).catch(e => {
|
|
reject(new BizError(e.message, 500))
|
|
})
|
|
})
|
|
}
|
|
export function post<T>(url:string,data?: any){
|
|
return request<T>({
|
|
url,
|
|
method:'post',
|
|
data
|
|
})
|
|
}
|
|
export function get<T>(url:string){
|
|
return request<T>({url})
|
|
} |