diff --git a/package.json b/package.json index b4bbe6f..3e12d1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tiesheng/npm-tool", - "version": "1.0.17", + "version": "1.0.19", "description": "npm tool package", "main": "index.js", "scripts": { diff --git a/src/utils/GlobalConfig.js b/src/utils/GlobalConfig.js index bede065..bdb2d9f 100644 --- a/src/utils/GlobalConfig.js +++ b/src/utils/GlobalConfig.js @@ -5,6 +5,9 @@ const defaultConfig = { base64Key: "WmdUzPJXbngVNiaSsQrihg==", prefix: "", + storePrefix: () => { + return ""; + }, urlDeal: (url) => { return url; }, diff --git a/src/utils/Storage.js b/src/utils/Storage.js index ccb3449..6b6db76 100644 --- a/src/utils/Storage.js +++ b/src/utils/Storage.js @@ -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; }; ///////////////////////////////////////// 导出模块