Blame view

src/stores/category.ts 1.5 KB
sanmu authored
1
2
3
4
5
6
7
8
9
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('') // 选中的二级类别
sanmu authored
10
11
12
  const selectedFuncCategory = ref('')
  let resetFuncValue = ''
  let resetCategoryValue = ''
sanmu authored
13
14
15
16
17

  const getList = () => {
    getCategoryList().then((res) => {
      const rootList = res.data?.data?.rootCategoryList
      list.value = rootList || []
sanmu authored
18
      resetCategoryValue = selectedCategory.value = rootList[0].categoryDisplayName
sanmu authored
19
      selectedSubCategory.value = rootList[0].list[0].id
sanmu authored
20
      resetFuncValue = selectedFuncCategory.value = rootList[0].productFunctions[0].id
sanmu authored
21
22
23
24
    })
  }

  const updateCategory = (value: string) => {
sanmu authored
25
    console.log('%c [ value ]-25', 'font-size:13px; background:pink; color:#bf2c9f;', value)
sanmu authored
26
    selectedCategory.value = value
sanmu authored
27
    updateFuncCategory(value === resetCategoryValue ? resetFuncValue : '')
sanmu authored
28
29
30
31
32
33
  }

  const updateSubCategory = (value: string) => {
    selectedSubCategory.value = value
  }
sanmu authored
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  const updateFuncCategory = (value?: string) => {
    selectedFuncCategory.value = value || ''
  }

  return {
    list,
    selectedCategory,
    selectedSubCategory,
    selectedFuncCategory,
    resetCategoryValue,
    getList,
    updateCategory,
    updateSubCategory,
    updateFuncCategory
  }
sanmu authored
49
})