153 lines
3.6 KiB
TypeScript
153 lines
3.6 KiB
TypeScript
import {Utils} from '../utils';
|
||
import axios, {AxiosInstance} from 'axios';
|
||
import {Dialog, Toast} from 'vant';
|
||
|
||
export class Axios {
|
||
private instance: AxiosInstance;
|
||
private Utils: Utils = new Utils()
|
||
|
||
// @ts-ignore
|
||
public async constructor() {
|
||
this.instance = axios.create({
|
||
baseURL: Utils.CheckAjaxUrl(),
|
||
timeout: 3000
|
||
});
|
||
this.ResponseInterceptor();
|
||
this.RequestInterceptor();
|
||
console.log(Utils.CheckAjaxUrl());
|
||
}
|
||
|
||
/**
|
||
* Get 请求
|
||
* 请求参数请参考接口:RequestParams
|
||
* @param params
|
||
*/
|
||
public async get(params: { url: string, data: Object }): Promise<Object> {
|
||
try {
|
||
return await this.instance.get(params.url, {params: params.data})
|
||
} catch (e) {
|
||
return e.message
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Post 请求
|
||
* 请求参数请参考接口:RequestParams
|
||
* @param params
|
||
*/
|
||
public async post(params: { url: string, data: Object, header?: Object }): Promise<Object> {
|
||
try {
|
||
return await this.instance.post(params.url, params.data, {
|
||
headers: Object.assign({}, params.header)
|
||
})
|
||
} catch (e) {
|
||
return e.message
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Put 请求
|
||
* 请求参数请参考接口:RequestParams
|
||
* @param params
|
||
*/
|
||
public async put(params: { url: string, data: Object }): Promise<Object> {
|
||
try {
|
||
return await this.instance.put(params.url, params.data)
|
||
} catch (e) {
|
||
return e.message
|
||
}
|
||
}
|
||
|
||
/**
|
||
* delete 请求
|
||
* 请求参数请参考接口:RequestParams
|
||
* @param params
|
||
*/
|
||
public async delete(params: { url: string, data: Object }): Promise<Object> {
|
||
try {
|
||
return await this.instance.delete(params.url, {params: params.data})
|
||
} catch (e) {
|
||
return e.message
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 添加响应拦截器
|
||
* @constructor
|
||
*/
|
||
public async ResponseInterceptor(): Promise<any> {
|
||
this.instance.interceptors.response.use( async response => {
|
||
this.CloseLoadingData();
|
||
switch (response.data.code) {
|
||
case 0:
|
||
switch (response.data.status) {
|
||
case 1:
|
||
if(response.data.data !== null && response.data.data !== void 0){
|
||
return response.data.data;
|
||
}else{
|
||
Toast(response.data.msg)
|
||
}
|
||
break;
|
||
default:
|
||
await Dialog.alert({
|
||
message: `[${response.data.status}]${response.data.msg}`
|
||
})
|
||
return -1
|
||
break;
|
||
}
|
||
break;
|
||
case 1001:
|
||
Dialog.alert({
|
||
message: response.data.msg
|
||
}).then(() => {
|
||
this.Utils.delStorage('access_token')
|
||
window.location.href = `${window.location.origin}${window.location.pathname}#/login`
|
||
});
|
||
break;
|
||
default:
|
||
alert(`[${response.data.code}]${response.data.msg}`)
|
||
break;
|
||
}
|
||
}, function (error) {
|
||
return Promise.reject(error)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 添加请求拦截器
|
||
* @constructor
|
||
*/
|
||
public async RequestInterceptor(): Promise<any> {
|
||
this.instance.interceptors.request.use(config => {
|
||
// 添加token
|
||
Object.assign(config.headers,{
|
||
'authorization': this.Utils.getToken()
|
||
})
|
||
this.OpenLoadingData();
|
||
return config
|
||
}, function (error) {
|
||
return Promise.reject(error)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 显示加载进度
|
||
* @constructor
|
||
*/
|
||
private OpenLoadingData(): void {
|
||
Toast.loading({
|
||
message: '加载中...',
|
||
forbidClick: true,
|
||
loadingType: 'circular'
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 清除加载进度条
|
||
*/
|
||
private CloseLoadingData(): void {
|
||
Toast.clear();
|
||
}
|
||
|
||
}
|