DataDao.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { _decorator, sys } from 'cc';
  2. class DataDao {
  3. // deepClone(obj: any): any {
  4. // if (obj === null) return null; // null
  5. // let clone = Object.assign({}, obj); // clone base type
  6. // Object.keys(clone).forEach( // go through
  7. // key => (clone[key] = typeof obj[key] === 'object' ? this.deepClone(obj[key]) : obj[key])
  8. // );
  9. // if (Array.isArray(obj)) { // array
  10. // clone.length = obj.length;
  11. // return Array.from(clone);
  12. // }
  13. // return clone;
  14. // }
  15. save(key: any, val: any) {
  16. // const jsonStr = JSON.stringify(val);
  17. chsdk.storage.set(key,val);
  18. // sys.localStorage.setItem(key,jsonStr);
  19. }
  20. remove(key: string) {
  21. chsdk.storage.remove(key);
  22. // sys.localStorage.removeItem(key);
  23. }
  24. clear(){
  25. chsdk.storage.clear();
  26. }
  27. /**
  28. * @description: load localstorage data,
  29. * @return {*}
  30. */
  31. load(key: string) {
  32. // const jsonStr = sys.localStorage.getItem(key);
  33. const userData = chsdk.storage.getObject(key);
  34. // const userData = jsonStr == 'undefined' ||!jsonStr? undefined :JSON.parse(jsonStr);
  35. return userData
  36. }
  37. getBoolean(key: string) {
  38. // const jsonStr = sys.localStorage.getItem(key);
  39. const userData = chsdk.storage.getBoolean(key);
  40. // const userData = jsonStr == 'undefined' ||!jsonStr? undefined :JSON.parse(jsonStr);
  41. return userData
  42. }
  43. // traverseObject(obj: any, callback: (key: string, value: any) => void): void {
  44. // for (const key in obj) {
  45. // if (obj.hasOwnProperty(key)) {
  46. // const value = obj[key];
  47. // callback(key, value);
  48. // if (typeof value === 'object') {
  49. // this.traverseObject(value, callback);
  50. // }
  51. // }
  52. // }
  53. // }
  54. // getDay() {
  55. // return Math.floor(Date.now() / (1000 * 60 * 60 * 24));
  56. // }
  57. /**
  58. * @description: generate a random data;
  59. * @return {*}
  60. */
  61. // getDateId(): string {
  62. // let d = new Date();
  63. // let dates = d.getDay().toString() + d.getHours().toString() + d.getMinutes().toString() + d.getSeconds().toString();
  64. // let result = Math.floor(Number(dates) + Math.floor(Math.random() * (500000 * Math.random() + 50000)) * 0.1);
  65. // return result.toString();
  66. // }
  67. }
  68. const utilsMgr = new DataDao;
  69. export default utilsMgr;