fb46854d644f9daeede1c882e9719f88eeb7ee00.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. let 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(`解析失败,str = ${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 (null == key) {
  91. console.error("存储的key不能为空");
  92. return null;
  93. }
  94. key = this.getKey(key);
  95. if (this.encrypted) {
  96. key = this.iss.encryptKey(key);
  97. }
  98. let str = sys.localStorage.getItem(key);
  99. if (null != str && '' !== str && this.encrypted) {
  100. str = this.iss.decrypt(str);
  101. }
  102. if (null === str) {
  103. return defaultValue;
  104. }
  105. return str;
  106. }
  107. /** 获取指定关键字的数值 */
  108. getNumber(key, defaultValue = 0) {
  109. const r = this.get(key);
  110. if (r == "0") {
  111. return Number(r);
  112. }
  113. return Number(r) || defaultValue;
  114. }
  115. /** 获取指定关键字的布尔值 */
  116. getBoolean(key) {
  117. const r = this.get(key);
  118. return r.toLowerCase() === 'true';
  119. }
  120. /** 获取指定关键字的JSON对象 */
  121. getJson(key, defaultValue) {
  122. const r = this.get(key);
  123. return r && JSON.parse(r) || defaultValue;
  124. }
  125. /**
  126. * 删除指定关键字的数据
  127. * @param key 需要移除的关键字
  128. * @returns
  129. */
  130. remove(key) {
  131. if (null == key) {
  132. console.error("存储的key不能为空");
  133. return;
  134. }
  135. let keywords = this.getKey(key);
  136. if (this.encrypted) {
  137. keywords = this.iss.encryptKey(keywords);
  138. }
  139. sys.localStorage.removeItem(keywords);
  140. }
  141. /** 清空整个本地存储 */
  142. clear() {
  143. sys.localStorage.clear();
  144. }
  145. /** 获取数据分组关键字 */
  146. getKey(key) {
  147. if (this.id == null || this.id == "") {
  148. return key;
  149. }
  150. return `${this.id}_${key}`;
  151. }
  152. });
  153. _cclegacy._RF.pop();
  154. _crd = false;
  155. }
  156. };
  157. });
  158. //# sourceMappingURL=fb46854d644f9daeede1c882e9719f88eeb7ee00.js.map