action.js
2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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: '超级组',
});
};
// 强制刷新分类(编辑/新增后使用)
const reloadCategories = async () => {
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,
reloadCategories,
hideCategory,
showCategory,
};
},
// 持久化配置
{
persist: {
enabled: true,
strategies: [
{
key: 'action-category-store',
paths: ['hiddenCategories'],
// 关键修复:插件默认 save 用 uni.setStorage(localStorage)、load 用 window.sessionStorage,
// 两端不一致会导致刷新后数据丢失。显式指定 storage 为 localStorage,保证存读同源。
storage: typeof window !== 'undefined' ? window.localStorage : undefined,
},
],
},
},
);