import { getStorage, setStorage, removeStorage } from '../utils/index.js' class base { constructor() { this.storageKey = ''; this.storageInfo = {}; this.defaultInfo = {}; } /** * 装载属性,只会压入配置过的属性 * @param {*} obj */ load(obj) { let info = {}; //合并,后覆盖前,所以必定有默认key Object.assign(info, this.defaultInfo, obj) for (let key in this.defaultInfo) { if (typeof (info[key]) !== 'undefined') { this.storageInfo[key] = info[key]; } } } /** * 追加 * @param {*} obj */ addInfo(obj) { let info = {}; Object.assign(info, this.defaultInfo, this.storageInfo, obj) for (let key in this.defaultInfo) { if (typeof (info[key]) !== 'undefined') { this.storageInfo[key] = info[key]; } } } //获取类里面的属性 getInfo(key) { if (key && (key in this.storageInfo)) { return this.storageInfo[key] } else { return this.storageInfo } } //保存到缓存,传进来>默认值 setStorage(obj) { if (obj) { this.load(obj); } if (!this.storageKey) { return false; } if (Object.getOwnPropertyNames(this.storageInfo).length === 0) { return false; } setStorage(this.storageKey, this.storageInfo); } //追加到缓存,传进来>缓存>默认值 addStorage(obj) { if (!this.storageKey) { return false; } let info = {}; let curStorageInfo = getStorage(this.storageKey); Object.assign(info, curStorageInfo, obj) this.load(obj); setStorage(this.storageKey, this.storageInfo); } //获取缓存的属性 getStorage(key) { if (!this.storageKey) { return false; } let info = getStorage(this.storageKey); Object.assign(this.storageInfo, info) if (key && (key in this.storageInfo)) { return this.storageInfo[key] } else { return this.storageInfo } } //销毁缓存的属性 destroyStorage() { if (!this.storageKey) { return false; } removeStorage(this.storageKey) this.load(); } } export default base;