perf:新增GlobalConfig,发布版本1.0.4
This commit is contained in:
@@ -2,6 +2,7 @@ const {extend} = require('umi-request');
|
|||||||
const Storage = require('../utils/Storage');
|
const Storage = require('../utils/Storage');
|
||||||
const Common = require('../utils/Common');
|
const Common = require('../utils/Common');
|
||||||
const crypto = require("../utils/Crypto");
|
const crypto = require("../utils/Crypto");
|
||||||
|
const GlobalConfig = require("../utils/GlobalConfig");
|
||||||
|
|
||||||
const codeMessage = {
|
const codeMessage = {
|
||||||
200: "服务器成功返回请求的数据。",
|
200: "服务器成功返回请求的数据。",
|
||||||
@@ -21,16 +22,6 @@ const codeMessage = {
|
|||||||
504: "网关超时。",
|
504: "网关超时。",
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* 默认配置
|
|
||||||
* @type {{prefix: string, encryptBody: boolean}}
|
|
||||||
*/
|
|
||||||
const defaultConfig = {
|
|
||||||
encryptBody: true,
|
|
||||||
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
|
|
||||||
prefix: "",
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常处理程序
|
* 异常处理程序
|
||||||
*/
|
*/
|
||||||
@@ -52,15 +43,6 @@ const request = extend({
|
|||||||
requestType: "json", // 表单提交post请求
|
requestType: "json", // 表单提交post请求
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取网络请求配置
|
|
||||||
*/
|
|
||||||
const getHttpConfig = () => {
|
|
||||||
return window.httpConfig || defaultConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用数据处理
|
* 通用数据处理
|
||||||
* @param options
|
* @param options
|
||||||
@@ -95,8 +77,8 @@ const dealParamsBody = (options) => {
|
|||||||
* @returns {Promise<<any>>}
|
* @returns {Promise<<any>>}
|
||||||
*/
|
*/
|
||||||
async function req(url, options) {
|
async function req(url, options) {
|
||||||
if (getHttpConfig().prefix) {
|
if (GlobalConfig.getConfig().prefix) {
|
||||||
url = getHttpConfig().prefix + url;
|
url = GlobalConfig.getConfig().prefix + url;
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request(url, {
|
request(url, {
|
||||||
@@ -142,7 +124,7 @@ async function get(url, params = {}, options = {}) {
|
|||||||
async function post(url, data = {}, options = {}) {
|
async function post(url, data = {}, options = {}) {
|
||||||
|
|
||||||
let newData = {}
|
let newData = {}
|
||||||
if (getHttpConfig().encryptBody) {
|
if (GlobalConfig.getConfig().encryptBody) {
|
||||||
newData.encryptData = crypto.encrypt(JSON.stringify(data));
|
newData.encryptData = crypto.encrypt(JSON.stringify(data));
|
||||||
} else {
|
} else {
|
||||||
newData = {...data};
|
newData = {...data};
|
||||||
@@ -162,19 +144,10 @@ async function form(url, data = {}, options = {}) {
|
|||||||
return req(url, {...options, method: 'POST', data, requestType: 'form'});
|
return req(url, {...options, method: 'POST', data, requestType: 'form'});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 网络请求配置
|
|
||||||
* @param obj
|
|
||||||
*/
|
|
||||||
function setConfig(obj = defaultConfig) {
|
|
||||||
window.httpConfig = {...getHttpConfig(), ...obj};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
req,
|
req,
|
||||||
get,
|
get,
|
||||||
post,
|
post,
|
||||||
form,
|
form,
|
||||||
setConfig,
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
const base64js = require("base64-js");
|
const base64js = require("base64-js");
|
||||||
const SM4 = require("./SM4");
|
const SM4 = require("./SM4");
|
||||||
|
const GlobalConfig = require("../utils/GlobalConfig");
|
||||||
|
|
||||||
class Crypto {
|
class Crypto {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.sm4 = new SM4({
|
this.sm4 = new SM4({
|
||||||
keyBuffer: base64js.toByteArray(window.httpConfig.base64Key),
|
keyBuffer: base64js.toByteArray(GlobalConfig.getConfig().base64Key),
|
||||||
mode: "ecb",
|
mode: "ecb",
|
||||||
cipherType: 'base64'
|
cipherType: 'base64'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加密数据
|
* 加密数据
|
||||||
* @param content
|
* @param content
|
||||||
|
|||||||
28
src/utils/GlobalConfig.js
Normal file
28
src/utils/GlobalConfig.js
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user