import { defineStore } from "pinia"; import { getLan } from "@/locale" const state = () => ({ shopList: [], }) export const useCacheStore = defineStore('cache', { state, getters: {}, actions: { setShopList(key, page, _list_, search = '') { const keys = `shop_${key}_${getLan()}`; let search_text = this.deleteSpace(search); const searchKey = `search_${search_text}`; const findIndex = this.shopList.findIndex(item => item.channel === keys); if (findIndex > -1) { let arr = this.shopList[findIndex].children; let arrs = arr[searchKey] || [] let children = {} children[searchKey] = [...arrs, ..._list_] let objs = this.shopList[findIndex].children this.shopList[findIndex] = { channel: keys, page, searchKey, children: { ...objs, ...children } } return }; // this.shopList.push({ // channel: keys, // page, // children: _list_ || [] // }) let children = {} children[searchKey] = _list_ || []; this.shopList.push({ channel: keys, page, searchKey, children }) }, getShopList(key) { const keys = `shop_${key}_${getLan()}`; const findIndex = this.shopList.findIndex(item => item.channel === keys); return this.shopList[findIndex] }, setDetail({ channel, goods_id, detail, search = '' }) { const keys = `shop_${channel}_${getLan()}`; let search_text = this.deleteSpace(search); const searchKey = `search_${search_text}`; const findIndex = this.shopList.findIndex(item => item.channel === keys); let children = this.shopList[findIndex].children[searchKey] const childrenItem = children.find(item => item.num_iid == goods_id); childrenItem['_detail'] = detail; }, getShopDetail({ channel, goods_id, search }) { const findItem = this.getShopList(channel) || {}; let search_text = this.deleteSpace(search); const searchKey = `search_${search_text}`; let children = findItem.children && findItem.children[searchKey] const childrenItem = children && children.find(item => item.num_iid == goods_id); if (childrenItem && childrenItem['_detail']) return childrenItem['_detail']; return null; }, deleteSpace(str) { return str.replace(/\s+/g, ''); }, changeShopCollect({ channel, goods_id, isCollect }) { const filteredData = this.shopList.filter(item => item.channel.includes(`shop_${channel}`)); if (filteredData) { const mergedSearch = filteredData.flatMap(item => Object.entries(item.children) .filter(([key]) => key.startsWith('search_')) .flatMap(([_, value]) => value) ); const childrenItem = mergedSearch.filter(item => item.num_iid == goods_id); if (childrenItem && childrenItem.length) { childrenItem.forEach(item => { item.isCollect = isCollect; if (item['_detail']) { item['_detail'].isCollect = isCollect; } }); } } }, }, });