BaseModal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <BasicModal @register="register" :title="title" :width="800" :min-height="600" v-bind="$attrs" @ok="onSubmit">
  3. <BasicForm @register="registerForm">
  4. <template #monitor="{ model, field }">
  5. <div class="vent-flex-row-between">
  6. <Select ref="selectRef" disabled v-model:value="pointData" :options="option" style="width: calc(100% - 65px)" />
  7. <a-button class="vent-margin-b-5" type="primary" @click="selectPoint(model['strtype'])" style="position: absolute; right: 0; top: 1px"
  8. >选择</a-button
  9. >
  10. </div>
  11. </template>
  12. </BasicForm>
  13. </BasicModal>
  14. <DevicePointTable @register="registerModal" :data-source="devicePointList" :selection-row-keys="pointData" @reload="setPoint" />
  15. </template>
  16. <script lang="ts" setup>
  17. import { onMounted, ref, defineEmits, unref, nextTick } from 'vue';
  18. import { BasicForm, useForm } from '/@/components/Form/index';
  19. import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  20. import type { FormSchema } from '/@/components/Form/src/types/form';
  21. import { Select, message } from 'ant-design-vue';
  22. import DevicePointTable from './DevicePointTable.vue';
  23. import { workFacePointList } from './warning.api';
  24. const props = defineProps({
  25. formSchemas: {
  26. type: Array as PropType<FormSchema[]>,
  27. default: () => [],
  28. },
  29. deviceId: { type: String },
  30. monitorType: {
  31. type: String,
  32. default: '2,3,9',
  33. },
  34. });
  35. const emit = defineEmits(['add', 'update', 'register']);
  36. const option = ref<any[]>([]);
  37. const devicePointList = ref<any[]>([]);
  38. const pointData = ref<string[]>([]);
  39. const title = ref('');
  40. const isUpdate = ref(false);
  41. // 注册 form
  42. const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
  43. schemas: props.formSchemas,
  44. showActionButtonGroup: false,
  45. });
  46. // 注册 modal
  47. const [register, { setModalProps }] = useModalInner(async (data) => {
  48. isUpdate.value = unref(data.isUpdate);
  49. title.value = unref(data.title);
  50. await resetFields();
  51. pointData.value = [];
  52. if (data.isUpdate) {
  53. //
  54. await getDevicePointList(data.record['deviceType']);
  55. devicePointList.value.forEach((item) => {
  56. if (item['id'] == data.record['monitorId']) {
  57. setPoint([item]);
  58. }
  59. });
  60. await setFieldsValue({ ...data.record });
  61. pointData.value = [data.record['monitorId']];
  62. // 初始打开有数据时候要查点表
  63. }
  64. if (data.record) {
  65. await getDevicePointList(data.record['deviceType']);
  66. devicePointList.value.forEach((item) => {
  67. if (item['id'] == data.record['monitorId']) {
  68. setPoint([item]);
  69. }
  70. });
  71. pointData.value = [data.record['monitorId']];
  72. const relId = data.isUpdate ? data.record['relId'] : data.record['relId'] || data.record['id'];
  73. await setFieldsValue({ relId, monitorId: '' });
  74. }
  75. });
  76. const [registerModal, { openModal }] = useModal();
  77. async function getDevicePointList(strtype) {
  78. try {
  79. const result = await workFacePointList({ deviceType: strtype, valueTypes: props.monitorType }); // 1控制点位、 2读取点位、3计算点位、9关联点位
  80. devicePointList.value = result;
  81. } catch (error) {
  82. devicePointList.value = [];
  83. }
  84. }
  85. async function onSubmit() {
  86. try {
  87. const data = await getFieldsValue();
  88. await setFieldsValue({ ...data, monitorId: pointData.value[0] });
  89. const values = await validate();
  90. setModalProps({ confirmLoading: true });
  91. // 提交表单
  92. if (!isUpdate.value) {
  93. emit('add', 'add', values);
  94. } else {
  95. emit('update', 'update', values);
  96. }
  97. // //关闭弹窗
  98. // closeModal();
  99. // //刷新列表
  100. // reload()
  101. } finally {
  102. setModalProps({ confirmLoading: false });
  103. }
  104. }
  105. async function selectPoint(strtype) {
  106. // 先将设备名称删除再赋值
  107. const data = await getFieldsValue();
  108. const deviceId = data['deviceId'];
  109. debugger;
  110. if (strtype) {
  111. await getDevicePointList(strtype);
  112. openModal();
  113. } else {
  114. message.info('请先选择设备!');
  115. }
  116. }
  117. function setPoint(value) {
  118. const data = value[0];
  119. option.value = [
  120. {
  121. value: data.id,
  122. label: data.valuename,
  123. },
  124. ];
  125. nextTick(() => {
  126. pointData.value = [data.id];
  127. });
  128. }
  129. onMounted(async () => {});
  130. </script>
  131. <style scoped lang="less"></style>