import { _decorator, sys } from 'cc'; class DataDao { // deepClone(obj: any): any { // if (obj === null) return null; // null // let clone = Object.assign({}, obj); // clone base type // Object.keys(clone).forEach( // go through // key => (clone[key] = typeof obj[key] === 'object' ? this.deepClone(obj[key]) : obj[key]) // ); // if (Array.isArray(obj)) { // array // clone.length = obj.length; // return Array.from(clone); // } // return clone; // } save(key: any, val: any) { // const jsonStr = JSON.stringify(val); chsdk.storage.set(key,val); // sys.localStorage.setItem(key,jsonStr); } remove(key: string) { chsdk.storage.remove(key); // sys.localStorage.removeItem(key); } clear(){ chsdk.storage.clear(); } /** * @description: load localstorage data, * @return {*} */ load(key: string) { // const jsonStr = sys.localStorage.getItem(key); const userData = chsdk.storage.getObject(key); // const userData = jsonStr == 'undefined' ||!jsonStr? undefined :JSON.parse(jsonStr); return userData } getBoolean(key: string) { // const jsonStr = sys.localStorage.getItem(key); const userData = chsdk.storage.getBoolean(key); // const userData = jsonStr == 'undefined' ||!jsonStr? undefined :JSON.parse(jsonStr); return userData } // traverseObject(obj: any, callback: (key: string, value: any) => void): void { // for (const key in obj) { // if (obj.hasOwnProperty(key)) { // const value = obj[key]; // callback(key, value); // if (typeof value === 'object') { // this.traverseObject(value, callback); // } // } // } // } // getDay() { // return Math.floor(Date.now() / (1000 * 60 * 60 * 24)); // } /** * @description: generate a random data; * @return {*} */ // getDateId(): string { // let d = new Date(); // let dates = d.getDay().toString() + d.getHours().toString() + d.getMinutes().toString() + d.getSeconds().toString(); // let result = Math.floor(Number(dates) + Math.floor(Math.random() * (500000 * Math.random() + 50000)) * 0.1); // return result.toString(); // } } const utilsMgr = new DataDao; export default utilsMgr;