/** * 分类状态管理 —— 替代旧 store.js 中 categories 部分 */ import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { getCategoryList } from '@/api/category' export const useCategoryStore = defineStore('category', () => { const categories = ref([]) const categoryMap = computed>(() => { const map: Record = {} categories.value.forEach((c: any) => { map[c.id] = c.name }) return map }) function getCategoryName(categoryId: string): string { return categoryMap.value[categoryId] || (categoryId && categoryId !== '0' ? '未知' : '未分类') } async function loadCategories(): Promise { try { const res = await getCategoryList() if (res.success) { categories.value = res.data || [] } } catch (e) { console.error('加载分类失败', e) } } return { categories, categoryMap, getCategoryName, loadCategories } })