PageWarningInfoItem.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div class="safety-platform-container">
  3. <header class="safety-platform-container__header">
  4. <BreadcrumbBack />
  5. <span class="breadcrumb-title">{{ headerTitle }}</span>
  6. </header>
  7. <main class="safety-platform-container__main">
  8. <component :is="dynamicComponent" :id="id" ref="dynamicComponentRef" />
  9. </main>
  10. <footer class="safety-platform-container__footer" v-if="operate">
  11. <el-button @click="router.back()">取消</el-button>
  12. <el-button type="primary" @click="submit">提交</el-button>
  13. </footer>
  14. <UploadLoading :form-loading="formLoading" v-if="formLoading" />
  15. </div>
  16. </template>
  17. <script lang="ts" setup>
  18. import { useRoute, useRouter } from 'vue-router';
  19. import { ref, computed, defineAsyncComponent } from 'vue';
  20. import { ElMessage } from 'element-plus';
  21. import UploadLoading from '@/components/UploadLoading.vue';
  22. import type { WarningInfoRuleForm } from './src/type';
  23. import { createWarningInfoItem, editWarningInfoItem } from '@/api/disaster-warning';
  24. const formLoading = ref(false);
  25. const router = useRouter();
  26. const route = useRoute();
  27. const operate = route.query.operate;
  28. const id = route.query.id;
  29. const headerTitle = computed(() => {
  30. const fixedTitle = '灾害预警信息';
  31. if (operate === 'create') {
  32. return `创建${fixedTitle}`;
  33. } else if (operate === 'edit') {
  34. return `编辑${fixedTitle}`;
  35. }
  36. return '未知操作';
  37. });
  38. const dynamicComponent = computed(() => {
  39. if (operate === 'create') {
  40. return defineAsyncComponent(() => import('./src/components/CreateWarningInfoItem.vue'));
  41. } else if (operate === 'edit') {
  42. return defineAsyncComponent(() => import('./src/components/EditWarningInfoItem.vue'));
  43. } else {
  44. return '';
  45. }
  46. });
  47. const dynamicComponentRef = ref();
  48. const createWarningInfoItemFunc = async (formData: WarningInfoRuleForm) => {
  49. const createParam = {
  50. disasterType: formData.disasterType,
  51. disasterLevel: formData.disasterLevel,
  52. warnTime: formData.warnTime,
  53. source: formData.source,
  54. content: formData.content,
  55. isPush: formData.isPush,
  56. userGroupList: formData.isPush ? formData.userGroupList : [],
  57. };
  58. await createWarningInfoItem(createParam);
  59. };
  60. const editWarningInfoItemFunc = async (formData: WarningInfoRuleForm) => {
  61. const editParam = {
  62. id: Number(id),
  63. disasterType: formData.disasterType,
  64. disasterLevel: formData.disasterLevel,
  65. warnTime: formData.warnTime,
  66. source: formData.source,
  67. content: formData.content,
  68. isPush: formData.isPush,
  69. userGroupList: formData.isPush ? formData.userGroupList : [],
  70. };
  71. await editWarningInfoItem(editParam);
  72. };
  73. const submit = async () => {
  74. if (!dynamicComponentRef.value) return;
  75. const res = await dynamicComponentRef.value.handleValidate();
  76. if (!res) return;
  77. const formData = dynamicComponentRef.value.getFormData();
  78. let message;
  79. try {
  80. formLoading.value = true;
  81. if (operate === 'create') {
  82. await createWarningInfoItemFunc(formData);
  83. message = '创建成功';
  84. } else if (operate === 'edit') {
  85. await editWarningInfoItemFunc(formData);
  86. message = '编辑成功';
  87. }
  88. ElMessage.success(message);
  89. router.back();
  90. } finally {
  91. formLoading.value = false;
  92. }
  93. };
  94. </script>
  95. <style lang="scss" scoped>
  96. @use '@/styles/page-details-layout.scss' as *;
  97. @use './src/common.scss' as *;
  98. </style>