qRcodeDialog.vue 10.7 KB
<template>
  <d2-dialog title="收款二维码"
             :is-visible="qRcodeVisible"
             class="qRcodeDailog"
             width="750px"
             btn-ok-label="全部下载"
             @closeDialog="handleClose"
             @confirmDialog="handleAddConfirm">
    <div class="channelBox">
      <span>支付渠道: </span>
      <el-select v-model="paymentChannelId"
                 placeholder="请选择"
                 class="elSelect">
        <el-option v-for="item in paymentChannelList"
                   :key="item.paymentChannelId"
                   :label="item.paymentChannelName"
                   :value="item.paymentChannelId">
        </el-option>
      </el-select>
      <el-button type="primary"
                 @click="savaAll">保存并重新生成</el-button>
    </div>
    <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.merchantName }}</div>
        </div>
        <el-button style="marginTop:20px"
                   type="primary "
                   @click="takeScreenshot('wxview', `${info.merchantName}微信支付收款码`)">下载</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.merchantName }}</div>
        </div>
        <el-button style="marginTop:20px"
                   type="primary "
                   @click="takeScreenshot('aliview', `${info.merchantName}支付宝支付收款码`)">下载
        </el-button>
      </div>
    </div>

  </d2-dialog>
</template>

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

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

  watch: {
  },

  created() {
  },
  components: {},

  mounted() {
    this.paymentChannelId = this.info.paymentChannelId
    this.initPaymentChannelList()
    this.alipayQrcode()
  },

  methods: {
    savaAll() {
      setDefaultPaymentChannel({
        paymentChannelId: this.paymentChannelId,
        merchantId: this.info.merchantId
      })
      this.createQrcodeImage()
      this.alipayQrcode()
      // this.takeScreenshot('wxview', `${this.info.merchantName}微信支付收款码`)
      // this.takeScreenshot('aliview', `${this.info.merchantName}支付宝支付收款码`)
    },
    initPaymentChannelList() {
      paymentChannelList().then((res) => {
        this.paymentChannelList = res
        this.createQrcodeImage()
      })
    },

    /**
     * @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() {
      console.log(this.paymentChannelList)
      console.log(this.paymentChannelList.find(item => item.paymentChannelId == this.paymentChannelId))
      const oParams = {}
      oParams.channelType = 1
      oParams.appId = this.paymentChannelList.find(item => item.paymentChannelId == this.paymentChannelId)?.payPlatform == 'allinpay' ? 'wx3d2ee7f8bda398cc' : 'wx06d1198e751435d1'
      // oParams.appId = this.paymentChannelId == '8881A96B22B84A348263EE4F314A2A23' ? 'wx06d1198e751435d1' : 'wx3d2ee7f8bda398cc'
      oParams.qrcodeType = 'QR_WXA_UNLIMIT'
      oParams.path = 'packagesPayment/pages/QRPayment/QRPayment'
      oParams.scene = `merchantId=${this.info.merchantId}&merchantName=${this.info.merchantName}&paymentChannelId=${this.paymentChannelId}`

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

    // 下载保存图片
    takeScreenshot(eleId, name) {
      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
      // pay 支付渠道
      const shareUrl = `${baseConfig.alipayPath}?M=${this.info.merchantId}&MN=${this.info.merchantName}&P=${this.paymentChannelId}`
      console.log(baseConfig.alipayPath)
      console.log(shareUrl)
      setTimeout(() => {
        console.log(document.getElementById('qrcodeDiv'))
        if (this.qrcode) {
          this.qrcode.clear() // 清除二维码
        } else {
          this.qrcode = new QRCode("qrcodeDiv", {
            text: shareUrl,
            width: 150, // 图像宽度
            height: 150 // 图像高度
          })
        }
        // qrcode.clear() // 清除二维码
        this.qrcode.makeCode(shareUrl) // 生成另一个新的二维码
        this.aliqrCodeURL = this.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.merchantId ,
      //   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.merchantName}微信支付收款码`)
      setTimeout(() => {
        this.takeScreenshot('aliview', `${this.info.merchantName}支付宝支付收款码`)
      }, 2000)
      // this.takeScreenshot()
    },
    handleClose() {
      this.$emit('dialogClose', true)
    }
  }
}
</script>
<style scoped lang="scss">
.qrcon {
  display: flex;
}

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

.wxcon {
  margin-right: 20px;
}

.codeDiv {
  width: 345px;
  height: 468px;
  margin: 0px auto;
  position: relative;
}

.qrImg {
  position: absolute;
  width: 150px;
  height: 150px;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  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: 60%;
  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;
}

.channelBox {
  margin: auto;

  .elSelect {
    margin: 0 10px 10px;
  }
}
</style>