perf:增加base64Key

This commit is contained in:
yz
2023-03-16 21:36:08 +08:00
parent 50aaf2957e
commit 4a60f7256b
2 changed files with 98 additions and 91 deletions

View File

@@ -21,6 +21,16 @@ const codeMessage = {
504: "网关超时。", 504: "网关超时。",
}; };
/**
* 默认配置
* @type {{prefix: string, encryptBody: boolean}}
*/
const defaultConfig = {
encryptBody: true,
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
prefix: "",
}
/** /**
* 异常处理程序 * 异常处理程序
*/ */
@@ -44,12 +54,10 @@ const request = extend({
/** /**
* 请求配置 * 获取网络请求配置
* @type {{encryptBody: boolean}}
*/ */
let httpConfig = { const getHttpConfig = () => {
encryptBody: true, return window.httpConfig || defaultConfig;
prefix: "",
} }
@@ -87,8 +95,8 @@ const dealParamsBody = (options) => {
* @returns {Promise<<any>>} * @returns {Promise<<any>>}
*/ */
async function req(url, options) { async function req(url, options) {
if (httpConfig.prefix) { if (getHttpConfig().prefix) {
url = httpConfig.prefix + url; url = getHttpConfig().prefix + url;
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request(url, { request(url, {
@@ -132,7 +140,7 @@ async function get(url, params = {}) {
async function post(url, data = {}) { async function post(url, data = {}) {
let newData = {} let newData = {}
if (httpConfig.encryptBody) { if (getHttpConfig().encryptBody) {
newData.encryptData = crypto.encrypt(JSON.stringify(data)); newData.encryptData = crypto.encrypt(JSON.stringify(data));
} else { } else {
newData = {...data}; newData = {...data};
@@ -155,8 +163,8 @@ async function form(url, data = {}) {
* 网络请求配置 * 网络请求配置
* @param obj * @param obj
*/ */
function setConfig(obj) { function setConfig(obj = defaultConfig) {
httpConfig = obj; window.httpConfig = {...getHttpConfig(), ...obj};
} }

View File

@@ -1,12 +1,11 @@
const base64js = require("base64-js"); const base64js = require("base64-js");
const keyBase64 = "WmdUzPJXbngVNiaSsQrihg==";
const SM4 = require("./SM4"); const SM4 = require("./SM4");
class Crypto { class Crypto {
constructor() { constructor() {
this.sm4 = new SM4({ this.sm4 = new SM4({
keyBuffer: base64js.toByteArray(keyBase64), keyBuffer: base64js.toByteArray(window.httpConfig.base64Key),
mode: "ecb", mode: "ecb",
cipherType: 'base64' cipherType: 'base64'
}) })