feat(utils): 添加存储前缀配置并更新存储逻辑

This commit is contained in:
曾文豪
2025-09-02 09:10:53 +08:00
parent eb89dd4a48
commit f2ff19f17b
3 changed files with 20 additions and 3 deletions

View File

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

View File

@@ -5,6 +5,9 @@
const defaultConfig = { const defaultConfig = {
base64Key: "WmdUzPJXbngVNiaSsQrihg==", base64Key: "WmdUzPJXbngVNiaSsQrihg==",
prefix: "", prefix: "",
storePrefix: () => {
return "";
},
urlDeal: (url) => { urlDeal: (url) => {
return url; return url;
}, },

View File

@@ -1,4 +1,5 @@
const Common = require("./Common"); const Common = require("./Common");
const {GlobalConfig} = require("../../index");
///////////////////////////////////////// 用户token ///////////////////////////////////////// 用户token
@@ -22,6 +23,19 @@ const getEncryptBody = () => {
return get("encrypt_body", true) return get("encrypt_body", true)
} }
/**
* 重构key
* @param key
* @returns {*}
*/
const wrapKey = (key) => {
const {storePrefix} = GlobalConfig.getConfig()
if (typeof storePrefix == 'function') {
key = storePrefix(key) + key
}
return key
}
/** /**
* 保存数据 * 保存数据
@@ -29,7 +43,7 @@ const getEncryptBody = () => {
* @param value * @param value
*/ */
const save = (key, value) => { const save = (key, value) => {
localStorage.setItem(key, JSON.stringify({data: value})); localStorage.setItem(wrapKey(key), JSON.stringify({data: value}));
}; };
/** /**
@@ -39,7 +53,7 @@ const save = (key, value) => {
* @returns {string} * @returns {string}
*/ */
const get = (key, def = "") => { const get = (key, def = "") => {
return Common.parseJSON(localStorage.getItem(key), {data: def}).data; return Common.parseJSON(localStorage.getItem(wrapKey(key)), {data: def}).data;
}; };
///////////////////////////////////////// 导出模块 ///////////////////////////////////////// 导出模块