// @ts-ignore import cryptoJS from 'crypto-js' const CryptoJSEncUtf8 = cryptoJS.enc.Utf8 const CryptoJSEncHex = cryptoJS.enc.Hex const CryptoJSEncBase64 = cryptoJS.enc.Base64 const CryptoJSAes = cryptoJS.AES const CryptoJSPadPkcs7 = cryptoJS.pad.Pkcs7 const CryptoJSModeCfb = cryptoJS.mode.CFB // 十六位十六进制数作为密钥 const SECRET_KEY = CryptoJSEncUtf8.parse('3333e6e143439152'); // 十六位十六进制数作为密钥偏移量 const SECRET_IV = CryptoJSEncUtf8.parse('e3bbe7e3ba84432a'); // 设置 setStorage const config = { type: 'localStorage', expire: 1000 * 60 * 60 * 24 * 7, // 默认七天有效 prefix: 'CS', isEncrypt: false, // 是否加密 开发的时候不进行加密 } if (window.JeecgComponentConfig?.JStorage?.isEncrypt) { config.isEncrypt = window.JeecgComponentConfig?.JStorage.isEncrypt } interface IStorage { value: T, time: number, expire: number, } /** * 存储 * @param key 键值 * @param value 值 * @param expire 过期时间 为0的时候不过期 */ export function setStorage(key: string, value: T, expire: number = 0) { // if (value === '' || value === null || value === undefined) { // return '' // } // if (isNaN(expire) || expire < 1) throw new Error("Expire must be a number"); expire = (expire ? expire : config.expire) * 60000; let data = { value: value, // 存储值 time: Date.now(), //存值时间戳 expire: expire // 过期时间 } const encryptString = config.isEncrypt ? encrypt(JSON.stringify(data)) : JSON.stringify(data); localDB().setItem(autoAddPrefix(key), encryptString); } export function getStorage(key: string): T | null { let data = localDB().getItem(autoAddPrefix(key)); if (!data) return null; if (config.isEncrypt) { data = decrypt(data) } const newData = JSON.parse((data as string)) as IStorage; if (Date.now() - newData.time > newData.expire && newData.expire !== 0) { // 过期 localDB().removeItem(autoAddPrefix(key)); return null; } else { return newData.value; } } export function removeStorage(key: string) { localDB().removeItem(autoAddPrefix(key)); } // 获取全部 getAllStorage export function getAllStorage(): any[] { let len = localDB().length // 获取长度 let arr = [] // 定义数据集 for (let i = 0; i < len; i++) { // 获取key 索引从0开始 let getKey = localDB().key(i) let getVal = null // 获取key对应的值 if (getKey && getKey.indexOf(config.prefix + '_') === 0) { // @ts-ignore getVal = localDB().getItem(getKey) } // 去掉前缀 if (getKey) { getKey = getKey.replace(config.prefix + '_', '') } // 放进数组 // @ts-ignore arr[i] = {'key': getKey, 'val': getVal,} } return arr } export function clearStorage() { getAllStorage().forEach((item) => { if (item.key && item.key.indexOf(config.prefix + '_') > -1) { localDB().removeItem(item.key) } }) } // 名称前自动添加前缀 const autoAddPrefix = (key: string) => { const prefix = config.prefix ? config.prefix + '_' : ''; return prefix + key; } function localDB(): Storage { if (config.type === 'localStorage') { return localStorage; } else { return sessionStorage; } } /** * 加密方法 * @param data * @returns {string} */ const encrypt = (data: any) => { if (typeof data === 'object') { try { data = JSON.stringify(data); } catch (error) { console.log('encrypt error:', error); } } const dataHex = CryptoJSEncUtf8.parse(data); const encrypted = CryptoJSAes.encrypt(dataHex, SECRET_KEY, { iv: SECRET_IV, mode: CryptoJSModeCfb, padding: CryptoJSPadPkcs7 }); return encrypted.ciphertext.toString(); }; /** * 解密方法 * @param data * @returns {string} */ const decrypt = (data: any) => { const encryptedHexStr = CryptoJSEncHex.parse(data); const str = CryptoJSEncBase64.stringify(encryptedHexStr); const decrypt = CryptoJSAes.decrypt(str, SECRET_KEY, { iv: SECRET_IV, mode: CryptoJSModeCfb, padding: CryptoJSPadPkcs7 }); const decryptedStr = decrypt.toString(CryptoJSEncUtf8); return decryptedStr.toString(); };