import utilsMgr from './DataDao'; // 数据的service层 export class DataMgr { private static _inst: DataMgr; public static get inst(): DataMgr { if (this._inst == null) { this._inst = new DataMgr(); } return this._inst; } // 判断本地有没有数据,这个是做是否使用显示数据用的 private use = 0 // 这个是将要保存到服务端的数据 private _tempStorages: { [key: string]: any } = {}; // 这个是不会保存到服务端的数据 private NolStorage: { [key: string]: any } = {}; remove(key: string) { this._tempStorages[key]= {}; utilsMgr.remove(key); // sys.localStorage.removeItem(key); } // 测试用 clearData(){ utilsMgr.clear() utilsMgr.save('DataMgr__isUse',0); this.use = 1 this._tempStorages= {}; // 这个是不会保存到服务端的数据 this.NolStorage = {}; } set(key: string, val: any) { const name = key; utilsMgr.save(name, val); this._tempStorages[name] = val; return val; } setNol(key: string, val: any) { const name = key; utilsMgr.save(name, val); this.NolStorage[name] = val; return val; } getNol(key: string) { const name = key || null; if (!name) { return null } const res = this.NolStorage[name]; if (this.NolStorage.hasOwnProperty(name)) { return res; } else { let data = utilsMgr.load(name); this.NolStorage[name] = data; return data; } } get(key: string) { const name = key || null; if (!name) { return null } const res = this._tempStorages[name]; if (this._tempStorages.hasOwnProperty(name)) { return res; } else { let data = utilsMgr.load(name); this._tempStorages[name] = data; return data; } } upload() { chsdk.saveGameData("my_key",this._tempStorages) } download(callback?: (ret) => void) { chsdk.loadGameData("my_key").then((ret) => { if(ret.code == chsdk.code.success ) { let onlinedata = ret.data||{} let onlineWight = onlinedata["DataMgr__Weight"] ||0 console.log('localWight==>',this.getWeight()) console.log('onlineWight==>',onlineWight) if(onlineWight>this.getWeight()){ this._tempStorages = onlinedata // // 刷新权重 // this.setWeight(onlineWight) } } callback(ret) }) } setWeight(wight:number){ this._tempStorages["DataMgr__Weight"] = wight this.setNol("DataMgr__Weight",wight) } getWeight(){ return this.get("DataMgr__Weight")||0 } // isUse() { // if (!this.use) { // this.use = utilsMgr.load('DataMgr__isUse') // } // return this.use // } }