使用weapp-qrcode插件生成二维码
wxml:
<canvas style="width: 200px; height: 200px;" canvas-id="{{myQrcode}}" ></canvas>
<button bindtap="save">保存二维码</button>
// pages/QRcode/QRcode.js
let QRCode = require("../../utils/weapp-qrcode") //引入生成二维码的插件
Page({
/**
* 页面的初始数据
*/
data: {
myQrcode: 1, //canvas的id
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getQrcode(this.data.myQrcode)
},
getQrcode(canvas) {
//canvas是canvas-id
console.log(canvas)
let qrcode = new QRCode(canvas, {
// text: 'https://www.baidu.com/img/pc_cc75653cd975aea6d4ba1f59b3697455.png',
text: '畅游甘孜',
width: 200,
height: 200,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.L //二维码辨识度
});
},
save() {
let that = this
console.log(11)
//将画布中的二维码导出来
wx.canvasToTempFilePath({
canvasId: this.data.myQrcode, //传入canvas的id
success: function (res) {
console.log(res)
var tempFilePath = res.tempFilePath;
console.log(tempFilePath);
//保存二维码
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success(){
wx.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: function(res) {
wx.showToast({
title: '图片保存成功',
icon: 'success',
duration: 2000 //持续的时间
})
},
fail: function (err) {
console.log(err)
wx.showToast({
title: '图片保存失败',
icon: 'none',
duration: 2000//持续的时间
})
}
})
},
fail: function (err){
console.log('点击了拒绝')
console.log(err)
}
})
}else{
wx.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: function(res) {
wx.showToast({
title: '图片保存成功',
icon: 'success',
duration: 2000 //持续的时间
})
},
fail: function (err) {
console.log(err)
wx.showToast({
title: '图片保存失败',
icon: 'none',
duration: 2000//持续的时间
})
}
})
}
}
})
},
fail: function (res) {
console.log(res);
}
},this);
},
})