qRcodeDialog.vue 10.1 KB
<template>
  <d2-dialog title="收款二维码" :is-visible="qRcodeVisible" class="qRcodeDailog" width="750px" btn-ok-label="全部下载"
             @closeDialog="handleClose" @confirmDialog="handleAddConfirm">
    <div class="qrcon">
      <div class="wxcon">
        <div class="codeDiv" id="wxview">
          <img style="width:100%;height:100%" src="./images/code3.png">
          <!-- v-if="qrCodeURL" <div class="imgDiv"> -->
          <img v-if="qrCodeURL" class="qrImg" crossorigin="anonymous" :src="qrCodeURL">
          <!-- crossorigin="anonymous" -->
          <div v-if="!qrCodeURL" class="el-icon-loading loadingIcon" />
          <!-- </div> -->
          <div class="merchantNameDiv">{{ info.yqMerchantName }}</div>
        </div>
        <el-button style="marginTop:20px" type="primary "
                   @click="takeScreenshot('wxview', `${info.yqMerchantName}微信支付收款码`)">下载</el-button>
      </div>
      <div class="alicon">
        <div class="codeDiv" id="aliview">
          <img style="width:100%;height:100%" src="./images/alipay.png">
          <!-- <img v-if="aliqrCodeURL"
               class="qrImg imgs"
               :src="aliqrCodeURL"
               crossorigin="anonymous"> -->
          <div id="qrcodeDiv" class="qrImg" />
          <!--  -->
          <!-- :src="aliqrCodeURL"> -->
          <div v-if="!aliqrCodeURL" class="el-icon-loading loadingIcon" />
          <!-- </div> -->
          <div class="merchantNameDiv">{{ info.yqMerchantName }}</div>
        </div>
        <el-button style="marginTop:20px" type="primary "
                   @click="takeScreenshot('aliview', `${info.yqMerchantName}支付宝支付收款码`)">下载
        </el-button>
      </div>
    </div>

  </d2-dialog>
</template>

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

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

  watch: {
  },

  created() {
  },
  components: {},

  mounted() {
    this.createQrcodeImage()
    this.alipayQrcode()
  },

  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.scene = `merchantId=${this.info.yqMerchantId}&merchantName=${this.info.yqMerchantName}`
      // oParams.appId = 'wx902f0b9c4b2c669d'
      // oParams.appId = 'wx570d9331f9ace0bd'
      // oParams.qrcodeType = 'QR_WXA_UNLIMIT'
      // oParams.path = 'pages/accountItems/accountItems'
      // oParams.scene = `noLogin=15816539334`
      // oParams.path = 'pages/webpage/webpage'
      // oParams.scene = `jumpLink=https://frontend.test.glsx.com.cn/dj-car-boutique-work-wechat/#/`

      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(eleId, name) {
      // const opts = {
      //   useCORS: true
      // }
      const that = this
      html2canvas(document.getElementById(eleId), {
        backgroundColor: null,
        allowTaint: true, // 允许污染
        taintTest: true, // 在渲染前测试图片(没整明白有啥用)
        useCORS: true // 使用跨域(当allowTaint为true时这段代码没什么用,下面解释)
      }).then(canvas => {
        that.convertCanvasToImg(canvas, name)
        // setTimeout(() => {
        // })
      })
    },

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

    // 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, name) {
      // 创建a标签 并设置其相关属性,最后触发其点击事件
      const a = document.createElement('a')
      const clickEvent = document.createEvent('MouseEvents')
      a.setAttribute('href', url)
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      clickEvent.initEvent('click', true, true)
      a.dispatchEvent(clickEvent)
    },
    // 生成支付宝中间二维码
    alipayQrcode() {
      // const that = this
      const shareUrl = `${baseConfig.alipayPath}?merchantId=${this.info.yqMerchantId}&merchantName=${this.info.yqMerchantName}`
      console.log(baseConfig.alipayPath)
      console.log(shareUrl)
      setTimeout(() => {
        console.log(document.getElementById('qrcodeDiv'))
        const qrcode = new QRCode("qrcodeDiv", {
          text: shareUrl,
          width: 150, // 图像宽度
          height: 150 // 图像高度
        })
        qrcode.clear() // 清除二维码
        qrcode.makeCode(shareUrl) // 生成另一个新的二维码
        this.aliqrCodeURL = qrcode
      }, 500)
      // this.$nextTick(() => {

      //   const qrcode = new QRCode('qrcodeDiv', {
      //     text,
      //     width: 150,
      //     height: 150,
      //     colorDark: '#000000',
      //     colorLight: '#ffffff',
      //     correctLevel: QRCode.CorrectLevel.H
      //   })
      //   console.log(qrcode)
      // }, 2000)

      // const params = {
      //   path: baseConfig.alipayPath,
      //   yqMerchantId: this.info.yqMerchantId,
      //   yqMerchantName: this.info.yqMerchantName
      // }

      // alipayQrcode(params).then((result) => {
      //   console.log(result)
      //   this.aliqrCodeURL = result
      //   this.$nextTick(() => {
      //     that.combinedPoster(result)
      //     // that.getBase64Image(result, document.querySelector('.imgs'))
      //   })
      // }).catch((err) => {
      //   console.log(err)
      // })
    },

    handleAddConfirm() {
      this.takeScreenshot('wxview', `${this.info.yqMerchantName}微信支付收款码`)
      setTimeout(() => {
        this.takeScreenshot('aliview', `${this.info.yqMerchantName}支付宝支付收款码`)
      }, 2000)
      // this.takeScreenshot()
    },
    handleClose() {
      this.$emit('dialogClose', true)
    }
  }
}
</script>
<style scoped>
.qrcon {
  display: flex;
}

.wxcon,
.alicon {
  text-align: center;
}

.wxcon {
  margin-right: 20px;
}

.codeDiv {
  width: 345px;
  height: 468px;
  margin: 0px auto;
  position: relative;
  /* background-image: url('./images/code3.png'); */
}

.qrImg {
  position: absolute;
  width: 150px;
  height: 150px;
  left: 50%;
  top: 42%;
  /* transform: translateX(-50%); */
  transform: translate(-50%, -50%);
  /* background-color: #d8d8d8; */
  z-index: 2;
  object-fit: contain;
  /* transform: scale(0.95); */
}

.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;

  overflow: hidden;

  text-overflow: ellipsis;

  display: -webkit-box;

  -webkit-box-orient: vertical;
  /* 自定义行数 */
  -webkit-line-clamp: 2;
}
</style>