79538e7ba270719c2fdeaf5f4b7d2ecd19c3108f.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, sys, LocalStorageUtil, _crd;
  4. _export("LocalStorageUtil", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. sys = _cc.sys;
  11. }],
  12. execute: function () {
  13. _crd = true;
  14. _cclegacy._RF.push({}, "d8e88J+Q9RPtY3fFaop4Z7i", "LocalStorageUtil", undefined);
  15. __checkObsolete__(['sys']);
  16. _export("LocalStorageUtil", LocalStorageUtil = class LocalStorageUtil {
  17. static getInstance() {
  18. if (!this._inst) this._inst = new LocalStorageUtil();
  19. return this._inst;
  20. } //缓存
  21. constructor() {
  22. this.temporary = void 0;
  23. this._user_id = void 0;
  24. this.temporary = new Map();
  25. }
  26. setUserId(uk) {
  27. this._user_id = uk;
  28. }
  29. /**
  30. * 缓存变量存储区别用户
  31. * @param {*} key
  32. * @param {*} value
  33. */
  34. set(key, value) {
  35. if (typeof value === 'object') {
  36. try {
  37. value = JSON.stringify(value);
  38. } catch (e) {
  39. console.error(`解析失败,str = ${value}`);
  40. return;
  41. }
  42. } else if (typeof value === 'number') {
  43. value = value + "";
  44. }
  45. if (this._user_id) sys.localStorage.setItem(this._user_id + "_" + key, value);else sys.localStorage.setItem(key, value);
  46. return value;
  47. }
  48. /**
  49. * 获取缓存变量区别用户
  50. * @param {*} key
  51. * @returns
  52. */
  53. get(key) {
  54. if (null == key) {
  55. console.error("存储的key不能为空");
  56. return null;
  57. }
  58. let data;
  59. if (this._user_id) data = sys.localStorage.getItem(this._user_id + "_" + key);else data = sys.localStorage.getItem(key);
  60. if (!data) return undefined;
  61. return data;
  62. }
  63. /** 获取指定关键字的数值 */
  64. getNumber(key, defaultValue = 0) {
  65. var r = this.get(key);
  66. if (r == "0") {
  67. return Number(r);
  68. }
  69. return Number(r) || defaultValue;
  70. }
  71. /** 获取指定关键字的布尔值 */
  72. getBoolean(key) {
  73. var r = this.get(key);
  74. return Boolean(r) || false;
  75. }
  76. /** 获取指定关键字的JSON对象 */
  77. getJson(key, defaultValue) {
  78. var r = this.get(key);
  79. return r && JSON.parse(r) || defaultValue;
  80. }
  81. /** 获取指定关键字的JSON对象 */
  82. getObject(key) {
  83. let data = this.get(key);
  84. try {
  85. const jsonObj = JSON.parse(data);
  86. return jsonObj;
  87. } catch (error) {
  88. return data;
  89. }
  90. }
  91. /**
  92. * 删除缓存变量
  93. * @param {*} key
  94. */
  95. removeNormalObject(key) {
  96. sys.localStorage.removeItem(key);
  97. }
  98. /**
  99. * 缓存变量存储
  100. * @param {*} key
  101. * @param {*} value
  102. */
  103. setNormalObject(key, value) {
  104. sys.localStorage.setItem(key, JSON.stringify(value));
  105. return value;
  106. }
  107. /**
  108. * 获取缓存变量
  109. * @param {*} key
  110. * @returns
  111. */
  112. getNormalObject(key) {
  113. let data;
  114. data = sys.localStorage.getItem(key);
  115. if (!data) return undefined;
  116. try {
  117. const jsonObj = JSON.parse(data);
  118. return jsonObj;
  119. } catch (error) {
  120. return data;
  121. }
  122. }
  123. /**
  124. * 删除缓存变量
  125. * @param {*} key
  126. */
  127. removeObject(key) {
  128. sys.localStorage.removeItem(key);
  129. }
  130. /**
  131. * 临时变量存储
  132. * @param {*} key
  133. * @param {*} value
  134. */
  135. setTempObject(key, value) {
  136. this.temporary.set(key, value);
  137. }
  138. /**
  139. * 获取临时变量
  140. * @param {*} key
  141. * @returns
  142. */
  143. getTempObject(key) {
  144. return this.temporary.get(key);
  145. }
  146. /**
  147. * 删除临时变量
  148. * @param {*} key
  149. */
  150. removeTempObject(key) {
  151. this.temporary.delete(key);
  152. }
  153. });
  154. LocalStorageUtil._inst = void 0;
  155. _cclegacy._RF.pop();
  156. _crd = false;
  157. }
  158. };
  159. });
  160. //# sourceMappingURL=79538e7ba270719c2fdeaf5f4b7d2ecd19c3108f.js.map