usePlanStore.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {
  2. addPlanFile,
  3. addPlanItem,
  4. deletePlanItem,
  5. editPlanItem,
  6. getPlanItem,
  7. getPlanList,
  8. GetPlanListParams,
  9. PlanFilePatams,
  10. PlanItemDetail,
  11. PlanItemParams,
  12. PlanListResponse,
  13. } from '@/api/plan';
  14. import { ElMessage } from 'element-plus';
  15. import { defineStore } from 'pinia';
  16. export interface IncidentStateType {
  17. plans: PlanListResponse;
  18. planItemDetail: PlanItemDetail;
  19. loading: boolean;
  20. }
  21. export interface IncidentActionsType {
  22. toggleLoading(): void;
  23. getPlanList(params: GetPlanListParams): void;
  24. getPlanItem(id: string): void;
  25. addPlanItem(params: PlanItemParams): Promise<boolean>;
  26. editPlanItem(params: PlanItemParams): Promise<boolean>;
  27. deletePlanItem(id: string): Promise<boolean>;
  28. addPlanFile(params: PlanFilePatams): Promise<boolean>;
  29. }
  30. export default defineStore<'plan', IncidentStateType, {}, IncidentActionsType>(
  31. 'plan',
  32. {
  33. state: () => ({
  34. plans: {
  35. rows: [],
  36. total: 0,
  37. },
  38. planItemDetail: {},
  39. loading: false,
  40. }),
  41. actions: {
  42. toggleLoading() {
  43. this.loading = !this.loading;
  44. },
  45. async getPlanList(params) {
  46. try {
  47. this.toggleLoading();
  48. this.plans = await getPlanList(params);
  49. } finally {
  50. this.toggleLoading();
  51. }
  52. },
  53. async getPlanItem(id) {
  54. try {
  55. this.toggleLoading();
  56. const { data } = await getPlanItem(id);
  57. if (data) this.planItemDetail = data;
  58. } finally {
  59. this.toggleLoading();
  60. }
  61. },
  62. async addPlanItem(id) {
  63. try {
  64. this.toggleLoading();
  65. await addPlanItem(id);
  66. ElMessage.success({ message: '保存成功' });
  67. return true;
  68. } finally {
  69. this.toggleLoading();
  70. }
  71. },
  72. async editPlanItem(params) {
  73. try {
  74. this.toggleLoading();
  75. await editPlanItem(params);
  76. ElMessage.success({ message: '修改成功' });
  77. return true;
  78. } catch {
  79. ElMessage.error({ message: '修改失败' });
  80. return false;
  81. } finally {
  82. this.toggleLoading();
  83. }
  84. },
  85. async deletePlanItem(params) {
  86. try {
  87. this.toggleLoading();
  88. await deletePlanItem(params);
  89. ElMessage.success({ message: '删除成功' });
  90. return true;
  91. } catch {
  92. ElMessage.error({ message: '删除失败' });
  93. return false;
  94. } finally {
  95. this.toggleLoading();
  96. }
  97. },
  98. async addPlanFile(id) {
  99. try {
  100. await addPlanFile(id);
  101. ElMessage.success({ message: '导入成功' });
  102. return true;
  103. } finally {
  104. }
  105. },
  106. },
  107. },
  108. );