perf:增加base64Key
This commit is contained in:
@@ -4,52 +4,60 @@ const Common = require('../utils/Common');
|
|||||||
const crypto = require("../utils/Crypto");
|
const crypto = require("../utils/Crypto");
|
||||||
|
|
||||||
const codeMessage = {
|
const codeMessage = {
|
||||||
200: "服务器成功返回请求的数据。",
|
200: "服务器成功返回请求的数据。",
|
||||||
201: "新建或修改数据成功。",
|
201: "新建或修改数据成功。",
|
||||||
202: "一个请求已经进入后台排队(异步任务)。",
|
202: "一个请求已经进入后台排队(异步任务)。",
|
||||||
204: "删除数据成功。",
|
204: "删除数据成功。",
|
||||||
400: "发出的请求有错误,服务器没有进行新建或修改数据的操作。",
|
400: "发出的请求有错误,服务器没有进行新建或修改数据的操作。",
|
||||||
401: "用户没有权限(令牌、用户名、密码错误)。",
|
401: "用户没有权限(令牌、用户名、密码错误)。",
|
||||||
403: "用户得到授权,但是访问是被禁止的。",
|
403: "用户得到授权,但是访问是被禁止的。",
|
||||||
404: "发出的请求针对的是不存在的记录,服务器没有进行操作。",
|
404: "发出的请求针对的是不存在的记录,服务器没有进行操作。",
|
||||||
406: "请求的格式不可得。",
|
406: "请求的格式不可得。",
|
||||||
410: "请求的资源被永久删除,且不会再得到的。",
|
410: "请求的资源被永久删除,且不会再得到的。",
|
||||||
422: "当创建一个对象时,发生一个验证错误。",
|
422: "当创建一个对象时,发生一个验证错误。",
|
||||||
500: "服务器发生错误,请检查服务器。",
|
500: "服务器发生错误,请检查服务器。",
|
||||||
502: "网关错误。",
|
502: "网关错误。",
|
||||||
503: "服务不可用,服务器暂时过载或维护。",
|
503: "服务不可用,服务器暂时过载或维护。",
|
||||||
504: "网关超时。",
|
504: "网关超时。",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认配置
|
||||||
|
* @type {{prefix: string, encryptBody: boolean}}
|
||||||
|
*/
|
||||||
|
const defaultConfig = {
|
||||||
|
encryptBody: true,
|
||||||
|
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
|
||||||
|
prefix: "",
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常处理程序
|
* 异常处理程序
|
||||||
*/
|
*/
|
||||||
const errorHandler = (error) => {
|
const errorHandler = (error) => {
|
||||||
const {response = {}} = error;
|
const {response = {}} = error;
|
||||||
let res = {code: -1, message: "参数错误或服务器异常"};
|
let res = {code: -1, message: "参数错误或服务器异常"};
|
||||||
if (!Common.isEmpty(res)) {
|
if (!Common.isEmpty(res)) {
|
||||||
res = {code: response.status, message: codeMessage[response.status] || response.statusText};
|
res = {code: response.status, message: codeMessage[response.status] || response.statusText};
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置request请求时的默认参数
|
* 配置request请求时的默认参数
|
||||||
*/
|
*/
|
||||||
const request = extend({
|
const request = extend({
|
||||||
errorHandler, // 默认错误处理
|
errorHandler, // 默认错误处理
|
||||||
credentials: "include", // 默认请求是否带上cookie,
|
credentials: "include", // 默认请求是否带上cookie,
|
||||||
requestType: "json", // 表单提交post请求
|
requestType: "json", // 表单提交post请求
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求配置
|
* 获取网络请求配置
|
||||||
* @type {{encryptBody: boolean}}
|
|
||||||
*/
|
*/
|
||||||
let httpConfig = {
|
const getHttpConfig = () => {
|
||||||
encryptBody: true,
|
return window.httpConfig || defaultConfig;
|
||||||
prefix: "",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -58,26 +66,26 @@ let httpConfig = {
|
|||||||
* @param options
|
* @param options
|
||||||
*/
|
*/
|
||||||
const dealParamsBody = (options) => {
|
const dealParamsBody = (options) => {
|
||||||
// 存在分页器的时候
|
// 存在分页器的时候
|
||||||
if (options.params?.pagination) {
|
if (options.params?.pagination) {
|
||||||
options.params.length = options.params.pagination.pageSize;
|
options.params.length = options.params.pagination.pageSize;
|
||||||
options.params.start = options.params.length * (options.params.pagination.current - 1);
|
options.params.start = options.params.length * (options.params.pagination.current - 1);
|
||||||
delete options.params['pagination'];
|
delete options.params['pagination'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// equals 处理
|
// equals 处理
|
||||||
if (options.params && options.params.equals) {
|
if (options.params && options.params.equals) {
|
||||||
let ids = [];
|
let ids = [];
|
||||||
let equalsObj = options.params.equals;
|
let equalsObj = options.params.equals;
|
||||||
Object.keys(equalsObj).map((key) => {
|
Object.keys(equalsObj).map((key) => {
|
||||||
if (!Common.isEmpty(equalsObj[key])) {
|
if (!Common.isEmpty(equalsObj[key])) {
|
||||||
ids.push(key + '=' + equalsObj[key]);
|
ids.push(key + '=' + equalsObj[key]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
options.params.equals = ids.join(',');
|
options.params.equals = ids.join(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,32 +95,32 @@ 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, {
|
||||||
...dealParamsBody(options),
|
...dealParamsBody(options),
|
||||||
headers: {
|
headers: {
|
||||||
...options.headers, token: Storage.getUserToken() || '',
|
...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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.then((res) => {
|
||||||
reject({code: err.data.status, message: err.data.error});
|
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
|
* @param params
|
||||||
*/
|
*/
|
||||||
async function get(url, 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 = {}) {
|
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};
|
||||||
}
|
}
|
||||||
|
|
||||||
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<unknown>}
|
* @returns {Promise<unknown>}
|
||||||
*/
|
*/
|
||||||
async function form(url, data = {}) {
|
async function form(url, data = {}) {
|
||||||
return req(url, {method: 'POST', data, requestType: 'form'});
|
return req(url, {method: 'POST', data, requestType: 'form'});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络请求配置
|
* 网络请求配置
|
||||||
* @param obj
|
* @param obj
|
||||||
*/
|
*/
|
||||||
function setConfig(obj) {
|
function setConfig(obj = defaultConfig) {
|
||||||
httpConfig = obj;
|
window.httpConfig = {...getHttpConfig(), ...obj};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
req,
|
req,
|
||||||
get,
|
get,
|
||||||
post,
|
post,
|
||||||
form,
|
form,
|
||||||
setConfig,
|
setConfig,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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'
|
||||||
})
|
})
|
||||||
@@ -31,4 +30,4 @@ class Crypto {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new Crypto();
|
module.exports = new Crypto();
|
||||||
|
|||||||
Reference in New Issue
Block a user