first commit
This commit is contained in:
199
滴滴淘/houtaiguanli/src/core/dao/index.ts
Normal file
199
滴滴淘/houtaiguanli/src/core/dao/index.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
import { Utils } from '../utils/utils';
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { Modal } from 'antd';
|
||||
import Loading from '@/components/axiosLoading';
|
||||
|
||||
/**
|
||||
* 将提示变为Promise可以操作点击确定后再进行下一步
|
||||
* @param msg
|
||||
*/
|
||||
function awaitSuccess(msg: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Modal.success({
|
||||
title: '提示',
|
||||
content: msg,
|
||||
onOk: () => {
|
||||
resolve();
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export class Axios {
|
||||
private instance: AxiosInstance;
|
||||
|
||||
constructor() {
|
||||
this.instance = axios.create({
|
||||
baseURL: Utils.CheckAjaxUrl(),
|
||||
timeout: 1000000,
|
||||
});
|
||||
this.ResponseInterceptor();
|
||||
this.RequestInterceptor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get 请求
|
||||
* 请求参数请参考接口:RequestParams
|
||||
* @param params
|
||||
*/
|
||||
public async get(params: {
|
||||
url: string;
|
||||
data: Object;
|
||||
header?: Object;
|
||||
loading?: boolean;
|
||||
}): Promise<Object> {
|
||||
let data: any = '';
|
||||
params.url == 'login' || params.url == 'reg'
|
||||
? (data = params.data)
|
||||
: (data = Object.assign(params.data, {
|
||||
authorization: localStorage.getItem('token'),
|
||||
}));
|
||||
|
||||
try {
|
||||
if (params.loading) {
|
||||
Loading.start();
|
||||
}
|
||||
return await this.instance.get(params.url, {
|
||||
params: data,
|
||||
headers: Object.assign({}, params.header),
|
||||
});
|
||||
} catch (e) {
|
||||
return e.message;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post 请求
|
||||
* 请求参数请参考接口:RequestParams
|
||||
* @param params
|
||||
*/
|
||||
public async post(params: {
|
||||
url: string;
|
||||
data: Object;
|
||||
header?: Object;
|
||||
loading?: boolean;
|
||||
}): Promise<Object> {
|
||||
let data: any = '';
|
||||
params.url == 'login' || params.url == 'reg'
|
||||
? (data = params.data)
|
||||
: (data = Object.assign(params.data, {
|
||||
authorization: localStorage.getItem('token'),
|
||||
}));
|
||||
|
||||
try {
|
||||
if (params.loading) {
|
||||
Loading.start();
|
||||
}
|
||||
return await this.instance.post(params.url, 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: any) => {
|
||||
this.CloseLoadingData();
|
||||
switch (response.data.code) {
|
||||
case 0:
|
||||
switch (response.data.status) {
|
||||
case 61003:
|
||||
Modal.error({
|
||||
title: '61003 错误提示',
|
||||
content: response.data.msg,
|
||||
});
|
||||
break;
|
||||
case 1:
|
||||
if (response.data.msg == null) return response.data.data;
|
||||
await awaitSuccess(response.data.msg);
|
||||
return response.data.data;
|
||||
default:
|
||||
Modal.error({
|
||||
title: `[${response.data.status}]${response.data.msg}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 1001:
|
||||
Modal.error({
|
||||
title: '温馨提示',
|
||||
content: '登录失效,请重新登录',
|
||||
okText: '确定',
|
||||
onOk: () => {
|
||||
location.replace('#/login');
|
||||
},
|
||||
});
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
// 处理code错误
|
||||
return null;
|
||||
}
|
||||
},
|
||||
function(error: any) {
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求拦截器
|
||||
* @constructor
|
||||
*/
|
||||
public async RequestInterceptor(): Promise<any> {
|
||||
this.OpenLoadingData();
|
||||
this.instance.interceptors.request.use(
|
||||
config => {
|
||||
return config;
|
||||
},
|
||||
function(error) {
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示加载进度
|
||||
* @constructor
|
||||
*/
|
||||
private OpenLoadingData(): void {}
|
||||
|
||||
/**
|
||||
* 清除加载进度条
|
||||
*/
|
||||
private CloseLoadingData(): void {
|
||||
Loading.done();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user