web
20 小时以前 49fa0d82a40345342966e810b44429aec0480ef3
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
<template>
    <view class="l-qrcode" :style="[styles]">
        <!-- #ifndef APP-NVUE -->
        <canvas :style="styles" type="2d" :canvas-id="canvasId" :id="canvasId"></canvas>
        <!-- #endif  -->
        <!-- #ifdef APP-NVUE  -->
        <web-view 
        ref="qrcodeRef"
        @pagefinish="onFinished" 
        @error="onError" 
        @onPostMessage="onMessage"
        :style="styles" src="/uni_modules/lime-qrcode/hybrid/html/index.html?v=1"></web-view>
        <!-- #endif  -->
        <!-- <view class="l-qrcode-mask" v-if="['loading', 'expired'].includes(props.status)">
            <l-loading v-if="props.status == 'loading'"></l-loading>
            <view class="l-qrcode-expired" v-if="props.status == 'expired'">
                <slot></slot>
            </view>
        </view> -->
    </view>
</template>
<script lang="ts">
    // @ts-nocheck
    import { computed, defineComponent, getCurrentInstance, watch, onUnmounted, onMounted } from '@/uni_modules/lime-shared/vue';
    import QRCodeProps from './props'
    // #ifndef APP-NVUE
    import { getCanvas, isCanvas2d } from './useCanvas'
    import { QRCodeCanvas } from './qrcode.js';
    // #endif
    import { addUnit } from '@/uni_modules/lime-shared/addUnit'
    import { createImage } from '@/uni_modules/lime-shared/createImage'
    import { unitConvert } from '@/uni_modules/lime-shared/unitConvert'
    import { isBase64 } from '@/uni_modules/lime-shared/isBase64'
    import { pathToBase64 } from '@/uni_modules/lime-shared/pathToBase64'
    import { debounce } from '@/uni_modules/lime-shared/debounce'
    const name = 'l-qrcode'
    export default defineComponent({
        name,
        props: QRCodeProps,
        emits: ['success'],
        setup(props, {emit}) {
            const context = getCurrentInstance();
            const canvasId = `l-qrcode${context.uid}` 
            const styles = computed(() => `width: ${addUnit(props.size)}; height: ${addUnit(props.size)};`)
            let qrcode = null
            let canvas = null
            const qrCodeProps = computed(() => {
                const { value, icon, size, color, bgColor, bordered, iconSize, errorLevel, marginSize } = props
                const imageSettings = {
                    src: icon,
                    x: undefined,
                    y: undefined,
                    height: unitConvert(iconSize),
                    width: unitConvert(iconSize),
                    excavate: true,
                }
                return {
                    value,
                    size: unitConvert(size),
                    level: errorLevel,
                    bgColor,
                    fgColor: color,
                    imageSettings: icon ? imageSettings : undefined,
                    includeMargin: bordered,
                    marginSize: marginSize ?? 0
                }
 
            })
            
            // #ifdef APP-NVUE
            const stacks = new Map()
            // #endif
            const canvasToTempFilePath = debounce((args: UniNamespace.CanvasToTempFilePathRes) => {
                if(!canvas) return
                // #ifndef APP-NVUE
                const copyArgs = Object.assign({
                    canvasId,
                    canvas: null
                }, args)
                
                if (isCanvas2d) {
                    copyArgs.canvas = canvas
                }
                if ('toTempFilePath' in canvas) {
                    canvas.toTempFilePath(copyArgs)
                } else {
                    uni.canvasToTempFilePath(copyArgs, context);
                }
                // #endif
                // #ifdef APP-NVUE
                if(!stacks.size) {
                    const flie = 'file-' + Math.random();
                    const stack = {args, time: +new Date()}
                    stacks.set(`${flie}`, stack)
                    canvas.toDataURL(flie)
                    setTimeout(() => {
                        const stack = stacks.get(flie)
                        if(stack && 'fail' in stack.args) {
                            stack.args.fail({
                                error: '超时'
                            })
                            stacks.delete(flie)
                        }
                    },5000)
                } 
                // #endif
            })
            const useCanvasToTempFilePath = () => {
                if(props.useCanvasToTempFilePath) {
                    canvasToTempFilePath({
                        success(res: UniNamespace.CanvasToTempFilePathRes) {
                            emit('success', res.tempFilePath)
                        }
                    })
                }
            }
            // #ifdef APP-NVUE
            const onFinished = () => {
                const { pixelRatio } = uni.getSystemInfoSync()
                canvas = {
                    toDataURL(flie: string) {
                        const ref: any = context.refs['qrcodeRef'];
                        if(ref) {
                            ref?.evalJS(`toDataURL('${flie}')`)
                        }
                    }
                };
                qrcode = {
                    async render(props: any) {
                        const ref: any = context.refs['qrcodeRef'];
                        const { src } = props.imageSettings || { };
                        if(!ref) return
                        if(src && !isBase64(src) && !/^http/.test(src) && /^\/static/.test(src)) {
                            props.imageSettings.src = await pathToBase64(src)
                        }
                        const _props = JSON.stringify(Object.assign({}, props, {pixelRatio}));
                        ref?.evalJS(`render(${_props})`);
                    }
                }
                qrcode.render(qrCodeProps.value)
                useCanvasToTempFilePath()
            }
            const onError = () => {
                console.warn('lime-qrcode 加载失败')
            }
            const onMessage = (e: any) => {
                const {detail:{data: [res]}} = e
                if(res.event == 'toDataURL') {
                    const {file, image, msg} = res.data;
                    const stack = stacks.get(file)
                    if(stack && image && 'success' in stack.args) {
                        stack.args.success({tempFilePath: image})
                        stacks.delete(file)
                    } else if(stack && 'fails' in stack.args) {
                        stack.args.fail({error: msg})
                        stacks.delete(file)
                    }
                } 
            }
            // #endif
            const propsWatch = watch(props, () => {
                if (qrcode) {
                    qrcode.render(qrCodeProps.value)
                    useCanvasToTempFilePath()
                }
            })
            onMounted(() => {
                // #ifndef APP-NVUE
                getCanvas(canvasId, { context }).then(res => {
                    canvas = res
                    qrcode = new QRCodeCanvas(res, {
                        // #ifdef H5
                        path2D: false,
                        // #endif
                        pixelRatio: isCanvas2d ? uni.getSystemInfoSync().pixelRatio : 1,
                        createImage
                    })
                    qrcode.render(qrCodeProps.value)
                    useCanvasToTempFilePath()
                })
                // #endif
            })
            onUnmounted(() => {
                propsWatch && propsWatch()
            })
            return {
                canvasId,
                styles,
                props,
                canvasToTempFilePath,
                
                // #ifdef APP-NVUE
                onFinished,
                onError,
                onMessage
                // #endif
            }
        }
    })
</script>
<style lang="scss">
    .l-qrcode {
        position: relative;
 
        &-mask {
            position: absolute;
            inset: 0;
            // inset-block-start: 0;
            // inset-inline-start: 0;
            z-index: 10;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            // width: 100%;
            // height: 100%;
            color: rgba(0, 0, 0, 0.88);
            line-height: 1.5714285714285714;
            background: rgba(255, 255, 255, 0.96);
            text-align: center;
        }
    }
</style>