DrillPlanRecordItem.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div v-if="recordData !== undefined">
  3. <DrillPlanRecordForm
  4. ref="drillPlanRecordFormRef"
  5. :disabled="disableForm"
  6. :drill-record="recordData"
  7. @submit-drill-record="finishSubmitDrillRecord"
  8. />
  9. </div>
  10. </template>
  11. <script setup lang="ts">
  12. import DrillPlanRecordForm from './DrillPlanRecordForm.vue';
  13. import { onMounted, ref } from 'vue';
  14. import { useRoute } from 'vue-router';
  15. import { DrillPlanRecord, DrillRecordRuleForm } from '../types';
  16. import {
  17. queryEmergencyDrillRecordInEdit,
  18. saveEmergencyDrillRecord,
  19. submitEmergencyDrillRecord,
  20. } from '@/api/emergency-drill/emergency-drill';
  21. import { ElMessage } from 'element-plus';
  22. import { UPLOAD_BIZ_TYPE, uploadFileApi } from '@/api/minio';
  23. const route = useRoute();
  24. const id = route.query.planId;
  25. const recordData = ref<DrillPlanRecord | null>();
  26. const disableForm = ref(false);
  27. const emits = defineEmits<{
  28. (e: 'recordSubmitted'): void;
  29. }>();
  30. async function getDrillData() {
  31. try {
  32. recordData.value = await queryEmergencyDrillRecordInEdit(Number(id));
  33. if (recordData.value.approvalStatus != null && recordData.value.approvalStatus !== 2) {
  34. emits('recordSubmitted');
  35. disableForm.value = true;
  36. }
  37. } catch (e) {
  38. recordData.value = null;
  39. console.log(e);
  40. }
  41. }
  42. onMounted(() => {
  43. getDrillData();
  44. });
  45. const drillPlanRecordFormRef = ref();
  46. async function saveDrillRecord() {
  47. if (!drillPlanRecordFormRef) return;
  48. const formData: DrillRecordRuleForm = drillPlanRecordFormRef.value.getFormData();
  49. formData.drillImage =
  50. '[' +
  51. (
  52. await Promise.all(
  53. (formData.drillImagesFile || []).map((item) => {
  54. if (!item.file && item.url) {
  55. return '"' + item.url + '"';
  56. } else {
  57. return formatImageList(item.file!);
  58. }
  59. }),
  60. )
  61. ).join(',') +
  62. ']';
  63. if (!formData.drillPlanId) formData.drillPlanId = Number(id);
  64. try {
  65. // console.log(formData);
  66. await saveEmergencyDrillRecord(formData);
  67. ElMessage.success('保存成功');
  68. drillPlanRecordFormRef.value.formClearValidate();
  69. recordData.value = undefined;
  70. getDrillData();
  71. } catch (e) {
  72. console.log(e);
  73. ElMessage.error('保存失败');
  74. }
  75. }
  76. async function startSubmitDrillRecord() {
  77. if (!drillPlanRecordFormRef) return;
  78. const res = await drillPlanRecordFormRef.value.formValidate();
  79. if (!res) return;
  80. drillPlanRecordFormRef.value.openApprovalDialog();
  81. }
  82. async function finishSubmitDrillRecord(innerFormData: DrillRecordRuleForm) {
  83. try {
  84. innerFormData.drillImage =
  85. '[' +
  86. (
  87. await Promise.all(
  88. (innerFormData.drillImagesFile || []).map((item) => {
  89. if (!item.file && item.url) {
  90. return '"' + item.url + '"';
  91. } else {
  92. return formatImageList(item.file!);
  93. }
  94. }),
  95. )
  96. ).join(',') +
  97. ']';
  98. if (!innerFormData.drillPlanId) innerFormData.drillPlanId = Number(id);
  99. await submitEmergencyDrillRecord(innerFormData);
  100. ElMessage.success('提交成功');
  101. drillPlanRecordFormRef.value.formClearValidate();
  102. drillPlanRecordFormRef.value.closeApprovalDialog();
  103. recordData.value = undefined;
  104. getDrillData();
  105. } catch (e) {
  106. console.log(e);
  107. ElMessage.error('提交失败');
  108. }
  109. }
  110. const formatImageList = async (file: File) => {
  111. if (!file) return file;
  112. const uuid = Math.random().toString(36).substring(2, 9);
  113. const timestamp = Date.now().toString();
  114. const random = Math.random().toString(36).substring(2, 4);
  115. const res = await uploadFileApi({
  116. bizType: UPLOAD_BIZ_TYPE.ATTACHMENT,
  117. fileName: `${uuid}-${timestamp}-${random}`,
  118. file,
  119. });
  120. return '"' + res.url + '"';
  121. };
  122. function enableEditRecord() {
  123. disableForm.value = false;
  124. }
  125. defineExpose({
  126. saveDrillRecord,
  127. startSubmitDrillRecord,
  128. enableEditRecord,
  129. });
  130. </script>
  131. <style scoped></style>