Browse Source

fix: DEC-11797 改用dec中自带的http请求方法

research/10.0
alan 5 years ago
parent
commit
fb5d3e8ee8
  1. 5
      src/modules/constants/env.ts
  2. 124
      src/modules/crud/crud.service.ts
  3. 118
      src/request.ts
  4. 14
      types/globals.d.ts
  5. 2
      webpack/webpack.dev.js

5
src/modules/constants/env.ts

@ -1,5 +1,6 @@
const fineServletURL = Dec.fineServletURL; export const fineServletURL = Dec.fineServletURL;
export const ReqPrefix = `${fineServletURL}/v10/config/connection`; export const ReqPath = '/v10/config/connection';
export const ReqPrefix = `${fineServletURL}${ReqPath}`;
export const ImgPrefix = `${fineServletURL}/resources?path=/com/fr/web/resources/dist/images/2x/icon/database/`; export const ImgPrefix = `${fineServletURL}/resources?path=/com/fr/web/resources/dist/images/2x/icon/database/`;
export const PluginImgPrefix = `${fineServletURL}/resources?path=`; export const PluginImgPrefix = `${fineServletURL}/resources?path=`;

124
src/modules/crud/crud.service.ts

@ -1,121 +1,35 @@
import 'es6-promise/auto'; import { ResultType } from './crud.typings.d';
import axios, { AxiosResponse, AxiosError } from 'axios'; import { ReqPath } from '../constants/env';
import { CrudReqOpts, CrudParams, ResultType } from './crud.typings.d';
import { ReqPrefix, errorCode } 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 = ''; export function requestGet(url: string, data?: any): Promise<ResultType> {
return new Promise(resolve => {
if (BI.isObject(paramValue)) { Dec.reqGet(`${ReqPath}/${url}`, '', re => {
value = encodeURIComponent(JSON.stringify(paramValue)); resolve(re);
} 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: ReqPrefix,
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, data?: any, params: CrudParams = {}) { export function requestPost(url: string, data = {}): Promise<ResultType> {
const timeStamp = new Date().getTime(); return new Promise(resolve => {
Dec.reqPost(`${ReqPath}/${url}`, data, re => {
return request({ resolve(re);
url: url.includes('?') ? `${url}&_=${timeStamp}` : `${url}?_=${timeStamp}`,
type: 'GET',
data,
params,
}); });
}
export function requestPost(url: string, data = {}, params: CrudParams = {}) {
return request({
url,
type: 'POST',
data,
params,
}); });
} }
export function requestDelete(url: string, data = {}) { export function requestDelete(url: string, data = {}) {
return request({ return new Promise(resolve => {
url, Dec.reqDelete(`${ReqPath}/${url}`, data, re => {
type: 'DELETE', resolve(re);
data, });
}); });
} }
export function requestPut(url: string, data = {}, params: CrudParams = {}) { export function requestPut(url: string, data = {}) {
return request({ return new Promise(resolve => {
url, Dec.reqPut(`${ReqPath}/${url}`, data, re => {
type: 'PUT', resolve(re);
data, });
params,
}); });
} }

118
src/request.ts

@ -0,0 +1,118 @@
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));
};

14
types/globals.d.ts vendored

@ -5,4 +5,16 @@ interface Obj {
declare let BI: Obj & import('fineui')._BI; declare let BI: Obj & import('fineui')._BI;
declare const Fix: Obj; declare const Fix: Obj;
declare const DecCst: Obj; declare const DecCst: Obj;
declare const Dec: Obj; declare const Dec: {
fineServletURL: string;
socket: {
connected: boolean;
};
personal: {
username: string;
};
reqGet: (url: string, data: any, callback: (re: any) => void) => void;
reqPost: (url: string, data: any, callback: (re: any) => void) => void;
reqPut: (url: string, data: any, callback: (re: any) => void) => void;
reqDelete: (url: string, data: any, callback: (re: any) => void) => void;
};

2
webpack/webpack.dev.js

@ -36,7 +36,7 @@ chokidar
module.exports = merge(common, { module.exports = merge(common, {
devtool: 'eval-source-map', devtool: 'eval-source-map',
entry: { entry: {
show: ['./src/i18n.ts', './src/index.ts'], show: ['./src/i18n.ts', './src/request.ts', './src/index.ts'],
}, },
output: { output: {
path: dirs.DEST, path: dirs.DEST,

Loading…
Cancel
Save