LocalStorageMgr.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { _decorator } from 'cc';
  2. import { APP_NAME } from '../A-FRAME/frame.config';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('LocalStorageMgr')
  5. export class LocalStorageMgr {
  6. public static userId_key = 'userId_key';
  7. public static country_key = 'country_key';
  8. public static language_key = 'language_key';
  9. public static device_id_key = 'device_id_key';
  10. public static localdataStore = {
  11. initUser: 'initUser',
  12. userInfo: 'userInfo',
  13. ConfCountry: 'ConfCountry',
  14. ConfCommon: 'ConfCommon',
  15. WithdrawConfig: 'WithdrawConfig',
  16. GetNewComerReward: 'GetNewComerReward',
  17. LotteryConfig: 'LotteryConfig',
  18. ForcePopConfig: 'ForcePopConfig',
  19. ForcePopReport: 'ForcePopReport',
  20. MissionReport: 'MissionReport',
  21. EliminateReport: 'EliminateReport',
  22. WithDraw: 'WithDraw',
  23. };
  24. public static isWDNewWard_key = 'isWDNewWard_key';
  25. public static gameInitFinish_key = "gameInitFinish_key";
  26. public static fullscTimes_key = 'fullscTimes_key';
  27. public static refrashPropNum_key = 'refrashProp_key';
  28. public static hummerPropNum_key = 'hummerProp_key';
  29. public static luckyWardNorgetTimes_key = 'luckyWardNorgetTimes_key';
  30. public static curlotteryTimes_key = 'curlotteryTimes_key';
  31. public static isCommer_key = 'isCommer_key';
  32. public static guideRecord_key = 'guideRecord_key';
  33. public static localBoard_key = 'localBoard_key';
  34. public static elimiTimes_key = 'elimiTimes_key';
  35. public static tempElimiTimes_lucky_key = 'tempElimiTimes_lucky_key';
  36. public static tempElimiTimes_lottery_key = 'tempElimiTimes_lottery_key';
  37. public static lastGameBoardData_key = 'lastGameBoardData_key';
  38. public static lastLoginDate_key = 'lastLoginDate_key';
  39. public static historyHighScore_key = 'historyHighScore_key';
  40. public static todayHighScore_key = 'todayHighScore_key';
  41. public static curScore_key = 'curScore_key';
  42. static wdlevelCahs_key: string = 'wdlevelCahs_key';
  43. static redPacket_key: string = 'redPacket_key';
  44. /**
  45. * 存储数据到本地存储中
  46. * @param key APP_NAME + 存储数据的键
  47. * @param value 存储的数据,任意类型
  48. */
  49. public static setItem(key: string, value: any): void {
  50. try {
  51. localStorage.setItem(APP_NAME + key, JSON.stringify(value));
  52. } catch (e) {
  53. console.error(`Failed to save data to localStorage: ${APP_NAME + key}`, e);
  54. }
  55. }
  56. /**
  57. * 从本地存储中获取数据
  58. * @param key APP_NAME + 存储数据的键
  59. * @param defaultValue 如果没有找到指定键的数据,返回的默认值
  60. * @returns 返回找到的数据或默认值
  61. */
  62. public static getItem<T>(key: string, defaultValue: T = null): T {
  63. try {
  64. const value = localStorage.getItem(APP_NAME + key);
  65. if (value === null || value === undefined) {
  66. return defaultValue;
  67. }
  68. return JSON.parse(value) as T;
  69. } catch (e) {
  70. console.error(`Failed to retrieve data from localStorage: ${APP_NAME + key}`, e);
  71. return defaultValue;
  72. }
  73. }
  74. /**
  75. * 判断本地存储中是否有指定键的数据
  76. * @param key APP_NAME + 存储数据的键
  77. * @returns 返回是否存在指定键的数据
  78. */
  79. public static hasItem(key: string): boolean {
  80. try {
  81. const value = localStorage.getItem(APP_NAME + key);
  82. return value !== null && value !== undefined;
  83. } catch (e) {
  84. console.error(`Failed to check localStorage for key: ${APP_NAME + key}`, e);
  85. return false;
  86. }
  87. }
  88. /**
  89. * 移除本地存储中指定键的数据
  90. * @param key APP_NAME + 存储数据的键
  91. */
  92. public static removeItem(key: string): void {
  93. try {
  94. localStorage.removeItem(APP_NAME + key);
  95. } catch (e) {
  96. console.error(`Failed to remove data from localStorage: ${APP_NAME + key}`, e);
  97. }
  98. }
  99. /**
  100. * 清空本地存储中所有数据
  101. */
  102. public static clear(): void {
  103. try {
  104. localStorage.clear();
  105. } catch (e) {
  106. console.error(`Failed to clear localStorage`, e);
  107. }
  108. }
  109. }