-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathqrcode.ts
More file actions
103 lines (92 loc) · 2.13 KB
/
qrcode.ts
File metadata and controls
103 lines (92 loc) · 2.13 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
import props from './props';
import config from '../common/config';
import { SuperComponent, wxComponent } from '../common/src/index';
const { prefix } = config;
const name = `${prefix}-qrcode`;
@wxComponent()
export default class QRCode extends SuperComponent {
externalClasses = [`${prefix}-class`, `${prefix}-class-canvas`];
options = {
multipleSlots: true,
virtualHost: true,
};
properties = {
...props,
statusRender: {
type: Boolean,
value: false,
},
style: {
type: String,
value: '',
},
customStyle: {
type: String,
value: '',
},
};
data = {
prefix,
showMask: false,
classPrefix: name,
canvasReady: false,
};
lifetimes = {
async ready() {
const canvasComp = this.selectComponent('#qrcodeCanvas'); // 获取 canvas 示例
const canvas = await canvasComp.getCanvasNode();
this.setData({ canvasNode: canvas });
},
attached() {
this.setData({
showMask: this.properties.status !== 'active',
});
},
};
observers = {
status: function (newVal: string) {
this.setData({
showMask: newVal !== 'active',
});
},
};
methods = {
// 用于外部调用,重新绘制二维码
init() {
const canvasComp = this.selectComponent('#qrcodeCanvas');
if (canvasComp) {
canvasComp.initCanvas();
}
},
handleDrawCompleted() {
this.setData({
canvasReady: true,
});
},
handleDrawError(err) {
console.error('二维码绘制失败', err);
},
handleRefresh() {
this.triggerEvent('refresh');
},
// 二维码下载方法
async handleDownload() {
if (!this.data.canvasNode) {
console.error('未找到 canvas 节点');
return;
}
wx.canvasToTempFilePath(
{
canvas: this.data.canvasNode,
success: (res) => {
wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath });
},
fail: (err) => {
console.error('canvasToTempFilePath failed', err);
},
},
this,
);
},
};
}