uniapp weapp-qrcode.js生成二维码,并下载二维码图片

找了很多插件,但是都太复杂,不符合自己的要求

找了个最简单的。weapp-qrcode.js ,生成二维码没有问题,但是下载图片获取不到图片路径,自己稍微改造了一下

https://github.com/yingye/weapp-qrcode/issues/18  github也提到通用的问题,callback回调不准确。

 

页面效果

js插件地址:https://github.com/yingye/weapp-qrcode

代码如下

<template>
    <view>
        <cu-custom bgColor="bg-gradual-white" :isBack="true">
            <block slot="backText">返回</block>
            <block slot="content">我的拉新二维码</block>
        </cu-custom> 
        <view style="position: absolute;">
            <view class="qrcode-bg">
                <view class="round_1" ></view>
                <view class="round_2" ></view>
            </view>
            <!-- <image class="qrcode-bg" src="../../static/login-bg.png"></image> -->
            <image  class="qr-img car_image" src="../../static/mine/mine_qrcode_car.png"></image>
        </view>
        
        <view class="qr-content">
            <!-- <image class="qr-img" src="../../static/mine/qrcode.jpg"></image> -->
            <canvas class="canvas-code"canvas-id="myQrcode" style=" padding-top: 20rpx;
            background:#fff;width: 200px;height: 200px;"/>
        </view>
        <view>
            <button class="qr-btn btn-position" @click="saveQrcode" @longpress="saveQrcode">下载二维码</button>
        </view>
        
    </view>
</template>
 
<script>
    import QRCode from '@/utils/weapp-qrcode.js'
    export default {
        data() {
            return {
                qrcodePath:'',
                myWidth:400,
                myHeight:400
            };
        },
        beforeCreate() {
            // LTJ:2020-12-31 判断是否已登录
            if(!this.$util.checkLogin("path=../plugin/qrcode"))
                return ;
            
        },
        
        onLoad: function (options) {
            console.log('https://d.cartour.club/api/register?refid=' + uni.getStorageSync("userId"))
            this.reateQrcode()
            let that = this
            
          },
        methods: {
            createRpx2px(rpx) {
                console.log(wx.getSystemInfoSync().windowWidth,'屏幕宽度')
                if(wx.getSystemInfoSync().windowWidth <= 375){
                    this.myWidth = 400
                    this.myHeight = 400
                }else if(wx.getSystemInfoSync().windowWidth >= 414){
                    this.myWidth = 363
                    this.myHeight = 363
                }
                return wx.getSystemInfoSync().windowWidth / 750 * rpx
              },
             reateQrcode() {
                var that = this;
                console.log(that.createRpx2px(363),'宽度')
                const imgPath = new Promise((resolve, reject) => {
                    new QRCode('myQrcode', {
                        // text:'https://www.baidu.com/',
                      text:'https://d.cartour.club/api/register?refid=' + uni.getStorageSync("userId"),
                      width: that.createRpx2px(this.myWidth),
                      height: that.createRpx2px(this.myHeight),
                      padding: 12, // 生成二维码四周自动留边宽度,不传入默认为0
                      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
                    })
                })
              },
            
         //用户二维码保存到本地相册
          saveQrcode: function () {
              uni.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 400,
                height: 400,
                destWidth: 400,
                destHeight: 400,
                canvasId: 'myQrcode',
                success: function(res) {
                // 在H5平台下,tempFilePath 为 base64
                  let data = res.tempFilePath
                console.log(res)
                uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function () {
                        console.log('save success');
                        uni.showToast({
                            title:'保存成功',
                            icon:'success',
                            duration:2000,
                        })
                    }
                });
                } 
              })
             
            // var that = this;
            // uni.getImageInfo({
            //   src: this.qrcodePath,
            //   success: function (ret) {
                  // debugger
                  // console.log(ret)
            //     var path = ret.path;
            //     uni.saveImageToPhotosAlbum({
            //       filePath: path,
            //       success(result) {
            //         if (result.errMsg === 'saveImageToPhotosAlbum:ok') {
            //           uni.showToast({
            //             title: '保存成功',
            //           })
            //         }
            //       }
            //     })
            //   }
            // })
          },
        }
    }
