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",
"version": "1.0.17",
"version": "1.0.19",
"description": "npm tool package",
"main": "index.js",
"scripts": {

View File

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

View File

@@ -1,4 +1,5 @@
const Common = require("./Common");
const {GlobalConfig} = require("../../index");
///////////////////////////////////////// 用户token
@@ -22,6 +23,19 @@ const getEncryptBody = () => {
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
*/
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}
*/
const get = (key, def = "") => {
return Common.parseJSON(localStorage.getItem(key), {data: def}).data;
return Common.parseJSON(localStorage.getItem(wrapKey(key)), {data: def}).data;
};
///////////////////////////////////////// 导出模块