PC端 使用qrcode generator動(dòng)態(tài)顯示二維碼

https://www.npmjs.com/package/qrcode-generator

圖片.png

一、使用
1.引入 JS

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.1/qrcode.min.js"></script>

2.產(chǎn)生容器

 <div id="qrcode"></div>

3.調(diào)用函數(shù),繪制二維碼

html:

    <!-- 微信支付彈窗 start  -tab1-->
    <div class="actionModel" v-show="wxPayFlag">
      <div class="wxPayModel" v-loading="loading">
        <span @click="wxClose()" class="el-icon-close closeBtn"></span>
        <div class="content" :class="{on:payStep===2}">
          <div class="codeBox">
            <div class="wxBg payBg" :class="{zfbBg:selectPay==='alipay'}">
              <div id="qrcode"></div>
            </div>
            <p class="payTips">掃碼支付完成后,我們將在10秒內(nèi)處理好訂單,支付后請不要關(guān)閉本窗口,耐心等待完成提示</p>
          </div>
        </div>
        <!-- 支付成功 -->
        <div class="content" :class="{on:payStep===3}">
          <div class="paySuccess">
            <img :src="$utils.getPng('pay_success')" />
            <span>購買成功</span>
          </div>
        </div>
      </div>
    </div>

css:

    //使用微信支付彈窗
    .wxPayModel {
      position: relative;
      background: #fff;
      width: 420px;
      height: auto;
      border-radius: 8px;
      overflow: hidden;
      color: #999;
      font-size: 16px;
      .closeBtn {
        font-size: 20px;
        cursor: pointer;
        position: absolute;
        top: 22px;
        right: 25px;
        color: #333;
        z-index: 9;
      }
      //code 內(nèi)容
      .content {
        position: relative;
        display: none;
      }
      .on {
        display: block;
      }
      .codeBox {
        text-align: center;
        padding-top: 70px;
        .payBg {
          height: 380px;
          width: 240px;
          display: inline-block;
          #qrcode {
            text-align: center;
            display: inline-block;
            padding: 6px;
            border: 1px solid #e5e9ef;
            border-radius: 4px;
            padding-bottom: 0;
            margin-top: 115px;
            width: 154px;
            height: 154px;
            img {
              width: 140px;
              height: 140px;
              vertical-align: middle;
            }
          }
        }
        .wxBg {
          @include background(wx_pay_bg);
          background-size: contain;
        }
        .zfbBg {
          @include background(zfb_pay_bg);
          background-size: contain;
        }
        .payTips {
          color: #ff5b4c;
          margin-top: 30px;
          padding: 0 40px 30px;
        }
      }
      //成功
      .paySuccess {
        margin: 90px auto;
        @include rowDouleCenter;
        img {
          width: 56px;
          margin-right: 20px;
        }
        span {
          font-size: 22px;
          color: #666;
        }
      }
    }

js完整代碼:

 // 微信支付
    wxPay () {
      this.loading = true
      this.$api.post(
        '/svip/wxpay',
        {
          svipid: this.SVIPID,
          cid: this.couponID,
          coupon: this.coupon
        },
        (res) => {
          if (res.status) {
            this.loading = false
            this.payStep = 2
            // 1.創(chuàng)建二維碼對象。qrcode(二維碼類型(1~40,輸入 0 以自動(dòng)檢測),容錯(cuò)級(jí)別(L、M、Q、H))
            var qr = qrcode(0, 'L')
            // 2.添加二維碼信息。
            qr.addData(res.msg)
            // 3.生成二維碼對象(并不顯示)。
            qr.make()
            // createImgTag(cellSize, margin, alt); cellSize 像素寬度,margin補(bǔ)白像素寬度
            document.getElementById('qrcode').innerHTML = qr.createImgTag(5, 0)
            if (this.wxPayTimer) {
              clearInterval(this.wxPayTimer)
            }
            this.wxPayTimer = setInterval(() => {
              this.$api.post(
                '/svip/checkpaystatus',
                { order: res.data.order, cate: 'wxpay' },
                (res) => {
                  if (res.status) {
                    clearInterval(this.wxPayTimer)
                    this.payStep = 3
                  }
                }
              )
            }, 3000)
          } else {
            this.$notify.warning({
              title: '提示',
              message: res.msg
            })
          }
        }
      )
    },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容