</script>

<style>
    @import "../../common.css";
    .qrcode-bg{
        width: 750rpx;
        height: 248rpx;
        background-image: linear-gradient(263deg, 
        #6717cd 0%, 
        #2871fa 100%);
        position: relative;
    }
    .round_1{
        width: 220px;
        height: 220px;
        border-radius: 100%;
        position: absolute;
        left: -40px;
        top: -96px;
        background-color: #3b75f8;
        opacity:0.7;
    }
    .round_2{
        width: 220px;
        height: 220px;
        border-radius: 100%;
        position: absolute;
        left: 93px;
        top: 43px;
        background-color: #5550e5;
        opacity: 0.6;
    }
    /* iphone678plus*/
    @media screen and  (width:414px) {
       .qr-content{
           width: 500rpx;
           height: 500rpx;
           position: relative;
           top: 195rpx;
           left: 17%;
           padding-top:61rpx;
           padding-left: 61rpx;
           background-color: white;
           border-radius: 8px;
       }   
    }
    /* iphone6*/
    @media screen and (width:375px) {
       .qr-content{
           width: 500rpx;
           height: 500rpx;
           position: relative;
           top: 195rpx;
           left: 17%;
           padding-top:50rpx;
           padding-left: 50rpx;
           background-color: white;
           border-radius: 8px;
       } 
    }
    
    .qr-img{
        width: 430rpx;
        position: absolute;
        height: 430rpx;
        top: 10%;
        left: 10%;
    }
    .car_image{
        width: 170rpx;
        height: 120rpx;
        position: relative;
        left: 76%;
        top: -87px;
    }
    .btn-position{
        position: absolute;
        bottom: 180px;
        width: 70%;
        height: 80rpx;
        left: 15%;
    }
    /* iphone678plus*/
    @media screen and  (width:414px) {
       .car_image{
           width: 170rpx;
           height: 120rpx;
           position: relative;
           left: 76%;
           top: -95px;
       }     
    }
    /* iphone6*/
    @media screen and (width:375px) {
       .car_image{
           width: 170rpx;
           height: 120rpx;
           position: relative;
           left: 76%;
           top: -87px;
       }    
    }
    /* iphone5*/
    @media screen and (width:320px) {
       .car_image{
           width: 170rpx;
           height: 120rpx;
           position: relative;
           left: 76%;
           top: -74px;
       }    
       .btn-position{
           position: absolute;
           bottom: 246rpx;
           width: 70%;
           height: 80rpx;
           left: 15%;
       }
    }
    
</style>
 

/** * weapp.qrcode.js v1.0.0 (https://github.com/yingye/weapp-qrcode#readme) */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.drawQrcode = factory()); }(this, (function () { 'use strict'; var hasOwn = Object.prototype.hasOwnProperty; var toStr = Object.prototype.toString; var defineProperty = Object.defineProperty; var gOPD = Object.getOwnPropertyDescriptor; var isArray = function isArray(arr) { if (typeof Array.isArray === 'function') { return Array.isArray(arr); } return toStr.call(arr) === '[object Array]'; }; var isPlainObject = function isPlainObject(obj) { if (!obj || toStr.call(obj) !== '[object Object]') { return false; } var hasOwnConstructor = hasOwn.call(obj, 'constructor'); var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf'); // Not own constructor property must be Object if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) { return false; } // Own properties are enumerated firstly, so to speed up, // if last one is own, then all properties are own. var key; for (key in obj) { /**/ } return typeof key === 'undefined' || hasOwn.call(obj, key); }; // If name is '__proto__', and Object.defineProperty is available, define __proto__ as an own property on target var setProperty = function setProperty(target, options) { if (defineProperty && options.name === '__proto__') { defineProperty(target, options.name, { enumerable: true, configurable: true, value: options.newValue, writable: true }); } else { target[options.name] = options.newValue; } }; // Return undefined instead of __proto__ if '__proto__' is not an own property var getProperty = function getProperty(obj, name) { if (name === '__proto__') { if (!hasOwn.call(obj, name)) { return void 0; } else if (gOPD) {
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值