/** @format */ import {Log} from '../utils/LogUtils' import {ConstValue} from '../data/ConstValue' export class LocalStorageManager { // 所有服务器通用数据标记 public readonly ONLY_TAG = 'ONLY_TAG:' // 正式环境数据标记 public readonly RELEASE_TAG = 'RELEASE_TAG:' private userID private serverID public getRealKey(curKey: string) { if (!curKey.includes(this.ONLY_TAG) && this.serverID && this.userID) { curKey += `_[${this.serverID}]|[${this.userID}]` } if (ConstValue.DEBUG === true && !curKey.includes(this.RELEASE_TAG)) { curKey += '_debug' } return curKey } init(userID: string, serverID: string) { this.userID = userID this.serverID = serverID } public get(k: string) { const realKey = this.getRealKey(k) const value = cc.sys.localStorage.getItem(realKey) return value } public getDefault(k: string, d: any) { let value = this.get(k) if (value !== d && (value === null || value === '' || value === 'null' || value === 'undefined')) { value = d this.set(k, value) } return value } public getNumber(k: string, d?: number) { let value if (d === undefined) { value = this.get(k) } else if (typeof d === 'number') { value = this.getDefault(k, d) } else { Log.error('getNumber', 'default is not number') return undefined } if (value === null || value === '' || value === 'undefined') { return undefined } return Number(value) } public getNumberByID(k: string, id: string, d?: number) { return this.getNumber(`${k}|ONLY_ID|${id}`, d) } public getString(k: string, d?: string) { let value if (d === undefined) { value = this.get(k) } else if (typeof d === 'string') { value = this.getDefault(k, d) } else { Log.error('getString', 'default is not string') return undefined } return value } public getBool(k: string, d?: boolean) { let value if (d === undefined) { value = this.get(k) } else if (typeof d === 'boolean') { value = this.getDefault(k, d.toString()) } else { Log.error('getBool', 'default is not boolean') return undefined } if (value === 'true') { value = true } else if (value === 'false') { value = false } else if (value === null || value === '' || value === 'undefined') { return undefined } else { Log.warn('getBool', 'key:', k, 'value is not boolean:', value) return undefined } return value } public getObject(k: string, d?: object) { let value if (d === undefined) { value = this.get(k) } else if (typeof d === 'object') { value = this.getDefault(k, JSON.stringify(d)) } else { Log.error('getObject', 'default is not object') return undefined } if (value === null || value === '' || value === 'undefined') { return undefined } return JSON.parse(value) } public set(k: string, v: any) { let value if (typeof v === 'number' || typeof v === 'boolean') { value = v.toString() } else if (typeof v === 'object') { value = JSON.stringify(v) } else if (typeof v === 'string') { value = v } else { return } this.setAyn(k, value) } public setByID(k: string, id: string, v: any) { this.set(`${k}|ONLY_ID|${id}`, v) } public setAyn(k: string, v: string) { const realKey = this.getRealKey(k) switch (cc.sys.platform) { case cc.sys.WECHAT_GAME: // @ts-ignore wx.setStorage({key: realKey, data: v}) break default: cc.sys.localStorage.setItem(realKey, v) break } } public delete(k: string) { const realKey = this.getRealKey(k) cc.sys.localStorage.removeItem(realKey) } public clear() { cc.sys.localStorage.clear() } }