SqlUtil.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { sys } from "cc";
  2. import { PREVIEW } from "cc/env";
  3. import { ObjectUtil } from "../util/ObjectUtil";
  4. import { EncryptUtil } from "./EncryptUtil";
  5. import { md5 } from "./Md5";
  6. export module storage {
  7. let _key: string | null = null;
  8. let _iv: string | null = null;
  9. let _id: string = "";
  10. /**
  11. * 初始化密钥
  12. * @param key aes加密的key
  13. * @param iv aes加密的iv
  14. */
  15. export function init(key: string, iv: string) {
  16. _key = md5(key);
  17. _iv = md5(iv);
  18. }
  19. /**
  20. * 设置用户标识
  21. * @param sign
  22. */
  23. export function setUser(sign: string) {
  24. _id = sign;
  25. }
  26. /**
  27. * 存储
  28. * @param key 存储key
  29. * @param value 存储值
  30. * @returns
  31. */
  32. export function set(key: string, value: any) {
  33. key = `${key}_${_id}`;
  34. if (null == key) {
  35. console.error("存储的key不能为空");
  36. return;
  37. }
  38. if (!PREVIEW) {
  39. key = md5(key);
  40. }
  41. if (null == value) {
  42. console.warn("存储的值为空,则直接移除该存储");
  43. remove(key);
  44. return;
  45. }
  46. if (typeof value === 'function') {
  47. console.error("储存的值不能为方法");
  48. return;
  49. }
  50. if (typeof value === 'object') {
  51. try {
  52. if (value instanceof Map) {
  53. let temp = ObjectUtil.mapToObject(value)
  54. value = JSON.stringify(temp);
  55. } else {
  56. value = JSON.stringify(value);
  57. }
  58. }
  59. catch (e) {
  60. console.error(`解析失败,str = ${value}`);
  61. return;
  62. }
  63. }
  64. else if (typeof value === 'number') {
  65. value = value + "";
  66. }
  67. if (!PREVIEW && null != _key && null != _iv) {
  68. try {
  69. value = EncryptUtil.aesEncrypt(value, _key, _iv);
  70. }
  71. catch (e) {
  72. value = null;
  73. }
  74. }
  75. sys.localStorage.setItem(key, value);
  76. }
  77. /**
  78. * 获取
  79. * @param key 获取的key
  80. * @param defaultValue 获取的默认值
  81. * @returns
  82. */
  83. export function get(key: string, defaultValue?: any) {
  84. if (null == key) {
  85. console.error("存储的key不能为空");
  86. return;
  87. }
  88. key = `${key}_${_id}`;
  89. if (!PREVIEW) {
  90. key = md5(key);
  91. }
  92. let str: string | null = sys.localStorage.getItem(key);
  93. if (null != str && '' !== str && !PREVIEW && null != _key && null != _iv) {
  94. try {
  95. str = EncryptUtil.aesDecrypt(str, _key, _iv);
  96. }
  97. catch (e) {
  98. str = null;
  99. }
  100. }
  101. if (null === str) {
  102. return defaultValue;
  103. }
  104. if (null == defaultValue || typeof defaultValue === 'string') {
  105. return str;
  106. }
  107. if (typeof defaultValue === 'number') {
  108. return Number(str) || 0;
  109. }
  110. if (typeof defaultValue === 'boolean') {
  111. return "true" == str; // 不要使用Boolean("false");
  112. }
  113. if (typeof defaultValue === 'object') {
  114. try {
  115. return JSON.parse(str);
  116. }
  117. catch (e) {
  118. console.error("解析数据失败,str=" + str);
  119. return defaultValue;
  120. }
  121. }
  122. return str;
  123. }
  124. /**
  125. * 移除某个值
  126. * @param key 需要移除的key
  127. * @returns
  128. */
  129. export function remove(key: string) {
  130. if (null == key) {
  131. console.error("存储的key不能为空");
  132. return;
  133. }
  134. key = `${key}_${_id}`;
  135. if (!PREVIEW) {
  136. key = md5(key);
  137. }
  138. sys.localStorage.removeItem(key);
  139. }
  140. /**
  141. * 清空整个本地存储
  142. */
  143. export function clear() {
  144. sys.localStorage.clear();
  145. }
  146. }