action.js 1.75 KB
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import LeftmotionApi from '../api/motion/equipments';

export const useActionStore = defineStore(
  'action',
  () => {
    // 1. 原始所有分类(接口拿回来的)
    const allCategories = ref([]);

    // 2. 隐藏的分类ID(需要持久化)
    const hiddenCategories = ref([]);

    // 3. 展示的分类 = 全部分类 - 隐藏分类(过滤 ID)
    const showCategories = computed(() => {
      return allCategories.value.filter((item) => !hiddenCategories.value.includes(item.id));
    });

    // 获取分类
    const getloadCategories = async () => {
      if (allCategories.value.length > 0) return;

      const response = await LeftmotionApi.getAllCategories();
      // 把接口数据存进去
      allCategories.value = response.data || [];
      // 追加超级组分类(保持原有逻辑)
      allCategories.value.push({
        id: 'super',
        name: '超级组',
      });
    };

    // 隐藏某个分类(传入 ID)
    const hideCategory = (categoryId) => {
      if (!hiddenCategories.value.includes(categoryId)) {
        hiddenCategories.value.push(categoryId);
      }
    };

    // 显示某个分类(取消隐藏,传入 ID)
    const showCategory = (categoryId) => {
      hiddenCategories.value = hiddenCategories.value.filter((id) => id !== categoryId);
    };

    return {
      allCategories,
      showCategories,
      hiddenCategories,
      getloadCategories,
      hideCategory,
      showCategory,
    };
  },
  // 持久化配置
  {
    persist: {
      enabled: true,
      strategies: [
        {
          key: 'action-category-store', // 修正 key 名称
          paths: ['hiddenCategories'],
        },
      ],
    },
  },
);