vant单张图片上传

vant单张图片上传

<template>
  <div class="upload-health-certificate">
    <van-form
      ref="form"
      class="form"
      error-message-align="right"
      input-align="right"
      :show-error="false"
    >
      <van-field
        clickable
        label="发证时间"
        placeholder="请选择发证时间"
        readonly
        right-icon="arrow"
        :rules="rules.checkDate"
        :value="form.checkDate"
        @click="picker.show = true"
      />
      <van-field
        clickable
        label="发证时间"
        placeholder="请选择发证时间"
        readonly
        :rules="rules.photo"
        :value="form.photo"
      >
        <template #input>
          <div class="file-wrapper">
            <div v-show="!form.photo" class="upload" @click="chooseImg">
              <i></i>
              <p>上传照片</p>
            </div>
            <van-image
              v-show="form.photo"
              class="image"
              fit="cover"
              :src="form.photo"
            />
            <i v-show="form.photo" class="delete" @click="form.photo = ''"></i>
          </div>
        </template>
      </van-field>
    </van-form>
    <div class="bottom-btn-group">
      <button @click="onSubmit">提交审核</button>
    </div>
    <van-popup v-model="picker.show" position="bottom">
      <van-datetime-picker
        v-model="picker.currentDate"
        :max-date="picker.maxDate"
        title="选择时间"
        type="date"
        @cancel="picker.show = false"
        @confirm="onConfirm"
      />
    </van-popup>
  </div>
</template>

<script>
  import { Form, Field, Popup, DatetimePicker, Image } from 'vant'
  import mixJsBridge from '@/utils/mixJsBridge'
  import { uploadFiles } from '@/api/public'
  import { uploadHealthCertificate } from '@/api/housekeeper'
  import dayJs from 'dayjs'

  export default {
    components: {
      [Form.name]: Form,
      [Field.name]: Field,
      [Popup.name]: Popup,
      [DatetimePicker.name]: DatetimePicker,
      [Image.name]: Image,
    },
    data() {
      return {
        housekeeperId: this.$route.query.housekeeperId,
        form: {
          checkDate: '',
          photo: '',
        },
        rules: {
          checkDate: [
            { required: true, message: '请选择发证时间', trigger: 'onChange' },
          ],
          photo: [
            { required: true, message: '请上传照片', trigger: 'onChange' },
          ],
        },
        picker: {
          show: false,
          minDate: new Date(2000, 0, 1),
          maxDate: new Date(),
          currentDate: new Date(),
        },
      }
    },
    methods: {
      chooseImg() {
        mixJsBridge
          ._chooseImage()
          .then((data) => {
            data.map((item) => {
              this.upload(item.split(',')[1])
            })
          })
          .catch(() => {
            // this.$baseToast(err.result)
          })
      },
      async upload(base64Str) {
        const { data } = await uploadFiles({
          attach_type: 'examine',
          attach_data: base64Str,
          suffix: 'jpeg',
        })
        this.form.photo = `${process.env.VUE_APP_IMG_URL}${data.url}`
      },
      onConfirm(val) {
        this.form.checkDate = dayJs(val).format('YYYY-MM-DD')
        this.picker.show = false
      },
      onSubmit() {
        this.$refs.form
          .validate()
          .then(() => {
            const { checkDate, photo } = this.form
            uploadHealthCertificate({
              checkup_date: checkDate,
              photo_urls: [photo],
            }).then(() => {
              this.$router.go(-1)
            })
          })
          .catch((err) => {
            console.log(err)
          })
      },
    },
  }
</script>

<style type="text/scss" lang="scss" scoped>
  ::v-deep {
    .van-field__right-icon {
      color: #bdbdbd;
    }
    .van-cell--required::before {
      left: 12px;
    }
    .van-cell {
      padding: 10px 16px;
    }
  }
  .file-wrapper {
    position: relative;
    .upload {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 146px;
      height: 168px;
      background: #f8f8f8;
      border: 2px dashed #cdcdcd;
      border-radius: 4px;
      i {
        width: 36px;
        height: 38px;
        background: url('~@/assets/img/tz@3x.png') no-repeat;
        background-size: contain;
      }
      p {
        margin-top: 20px;
        font-size: 24px;
        line-height: 34px;
        color: #999;
      }
    }
    .image {
      width: 146px;
      height: 168px;
    }
    i.delete {
      position: absolute;
      top: -12px;
      right: -12px;
      width: 36px;
      height: 36px;
      background: url('~@/assets/img/sc@3x.png') no-repeat;
      background-size: contain;
    }
  }
  .bottom-btn-group {
    background: transparent;
  }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周亚鑫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值