category.ts 966 Bytes
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 getList = () => {
    getCategoryList().then((res) => {
      const rootList = res.data?.data?.rootCategoryList
      list.value = rootList || []
      selectedCategory.value = rootList[0].categoryDisplayName
      selectedSubCategory.value = rootList[0].list[0].id
    })
  }

  const updateCategory = (value: string) => {
    selectedCategory.value = value
  }

  const updateSubCategory = (value: string) => {
    selectedSubCategory.value = value
  }

  return { list, selectedCategory, selectedSubCategory, getList, updateCategory, updateSubCategory }
})