perf:默认开启加密

This commit is contained in:
曾文豪
2024-04-15 20:03:02 +08:00
parent 35e5fc003c
commit 9efce33810
4 changed files with 24 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@tiesheng/npm-tool", "name": "@tiesheng/npm-tool",
"version": "1.0.15", "version": "1.0.16",
"description": "npm tool package", "description": "npm tool package",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@@ -133,7 +133,7 @@ async function get(url, params = {}, options = {}) {
async function post(url, data = {}, options = {}) { async function post(url, data = {}, options = {}) {
let newData = {} let newData = {}
if (GlobalConfig.getConfig().encryptBody) { if (Storage.getEncryptBody()) {
newData.encryptData = crypto.encrypt(JSON.stringify(data)); newData.encryptData = crypto.encrypt(JSON.stringify(data));
} else { } else {
newData = {...data}; newData = {...data};

View File

@@ -1,9 +1,8 @@
/** /**
* 默认配置 * 默认配置
* @type {{onHttpError: defaultConfig.onHttpError, prefix: string, base64Key: string, encryptBody: boolean}} * @type {{onHttpError: defaultConfig.onHttpError, prefix: string, base64Key: string}}
*/ */
const defaultConfig = { const defaultConfig = {
encryptBody: true,
base64Key: "WmdUzPJXbngVNiaSsQrihg==", base64Key: "WmdUzPJXbngVNiaSsQrihg==",
prefix: "", prefix: "",
onHttpError: (res) => { onHttpError: (res) => {

View File

@@ -7,36 +7,48 @@ const Common = require("./Common");
* @param token * @param token
*/ */
const saveUserToken = (token) => { const saveUserToken = (token) => {
save('token', token); save('token', token);
}; };
const getUserToken = () => { const getUserToken = () => {
return get('token') || ''; return get('token') || '';
}; };
const saveEncryptBody = (bool = true) => {
save("encrypt_body", bool);
}
const getEncryptBody = () => {
return get("encrypt_body", true)
}
/** /**
* 保存数据 * 保存数据
* @param key * @param key
* @param value * @param value
*/ */
const save = (key, value) => { const save = (key, value) => {
localStorage.setItem(key, JSON.stringify({data: value})); localStorage.setItem(key, JSON.stringify({data: value}));
}; };
/** /**
* 获取数据 * 获取数据
* @param key * @param key
* @param def
* @returns {string} * @returns {string}
*/ */
const get = (key) => { const get = (key, def = "") => {
return Common.parseJSON(localStorage.getItem(key), {data: ""}).data; return Common.parseJSON(localStorage.getItem(key), {data: def}).data;
}; };
///////////////////////////////////////// 导出模块 ///////////////////////////////////////// 导出模块
module.exports = { module.exports = {
save, save,
get, get,
saveUserToken, saveUserToken,
getUserToken, getUserToken,
saveEncryptBody,
getEncryptBody,
}; };