76507980f7171b119d2e2687420ea742a2d15aac.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. System.register(["cc", "cc/env"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, sys, PREVIEW, StorageManager, _crd;
  4. _export("StorageManager", 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. }, function (_ccEnv) {
  12. PREVIEW = _ccEnv.PREVIEW;
  13. }],
  14. execute: function () {
  15. _crd = true;
  16. _cclegacy._RF.push({}, "ed226gLF85Oyr+WhA9TJZLX", "StorageManager", undefined);
  17. __checkObsolete__(['sys']);
  18. /**
  19. * 本地存储
  20. * @help https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037957&doc_id=2873565
  21. */
  22. _export("StorageManager", StorageManager = class StorageManager {
  23. constructor() {
  24. this.id = null;
  25. this.iss = null;
  26. }
  27. /** 数据加密开关 */
  28. get encrypted() {
  29. return !PREVIEW;
  30. }
  31. /** 本地存储数据加密方式初始化 */
  32. init(iis) {
  33. this.iss = iis;
  34. }
  35. /**
  36. * 设置用户唯一标识
  37. * @param id
  38. */
  39. setUser(id) {
  40. this.id = id;
  41. }
  42. /**
  43. * 存储本地数据
  44. * @param key 存储key
  45. * @param value 存储值
  46. * @returns
  47. */
  48. set(key, value) {
  49. var keywords = this.getKey(key);
  50. if (null == key) {
  51. console.error("存储的key不能为空");
  52. return;
  53. }
  54. if (this.encrypted) {
  55. keywords = this.iss.encryptKey(keywords);
  56. }
  57. if (null == value) {
  58. console.warn("存储的值为空,则直接移除该存储");
  59. this.remove(key);
  60. return;
  61. }
  62. if (typeof value === 'function') {
  63. console.error("储存的值不能为方法");
  64. return;
  65. }
  66. if (typeof value === 'object') {
  67. try {
  68. value = JSON.stringify(value);
  69. } catch (e) {
  70. console.error("\u89E3\u6790\u5931\u8D25\uFF0Cstr = " + value);
  71. return;
  72. }
  73. } else if (typeof value === 'number') {
  74. value = value + "";
  75. } else if (typeof value === 'boolean') {
  76. value = String(value);
  77. }
  78. if (this.encrypted) {
  79. value = this.iss.encrypt(value);
  80. }
  81. sys.localStorage.setItem(keywords, value);
  82. }
  83. /**
  84. * 获取指定关键字的数据
  85. * @param key 获取的关键字
  86. * @param defaultValue 获取的默认值
  87. * @returns
  88. */
  89. get(key, defaultValue) {
  90. if (defaultValue === void 0) {
  91. defaultValue = "";
  92. }
  93. if (null == key) {
  94. console.error("存储的key不能为空");
  95. return null;
  96. }
  97. key = this.getKey(key);
  98. if (this.encrypted) {
  99. key = this.iss.encryptKey(key);
  100. }
  101. var str = sys.localStorage.getItem(key);
  102. if (null != str && '' !== str && this.encrypted) {
  103. str = this.iss.decrypt(str);
  104. }
  105. if (null === str) {
  106. return defaultValue;
  107. }
  108. return str;
  109. }
  110. /** 获取指定关键字的数值 */
  111. getNumber(key, defaultValue) {
  112. if (defaultValue === void 0) {
  113. defaultValue = 0;
  114. }
  115. var r = this.get(key);
  116. if (r == "0") {
  117. return Number(r);
  118. }
  119. return Number(r) || defaultValue;
  120. }
  121. /** 获取指定关键字的布尔值 */
  122. getBoolean(key) {
  123. var r = this.get(key);
  124. return r.toLowerCase() === 'true';
  125. }
  126. /** 获取指定关键字的JSON对象 */
  127. getJson(key, defaultValue) {
  128. var r = this.get(key);
  129. return r && JSON.parse(r) || defaultValue;
  130. }
  131. /**
  132. * 删除指定关键字的数据
  133. * @param key 需要移除的关键字
  134. * @returns
  135. */
  136. remove(key) {
  137. if (null == key) {
  138. console.error("存储的key不能为空");
  139. return;
  140. }
  141. var keywords = this.getKey(key);
  142. if (this.encrypted) {
  143. keywords = this.iss.encryptKey(keywords);
  144. }
  145. sys.localStorage.removeItem(keywords);
  146. }
  147. /** 清空整个本地存储 */
  148. clear() {
  149. sys.localStorage.clear();
  150. }
  151. /** 获取数据分组关键字 */
  152. getKey(key) {
  153. if (this.id == null || this.id == "") {
  154. return key;
  155. }
  156. return this.id + "_" + key;
  157. }
  158. });
  159. _cclegacy._RF.pop();
  160. _crd = false;
  161. }
  162. };
  163. });
  164. //# sourceMappingURL=76507980f7171b119d2e2687420ea742a2d15aac.js.map