123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /** @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()
- }
- }
|