qRcodeDialog.vue 6.88 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">
      <!-- v-if="qrCodeURL" <div class="imgDiv"> -->
      <img v-if="qrCodeURL"
           class="qrImg"
           id="img1"
           crossorigin="anonymous"
           :src="qrCodeURL">
      <div v-if="!qrCodeURL"
           class="el-icon-loading loadingIcon"></div>
      <!-- </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: {
    /**
     * @description: 图片转base64
     * @param {String} url 需要转换的图片原链接(http://....jpg)
     * @param {String} outputFormat 转换出来的图片的类型(canvas支持jpg/png格式)
     * @return: 返回图片对应的base64编码
     */
    getBas64(url, outputFormat = 'image/png') {
      return new Promise(function (resolve, reject) {
        let canvas = document.createElement('CANVAS')
        const ctx = canvas.getContext('2d')
        const img = new Image()

        img.crossOrigin = 'Anonymous' // 重点!设置image对象可跨域请求
        img.onload = function () {
          canvas.height = img.height
          canvas.width = img.width
          ctx.drawImage(img, 0, 0)
          const dataURL = canvas.toDataURL(outputFormat)
          canvas = null
          resolve(dataURL)
        }
        // img.src = url; // 旧的方式
        img.src = url + '?t=' + new Date().valueOf() // 防止oss的缓存问题
      })
    },
    createQrcodeImage() {
      const oParams = {}
      oParams.channelType = 1
      oParams.appId = 'wx06d1198e751435d1'
      // oParams.appId = 'wx902f0b9c4b2c669d'
      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

        // function getBase64(imgUrl) {
        //   window.URL = window.URL || window.webkitURL
        //   var xhr = new XMLHttpRequest()
        //   xhr.open('get', imgUrl, true)
        //   xhr.responseType = 'blob'
        //   xhr.onload = function () {
        //     if (this.status == 200) {
        //       // 得到一个blob对象
        //       var blob = this.response
        //       console.log('blob', blob)
        //       // 至关重要
        //       const oFileReader = new FileReader()
        //       oFileReader.onloadend = function (e) {
        //         // 此处拿到的已经是base64的图片了,可以赋值做相应的处理
        //         console.log(e.target.result)
        //       }
        //       oFileReader.readAsDataURL(blob)
        //     }
        //   }
        //   xhr.send()
        // }
        // console.log(getBase64(res))
        // getBase64('需要转成base64的图片url')
      })
    },

    // 下载保存图片
    takeScreenshot() {
      // const opts = {
      //   useCORS: true
      // }
      html2canvas(document.getElementById('view'), {
        backgroundColor: null,
        allowTaint: true, // 允许污染
        taintTest: true, // 在渲染前测试图片(没整明白有啥用)
        useCORS: true // 使用跨域(当allowTaint为true时这段代码没什么用,下面解释)
      }).then(canvas => {
        this.convertCanvasToImg(canvas)
      })
    },
    // 转换图片格式
    convertCanvasToImg(canvas) {
      // canvas base64 转 blob
      var myBlob = this.dataURLtoBlob(canvas.toDataURL('img/png', 0.92))
      // blob转URL对象
      const myUrl = URL.createObjectURL(myBlob)
      // 创建a标签,下载图片
      this.downImg(myUrl)
    },

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

    // 下载图片 filename是图片名称
    downImg(url) {
      // 创建a标签 并设置其相关属性,最后触发其点击事件
      const a = document.createElement('a')
      const 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'); */
}
.qrImg {
  width: 150px;
  height: 150px;
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  /* background-color: #d8d8d8; */
  z-index: 2;
}
.loadingIcon {
  position: absolute;
  /* width: 44px;
  height: 44px; */
  font-size: 30px;
  /* top: 60px;
  left: 60px; */
  /* transform: translate(-50%, -50%); */
  /* width: 150px;
  height: 150px; */
  position: absolute;
  left: 46%;
  top: 40%;
  transform: translate(-50%, -50%);
}
.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>