From fb5d3e8ee80da8187f16e502d515533988c89f7c Mon Sep 17 00:00:00 2001 From: alan Date: Tue, 7 Jan 2020 11:13:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20DEC-11797=20=E6=94=B9=E7=94=A8dec?= =?UTF-8?q?=E4=B8=AD=E8=87=AA=E5=B8=A6=E7=9A=84http=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/constants/env.ts | 5 +- src/modules/crud/crud.service.ts | 126 +++++-------------------------- src/request.ts | 118 +++++++++++++++++++++++++++++ types/globals.d.ts | 14 +++- webpack/webpack.dev.js | 2 +- 5 files changed, 155 insertions(+), 110 deletions(-) create mode 100644 src/request.ts diff --git a/src/modules/constants/env.ts b/src/modules/constants/env.ts index 3030fbb..8ff0057 100644 --- a/src/modules/constants/env.ts +++ b/src/modules/constants/env.ts @@ -1,5 +1,6 @@ -const fineServletURL = Dec.fineServletURL; -export const ReqPrefix = `${fineServletURL}/v10/config/connection`; +export const fineServletURL = Dec.fineServletURL; +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 PluginImgPrefix = `${fineServletURL}/resources?path=`; diff --git a/src/modules/crud/crud.service.ts b/src/modules/crud/crud.service.ts index 483b918..a9282ff 100644 --- a/src/modules/crud/crud.service.ts +++ b/src/modules/crud/crud.service.ts @@ -1,121 +1,35 @@ -import 'es6-promise/auto'; -import axios, { AxiosResponse, AxiosError } from 'axios'; -import { CrudReqOpts, CrudParams, ResultType } from './crud.typings.d'; -import { ReqPrefix, errorCode } from '../constants/env'; -const defaultHeaders = { - 'Content-Type': 'application/json', - 'X-Requested-With': 'XMLHttpRequest', -}; +import { ResultType } from './crud.typings.d'; +import { ReqPath } from '../constants/env'; -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); +export function requestGet(url: string, data?: any): Promise { + return new Promise(resolve => { + Dec.reqGet(`${ReqPath}/${url}`, '', re => { + resolve(re); }); - - return new Promise(() => {}); - } - - return resData; -} - -export async function request(reqOptions: CrudReqOpts = {}): Promise { - 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 = {}) { - const timeStamp = new Date().getTime(); - - return request({ - 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 requestPost(url: string, data = {}): Promise { + return new Promise(resolve => { + Dec.reqPost(`${ReqPath}/${url}`, data, re => { + resolve(re); + }); }); } export function requestDelete(url: string, data = {}) { - return request({ - url, - type: 'DELETE', - data, + return new Promise(resolve => { + Dec.reqDelete(`${ReqPath}/${url}`, data, re => { + resolve(re); + }); }); } -export function requestPut(url: string, data = {}, params: CrudParams = {}) { - return request({ - url, - type: 'PUT', - data, - params, +export function requestPut(url: string, data = {}) { + return new Promise(resolve => { + Dec.reqPut(`${ReqPath}/${url}`, data, re => { + resolve(re); + }); }); } diff --git a/src/request.ts b/src/request.ts new file mode 100644 index 0000000..b7f4d9a --- /dev/null +++ b/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 { + 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)); +}; diff --git a/types/globals.d.ts b/types/globals.d.ts index ea354bf..e761929 100644 --- a/types/globals.d.ts +++ b/types/globals.d.ts @@ -5,4 +5,16 @@ interface Obj { declare let BI: Obj & import('fineui')._BI; declare const Fix: Obj; declare const DecCst: Obj; -declare const Dec: Obj; \ No newline at end of file +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; +}; \ No newline at end of file diff --git a/webpack/webpack.dev.js b/webpack/webpack.dev.js index f7e3bd1..d93e397 100644 --- a/webpack/webpack.dev.js +++ b/webpack/webpack.dev.js @@ -36,7 +36,7 @@ chokidar module.exports = merge(common, { devtool: 'eval-source-map', entry: { - show: ['./src/i18n.ts', './src/index.ts'], + show: ['./src/i18n.ts', './src/request.ts', './src/index.ts'], }, output: { path: dirs.DEST,