DeviceModal.vue 4.03 KB
<template>
  <BasicModal
    v-bind="$attrs"
    width="55rem"
    @register="register"
    :title="getTitle"
    @cancel="handleCancel"
    @ok="handleOk"
    centered
  >
    <div class="step-form-form">
      <a-steps :current="current">
        <a-step title="填写设备信息" />
        <a-step title="添加设备凭证" />
      </a-steps>
    </div>
    <div class="mt-5">
      <DeviceStep1 @next="handleStep1Next" v-show="current === 0" ref="DeviceStep1Ref" />
      <DeviceStep2
        ref="DeviceStep2Ref"
        @prev="handleStepPrev"
        @next="handleStep2Next"
        v-show="current === 1"
        v-if="initStep2"
      />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, reactive, toRefs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { createOrEditDevice } from '/@/api/device/deviceManager';
  import DeviceStep1 from '/@/views/device/step/DeviceStep1.vue';
  import DeviceStep2 from '/@/views/device/step/DeviceStep2.vue';
  import { Steps } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  export default defineComponent({
    name: 'DeviceModal',
    components: {
      BasicModal,
      DeviceStep1,
      DeviceStep2,
      [Steps.name]: Steps,
      [Steps.Step.name]: Steps.Step,
    },
    props: {
      userData: { type: Object },
    },
    setup(_, { emit }) {
      const DeviceStep1Ref = ref<InstanceType<typeof DeviceStep1>>();
      const DeviceStep2Ref = ref<InstanceType<typeof DeviceStep2>>();
      const { createMessage } = useMessage();
      const state = reactive({
        initStep2: false,
      });
      const current = ref(0);
      const isUpdate = ref(true);
      const getTitle = computed(() => (!unref(isUpdate) ? '新增设备' : '编辑设备'));
      // 所有参数
      let stepState = ref();
      // 编辑回显
      const [register, { closeModal }] = useModalInner((data) => {
        if (data.isUpdate) {
          DeviceStep1Ref.value?.parentSetFieldsValue(data.record);
          DeviceStep1Ref.value?.parentSetFieldsValue({
            profile: data.record.deviceProfile.name,
            remark: data.record.deviceInfo.description,
            profileId: data.record.profileId,
          });
        }
        isUpdate.value = !!data?.isUpdate;
      });

      // 上一步
      function handleStepPrev() {
        current.value--;
      }
      // 下一步
      function handleStep1Next(step1Values: any) {
        current.value++;
        state.initStep2 = true;
        stepState = { ...step1Values };
      }
      //
      function handleStep2Next(step2Values: any) {
        stepState = { ...stepState, ...step2Values };
      }
      function handleCancel() {
        current.value = 0;
        DeviceStep1Ref?.value?.resetFields();
        DeviceStep2Ref?.value?.resetFields();
      }
      // 提交
      async function handleOk() {
        if (current.value === 0) {
          // 验证
          const valid = await DeviceStep1Ref.value?.parentValidate();
          if (!valid) return;

          stepState.value = DeviceStep1Ref?.value?.parentGetFieldsValue();
          handleCancel();
          closeModal();
        } else {
          if (!DeviceStep2Ref?.value?.creaentialsPassword.isCreaentials) {
            const valid = await DeviceStep2Ref?.value?.validate();
            if (!valid) return;
            // 第二页验证通过情况
            handleCancel();
            closeModal();
          }
        }
        // 验证成功 --掉新增或者编辑接口
        const msg = computed(() => (stepState.value.id ? '更新设备成功' : '新增设备成功'));
        await createOrEditDevice(stepState.value);
        createMessage.success(msg.value);
        emit('reload');
      }
      return {
        register,
        getTitle,
        current,
        DeviceStep1Ref,
        DeviceStep2Ref,
        ...toRefs(state),
        handleStepPrev,
        handleStep1Next,
        handleStep2Next,
        handleCancel,
        handleOk,
      };
    },
  });
</script>