qRcodeDialog.vue 3.91 KB
<!-- 下载对账单 -->
<template>
  <d2-dialog title="收款二维码"
             :isVisible="qRcodeVisible"
             class="qRcodeDailog"
             width="40%"
             btnOkLabel="下载二维码"
             @closeDialog="handleClose"
             @confirmDialog="handleAddConfirm">
    <div class="codeDiv"
         id="view">
      <img style="width:100%;height:100%"
           src="./images/code3.png">
      <div class="imgDiv">
        <img style="width:100%;height:100%"
             :src="qrCodeURL?qrCodeURL:defaultImg">
      </div>
      <div class="merchantNameDiv">{{info.yqMerchantName}}</div>
    </div>
  </d2-dialog>
</template>

<script>
import { createWeappQrcode } from '@api/serviceManage'
import html2canvas from 'html2canvas'
export default {
  name: 'qRcodeDialog',
  data() {
    return {
      infoForm: {
        merchantType: '',
        merchant: [],
        payStartDate: '',
        payEndDate: '',
        reportType: '1'
      },
      // 基本数据
      baseData: {
        merchantList: []
      },
      qrCodeURL: '',
      defaultImg: require('./images/defaultImg.png')
    }
  },

  props: {
    qRcodeVisible: {
      type: Boolean,
      default: false
    },
    info: {
      type: Object,
      default: null
    },
  },

  watch: {
  },

  created() {
  },
  components: {},

  mounted() {
    this.createQrcodeImage()
  },

  methods: {

    createQrcodeImage() {
      let oParams = {}
      oParams.channelType = 1
      oParams.appId = 'wx06d1198e751435d1'
      oParams.qrcodeType = 'QR_WXA_UNLIMIT'
      // oParams.path = 'packagesPayment/pages/QRPayment/QRPayment'  
      oParams.path = 'packagesPayment/pages/payment/index'
      oParams.scene = `merchantId=${this.info.yqMerchantId}&merchantName=${this.info.yqMerchantName}`

      createWeappQrcode(oParams).then(res => {
        this.qrCodeURL = res
      })
    },

    //下载保存图片
    takeScreenshot() {
      html2canvas(document.getElementById('view')).then(canvas => {
        this.convertCanvasToImg(canvas)
      });
    },

    //转换图片格式
    convertCanvasToImg(canvas) {
      // canvas base64 转 blob
      var myBlob = this.dataURLtoBlob(canvas.toDataURL('img/png', 0.92))
      // blob转URL对象
      let myUrl = URL.createObjectURL(myBlob)
      // 创建a标签,下载图片
      this.downImg(myUrl)
    },

    //base64 转 blob
    dataURLtoBlob(dataurl) {
      var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    },

    //下载图片 filename是图片名称
    downImg(url) {
      // 创建a标签 并设置其相关属性,最后触发其点击事件
      let a = document.createElement("a")
      let clickEvent = document.createEvent("MouseEvents");
      a.setAttribute("href", url)
      a.setAttribute("download", "filename")
      a.setAttribute("target", '_blank')
      clickEvent.initEvent('click', true, true)
      a.dispatchEvent(clickEvent);
    },

    handleAddConfirm() {
      this.takeScreenshot()
    },

    handleClose() {
      this.$emit('dialogClose', true)
    }
  }
}
</script>
<style>
.dateItem {
  display: flex;
}
.dateItem .el-form-item__content {
  display: flex;
  margin-left: 0 !important;
}
.codeDiv {
  width: 345px;
  height: 468px;
  margin: 0px auto;
  position: relative;
  /* background-image: url('./images/code3.png'); */
}
.imgDiv {
  width: 150px;
  height: 150px;
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  background-color: #d8d8d8;
}
.merchantNameDiv {
  width: 150px;
  height: 20px;
  position: absolute;
  left: 50%;
  top: 63%;
  transform: translate(-50%, -50%);
  text-align: center;
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
  line-height: 17px;
}
</style>