perf:新增GlobalConfig,发布版本1.0.4

This commit is contained in:
曾文豪
2023-03-17 12:43:47 +08:00
parent 580d7c5d62
commit 561be80efb
3 changed files with 34 additions and 33 deletions

View File

@@ -2,6 +2,7 @@ const {extend} = require('umi-request');
const Storage = require('../utils/Storage');
const Common = require('../utils/Common');
const crypto = require("../utils/Crypto");
const GlobalConfig = require("../utils/GlobalConfig");
const codeMessage = {
200: "服务器成功返回请求的数据。",
@@ -21,16 +22,6 @@ const codeMessage = {
504: "网关超时。",
};
/**
* 默认配置
* @type {{prefix: string, encryptBody: boolean}}
*/
const defaultConfig = {
encryptBody: true,
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
prefix: "",
}
/**
* 异常处理程序
*/
@@ -52,15 +43,6 @@ const request = extend({
requestType: "json", // 表单提交post请求
});
/**
* 获取网络请求配置
*/
const getHttpConfig = () => {
return window.httpConfig || defaultConfig;
}
/**
* 通用数据处理
* @param options
@@ -95,8 +77,8 @@ const dealParamsBody = (options) => {
* @returns {Promise<<any>>}
*/
async function req(url, options) {
if (getHttpConfig().prefix) {
url = getHttpConfig().prefix + url;
if (GlobalConfig.getConfig().prefix) {
url = GlobalConfig.getConfig().prefix + url;
}
return new Promise((resolve, reject) => {
request(url, {
@@ -142,7 +124,7 @@ async function get(url, params = {}, options = {}) {
async function post(url, data = {}, options = {}) {
let newData = {}
if (getHttpConfig().encryptBody) {
if (GlobalConfig.getConfig().encryptBody) {
newData.encryptData = crypto.encrypt(JSON.stringify(data));
} else {
newData = {...data};
@@ -162,19 +144,10 @@ async function form(url, data = {}, options = {}) {
return req(url, {...options, method: 'POST', data, requestType: 'form'});
}
/**
* 网络请求配置
* @param obj
*/
function setConfig(obj = defaultConfig) {
window.httpConfig = {...getHttpConfig(), ...obj};
}
module.exports = {
req,
get,
post,
form,
setConfig,
}

View File

@@ -1,17 +1,17 @@
const base64js = require("base64-js");
const SM4 = require("./SM4");
const GlobalConfig = require("../utils/GlobalConfig");
class Crypto {
constructor() {
this.sm4 = new SM4({
keyBuffer: base64js.toByteArray(window.httpConfig.base64Key),
keyBuffer: base64js.toByteArray(GlobalConfig.getConfig().base64Key),
mode: "ecb",
cipherType: 'base64'
})
}
/**
* 加密数据
* @param content

28
src/utils/GlobalConfig.js Normal file
View File

@@ -0,0 +1,28 @@
/**
* 默认配置
* @type {{prefix: string, encryptBody: boolean}}
*/
const defaultConfig = {
encryptBody: true,
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
prefix: "",
}
/**
* 获取网络请求配置
*/
const getConfig = () => {
return window.httpConfig || defaultConfig;
}
/**
* 网络请求配置
* @param obj
*/
function setConfig(obj = defaultConfig) {
window.httpConfig = {...getConfig(), ...obj};
}
module.exports = {
setConfig, getConfig
}