index.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { CategoryItem } from "@/typings/asset";
  2. import { defineStore } from "pinia";
  3. import { categoryActions } from "./actions";
  4. import { ListController } from "@/controllers/ListController";
  5. import { request } from "../../objects";
  6. import loading from "@/components/Provider/Loading";
  7. import { message } from "ant-design-vue";
  8. import Modal from "@/components/Provider/Modal";
  9. function setMapItem(
  10. id: string,
  11. item: any,
  12. itemMaps: Map<string, CategoryItem>
  13. ) {
  14. itemMaps.set(id, item);
  15. if (item.children instanceof Array) {
  16. return item.children.forEach((d: any) => {
  17. d.pid = item._id;
  18. setMapItem(d._id, d, itemMaps);
  19. });
  20. }
  21. }
  22. export const useCategory = defineStore("category", {
  23. state: () => ({
  24. // categories: [],
  25. listController: new ListController(request),
  26. }),
  27. getters: {
  28. categories(state) {
  29. return state.listController.state.list;
  30. },
  31. categoryMap() {
  32. const itemMaps = new Map<string, CategoryItem>();
  33. setMapItem("_", { children: this.categories }, itemMaps);
  34. return itemMaps;
  35. },
  36. },
  37. actions: {
  38. async initCategories() {
  39. this.listController.setCrudPrefix("/category");
  40. await this.listController.loadPage(1, 100);
  41. },
  42. async addCategoryItem(pid = "top") {
  43. const base = await categoryActions.EditCategoryItem();
  44. const item = { pid, ...base };
  45. loading.show("保存中");
  46. await this.listController.addItem(item);
  47. loading.hidden();
  48. message.success("添加成功");
  49. },
  50. async updateCategoryItem(item: any) {
  51. const base = await categoryActions.EditCategoryItem(item);
  52. loading.show("保存中");
  53. await this.listController.saveItem(base);
  54. loading.hidden();
  55. message.success("更新成功");
  56. },
  57. async deleteCategoryItem(item: any) {
  58. const ok = await Modal.confirm({
  59. title: "删除确认",
  60. content: `删除后数据无法恢复,确认删除分类:${item.name}?`,
  61. type: "danger",
  62. });
  63. if (ok) {
  64. loading.show("删除中");
  65. await this.listController.deleteItem(item._id);
  66. loading.hidden();
  67. message.success("删除成功");
  68. }
  69. },
  70. },
  71. });