| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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
- // }
- }
|