my-qrcode.js
3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import QR from '../../utils/qrcode'
let app = getApp();
Page({
data: {
authorizeVisible: false,
userInfo: {},
qrImagePath: "",
imageUrl: "",
},
onShareAppMessage() {},
showAuth() {
this.setData({
authorizeVisible: true
})
},
onLoad(options) {
this.initData();
},
initData() {
this.queryMember().then((result) => {
});
},
/**
* 获取会员信息
*/
queryMember() {
let _this = this;
return new Promise((resolve, reject) => {
app.post({
url: app.api.member,
data: {}
}).then((result) => {
let userInfo = result
_this.setData({
userInfo: userInfo,
qrImagePath: userInfo.memberUrl,
})
wx.downloadFile({
url: userInfo.memberUrl,
success(res) {
if (res.statusCode === 200) {
_this.setData({
imageUrl: res.tempFilePath
})
}
}
})
// 生成个人二维码
// let tlMemberCode = userInfo.memberCode;
// let qrSize = this.setCanvasSize(440);
// let codeContent = `tlMemberCode=${tlMemberCode}`
// this.createQrCode(codeContent, 'qrcanvas', qrSize.w, qrSize.h);
resolve();
})
});
},
createQrCode(content, canvasId, cavW, cavH) {
//调用插件中的draw方法,绘制二维码图片
QR.api.draw(content, canvasId, cavW, cavH);
this.canvasToTempImage(canvasId);
},
//获取临时缓存图片路径,存入data中
canvasToTempImage(canvasId) {
let that = this;
wx.canvasToTempFilePath({
canvasId, // 这里canvasId即之前创建的canvas-id
success: function (res) {
let tempFilePath = res.tempFilePath;
console.log(tempFilePath);
that.setData({ // 如果采用mpvue,即 this.imagePath = tempFilePath
qrImagePath: tempFilePath,
});
},
fail: function (res) {
console.log(res);
}
});
},
//适配不同屏幕大小的canvas
setCanvasSize(sz) {
var size = {};
try {
var res = wx.getSystemInfoSync();
var scale = 750 / sz; //不同屏幕下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;
},
/**
* 保存图片到本地
*/
saveImageToPhotosAlbum() {
console.log("saveImageToPhotosAlbum");
let _this = this;
if (!_this.data.imageUrl) {
wx.showToast({
title: "图片加载中,请重试",
icon: "none"
})
return;
}
wx.saveImageToPhotosAlbum({
filePath: _this.data.imageUrl,
success(res) {
wx.showToast({
title: '保存成功',
icon: 'success'
});
},
fail(err) {
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.writePhotosAlbum']) {
// 未授权
wx.showModal({
title: '提示',
content: '小程序请求访问相册权限',
confirmText: '前往授权',
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {}
})
} else if (res.cancel) {}
}
})
}
}
})
}
})
},
})