web
9 小时以前 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
224
225
226
227
228
229
230
231
232
233
234
235
<template>
    <!-- #ifdef MP -->
    <canvas :style="styles" type="2d" :canvas-id="canvasId" :id="canvasId"></canvas>
    <!-- #endif  -->
    <!-- #ifdef APP -->
    <view class="l-qrcode" ref="drawableRef" :style="[styles]">
        <image class="l-qrcode__icon" v-if="icon" :src="icon" :style="[iconStyle]"></image>
    </view>
    <!-- #endif  -->
    <!-- #ifdef WEB -->
    <view class="l-qrcode" ref="drawableRef" :style="[styles]">
        
    </view>
    <!-- #endif  -->
</template>
<script lang="uts" setup>
    import { type PropType, nextTick } from 'vue'
    // #ifndef APP
    import { QRCodeCanvas } from './qrcode.js';
    import { QRCodePropsTypes , ImageSettings } from './type'
    // #endif
    // #ifdef APP
    import { QRCodeCanvas, QRCodePropsTypes , ImageSettings } from '@/uni_modules/lime-qrcodegen'
    // #endif
    // import { addUnit } from '@/uni_modules/lime-shared/addUnit'
    // import { unitConvert } from '@/uni_modules/lime-shared/unitConvert'
    // import { toBoolean } from '@/uni_modules/lime-shared/toBoolean'
    import { addUnit, unitConvert } from './utils'
    import { LQrcodeFailCallback, LQrcodeCompleteCallback, LQrcodeSuccessCallback} from './type'
    
    const name = 'l-qrcode'
    const props = defineProps({
        value: {
            type: String
        },
        icon: {
            type: String
        },
        // #ifdef APP-ANDROID
        size: {
            type: Object,
            default: 160
        },
        iconSize: {
            type: Object,
            default: 40
        },
        // #endif
        // #ifndef APP-ANDROID
        size: {
            type: [Number, String],
            default: 160
        },
        iconSize: {
            type: [Number, String],
            default: 40
        },
        // #endif
        marginSize: {
            type: Number,
            default: 0
        },
        color: {
            type: String,
            default: '#000'
        },
        bgColor: {
            type: String,
            default: 'transparent'
        },
        bordered: {
            type: Boolean,
            default: true
        },
        errorLevel: {
            type: String as PropType<'L' | 'M' | 'Q' | 'H'>,
            default: 'M' // 'L' | 'M' | 'Q' | 'H'
        },
        useCanvasToTempFilePath: {
            type: Boolean,
            default: false
        }
        // status: {
        //     type: String as PropType<'active'|'expired'|'loading'>,
        //     default: 'active' // active | expired | loading
        // }
    })
    const emits = defineEmits(['success'])
    const context = getCurrentInstance();
    const canvasId = `l-qrcode${context!.uid}` 
    const styles = computed<Map<string, any>>(():Map<string, any>=>{
        const style = new Map<string, any>()
        const size = addUnit(props.size);
        if(size!=null){
            style.set('width', size)
            style.set('height', size)
        }
        style.set('background', props.bgColor)
        return style
    })
    // #ifdef APP
    const iconStyle = computed<Map<string, any>>(():Map<string, any>=>{
        const style = new Map<string, any>()
        const size = addUnit(props.iconSize);
        if(size!=null){
            style.set('width', size)
            style.set('height', size)
        }
        return style
    })
    // #endif
    const drawableRef = ref<UniElement|null>(null);
    // #ifdef WEB
    let canvas:HTMLCanvasElement|null = null
    // #endif
    let qrcode:QRCodeCanvas|null = null
    
    const canvasToTempFilePath = (options: UTSJSONObject)=>{
        
        const format =   options.getString('format') ?? 'png';
        const fail =     options.get('fail') as LQrcodeFailCallback | null;
        const complete = options.get('complete') as LQrcodeCompleteCallback | null;
        const success =  options.get('success') as LQrcodeSuccessCallback | null;
        // #ifdef APP
        const newOptions = {
            format,
            fail,
            complete,
            success,
        } as TakeSnapshotOptions
        drawableRef.value!.takeSnapshot(newOptions)
        // #endif
        // #ifdef WEB
        success?.({
            tempFilePath: canvas?.toDataURL('image/'+format)
        })
        // #endif
        
    }
    const render = ()=>{
        const param:QRCodePropsTypes = {
            value: props.value,
            size:  unitConvert(props.size),
            fgColor: props.color,
            level: ['L', 'M', 'Q', 'H'].includes(props.errorLevel) ? props.errorLevel : 'M',
            marginSize: props.marginSize,
            includeMargin: props.bordered,
            imageSettings: null,
        } as QRCodePropsTypes
        
        if(props.icon != null){
        // if(toBoolean(props.iconSize) && toBoolean(props.icon)){
            const size = unitConvert(props.iconSize)
            param.imageSettings = {
                src: props.icon,
                width: size,
                height: size,
                excavate: true
            } as ImageSettings
        }
        qrcode?.render(param)
        
        if(props.useCanvasToTempFilePath){
            setTimeout(()=>{
                canvasToTempFilePath({
                    success: (res: TakeSnapshotSuccess)=>{
                        emits('success', res.tempFilePath)
                    }
                })
            },100)
        }
    }
    defineExpose({
        canvasToTempFilePath
    })
    onMounted(()=>{
        nextTick(()=>{
            // #ifdef APP
            const ctx = drawableRef.value!.getDrawableContext();
            qrcode = new QRCodeCanvas(ctx!)
            // #endif
            // #ifdef WEB
            canvas = document.createElement('canvas')
            canvas.style.width = '100%'
            canvas.style.height = '100%'
            drawableRef.value!.appendChild(canvas)
            qrcode = new QRCodeCanvas(canvas, {
                pixelRatio: uni.getSystemInfoSync().pixelRatio,
                createImage: () => {
                    const image = new Image();
                    image.crossOrigin = 'anonymous'; 
                    return image;
                }
            })
            // #endif
            
            watchEffect(()=>{
                render()
            })
        })
        
    })
    onUnmounted(()=>{
        // #ifdef WEB
        canvas?.remove();
        // #endif
        qrcode = null;
    })
</script>
<style lang="scss">
    .l-qrcode {
        position: relative;
        background-color: aqua;
        justify-content: center;
        align-items: center;
 
        &-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>