category.ts
1.41 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
import { getCategoryList } from './../service'
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('')
let resetFuncValue = ''
let resetCategoryValue = ''
const getList = () => {
getCategoryList().then((res) => {
const rootList = res.data?.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
})
}
const updateCategory = (value: string) => {
selectedCategory.value = value
updateFuncCategory(value === resetCategoryValue ? resetFuncValue : '')
}
const updateSubCategory = (value: string) => {
selectedSubCategory.value = value
}
const updateFuncCategory = (value?: string) => {
selectedFuncCategory.value = value || ''
}
return {
list,
selectedCategory,
selectedSubCategory,
selectedFuncCategory,
resetCategoryValue,
getList,
updateCategory,
updateSubCategory,
updateFuncCategory
}
})