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.
130 lines
3.1 KiB
130 lines
3.1 KiB
import 'es6-promise/auto'; |
|
import axios, { AxiosResponse, AxiosError } from 'axios'; |
|
import { fineServletURL } from '../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 resData = status === 200 |
|
? typeof response.data === 'string' |
|
? BI.jsonDecode(response.data) |
|
: response.data |
|
: {}; |
|
|
|
return resData; |
|
} |
|
|
|
export async function request(reqOptions: CrudReqOpts = {}): Promise<any> { |
|
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); |
|
}); |
|
} |
|
|
|
export function requestGet(url: string, params: CrudParams = {}) { |
|
const timeStamp = new Date().getTime(); |
|
|
|
return request({ |
|
url, |
|
type: 'GET', |
|
params: { |
|
...params, |
|
_: timeStamp, |
|
}, |
|
}); |
|
} |
|
|
|
export function requestPost(url: string, data = {}, params: CrudParams = {}) { |
|
return request({ |
|
url, |
|
type: 'POST', |
|
data, |
|
params, |
|
}); |
|
} |
|
|
|
export function requestDelete(url: string, data = {}) { |
|
return request({ |
|
url, |
|
type: 'DELETE', |
|
data, |
|
}); |
|
} |
|
|
|
export function requestPut(url: string, data = {}, params: CrudParams = {}) { |
|
return request({ |
|
url, |
|
type: 'PUT', |
|
data, |
|
params, |
|
}); |
|
} |
|
|
|
interface CrudReqOpts { |
|
url?: string; |
|
type?: 'GET' | 'POST' | 'DELETE' | 'PUT'; |
|
data?: any; |
|
headers?: { |
|
[key: string]: string; |
|
}; |
|
noProgress?: boolean; |
|
params?: CrudParams; |
|
} |
|
|
|
interface CrudParams { |
|
[key: string]: string | number | { [key: string]: any }; |
|
} |
|
|
|
|