|
1
2
3
4
5
6
7
8
9
10
11
12
|
import { ref } from "vue";
import { defineStore } from "pinia";
import type { Category, CategoryRootType } from "../type";
export const useCategoryStore = defineStore("category", () => {
const list = ref<CategoryRootType[]>([]);
const selectedCategory = ref(""); // 选中的一级类别
const selectedSubCategory = ref(""); // 选中的二级类别
const selectedFuncCategory = ref("");
const categoryVisible = ref(true);
let resetFuncValue = "";
let resetCategoryValue = "";
|
sanmu
authored
|
13
14
|
const getList = async () => {
|
|
15
|
const config = useRuntimeConfig();
|
sanmu
authored
|
16
|
// 在SSR中,数据仅在服务器端获取并传递到客户端。
|
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
const { data } = await useAsyncData(
"category",
() =>
$fetch("/shop/product/category", {
method: "GET",
}),
{
server: true, // 仅在服务器端获取数据
}
);
const rootList = data.value.data.rootCategoryList;
list.value = rootList || [];
resetCategoryValue = selectedCategory.value =
rootList[0].categoryDisplayName;
selectedSubCategory.value = rootList[0].list[0].id;
resetFuncValue = selectedFuncCategory.value =
rootList[0].productFunctions[0].id;
};
|
sanmu
authored
|
36
37
|
const updateCategory = (value: string) => {
|
|
38
39
40
|
selectedCategory.value = value;
updateFuncCategory(value === resetCategoryValue ? resetFuncValue : "");
};
|
sanmu
authored
|
41
42
|
const updateSubCategory = (value: string) => {
|
|
43
44
|
selectedSubCategory.value = value;
};
|
sanmu
authored
|
45
46
|
const updateFuncCategory = (value?: string) => {
|
|
47
48
|
selectedFuncCategory.value = value || "";
};
|
sanmu
authored
|
49
50
|
const updateDisplay = (visible: boolean) => {
|
|
51
52
|
categoryVisible.value = visible;
};
|
sanmu
authored
|
53
54
55
56
57
58
59
60
61
62
63
|
return {
list,
selectedCategory,
selectedSubCategory,
selectedFuncCategory,
resetCategoryValue,
categoryVisible,
getList,
updateCategory,
updateSubCategory,
updateFuncCategory,
|
|
64
65
66
|
updateDisplay,
};
});
|