cache.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { defineStore } from "pinia";
  2. import { getLan } from "@/locale"
  3. const state = () => ({
  4. shopList: [],
  5. })
  6. export const useCacheStore = defineStore('cache', {
  7. state,
  8. getters: {},
  9. actions: {
  10. setShopList(key, page, _list_, search = '') {
  11. const keys = `shop_${key}_${getLan()}`;
  12. let search_text = this.deleteSpace(search);
  13. const searchKey = `search_${search_text}`;
  14. const findIndex = this.shopList.findIndex(item => item.channel === keys);
  15. if (findIndex > -1) {
  16. let arr = this.shopList[findIndex].children;
  17. let arrs = arr[searchKey] || []
  18. let children = {}
  19. children[searchKey] = [...arrs, ..._list_]
  20. let objs = this.shopList[findIndex].children
  21. this.shopList[findIndex] = {
  22. channel: keys,
  23. page,
  24. searchKey,
  25. children: { ...objs, ...children }
  26. }
  27. return
  28. };
  29. // this.shopList.push({
  30. // channel: keys,
  31. // page,
  32. // children: _list_ || []
  33. // })
  34. let children = {}
  35. children[searchKey] = _list_ || [];
  36. this.shopList.push({
  37. channel: keys,
  38. page,
  39. searchKey,
  40. children
  41. })
  42. },
  43. getShopList(key) {
  44. const keys = `shop_${key}_${getLan()}`;
  45. const findIndex = this.shopList.findIndex(item => item.channel === keys);
  46. return this.shopList[findIndex]
  47. },
  48. setDetail({ channel, goods_id, detail, search = '' }) {
  49. const keys = `shop_${channel}_${getLan()}`;
  50. let search_text = this.deleteSpace(search);
  51. const searchKey = `search_${search_text}`;
  52. const findIndex = this.shopList.findIndex(item => item.channel === keys);
  53. let children = this.shopList[findIndex].children[searchKey]
  54. const childrenItem = children.find(item => item.num_iid == goods_id);
  55. childrenItem['_detail'] = detail;
  56. },
  57. getShopDetail({ channel, goods_id, search }) {
  58. const findItem = this.getShopList(channel) || {};
  59. let search_text = this.deleteSpace(search);
  60. const searchKey = `search_${search_text}`;
  61. let children = findItem.children && findItem.children[searchKey]
  62. const childrenItem = children && children.find(item => item.num_iid == goods_id);
  63. if (childrenItem && childrenItem['_detail']) return childrenItem['_detail'];
  64. return null;
  65. },
  66. deleteSpace(str) {
  67. return str.replace(/\s+/g, '');
  68. },
  69. changeShopCollect({ channel, goods_id, isCollect }) {
  70. const filteredData = this.shopList.filter(item => item.channel.includes(`shop_${channel}`));
  71. if (filteredData) {
  72. const mergedSearch = filteredData.flatMap(item =>
  73. Object.entries(item.children)
  74. .filter(([key]) => key.startsWith('search_'))
  75. .flatMap(([_, value]) => value)
  76. );
  77. const childrenItem = mergedSearch.filter(item => item.num_iid == goods_id);
  78. if (childrenItem && childrenItem.length) {
  79. childrenItem.forEach(item => {
  80. item.isCollect = isCollect;
  81. if (item['_detail']) {
  82. item['_detail'].isCollect = isCollect;
  83. }
  84. });
  85. }
  86. }
  87. },
  88. },
  89. });