You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
983 B
32 lines
983 B
/**
|
|
* 分类状态管理 —— 替代旧 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<any[]>([])
|
|
const categoryMap = computed<Record<string, string>>(() => {
|
|
const map: Record<string, string> = {}
|
|
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<void> {
|
|
try {
|
|
const res = await getCategoryList()
|
|
if (res.success) {
|
|
categories.value = res.data || []
|
|
}
|
|
} catch (e) {
|
|
console.error('加载分类失败', e)
|
|
}
|
|
}
|
|
|
|
return { categories, categoryMap, getCategoryName, loadCategories }
|
|
})
|