import { CategoryItem } from "@/typings/asset"; import { defineStore } from "pinia"; import { categoryActions } from "./actions"; import { ListController } from "@/controllers/ListController"; import { request } from "../../objects"; import loading from "@/components/Provider/Loading"; import { message } from "ant-design-vue"; import Modal from "@/components/Provider/Modal"; function setMapItem( id: string, item: any, itemMaps: Map ) { itemMaps.set(id, item); if (item.children instanceof Array) { return item.children.forEach((d: any) => { d.pid = item._id; setMapItem(d._id, d, itemMaps); }); } } export const useCategory = defineStore("category", { state: () => ({ // categories: [], listController: new ListController(request), }), getters: { categories(state) { return state.listController.state.list; }, categoryMap() { const itemMaps = new Map(); setMapItem("_", { children: this.categories }, itemMaps); return itemMaps; }, }, actions: { async initCategories() { this.listController.setCrudPrefix("/category"); await this.listController.loadPage(1, 100); }, async addCategoryItem(pid = "top") { const base = await categoryActions.EditCategoryItem(); const item = { pid, ...base }; loading.show("保存中"); await this.listController.addItem(item); loading.hidden(); message.success("添加成功"); }, async updateCategoryItem(item: any) { const base = await categoryActions.EditCategoryItem(item); loading.show("保存中"); await this.listController.saveItem(base); loading.hidden(); message.success("更新成功"); }, async deleteCategoryItem(item: any) { const ok = await Modal.confirm({ title: "删除确认", content: `删除后数据无法恢复,确认删除分类:${item.name}?`, type: "danger", }); if (ok) { loading.show("删除中"); await this.listController.deleteItem(item._id); loading.hidden(); message.success("删除成功"); } }, }, });