Store.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * 本地化数据存储
  3. */
  4. export class Store {
  5. /** 是否启用本地缓存(加速访问) */
  6. public static enabledStoreCache: boolean = true;
  7. /** 本地化数据缓存(加速访问) */
  8. private static storeCacheDatas = {};
  9. /** 本地化存储keys缓存 */
  10. private static StoreKeys = null;
  11. /**
  12. * 保存数据
  13. * @param {string} key 索引键
  14. * @param {*} value 值
  15. */
  16. static set(key: string, value: any): void {
  17. if (Store.storeCacheDatas[key] === value) {
  18. const typeName = typeof value;
  19. if (typeName === 'number' || typeName === 'string' || typeName === 'boolean') {return;}
  20. }
  21. Store.enabledStoreCache && (Store.storeCacheDatas[key] = value);
  22. let val: any = {
  23. type: typeof(value),
  24. value: value
  25. };
  26. val = JSON.stringify(val);
  27. // 记录key
  28. if (key !== 'StoreKeys') {
  29. let keys: any = Store.StoreKeys || Store.get('StoreKeys', {});
  30. Store.StoreKeys = keys;
  31. if (!keys[key]) {
  32. keys[key] = 1;
  33. Store.set('StoreKeys', keys);
  34. }
  35. }
  36. cc.sys.localStorage.setItem(key, val);
  37. }
  38. /**
  39. * 保存今天的数据
  40. * @param {string} key 索引键
  41. * @param {*} defaultValue 当没有取到值的时候返回的默认值
  42. */
  43. static setTodayValue(key: string, value: any): void {
  44. let date: Date = new Date();
  45. let day: string = date.getFullYear() + '_' + (date.getMonth()+1) + '_' + date.getDate();
  46. return Store.set(key + '_' + day, value);
  47. }
  48. /**
  49. * 获取数据
  50. * @param {string} key 索引键
  51. * @param {*} defaultValue 当没有取到值的时候返回的默认值
  52. */
  53. static get(key: string, defaultValue?: any): any {
  54. let data: any = Store.storeCacheDatas[key];
  55. if (data !== undefined && data !== null) {return data;}
  56. data = cc.sys.localStorage.getItem(key);
  57. if (!data) {
  58. Store.enabledStoreCache && Store.set(key, defaultValue);
  59. return defaultValue;
  60. }
  61. let val: any = JSON.parse(data);
  62. if (val.value === undefined || val.value === null) {
  63. Store.enabledStoreCache && Store.set(key, defaultValue);
  64. return defaultValue;
  65. }
  66. Store.enabledStoreCache && Store.set(key, val.value);
  67. return val.value;
  68. }
  69. /**
  70. * 获取今天的数据
  71. * @param {string} key 索引键
  72. * @param {*} defaultValue 当没有取到值的时候返回的默认值
  73. */
  74. static getTodayValue(key: string, defaultValue?: any): any {
  75. let date: Date = new Date();
  76. let day: string = date.getFullYear() + '_' + (date.getMonth()+1) + '_' + date.getDate();
  77. return Store.get(key + '_' + day, defaultValue);
  78. }
  79. /**
  80. * 获取时间阶段的数据
  81. * @param {string} key 索引键
  82. * @param {string} interval 时间区间
  83. * @param {*} defaultValue 当没有取到值的时候返回的默认值
  84. */
  85. static getTimeIntervalValue(key: string, interval : number, defaultValue?: any): any {
  86. try {
  87. const time = Math.floor(Date.now() / 1000);
  88. const preKey = Math.floor(time / interval);
  89. return Store.get(key + '_' + preKey, defaultValue);
  90. } catch (error) {
  91. cc.log("Store.getTimeIntervalValue() : interval 不能为0!");
  92. }
  93. }
  94. /**
  95. * 设置时间阶段的数据
  96. * @param {string} key 索引键
  97. * @param {string} interval 时间区间
  98. * @param {*} defaultValue 当没有取到值的时候返回的默认值
  99. */
  100. static setTimeIntervalValue(key: string, interval : number, value: any): any {
  101. try {
  102. const time = Math.floor(Date.now() / 1000);
  103. const preKey = Math.floor(time / interval);
  104. return Store.set(key + '_' + preKey, value);
  105. } catch (error) {
  106. cc.log("Store.getTimeIntervalValue() : interval 不能为0!");
  107. }
  108. }
  109. /**
  110. * 获取bool值
  111. * @param {string} key
  112. * @param {bool} defaultValue
  113. */
  114. static getBool(key: string, defaultValue?: boolean): boolean {
  115. let value: any = Store.get(key);
  116. if (value === 'true') {
  117. return true;
  118. } else if (value === 'false') {
  119. return false;
  120. }
  121. return defaultValue;
  122. }
  123. /**
  124. * 设置bool值
  125. * @param {string} key
  126. * @param {bool} value
  127. */
  128. static setBool(key: string, value: boolean): void {
  129. if (value === true) {
  130. Store.set(key, 'true');
  131. } else {
  132. Store.set(key, 'false');
  133. }
  134. }
  135. /**
  136. * 移除数据
  137. * @param {string} key
  138. */
  139. static remove(key: string): void {
  140. cc.sys.localStorage.removeItem(key);
  141. }
  142. /**
  143. * 清空所有缓存数据
  144. */
  145. static clear(): void {
  146. let keys = Store.get('StoreKeys', {});
  147. for (let key in keys) {
  148. cc.sys.localStorage.removeItem(key);
  149. keys[key] = undefined;
  150. }
  151. Store.set('StoreKeys', {});
  152. }
  153. /**
  154. * 获取本地化存储中所有的key
  155. */
  156. static getAllKeys(): Array<string> {
  157. const result = [];
  158. const keys = Store.get('StoreKeys', {});
  159. for (let key in keys) {
  160. result.push(key);
  161. }
  162. return result;
  163. }
  164. }
  165. try {(window as any).Store = Store;} catch (error) {}