微信小程序-----生成二维码

此功能参照GitHub上的有插件源码,现在找不到那个网址了,就是借鉴别人的。

onLoad: function () {
    var that = this
    wx.getStorage({
      key: 'accessToken',
      success: function (res) {
        //console.log(res)
        wx.request({
          url: +/user/userInfo.htm",
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: { accessToken: res.data },
          success: function (res) {
            //console.log(res.data.data.id)
            var str = res.data.data.id + ''
            if (res.data.data.avatarUrl) {
              that.setData({
                avatarUrl: res.data.data.avatarUrl,
                nickName: res.data.data.nickName,
                placeholder: str
              })
            } else {
              that.setData({
                avatarUrl: '../../resources/login-1.jpg',
                placeholder: str
              })
            }
            //console.log(that.data.placeholder)
            that.draw();
          }
        })
      }
    })
  },
  draw(){
    var size = this.setCanvasSize();//动态设置画布大小
    var initUrl = this.data.placeholder;
    this.createQrCode(initUrl, "mycanvas", 192, 192);
  },


  
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 686;//不同屏幕下canvas的适配比例;设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法,绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 1000);

  },
  //获取临时缓存照片路径,存入data中
  canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //点击图片进行预览,长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  saveCodeImg: function () {
    console.log(111)
    wx.saveImageToPhotosAlbum({
      filePath: this.data.imagePath,
      success(res) {
        wx.showToast({
          title: '图片保存成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  }
  
})

 

还有一个canvas用于生成二维码的文件,没有办法发出来,要了联系我1136395932

 

微信小程序-----生成二维码_第1张图片

你可能感兴趣的:(原创,小程序,二维码,收款码)