use-card-store.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { defineStore } from "pinia";
  2. import ls from "@/utils/store2";
  3. import { ref } from "vue";
  4. import { userToken } from "../composables/config";
  5. import crypt from "../composables/crypt";
  6. const CARD_KEY = "cardList";
  7. const APPLY_CARD_KEY = "applyList";
  8. const CURRENCY_LIST_KEY = "currencyList"; // 新增币种列表的本地存储键
  9. const GLOBAL_FIELD_PARAMS_KEY = "globalFieldParams"; // 新增币种列表的本地存储键
  10. const ORDER_DETAIL_KEY = "orderDetailEncrypted"; // 订单详情加密缓存键
  11. const useCardStore = defineStore("cardStore", () => {
  12. // 用户卡和申请卡列表
  13. const userCard = ref([]);
  14. const applyCard = ref([]);
  15. // 新增币种列表
  16. const currencyList = ref([]);
  17. // 新增币种列表
  18. const globalFieldParams = ref([]);
  19. // 订单详情(使用加密存储)
  20. const orderDetail = ref<any | null>(null);
  21. // 初始化用户卡
  22. const initUserCard = () => {
  23. const encryptedCard = ls.get(CARD_KEY);
  24. if (encryptedCard) {
  25. const decryptedCard = crypt.decrypt(encryptedCard);
  26. if (decryptedCard) {
  27. userCard.value = JSON.parse(decryptedCard);
  28. }
  29. }
  30. };
  31. // 初始化申请卡
  32. const initApplyCard = () => {
  33. const encryptedCard = ls.get(APPLY_CARD_KEY);
  34. if (encryptedCard) {
  35. const decryptedCard = crypt.decrypt(encryptedCard);
  36. if (decryptedCard) {
  37. applyCard.value = JSON.parse(decryptedCard);
  38. }
  39. }
  40. };
  41. // 初始化币种列表
  42. const initCurrencyList = () => {
  43. const encryptedCurrencies = ls.get(CURRENCY_LIST_KEY);
  44. if (encryptedCurrencies) {
  45. const decryptedCurrencies = crypt.decrypt(encryptedCurrencies);
  46. if (decryptedCurrencies) {
  47. currencyList.value = JSON.parse(decryptedCurrencies);
  48. }
  49. }
  50. };
  51. // 初始化订单详情(解密)
  52. const initOrderDetail = () => {
  53. const encryptedDetail = ls.get(ORDER_DETAIL_KEY);
  54. if (encryptedDetail) {
  55. const decryptedDetail = crypt.decrypt(encryptedDetail);
  56. if (decryptedDetail) {
  57. try {
  58. orderDetail.value = JSON.parse(decryptedDetail);
  59. } catch (e) {
  60. orderDetail.value = null;
  61. }
  62. }
  63. }
  64. };
  65. // 初始化参数列表(解密)
  66. const initGlobalFieldParams = () => {
  67. const encryptedDetail = ls.get(GLOBAL_FIELD_PARAMS_KEY);
  68. if (encryptedDetail) {
  69. const decryptedDetail = crypt.decrypt(encryptedDetail);
  70. if (decryptedDetail) {
  71. try {
  72. globalFieldParams.value = JSON.parse(decryptedDetail);
  73. } catch (e) {
  74. globalFieldParams.value = null;
  75. }
  76. }
  77. }
  78. };
  79. // 保存用户卡
  80. const saveUserCard = (info: any) => {
  81. // 保证 userCard.value 总是新数组,触发响应式
  82. userCard.value = Array.isArray(info) ? [...info] : info;
  83. const encryptedCard = crypt.encrypt(JSON.stringify(info));
  84. ls.set(CARD_KEY, encryptedCard);
  85. };
  86. // 保存申请卡
  87. const saveApplyCard = (info: any) => {
  88. applyCard.value = info;
  89. const encryptedCard = crypt.encrypt(JSON.stringify(info));
  90. ls.set(APPLY_CARD_KEY, encryptedCard);
  91. };
  92. // 保存币种列表
  93. const saveCurrencyList = (list: any) => {
  94. currencyList.value = list;
  95. const encryptedCurrencies = crypt.encrypt(JSON.stringify(list));
  96. ls.set(CURRENCY_LIST_KEY, encryptedCurrencies);
  97. };
  98. // 保存订单详情(加密)
  99. const saveOrderDetail = (detail: any) => {
  100. orderDetail.value = detail;
  101. const encryptedDetail = crypt.encrypt(JSON.stringify(detail));
  102. ls.set(ORDER_DETAIL_KEY, encryptedDetail);
  103. };
  104. // 保存参数列表(加密)
  105. const saveGlobalFieldParams = (params: any) => {
  106. globalFieldParams.value = params;
  107. const encryptedParams = crypt.encrypt(JSON.stringify(params));
  108. ls.set(GLOBAL_FIELD_PARAMS_KEY, encryptedParams);
  109. };
  110. // 清空订单详情(缓存 + 内存)
  111. const clearOrderDetail = () => {
  112. orderDetail.value = null;
  113. // @ts-ignore: store2 兼容 remove/removeItem
  114. if (typeof (ls as any).remove === "function") {
  115. (ls as any).remove(ORDER_DETAIL_KEY);
  116. } else if (typeof (ls as any).removeItem === "function") {
  117. (ls as any).removeItem(ORDER_DETAIL_KEY);
  118. } else {
  119. ls.set(ORDER_DETAIL_KEY, null);
  120. }
  121. };
  122. // 初始化所有数据
  123. initUserCard();
  124. initApplyCard();
  125. initCurrencyList();
  126. initOrderDetail();
  127. initGlobalFieldParams();
  128. return {
  129. userCard,
  130. applyCard,
  131. currencyList, // 新增币种列表
  132. orderDetail, // 订单详情(已解密)
  133. globalFieldParams, // 新增参数列表
  134. saveUserCard,
  135. saveApplyCard,
  136. saveCurrencyList, // 新增保存币种列表的操作
  137. saveOrderDetail, // 保存订单详情(加密)
  138. saveGlobalFieldParams, // 新增保存参数列表的操作
  139. clearOrderDetail, // 清空订单详情
  140. };
  141. });
  142. export default useCardStore;