import { defineStore } from "pinia"; import ls from "@/utils/store2"; import { ref } from "vue"; import { userToken } from "../composables/config"; import crypt from "../composables/crypt"; const CARD_KEY = "cardList"; const APPLY_CARD_KEY = "applyList"; const CURRENCY_LIST_KEY = "currencyList"; // 新增币种列表的本地存储键 const GLOBAL_FIELD_PARAMS_KEY = "globalFieldParams"; // 新增币种列表的本地存储键 const ORDER_DETAIL_KEY = "orderDetailEncrypted"; // 订单详情加密缓存键 const useCardStore = defineStore("cardStore", () => { // 用户卡和申请卡列表 const userCard = ref([]); const applyCard = ref([]); // 新增币种列表 const currencyList = ref([]); // 新增币种列表 const globalFieldParams = ref([]); // 订单详情(使用加密存储) const orderDetail = ref(null); // 初始化用户卡 const initUserCard = () => { const encryptedCard = ls.get(CARD_KEY); if (encryptedCard) { const decryptedCard = crypt.decrypt(encryptedCard); if (decryptedCard) { userCard.value = JSON.parse(decryptedCard); } } }; // 初始化申请卡 const initApplyCard = () => { const encryptedCard = ls.get(APPLY_CARD_KEY); if (encryptedCard) { const decryptedCard = crypt.decrypt(encryptedCard); if (decryptedCard) { applyCard.value = JSON.parse(decryptedCard); } } }; // 初始化币种列表 const initCurrencyList = () => { const encryptedCurrencies = ls.get(CURRENCY_LIST_KEY); if (encryptedCurrencies) { const decryptedCurrencies = crypt.decrypt(encryptedCurrencies); if (decryptedCurrencies) { currencyList.value = JSON.parse(decryptedCurrencies); } } }; // 初始化订单详情(解密) const initOrderDetail = () => { const encryptedDetail = ls.get(ORDER_DETAIL_KEY); if (encryptedDetail) { const decryptedDetail = crypt.decrypt(encryptedDetail); if (decryptedDetail) { try { orderDetail.value = JSON.parse(decryptedDetail); } catch (e) { orderDetail.value = null; } } } }; // 初始化参数列表(解密) const initGlobalFieldParams = () => { const encryptedDetail = ls.get(GLOBAL_FIELD_PARAMS_KEY); if (encryptedDetail) { const decryptedDetail = crypt.decrypt(encryptedDetail); if (decryptedDetail) { try { globalFieldParams.value = JSON.parse(decryptedDetail); } catch (e) { globalFieldParams.value = null; } } } }; // 保存用户卡 const saveUserCard = (info: any) => { // 保证 userCard.value 总是新数组,触发响应式 userCard.value = Array.isArray(info) ? [...info] : info; const encryptedCard = crypt.encrypt(JSON.stringify(info)); ls.set(CARD_KEY, encryptedCard); }; // 保存申请卡 const saveApplyCard = (info: any) => { applyCard.value = info; const encryptedCard = crypt.encrypt(JSON.stringify(info)); ls.set(APPLY_CARD_KEY, encryptedCard); }; // 保存币种列表 const saveCurrencyList = (list: any) => { currencyList.value = list; const encryptedCurrencies = crypt.encrypt(JSON.stringify(list)); ls.set(CURRENCY_LIST_KEY, encryptedCurrencies); }; // 保存订单详情(加密) const saveOrderDetail = (detail: any) => { orderDetail.value = detail; const encryptedDetail = crypt.encrypt(JSON.stringify(detail)); ls.set(ORDER_DETAIL_KEY, encryptedDetail); }; // 保存参数列表(加密) const saveGlobalFieldParams = (params: any) => { globalFieldParams.value = params; const encryptedParams = crypt.encrypt(JSON.stringify(params)); ls.set(GLOBAL_FIELD_PARAMS_KEY, encryptedParams); }; // 清空订单详情(缓存 + 内存) const clearOrderDetail = () => { orderDetail.value = null; // @ts-ignore: store2 兼容 remove/removeItem if (typeof (ls as any).remove === "function") { (ls as any).remove(ORDER_DETAIL_KEY); } else if (typeof (ls as any).removeItem === "function") { (ls as any).removeItem(ORDER_DETAIL_KEY); } else { ls.set(ORDER_DETAIL_KEY, null); } }; // 初始化所有数据 initUserCard(); initApplyCard(); initCurrencyList(); initOrderDetail(); initGlobalFieldParams(); return { userCard, applyCard, currencyList, // 新增币种列表 orderDetail, // 订单详情(已解密) globalFieldParams, // 新增参数列表 saveUserCard, saveApplyCard, saveCurrencyList, // 新增保存币种列表的操作 saveOrderDetail, // 保存订单详情(加密) saveGlobalFieldParams, // 新增保存参数列表的操作 clearOrderDetail, // 清空订单详情 }; }); export default useCardStore;