import Plat from "../utils/Palt"; import GameUtil from "../utils/util/GameUtil"; const { ccclass, property } = cc._decorator; @ccclass("LocalManager") export default class LocalData { //#region 基本函数 public static str_datas: { [key: string]: string } = {}; public static getItemToStr(key, val?: string): string { if (this.str_datas[key] == null) { let str = cc.sys.localStorage.getItem(key); if (str == null || str == "") { str = ""; if (val) { str = val; } this.str_datas[key] = str; } else { this.str_datas[key] = str; } } return this.str_datas[key]; } public static setItemToStr(key: string, val: string) { this.str_datas[key] = val; cc.sys.localStorage.setItem(key, val); } public static int_datas: { [key: string]: number } = {}; public static getItemToNumber(key, val?: number): number { if (this.int_datas[key] == null) { let str = cc.sys.localStorage.getItem(key); if (str == null || str == "") { let v = 0; if (val) { v = val; } this.int_datas[key] = v; } else { this.int_datas[key] = parseInt(str); } } return this.int_datas[key]; } public static setItemToNumber(key: string, val: number): any { this.int_datas[key] = val; cc.sys.localStorage.setItem(key, val + ""); } //#endregion public static yx: boolean = true; public static yy: boolean = true; public static set lv(curLevel) { Plat.setRank(curLevel) this.setItemToNumber("curLevel", curLevel) } public static get lv() { return this.getItemToNumber("curLevel", 1); } public static set openid(id) { this.setItemToStr("openid", id); } public static get openid() { return this.getItemToStr("openid"); } private static _userinfo: any = null; public static set userinfo(info) { this._userinfo = info; this.setItemToStr("userinfo", JSON.stringify(info)); } public static get userinfo() { if (this._userinfo == null) { let str = this.getItemToStr("userinfo"); if (str == "") { this._userinfo = null; } else { this._userinfo = JSON.parse(str); } } return this._userinfo; } public static set userid(id) { this.setItemToStr("userid", id); } public static get userid() { let str = this.getItemToStr("userid"); if (str == "") { str = "" + Date.now() + GameUtil.randomRange(0, 10000000); this.setItemToStr("userid", str); } return str; } public static get catSkinList(): number[] { let skinListStr = localStorage.getItem("local_data_list"); if (!skinListStr) { skinListStr = "{}" } const skinList = JSON.parse(skinListStr) return skinList } public static set catSkinList(v: number[]) { } }