index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { defineStore } from "pinia";
  2. import { categoryActions } from "./actions";
  3. import { ListController } from "@/controllers/ListController";
  4. import { request } from "../../objects";
  5. import loading from "@/components/Provider/Loading";
  6. import { message } from "ant-design-vue";
  7. import Modal from "@/components/Provider/Modal";
  8. function parseTreeData(categoryList: any) {
  9. const list = [...categoryList];
  10. let roots = list.filter((item) => item.pid == "top");
  11. function findChild(root: CategoryItem, list: CategoryItem[]) {
  12. root.children = [];
  13. list.forEach((it) => {
  14. if (it.pid == root._id) {
  15. root.children?.push(it);
  16. }
  17. });
  18. root.children.forEach((child) => findChild(child, list));
  19. }
  20. roots.forEach((item) => findChild(item, list));
  21. function parseTreeItem(item: CategoryItem) {
  22. let ret: any = {
  23. _id: item._id,
  24. name: item.name,
  25. type: item.type,
  26. };
  27. if (item.children?.length) {
  28. ret.children = [];
  29. item.children.forEach((it) => {
  30. ret.children.push(parseTreeItem(it));
  31. });
  32. }
  33. return ret;
  34. }
  35. return roots.map((item) => parseTreeItem(item));
  36. }
  37. export const useCategory = defineStore("category", {
  38. state: () => ({
  39. // categories: [],
  40. listController: new ListController<CategoryItem, any>(request),
  41. }),
  42. getters: {
  43. categories(state) {
  44. return state.listController.state.list;
  45. },
  46. categoryTree() {
  47. const tree = parseTreeData(this.categories);
  48. return tree;
  49. },
  50. },
  51. actions: {
  52. async initCategories() {
  53. this.listController.setCrudPrefix("/category");
  54. await this.listController.loadPage(1, 200);
  55. },
  56. async addCategoryItem(pid = "top") {
  57. const base = await categoryActions.EditCategoryItem();
  58. const item = { pid, ...base };
  59. loading.show("保存中");
  60. await this.listController.addItem(item);
  61. loading.hidden();
  62. message.success("添加成功");
  63. },
  64. async updateCategoryItem(item: any) {
  65. const res = await this.listController.itemDetail(item._id);
  66. if (res.errorNo != 200) {
  67. message.warn("未查询到数据!");
  68. return;
  69. }
  70. const base = await categoryActions.EditCategoryItem(res.result);
  71. loading.show("保存中");
  72. await this.listController.saveItem(base);
  73. loading.hidden();
  74. message.success("更新成功");
  75. },
  76. async deleteCategoryItem(item: any) {
  77. const ok = await Modal.confirm({
  78. title: "删除确认",
  79. content: `删除后数据无法恢复,确认删除菜单:${item.name}?`,
  80. type: "danger",
  81. });
  82. if (ok) {
  83. loading.show("删除中");
  84. await this.listController.deleteItem(item._id);
  85. loading.hidden();
  86. message.success("删除成功");
  87. }
  88. },
  89. },
  90. });