DeviceStep2.vue 5.3 KB
<template>
  <div class="step2">
    <div
      ><input type="checkbox" v-model="creaentialsPassword.isCreaentials" @click="checked" />
      添加凭证
    </div>
    <a-form
      ref="formRef"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      v-show="creaentialsPassword.isCreaentials"
      :model="creaentialsPassword"
      labelAlign="left"
    >
      <a-form-item label="凭据类型" name="creaentialsType">
        <a-select
          v-model:value="creaentialsPassword.creaentialsType"
          style="width: 200px"
          @change="handleChange"
          placeholder="请选择凭据类型"
          :options="options"
        />
      </a-form-item>
      <div v-if="creaentialsPassword.creaentialsType === 'Access token'">
        <a-form-item label="访问令牌" name="token">
          <a-input type="password" style="width: 200px" v-model:value="creaentialsPassword.token" />
        </a-form-item>
      </div>
      <div v-else-if="creaentialsPassword.creaentialsType === 'X.509'">
        <a-form-item label="RSA公钥" name="publicKey">
          <a-input
            type="password"
            style="width: 200px"
            v-model:value="creaentialsPassword.publicKey"
          />
        </a-form-item>
      </div>
      <div v-else>
        <a-form-item label="客户端ID" name="clientId">
          <a-input type="input" style="width: 200px" v-model:value="creaentialsPassword.clientId" />
        </a-form-item>
        <a-form-item label="用户名" name="username">
          <a-input type="input" style="width: 200px" v-model:value="creaentialsPassword.username" />
        </a-form-item>
        <a-form-item label="密码" name="password">
          <a-input
            type="password"
            style="width: 200px"
            v-model:value="creaentialsPassword.password"
          />
        </a-form-item>
      </div>
    </a-form>
    <div class="flex justify-start">
      <a-button class="mr-5" @click="prevStep">上一步</a-button>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref, watch } from 'vue';

  import { Input, Form, Select, Button } from 'ant-design-vue';
  export default defineComponent({
    components: {
      [Form.name]: Form,
      [Form.Item.name]: Form.Item,
      [Input.name]: Input,
      [Select.name]: Select,
      [Button.name]: Button,
    },

    emits: ['prev', 'next'],
    setup(_, { emit }) {
      const formRef = ref();
      const creaentialsPassword = reactive({
        isCreaentials: false,
        creaentialsType: 'Access token',
        token: '',
        publicKey: '',
        clientId: '',
        username: '',
        password: '',
      });
      const rules = {
        token: [{ required: true, message: '请输入访问令牌', trigger: 'blur' }],
        publicKey: [{ required: true, message: '请输入RSA公钥', trigger: 'blur' }],
        clientId: [{ required: true, message: '请输入客户端ID', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      };
      const options = ref([
        {
          value: 'Access token',
          label: 'Access token',
        },

        {
          value: 'X.509',
          label: 'X.509',
        },
        {
          value: 'MQTT Basic',
          label: 'MQTT Basic',
        },
      ]);
      // 切换是否添加凭证
      const checked = () => {
        creaentialsPassword.isCreaentials = !creaentialsPassword.isCreaentials;
        formRef.value.resetFields();
        creaentialsPassword.creaentialsType = 'Access token';
      };
      const prevStep = () => {
        emit('prev');
      };
      watch(creaentialsPassword, (newValue) => {
        emit('next', newValue);
      });

      // 切换凭证类型时,重置字段
      const handleChange = (value: string) => {
        if (value === 'Access token') {
          resetCreantialsType();
        } else if (value === 'X.509') {
          resetCreantialsType();
        } else {
          resetCreantialsType();
        }
      };
      // 切换凭证类型时,重置字段
      function resetCreantialsType() {
        creaentialsPassword.token = '';
        creaentialsPassword.publicKey = '';
        creaentialsPassword.clientId = '';
        creaentialsPassword.username = '';
        creaentialsPassword.password = '';
      }

      // 重置所有字段
      function resetFields() {
        creaentialsPassword.isCreaentials = false;
        creaentialsPassword.creaentialsType = 'Access token';
        creaentialsPassword.token = '';
        creaentialsPassword.publicKey = '';
        creaentialsPassword.clientId = '';
        creaentialsPassword.username = '';
        creaentialsPassword.password = '';
        formRef.value.resetFields();
      }

      // 表单验证
      function validate() {
        if (creaentialsPassword.isCreaentials) {
          return formRef.value.validate();
        }
        return true;
      }
      return {
        options,
        formRef,
        creaentialsPassword,
        handleChange,
        prevStep,
        checked,
        labelCol: { style: { width: '150px' } },
        wrapperCol: { span: 18 },
        resetFields,
        validate,
        rules,
      };
    },
  });
</script>
<style lang="less" scoped>
  .step2 {
    width: 700px;
    margin: 0 auto;
  }
</style>