帆软决策平台数据连接界面库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
3.3 KiB

import 'es6-promise/auto';
import axios, { AxiosResponse, AxiosError } from 'axios';
import { CrudReqOpts, ResultType } from './modules/crud/crud.typings';
import { fineServletURL, errorCode } from './modules/constants/env';
const defaultHeaders = {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
};
export function paramsSerializer(params: { [key: string]: any }) {
return Object.keys(params || {})
.map(paramKey => {
const paramValue = params[paramKey];
let value = '';
if (BI.isObject(paramValue)) {
value = encodeURIComponent(JSON.stringify(paramValue));
} else {
value = paramValue;
}
return BI.isNull(value) ? '' : `${paramKey}=${value}`;
})
.filter(v => v !== '')
.join('&');
}
function getCookieByName(name: string):string {
let value = null;
const regExpName = new RegExp(name);
document.cookie.split(';').forEach((item: string) => {
if (item.match(regExpName)) {
value = item.split(`${name}=`)[1];
return false;
}
});
return value;
}
function checkStatus(response: AxiosResponse) {
const status = response.status;
const noLoginErr = [errorCode.LOGIN_INFO_ERROR, errorCode.LOGIN_INFO_NOT_AVAILABLE, errorCode.TIMEOUT];
const resData = status === 200
? typeof response.data === 'string'
? BI.jsonDecode(response.data)
: response.data
: {};
if (noLoginErr.includes(BI.get(resData, 'errorCode'))) {
BI.Msg.alert(BI.i18nText('BI-Basic_Prompt'), BI.i18nText('Dec-Dcm_Login_Error'), () => {
window.location.reload(true);
});
return new Promise(() => {});
}
return resData;
}
export async function request(reqOptions: CrudReqOpts = {}): Promise<ResultType> {
const { url, type, headers, data, params } = reqOptions;
return axios
.request({
url,
baseURL: fineServletURL,
method: type,
headers: {
...defaultHeaders,
...headers,
Authorization: `Bearer ${getCookieByName('fine_auth_token')}`,
'Content-Type': 'application/json;charset=UTF-8',
},
params,
paramsSerializer,
data,
})
.then(checkStatus)
.catch((error: AxiosError) => {
console.log(error);
});
}
Dec.reqGet = (url: string, data: any, callback: (re: any) => void) => {
const timeStamp = new Date().getTime();
request({
url: url.includes('?') ? `${url}&_=${timeStamp}` : `${url}?_=${timeStamp}`,
type: 'GET',
data,
}).then(re => callback(re));
};
Dec.reqPost = (url: string, data: any, callback: (re: any) => void) => {
request({
url,
type: 'POST',
data,
}).then(re => callback(re));
};
Dec.reqDelete = (url: string, data: any, callback: (re: any) => void) => {
request({
url,
type: 'DELETE',
data,
}).then(re => callback(re));
};
Dec.reqPut = (url: string, data: any, callback: (re: any) => void) => {
request({
url,
type: 'PUT',
data,
}).then(re => callback(re));
};