|
@@ -6,38 +6,56 @@ 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<string, CategoryItem>
|
|
|
-) {
|
|
|
- itemMaps.set(id, item);
|
|
|
- if (item.children instanceof Array) {
|
|
|
- return item.children.forEach((d: any) => {
|
|
|
- d.pid = item._id;
|
|
|
- setMapItem(d._id, d, itemMaps);
|
|
|
+
|
|
|
+function parseTreeData(categoryList: any) {
|
|
|
+ const list = [...categoryList];
|
|
|
+
|
|
|
+ let roots = list.filter((item) => item.pid == "top");
|
|
|
+ function findChild(root: CategoryItem, list: CategoryItem[]) {
|
|
|
+ root.children = [];
|
|
|
+ list.forEach((it) => {
|
|
|
+ if (it.pid == root._id) {
|
|
|
+ root.children?.push(it);
|
|
|
+ }
|
|
|
});
|
|
|
+ root.children.forEach((child) => findChild(child, list));
|
|
|
+ }
|
|
|
+ roots.forEach((item) => findChild(item, list));
|
|
|
+
|
|
|
+ function parseTreeItem(item: CategoryItem) {
|
|
|
+ let ret: any = {
|
|
|
+ _id: item._id,
|
|
|
+ name: item.name,
|
|
|
+ type: item.type,
|
|
|
+ };
|
|
|
+ if (item.children?.length) {
|
|
|
+ ret.children = [];
|
|
|
+ item.children.forEach((it) => {
|
|
|
+ ret.children.push(parseTreeItem(it));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
}
|
|
|
+ return roots.map((item) => parseTreeItem(item));
|
|
|
}
|
|
|
export const useCategory = defineStore("category", {
|
|
|
state: () => ({
|
|
|
// categories: [],
|
|
|
- listController: new ListController(request),
|
|
|
+ listController: new ListController<CategoryItem, any>(request),
|
|
|
}),
|
|
|
getters: {
|
|
|
categories(state) {
|
|
|
return state.listController.state.list;
|
|
|
},
|
|
|
- categoryMap() {
|
|
|
- const itemMaps = new Map<string, CategoryItem>();
|
|
|
- setMapItem("_", { children: this.categories }, itemMaps);
|
|
|
- return itemMaps;
|
|
|
+ categoryTree() {
|
|
|
+ const tree = parseTreeData(this.categories);
|
|
|
+ return tree;
|
|
|
},
|
|
|
},
|
|
|
actions: {
|
|
|
async initCategories() {
|
|
|
this.listController.setCrudPrefix("/category");
|
|
|
- await this.listController.loadPage(1, 100);
|
|
|
+ await this.listController.loadPage(1, 200);
|
|
|
},
|
|
|
async addCategoryItem(pid = "top") {
|
|
|
const base = await categoryActions.EditCategoryItem();
|
|
@@ -48,7 +66,12 @@ export const useCategory = defineStore("category", {
|
|
|
message.success("添加成功");
|
|
|
},
|
|
|
async updateCategoryItem(item: any) {
|
|
|
- const base = await categoryActions.EditCategoryItem(item);
|
|
|
+ const res = await this.listController.itemDetail(item._id);
|
|
|
+ if (res.errorNo != 200) {
|
|
|
+ message.warn("未查询到数据!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const base = await categoryActions.EditCategoryItem(res.result);
|
|
|
loading.show("保存中");
|
|
|
await this.listController.saveItem(base);
|
|
|
loading.hidden();
|
|
@@ -57,7 +80,7 @@ export const useCategory = defineStore("category", {
|
|
|
async deleteCategoryItem(item: any) {
|
|
|
const ok = await Modal.confirm({
|
|
|
title: "删除确认",
|
|
|
- content: `删除后数据无法恢复,确认删除分类:${item.name}?`,
|
|
|
+ content: `删除后数据无法恢复,确认删除菜单:${item.name}?`,
|
|
|
type: "danger",
|
|
|
});
|
|
|
if (ok) {
|