edit.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div>
  3. <a-modal
  4. v-model:visible="visible"
  5. width="auto"
  6. :title="id ? $t('searchTable.form.edit') : $t('searchTable.form.add')"
  7. @cancel="() => handleCancel()"
  8. @before-ok="handleBeforeOk"
  9. >
  10. <a-form :model="form" auto-label-width ref="formRef">
  11. <a-row :gutter="8">
  12. <a-col :span="12">
  13. <a-form-item
  14. field="name"
  15. :label="t('device.form.name')"
  16. :rules="getRules(t).required"
  17. >
  18. <a-input v-model="form.name" />
  19. </a-form-item>
  20. </a-col>
  21. <a-col :span="12">
  22. <a-form-item
  23. field="address"
  24. :label="t('device.form.address')"
  25. :rules="getRules(t).required"
  26. >
  27. <a-input v-model="form.address" />
  28. </a-form-item>
  29. </a-col>
  30. <a-col :span="12">
  31. <a-form-item
  32. field="deviceType"
  33. :label="t('device.form.entityType')"
  34. :rules="getRules(t).required"
  35. >
  36. <a-select
  37. v-model="form.deviceType"
  38. allow-clear
  39. @clear="form.deviceType = null"
  40. >
  41. <a-option
  42. v-for="item of deviceTypeList"
  43. :value="item.dictCode"
  44. :label="item.name"
  45. />
  46. </a-select>
  47. </a-form-item>
  48. </a-col>
  49. </a-row>
  50. </a-form>
  51. </a-modal>
  52. </div>
  53. </template>
  54. <script setup lang="ts" name="EditDialog">
  55. import { reactive, ref, shallowRef, watch, getCurrentInstance } from 'vue';
  56. import { getDeviceDetails, saveDeviceDetails } from '@/api/device';
  57. import type { DataList } from '@/api/device';
  58. import type { AdditionalProp } from '@/api/dict';
  59. import { useI18n } from 'vue-i18n';
  60. import { getRules } from '@/utils/const';
  61. import useDictList from '@/hooks/dict-list';
  62. import { useStorage } from '@vueuse/core';
  63. import { STATIONKEY, StationKey } from '@/utils/const';
  64. const formRef = ref();
  65. const { t } = useI18n();
  66. const this_ = getCurrentInstance()?.appContext.config.globalProperties;
  67. interface EditDialogProps {
  68. modelValue: boolean;
  69. id: number | null;
  70. }
  71. interface EditDialogEmits {
  72. (e: 'update:modelValue', value: boolean): void;
  73. (e: 'update-list'): void;
  74. }
  75. const props = withDefaults(defineProps<EditDialogProps>(), {
  76. modelValue: false,
  77. id: null,
  78. });
  79. const emit = defineEmits<EditDialogEmits>();
  80. const visible = shallowRef<boolean>(false);
  81. const deviceTypeList = ref<AdditionalProp[]>([] as AdditionalProp[]);
  82. const obj = useStorage<StationKey>(STATIONKEY, {
  83. id: null,
  84. station: null,
  85. });
  86. watch(
  87. () => props.modelValue,
  88. value => {
  89. visible.value = value;
  90. if (value && props.id) {
  91. getDeviceDetails({ id: props.id }).then(res => {
  92. form.value = res.data;
  93. });
  94. }
  95. }
  96. );
  97. const formModel = () => {
  98. return {
  99. id: 0,
  100. name: '',
  101. address: '',
  102. deviceType: null,
  103. routeInfoId: null,
  104. } as DataList;
  105. };
  106. const form = ref<DataList>(formModel());
  107. const handleBeforeOk = (done: (closed: boolean) => void) => {
  108. formRef.value.validate().then((data: DataList['data']) => {
  109. if (!data) {
  110. form.value.routeInfoId = obj.value.id;
  111. saveDeviceDetails(form.value)
  112. .then(res => {
  113. if (res.success) {
  114. this_?.$message.success(t('message.success'));
  115. handleCancel();
  116. done(true); // 关闭模态框
  117. } else {
  118. res.message && this_?.$message.error(res.message);
  119. done(false); // 关闭loading
  120. }
  121. })
  122. .catch(() => {
  123. done(false); // 不关闭模态框(例如提交失败时)
  124. });
  125. } else {
  126. done(false); // 不关闭模态框(例如提交失败时)
  127. }
  128. });
  129. };
  130. useDictList(['DeviceType']).then(res => {
  131. deviceTypeList.value = res['DeviceType'];
  132. });
  133. const handleCancel = () => {
  134. form.value = formModel();
  135. visible.value = false;
  136. emit('update:modelValue', false);
  137. emit('update-list');
  138. };
  139. </script>