DeviceProfileStep1.vue 5.15 KB
<template>
  <div class="step1">
    <div class="step1-form">
      <div
        style="
          width: 12vw;
          height: 24vh;
          margin-left: 25px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <div style="width: 4vw; height: 24vh">请上传图片</div>
        <div class="device-icon-style" style="width: 8vw; height: 24vh">
          <Upload
            style="width: 20vw"
            name="avatar"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :customRequest="customUploadqrcodePic"
            :before-upload="beforeUploadqrcodePic"
          >
            <img
              style="text-align: center; width: 13vw; height: 15vh"
              v-if="peresonalPic"
              :src="peresonalPic"
              alt="avatar"
            />
            <div v-else>
              <div style="margin-top: 30px">
                <PlusOutlined style="font-size: 30px; margin-left: -166px" />
              </div>
              <div
                class="ant-upload-text flex"
                style="width: 280px; height: 130px; align-items: center; margin-left: 43px"
              >
                请上传图片</div
              >
            </div>
          </Upload>
        </div>
      </div>
      <div style="margin-top: -50px">
        <BasicForm @register="register" />
      </div>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { step1Schemas } from './data';
  import { Select, Input, Divider } from 'ant-design-vue';
  import { uploadApi } from '/@/api/personal/index';
  import { Upload } from 'ant-design-vue';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import type { FileItem } from '/@/components/Upload/src/typing';

  export default defineComponent({
    components: {
      BasicForm,
      [Select.name]: Select,
      [Input.name]: Input,
      [Input.Group.name]: Input.Group,
      [Divider.name]: Divider,
      Upload,
      PlusOutlined,
    },
    emits: ['next', 'resetFunc', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const [register, { validate, setFieldsValue, resetFields }] = useForm({
        labelWidth: 100,
        schemas: step1Schemas,
        actionColOptions: {
          span: 14,
        },
        showResetButton: false,
        submitButtonOptions: {
          text: '下一步',
        },
        submitFunc: customSubmitFunc,
      });
      //回显数据
      const setStepOneFieldsValueFunc = (v) => {
        setFieldsValue(v);
      };
      const peresonalPic = ref();

      const resetIconFunc = () => {
        peresonalPic.value = '';
      };

      const editIconFunc = (v) => {
        peresonalPic.value = v;
      };

      const customUploadqrcodePic = async ({ file }) => {
        if (beforeUploadqrcodePic(file)) {
          const formData = new FormData();
          formData.append('file', file);
          const response = await uploadApi(formData);
          if (response.fileStaticUri) {
            peresonalPic.value = response.fileStaticUri;
          }
        }
      };

      const beforeUploadqrcodePic = (file: FileItem) => {
        const isJpgOrPng =
          file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg';
        if (!isJpgOrPng) {
          createMessage.error('只能上传图片文件!');
        }
        const isLt2M = (file.size as number) / 1024 / 1024 < 2;
        if (!isLt2M) {
          createMessage.error('图片大小不能超过5MB!');
        }
        return isJpgOrPng && isLt2M;
      };
      async function customSubmitFunc() {
        try {
          const values = await validate();
          emit('next', values, peresonalPic.value);
        } catch (error) {}
      }
      //清空数据
      const customResetStepOneFunc = () => {
        resetFields();
      };
      return {
        editIconFunc,
        resetIconFunc,
        register,
        setStepOneFieldsValueFunc,
        customResetStepOneFunc,
        uploadApi,
        peresonalPic,
        beforeUploadqrcodePic,
        customUploadqrcodePic,
      };
    },
  });
</script>
<style lang="less" scoped>
  .step1 {
    &-form {
      width: 500px;
      margin: 0 auto;
    }

    h3 {
      margin: 0 0 12px;
      font-size: 16px;
      line-height: 32px;
      color: @text-color;
    }

    h4 {
      margin: 0 0 4px;
      font-size: 14px;
      line-height: 22px;
      color: @text-color;
    }

    p {
      color: @text-color;
    }
    .device-icon-style {
      :deep .ant-upload-select-picture-card {
        display: inherit;
        float: none;
        width: 8.6vw;
        height: 17vh;
        margin-right: 8px;
        text-align: center;
        vertical-align: top;
        background-color: #fafafa;
        border: 1px dashed #d9d9d9;
        cursor: pointer;
        transition: border-color 0.3s ease;
      }
    }
  }

  .pay-select {
    width: 20%;
  }

  .pay-input {
    width: 70%;
  }
</style>