import { BitEncrypt } from "../extentions/BitEncrypt" ; description type StorageVauleType = "number" | "string" | "boolean" | "object" ; interface StorageData { type : StorageVauleType ; value : string | number | boolean | object ; } export class LocalStorage { private static _instance : LocalStorage = null ; public static Instance ( ) { return this . _instance || ( this . _instance = new LocalStorage ( ) ) ; } public key = "VuxiAKihQ0VR9WRe" ; private encrypt ( obj ) { return BitEncrypt . encode ( JSON . stringify ( obj ) , this . key ) ; } private decryption ( word ) { return BitEncrypt . decode ( word , this . key ) ; } public getItem ( key , defaultValue = null ) { let value = cc . sys . localStorage . getItem ( key ) ; if ( value ) { try { let data = this . decryption ( value ) ; let result = JSON . parse ( data ) ; if ( result . type ) { return result . value ; } else { return value ; } } catch ( error ) { return value ; } } else { return defaultValue ; } } public setItem ( key , value ) { let type = typeof value ; if ( type == "number" || type == "string" || type == "boolean" || type == "object" ) { let saveObj = { type : type , value : value } ; try { let data = this . encrypt ( saveObj ) ; cc . sys . localStorage . setItem ( key , data ) ; } catch ( error ) { if ( CC_DEBUG ) cc . error ( error ) ; } } else { if ( CC_DEBUG ) cc . error ( `存储数据类型不支持 当前的存储类型: ${ type }` ) ; } } public removeItem ( key ) { cc . sys . localStorage . removeItem ( key ) ; } }