let drawQrcode = require("../../../utils/erweima.js");
wxml中:
<canvas class="code" canvas-id="myQrcode"></canvas>
<button class='downCode' bindtap='downloadCode'>下载二维码</button>
<view class="code-content">{{text}}<view class="copy" bindtap="clickCopy">复制</view></view>
js文件中数据:
data: {
text: '',
imagePath: ""
},
通过后台接口拿到数据 text
draw() {
drawQrcode({
width: 200,
height: 200,
x:20,
y:20,
canvasId: 'myQrcode',
text: this.data.text
})
this.canvasToTempImage()
},
clickCopy(e) {
var that = this;
wx.setClipboardData({
data: that.data.text,
success: function (res) {
wx.showToast({
title: '复制成功',
icon: 'none'
});
}
});
},
canvasToTempImage: function () {
var that = this;
wx.canvasToTempFilePath({
canvasId: 'myQrcode',
success: function (res) {
var tempFilePath = res.tempFilePath;
that.setData({
imagePath: tempFilePath,
});
},
fail: function (res) {
console.log(res);
}
});
},
downloadCode: function (res) {
var filePath = this.data.imagePath
console.log('下载中' + filePath)
wx.saveImageToPhotosAlbum({
filePath: filePath,
success: function(res) {
wx.showToast({
title: '图片保存成功',
icon: 'success',
duration: 2000
})
},
fail: function (err) {
console.log(err)
wx.showToast({
title: '图片保存失败',
icon: 'none',
duration: 2000
})
}
})
}
|