BaseModalCtrl.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <BasicModal @register="register" :title="title" :width="800" :min-height="400" 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" style="position: absolute; right: 0; top: 1px">选择</a-button>
  8. </div>
  9. </template>
  10. </BasicForm>
  11. </BasicModal>
  12. <DevicePointTable @register="registerModal" :data-source="devicePointList" :selection-row-keys="pointData" @reload="setPoint" />
  13. </template>
  14. <script lang="ts" setup>
  15. import { onMounted, ref, unref, nextTick } from 'vue';
  16. import { BasicForm, useForm } from '/@/components/Form/index';
  17. import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  18. import type { FormSchema } from '/@/components/Form/src/types/form';
  19. import { Select, message } from 'ant-design-vue';
  20. import { workFacePointList } from './warning.api';
  21. import DevicePointTable from './DeviceCtrlPointTable.vue';
  22. const props = defineProps({
  23. formSchemas: {
  24. type: Array as PropType<FormSchema[]>,
  25. default: () => [],
  26. },
  27. deviceId: { type: String },
  28. monitorType: {
  29. type: String,
  30. default: '2',
  31. },
  32. deviceType: { type: String },
  33. });
  34. const emit = defineEmits(['add', 'update', 'register']);
  35. const option = ref<any[]>([]);
  36. const devicePointList = ref<any[]>([]);
  37. const pointData = ref<String[]>([]);
  38. const selectedPoint = ref<any>(null);
  39. const selectedDevice = ref<any>(null);
  40. const title = ref('');
  41. const isUpdate = ref(false);
  42. // 注册 form
  43. const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
  44. schemas: props.formSchemas,
  45. showActionButtonGroup: false,
  46. });
  47. // 注册 modal
  48. const [register, { setModalProps }] = useModalInner(async (data) => {
  49. isUpdate.value = unref(data.isUpdate);
  50. title.value = unref(data.title);
  51. await resetFields();
  52. pointData.value = [];
  53. selectedPoint.value = null;
  54. selectedDevice.value = null;
  55. option.value = [];
  56. await resetFields();
  57. if (data.isUpdate) {
  58. await setFieldsValue({ ...data.record });
  59. pointData.value = [data.record['linkMonitorCode']];
  60. await getDevicePointList(data.record['deviceId'] || data.record['linkDeviceId']);
  61. const matchItem = devicePointList.value.find((item) => item['id'] == pointData.value[0] || item['valuecode'] == pointData.value[0]);
  62. if (matchItem) {
  63. setPoint([matchItem]);
  64. }
  65. } else if (data.record) {
  66. await getDevicePointList(data.record['linkDeviceType']);
  67. devicePointList.value.forEach((item) => {
  68. if (item['id'] == data.record['linkMonitorCode'] || item['valuecode'] == data.record['linkMonitorCode']) {
  69. setPoint([item]);
  70. }
  71. });
  72. pointData.value = [data.record['linkMonitorCode']];
  73. await setFieldsValue({ linkMonitorCode: '' });
  74. }
  75. });
  76. const [registerModal, { openModal }] = useModal();
  77. async function getDevicePointList(strtype) {
  78. try {
  79. const result = await workFacePointList({ deviceType: strtype, valueTypes: '1' });
  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, linkMonitorCode: pointData.value[0] });
  89. const values = await validate();
  90. values.deviceId = props.deviceId;
  91. if (selectedPoint.value) {
  92. values.linkMonitorCode = selectedPoint.value.valuecode;
  93. values.linkMonitorId = String(selectedPoint.value.id);
  94. values.linkMonitorName = selectedPoint.value.valuename;
  95. }
  96. console.log(values, 'values');
  97. setModalProps({ confirmLoading: true });
  98. if (!isUpdate.value) {
  99. emit('add', 'add', values);
  100. } else {
  101. emit('update', 'update', values);
  102. }
  103. } finally {
  104. setModalProps({ confirmLoading: false });
  105. }
  106. }
  107. async function selectPoint() {
  108. const values = await getFieldsValue();
  109. const strtype = values.linkDeviceType || props.deviceType;
  110. if (!strtype) {
  111. message.info('请先选择设备类型!');
  112. return;
  113. }
  114. if (!values.linkDeviceId) {
  115. message.info('请先选择关联设备!');
  116. return;
  117. }
  118. await getDevicePointList(strtype);
  119. openModal();
  120. }
  121. function setPoint(value) {
  122. const data = value[0];
  123. selectedPoint.value = data;
  124. option.value = [
  125. {
  126. value: data.id,
  127. label: data.valuename,
  128. },
  129. ];
  130. nextTick(() => {
  131. pointData.value = [data.id];
  132. });
  133. }
  134. onMounted(async () => {});
  135. </script>
  136. <style scoped lang="less"></style>