edit.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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('dashboard.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('dashboard.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('dashboard.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/dashboard';
  57. import type { DataList } from '@/api/dashboard';
  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. const formRef = ref();
  63. const { t } = useI18n();
  64. const this_ = getCurrentInstance()?.appContext.config.globalProperties;
  65. interface EditDialogProps {
  66. modelValue: boolean;
  67. id: number | null;
  68. }
  69. interface EditDialogEmits {
  70. (e: 'update:modelValue', value: boolean): void;
  71. (e: 'update-list'): void;
  72. }
  73. const props = withDefaults(defineProps<EditDialogProps>(), {
  74. modelValue: false,
  75. id: null,
  76. });
  77. const emit = defineEmits<EditDialogEmits>();
  78. const visible = shallowRef<boolean>(false);
  79. const deviceTypeList = ref<AdditionalProp[]>([] as AdditionalProp[]);
  80. watch(
  81. () => props.modelValue,
  82. value => {
  83. visible.value = value;
  84. if (value && props.id) {
  85. getDeviceDetails({ id: props.id }).then(res => {
  86. form.value = res.data;
  87. });
  88. }
  89. }
  90. );
  91. const formModel = () => {
  92. return {
  93. id: 0,
  94. name: '',
  95. address: '',
  96. deviceType: null,
  97. } as DataList;
  98. };
  99. const form = ref<DataList>(formModel());
  100. const handleBeforeOk = (done: (closed: boolean) => void) => {
  101. formRef.value.validate().then((data: DataList['data']) => {
  102. if (!data) {
  103. saveDeviceDetails(form.value)
  104. .then(res => {
  105. if (res.success) {
  106. this_?.$message.success(t('message.success'));
  107. handleCancel();
  108. done(true); // 关闭模态框
  109. } else {
  110. res.message && this_?.$message.error(res.message);
  111. done(false); // 关闭loading
  112. }
  113. })
  114. .catch(() => {
  115. done(false); // 不关闭模态框(例如提交失败时)
  116. });
  117. } else {
  118. done(false); // 不关闭模态框(例如提交失败时)
  119. }
  120. });
  121. };
  122. useDictList(['DeviceType']).then(res => {
  123. deviceTypeList.value = res['DeviceType'];
  124. });
  125. const handleCancel = () => {
  126. form.value = formModel();
  127. visible.value = false;
  128. emit('update:modelValue', false);
  129. emit('update-list');
  130. };
  131. </script>