diff --git a/src/https/HttpUtil.js b/src/https/HttpUtil.js index b9f9424..15ab28d 100644 --- a/src/https/HttpUtil.js +++ b/src/https/HttpUtil.js @@ -4,52 +4,60 @@ const Common = require('../utils/Common'); const crypto = require("../utils/Crypto"); const codeMessage = { - 200: "服务器成功返回请求的数据。", - 201: "新建或修改数据成功。", - 202: "一个请求已经进入后台排队(异步任务)。", - 204: "删除数据成功。", - 400: "发出的请求有错误,服务器没有进行新建或修改数据的操作。", - 401: "用户没有权限(令牌、用户名、密码错误)。", - 403: "用户得到授权,但是访问是被禁止的。", - 404: "发出的请求针对的是不存在的记录,服务器没有进行操作。", - 406: "请求的格式不可得。", - 410: "请求的资源被永久删除,且不会再得到的。", - 422: "当创建一个对象时,发生一个验证错误。", - 500: "服务器发生错误,请检查服务器。", - 502: "网关错误。", - 503: "服务不可用,服务器暂时过载或维护。", - 504: "网关超时。", + 200: "服务器成功返回请求的数据。", + 201: "新建或修改数据成功。", + 202: "一个请求已经进入后台排队(异步任务)。", + 204: "删除数据成功。", + 400: "发出的请求有错误,服务器没有进行新建或修改数据的操作。", + 401: "用户没有权限(令牌、用户名、密码错误)。", + 403: "用户得到授权,但是访问是被禁止的。", + 404: "发出的请求针对的是不存在的记录,服务器没有进行操作。", + 406: "请求的格式不可得。", + 410: "请求的资源被永久删除,且不会再得到的。", + 422: "当创建一个对象时,发生一个验证错误。", + 500: "服务器发生错误,请检查服务器。", + 502: "网关错误。", + 503: "服务不可用,服务器暂时过载或维护。", + 504: "网关超时。", }; +/** + * 默认配置 + * @type {{prefix: string, encryptBody: boolean}} + */ +const defaultConfig = { + encryptBody: true, + base64Key: "WmdUzPJXbngVNiaSsQrihg==", + prefix: "", +} + /** * 异常处理程序 */ const errorHandler = (error) => { - const {response = {}} = error; - let res = {code: -1, message: "参数错误或服务器异常"}; - if (!Common.isEmpty(res)) { - res = {code: response.status, message: codeMessage[response.status] || response.statusText}; - } - return res; + const {response = {}} = error; + let res = {code: -1, message: "参数错误或服务器异常"}; + if (!Common.isEmpty(res)) { + res = {code: response.status, message: codeMessage[response.status] || response.statusText}; + } + return res; }; /** * 配置request请求时的默认参数 */ const request = extend({ - errorHandler, // 默认错误处理 - credentials: "include", // 默认请求是否带上cookie, - requestType: "json", // 表单提交post请求 + errorHandler, // 默认错误处理 + credentials: "include", // 默认请求是否带上cookie, + requestType: "json", // 表单提交post请求 }); /** - * 请求配置 - * @type {{encryptBody: boolean}} + * 获取网络请求配置 */ -let httpConfig = { - encryptBody: true, - prefix: "", +const getHttpConfig = () => { + return window.httpConfig || defaultConfig; } @@ -58,26 +66,26 @@ let httpConfig = { * @param options */ const dealParamsBody = (options) => { - // 存在分页器的时候 - if (options.params?.pagination) { - options.params.length = options.params.pagination.pageSize; - options.params.start = options.params.length * (options.params.pagination.current - 1); - delete options.params['pagination']; - } + // 存在分页器的时候 + if (options.params?.pagination) { + options.params.length = options.params.pagination.pageSize; + options.params.start = options.params.length * (options.params.pagination.current - 1); + delete options.params['pagination']; + } - // equals 处理 - if (options.params && options.params.equals) { - let ids = []; - let equalsObj = options.params.equals; - Object.keys(equalsObj).map((key) => { - if (!Common.isEmpty(equalsObj[key])) { - ids.push(key + '=' + equalsObj[key]); - } - }); - options.params.equals = ids.join(','); - } + // equals 处理 + if (options.params && options.params.equals) { + let ids = []; + let equalsObj = options.params.equals; + Object.keys(equalsObj).map((key) => { + if (!Common.isEmpty(equalsObj[key])) { + ids.push(key + '=' + equalsObj[key]); + } + }); + options.params.equals = ids.join(','); + } - return options; + return options; }; /** @@ -87,32 +95,32 @@ const dealParamsBody = (options) => { * @returns {Promise<>} */ async function req(url, options) { - if (httpConfig.prefix) { - url = httpConfig.prefix + url; - } - return new Promise((resolve, reject) => { - request(url, { - ...dealParamsBody(options), - headers: { - ...options.headers, token: Storage.getUserToken() || '', - }, - }) - .then((res) => { - if (res.code === 200) { - let data = res.data; - if (res.encrypt) { - data = crypto.decrypt(data); - data = Common.parseJSON(data, data); - } - resolve(data) - } else { - reject(res) - } + if (getHttpConfig().prefix) { + url = getHttpConfig().prefix + url; + } + return new Promise((resolve, reject) => { + request(url, { + ...dealParamsBody(options), + headers: { + ...options.headers, token: Storage.getUserToken() || '', + }, }) - .catch((err) => { - reject({code: err.data.status, message: err.data.error}); - }); - }); + .then((res) => { + if (res.code === 200) { + let data = res.data; + if (res.encrypt) { + data = crypto.decrypt(data); + data = Common.parseJSON(data, data); + } + resolve(data) + } else { + reject(res) + } + }) + .catch((err) => { + reject({code: err.data.status, message: err.data.error}); + }); + }); } /** @@ -121,7 +129,7 @@ async function req(url, options) { * @param params */ async function get(url, params = {}) { - return req(url, {method: 'GET', params}); + return req(url, {method: 'GET', params}); } /** @@ -131,14 +139,14 @@ async function get(url, params = {}) { */ async function post(url, data = {}) { - let newData = {} - if (httpConfig.encryptBody) { - newData.encryptData = crypto.encrypt(JSON.stringify(data)); - } else { - newData = {...data}; - } + let newData = {} + if (getHttpConfig().encryptBody) { + newData.encryptData = crypto.encrypt(JSON.stringify(data)); + } else { + newData = {...data}; + } - return req(url, {method: 'POST', data: newData}); + return req(url, {method: 'POST', data: newData}); } /** @@ -148,22 +156,22 @@ async function post(url, data = {}) { * @returns {Promise} */ async function form(url, data = {}) { - return req(url, {method: 'POST', data, requestType: 'form'}); + return req(url, {method: 'POST', data, requestType: 'form'}); } /** * 网络请求配置 * @param obj */ -function setConfig(obj) { - httpConfig = obj; +function setConfig(obj = defaultConfig) { + window.httpConfig = {...getHttpConfig(), ...obj}; } module.exports = { - req, - get, - post, - form, - setConfig, + req, + get, + post, + form, + setConfig, } diff --git a/src/utils/Crypto.js b/src/utils/Crypto.js index 520a75d..02902cf 100644 --- a/src/utils/Crypto.js +++ b/src/utils/Crypto.js @@ -1,12 +1,11 @@ const base64js = require("base64-js"); -const keyBase64 = "WmdUzPJXbngVNiaSsQrihg=="; const SM4 = require("./SM4"); class Crypto { constructor() { this.sm4 = new SM4({ - keyBuffer: base64js.toByteArray(keyBase64), + keyBuffer: base64js.toByteArray(window.httpConfig.base64Key), mode: "ecb", cipherType: 'base64' }) @@ -31,4 +30,4 @@ class Crypto { } } -module.exports = new Crypto(); \ No newline at end of file +module.exports = new Crypto();