zhangwei
2025-03-07 d3e6dd6b3032c119676efa1e9fcee3a8a8043fc5
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID: 1 63,营业执照号:91 510     13   1  332 0061 9 3   K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <!-- #ifdef APP-NVUE -->
    <gcanvas @longpress="longtap" @touchstart="touchstart" @touchend="touchend" :ref="canvasId"
        :style="{ width: w + 'px', height:h + 'px'  }">
    </gcanvas>
    <!-- #endif -->
 
    <!-- #ifdef MP-QQ -->
    <canvas canvas-id="canvas_qrcode" :style="{width:w+'px',height:h+'px'}" @longtap="longtap" @touchstart="touchstart"
        @touchend="touchend"></canvas>
    <!-- #endif -->
 
    <!-- #ifdef MP-WEIXIN -->
    <canvas type="2d" :canvas-id="canvasId" :id="canvasId" :style="{width:w+'px',height:h+'px'}" @longtap="longtap"
        @touchstart="touchstart" @touchend="touchend" v-if="canvasId"></canvas>
    <!-- #endif -->
 
    <!-- #ifndef APP-NVUE || MP-QQ || MP-WEIXIN-->
    <canvas :canvas-id="canvasId" :id="canvasId" :style="{width:w+'px',height:h+'px'}" @longtap="longtap"
        @touchstart="touchstart" @touchend="touchend" v-if="canvasId"></canvas>
    <!-- #endif -->
 
</template>
 
<script>
    import fuiQr from './fui-qr/index.js'
 
    // #ifdef APP-NVUE
    import {
        enable,
        WeexBridge
    } from './gcanvas/index.js';
    // #endif
 
    const utf16to8 = (str) => {
        const len = str.length
        let out = ''
 
        for (let i = 0; i < len; i++) {
            const c = str.charCodeAt(i)
 
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i)
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F))
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F))
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F))
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
            }
        }
 
        return out
    }
    // #ifdef MP-WEIXIN
    const canvasId = `fui_qr_${Math.ceil(Math.random() * 10e5).toString(36)}`
    // #endif
 
    // #ifdef MP-QQ
    const canvasId = 'canvas_qrcode'
    // #endif
    export default {
        name: "fui-qrcode",
        emits: ['ready', 'longclick', 'touchStart', 'touchEnd'],
        props: {
            width: {
                type: [Number, String],
                default: 400
            },
            height: {
                type: [Number, String],
                default: 400
            },
            value: {
                type: String,
                default: ''
            },
            foreground: {
                type: String,
                default: '#181818'
            },
            background: {
                type: String,
                default: '#ffffff'
            }
        },
        data() {
            // #ifndef MP-WEIXIN || MP-QQ
            const canvasId = `fui_qr_${Math.ceil(Math.random() * 10e5).toString(36)}`
            // #endif
            return {
                canvasId,
                //误差校正等级
                errorCorrectLevel: 2,
                //类型
                typeNumber: -1,
                w: 200,
                h: 200
            };
        },
        computed: {
            initDraw() {
                return `${this.width}_${this.height}_${this.foreground}_${this.background}_${this.value}`
            }
        },
        watch: {
            initDraw(val) {
                this.w = uni.upx2px(this.width || 400)
                this.h = uni.upx2px(this.height || 400)
                this.$nextTick(() => {
                    this.draw()
                })
            }
        },
        created() {
            this.w = uni.upx2px(this.width || 400)
            this.h = uni.upx2px(this.height || 400)
            this.ctx = null;
        },
        mounted() {
            this.$nextTick(() => {
                setTimeout(() => {
                    this.draw()
                    this.$emit('ready', {
                        canvasId: this.canvasId
                    })
                }, 50)
            })
        },
        // #ifndef VUE3
        beforeDestroy() {
            this.ctx = null;
        },
        // #endif
        // #ifdef VUE3
        beforeUnmount() {
            this.ctx = null;
        },
        // #endif
        methods: {
            // #ifdef MP-WEIXIN
            getContext(callback) {
                const query = uni.createSelectorQuery().in(this)
                query.select(`#${this.canvasId}`)
                    .fields({
                        node: true,
                        size: true
                    })
                    .exec((res) => {
                        const canvas = res[0].node
                        const ctx = canvas.getContext('2d')
                        const dpr = uni.getSystemInfoSync().pixelRatio
                        canvas.width = res[0].width * dpr
                        canvas.height = res[0].height * dpr
                        ctx.scale(dpr, dpr)
                        this.ctx = ctx;
                        callback && callback()
                    })
            },
            // #endif
            drawQrcode() {
                // #ifdef MP-WEIXIN
                this.ctx.clearRect(0, 0, this.w, this.h);
                // #endif
                const qrcode = fuiQr(utf16to8(this.value), {
                    typeNumber: this.typeNumber,
                    errorCorrectLevel: this.errorCorrectLevel,
                })
                const cells = qrcode.modules
                const tileW = this.w / cells.length
                const tileH = this.h / cells.length
 
                if (!this.ctx) {
                    // #ifdef APP-NVUE
                    let ganvas = this.$refs[this.canvasId];
                    /*通过元素引用获取canvas对象*/
                    let canvasObj = enable(ganvas, {
                        bridge: WeexBridge
                    });
                    /*获取绘图所需的上下文,暂不支持3d*/
                    this.ctx = canvasObj.getContext('2d');
                    // #endif
 
                    // #ifndef APP-NVUE || MP-WEIXIN
                    this.ctx = uni.createCanvasContext(this.canvasId, this)
                    // #endif
                }
                // #ifndef MP-WEIXIN
                this.ctx.scale(1, 1)
                // #endif
                cells.forEach((row, rdx) => {
                    row.forEach((cell, cdx) => {
                        // #ifdef MP-WEIXIN
                        this.ctx.fillStyle = cell ? this.foreground : this.background
                        // #endif
                        // #ifndef MP-WEIXIN
                        this.ctx.setFillStyle(cell ? this.foreground : this.background)
                        // #endif
                        const w = (Math.ceil((cdx + 1) * tileW) - Math.floor(cdx * tileW))
                        const h = (Math.ceil((rdx + 1) * tileH) - Math.floor(rdx * tileH))
                        this.ctx.fillRect(Math.round(cdx * tileW), Math.round(rdx * tileH), w, h)
                    })
                })
 
                // #ifndef MP-WEIXIN
                this.ctx.draw()
                // #endif
            },
            draw() {
                // #ifdef MP-WEIXIN
                if (!this.ctx) {
                    this.getContext(() => {
                        this.drawQrcode()
                    })
                } else {
                    this.drawQrcode()
                }
                // #endif
 
                // #ifndef MP-WEIXIN
                this.drawQrcode()
                // #endif
            },
            longtap() {
                this.$emit('longclick', {})
            },
            touchstart() {
                this.$emit('touchStart', {})
            },
            touchend() {
                this.$emit('touchEnd', {})
            }
        }
    }
</script>
 
<style scoped></style>