|
|
@@ -1,4 +1,4 @@
|
|
|
-// components/My_TwoDimensionalCode/index.js
|
|
|
+const QR = require('../../miniprogram_npm/qrcode-base64/index.js')
|
|
|
Component({
|
|
|
/**
|
|
|
* 组件的属性列表
|
|
|
@@ -9,18 +9,30 @@ Component({
|
|
|
},
|
|
|
codeName: {
|
|
|
type: String
|
|
|
- },
|
|
|
- imageUrl: {
|
|
|
- type: String,
|
|
|
- value: '/static/changeImg.png'
|
|
|
}
|
|
|
},
|
|
|
-
|
|
|
+ lifetimes: {
|
|
|
+ attached: function () {
|
|
|
+ // 在组件实例进入页面节点树时执行
|
|
|
+ var imgData = QR.drawImg(this.data.codeText, {
|
|
|
+ typeNumber: 4,
|
|
|
+ errorCorrectLevel: 'M',
|
|
|
+ size: 500
|
|
|
+ })
|
|
|
+ this.setData({
|
|
|
+ qrcodeURL: imgData
|
|
|
+ })
|
|
|
+ },
|
|
|
+ detached: function () {
|
|
|
+ // 在组件实例被从页面节点树移除时执行
|
|
|
+ },
|
|
|
+ },
|
|
|
/**
|
|
|
* 组件的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
-
|
|
|
+ codeText: 'https://www.baidu.com',
|
|
|
+ qrcodeURL: "",
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
@@ -29,47 +41,101 @@ Component({
|
|
|
methods: {
|
|
|
/* 预览大图 */
|
|
|
preViewImage() {
|
|
|
- const url = [this.data.imageUrl];
|
|
|
+ const url = [this.data.qrcodeURL];
|
|
|
wx.previewImage({
|
|
|
urls: url,
|
|
|
})
|
|
|
},
|
|
|
/* 下载二维码 */
|
|
|
download() {
|
|
|
- wx.downloadFile({
|
|
|
- url: this.data.imageUrl,
|
|
|
- success: function (res) {
|
|
|
- const tempFilePath = res.tempFilePath;
|
|
|
- // 保存文件
|
|
|
- wx.saveFile({
|
|
|
- tempFilePath,
|
|
|
- success: function (res) {
|
|
|
- const savedFilePath = res.savedFilePath;
|
|
|
- // 打开文件
|
|
|
- wx.openDocument({
|
|
|
- filePath: savedFilePath,
|
|
|
- success: function (res) {
|
|
|
- console.log(res)
|
|
|
- },
|
|
|
- });
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- console.log('保存失败:', err)
|
|
|
- wx.showToast({
|
|
|
- title: '保存失败',
|
|
|
- icon: "none"
|
|
|
- })
|
|
|
+ const that = this;
|
|
|
+ wx.authorize({
|
|
|
+ /* 这个就是保存相册的 */
|
|
|
+ scope: 'scope.writePhotosAlbum',
|
|
|
+ success() {
|
|
|
+ /* 保存图片方法 */
|
|
|
+ that.img();
|
|
|
+ },
|
|
|
+ complete(res) {
|
|
|
+ /* 这里判断一下如果没有授权重新打开设置选项 */
|
|
|
+ wx.getSetting({
|
|
|
+ success(res) {
|
|
|
+ if (!res.authSetting['scope.writePhotosAlbum']) {
|
|
|
+ /* 打开设置的方法 */
|
|
|
+ that.opensit();
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* 授权提示 ,这里就是重复提示用户去授权*/
|
|
|
+ opensit() {
|
|
|
+ wx.showModal({
|
|
|
+ content: '由于您还没有允许保存图片到您相册里,这无法进行分享操作点击确定去允许授权',
|
|
|
+ success: function (res) {
|
|
|
+ if (res.confirm) {
|
|
|
+ /* 这个就是打开设置的API*/
|
|
|
+ wx.openSetting({
|
|
|
+ success(res) {
|
|
|
+ console.log(res.authSetting);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if (res.cancel) {
|
|
|
+ wx.showModal({
|
|
|
+ cancelText: '依然取消',
|
|
|
+ confirmText: '重新授权',
|
|
|
+ content: '很遗憾你点击了取消,这将无法进行分享操作,请慎重考虑',
|
|
|
+ success: function (res) {
|
|
|
+ if (res.confirm) {
|
|
|
+ wx.openSetting({
|
|
|
+ success(res) {
|
|
|
+ console.log(res.authSetting);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if (res.cancel) {
|
|
|
+ console.log('用户不授权');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* 特别注意要先获取图片信息在进行保存,不让保存不了 */
|
|
|
+ img() {
|
|
|
+ const that = this;
|
|
|
+ /// 通过微信小程序自带方法将base64转为二进制去除特殊符号,再转回base64
|
|
|
+ const base64Data = wx.arrayBufferToBase64(wx.base64ToArrayBuffer(this.data.qrcodeURL));
|
|
|
+ /// 拼接请求头,data格式可以为image/png或者image/jpeg等,看需求
|
|
|
+ const base64ImgUrl = "data:image/png;base64," + base64Data;
|
|
|
+
|
|
|
+ /* 保存图片到相册 */
|
|
|
+ wx.saveImageToPhotosAlbum({
|
|
|
+ filePath: base64ImgUrl,
|
|
|
+ success: function () {
|
|
|
+ console.log('save success');
|
|
|
+ wx.showModal({
|
|
|
+ title: '保存成功',
|
|
|
+ content: '图片已成功保存到相册,快去分享到您的圈子吧',
|
|
|
+ showCancel: false
|
|
|
+ });
|
|
|
},
|
|
|
- fail: function (err) {
|
|
|
- console.log('下载失败:', err);
|
|
|
- wx.showToast({
|
|
|
- title: '下载失败',
|
|
|
- icon: "none"
|
|
|
- })
|
|
|
- },
|
|
|
+ complete(res) {
|
|
|
+ console.log(res);
|
|
|
+ }
|
|
|
});
|
|
|
+ /* 获取图片信息 */
|
|
|
+ /* wx.getImageInfo({
|
|
|
+ src: that.data.qrcodeURL,
|
|
|
+ success: function (res) {
|
|
|
+ console.log(res)
|
|
|
+
|
|
|
+ },
|
|
|
+ fail:(err => {
|
|
|
+ console.log(err)
|
|
|
+ })
|
|
|
+ }); */
|
|
|
}
|
|
|
}
|
|
|
})
|