use-card-store.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 = info;
  82. const encryptedCard = crypt.encrypt(JSON.stringify(info));
  83. ls.set(CARD_KEY, encryptedCard);
  84. };
  85. // 保存申请卡
  86. const saveApplyCard = (info: any) => {
  87. applyCard.value = info;
  88. const encryptedCard = crypt.encrypt(JSON.stringify(info));
  89. ls.set(APPLY_CARD_KEY, encryptedCard);
  90. };
  91. // 保存币种列表
  92. const saveCurrencyList = (list: any) => {
  93. currencyList.value = list;
  94. const encryptedCurrencies = crypt.encrypt(JSON.stringify(list));
  95. ls.set(CURRENCY_LIST_KEY, encryptedCurrencies);
  96. };
  97. // 保存订单详情(加密)
  98. const saveOrderDetail = (detail: any) => {
  99. orderDetail.value = detail;
  100. const encryptedDetail = crypt.encrypt(JSON.stringify(detail));
  101. ls.set(ORDER_DETAIL_KEY, encryptedDetail);
  102. };
  103. // 保存参数列表(加密)
  104. const saveGlobalFieldParams = (params: any) => {
  105. globalFieldParams.value = params;
  106. const encryptedParams = crypt.encrypt(JSON.stringify(params));
  107. ls.set(GLOBAL_FIELD_PARAMS_KEY, encryptedParams);
  108. };
  109. // 清空订单详情(缓存 + 内存)
  110. const clearOrderDetail = () => {
  111. orderDetail.value = null;
  112. // @ts-ignore: store2 兼容 remove/removeItem
  113. if (typeof (ls as any).remove === "function") {
  114. (ls as any).remove(ORDER_DETAIL_KEY);
  115. } else if (typeof (ls as any).removeItem === "function") {
  116. (ls as any).removeItem(ORDER_DETAIL_KEY);
  117. } else {
  118. ls.set(ORDER_DETAIL_KEY, null);
  119. }
  120. };
  121. // 初始化所有数据
  122. initUserCard();
  123. initApplyCard();
  124. initCurrencyList();
  125. initOrderDetail();
  126. initGlobalFieldParams();
  127. return {
  128. userCard,
  129. applyCard,
  130. currencyList, // 新增币种列表
  131. orderDetail, // 订单详情(已解密)
  132. globalFieldParams, // 新增参数列表
  133. saveUserCard,
  134. saveApplyCard,
  135. saveCurrencyList, // 新增保存币种列表的操作
  136. saveOrderDetail, // 保存订单详情(加密)
  137. saveGlobalFieldParams, // 新增保存参数列表的操作
  138. clearOrderDetail, // 清空订单详情
  139. };
  140. });
  141. export default useCardStore;