Houjie
2025-04-11 1bf977929dd324f3ac64b70debd8a79443c54392
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
<template>
    <view class="container">
        <button class="scan" @click="openQrcode">点击扫码</button>
        <view class="reader-box" v-if="isScaning">
            <view class="reader" id="reader"></view>
        </view>
        <view>扫码内容:{{scanVal}}</view>
    </view>
</template>
<script>
    import {
        Html5Qrcode
    } from "html5-qrcode";
    export default {
        name: 'spare',
        watch: {
            cur: {
                immediate: true,
                handler() {
                    console.log('watch', this.cur)
                },
            },
        },
        data() {
            return {
                html5QrCode: null,
                isScaning: false,
                cameraId: null,
                scanVal: ''
            }
        },
        methods: {
 
            isValidURL(url) {
                const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
                    '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
                    '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
                    '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
                    '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
                    '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
                return !!pattern.test(url);
            },
            checkURL() {
                const url = this.scanVal;
                if (this.isValidURL(url)) {
                    uni.openURL({
                        url: url
                    })
                } else {
                    uni.showToast({
                        title: 'url地址错误,请重试',
                        icon: 'none',
                        duration: 2000
                    });
                }
            },
        
        openQrcode() {
            this.isScaning = true;
            Html5Qrcode.getCameras().then((devices) => {
                if (devices && devices.length) {
                    if (devices.length > 1) {
                        this.cameraId = devices[1].id;
                    } else {
                        this.cameraId = devices[0].id;
                    }
                    this.html5QrCode = new Html5Qrcode('reader');
                    this.startInit();
                }
            });
        },
        startInit() {
            const that = this;
            this.html5QrCode.start(
                    this.cameraId, // retreived in the previous step.
                    // {
                    //     facingMode: "environment" //  environment后置摄像头 user前置摄像头
                    // }, 
                    {
                        fps: 10, // sets the framerate to 10 frame per second
                        qrbox: 250 // sets only 250 X 250 region of viewfinder to
                    },
                    qrCodeMessage => {
                        uni.showModal({
                            title: 'success',
                            content: JSON.stringify(qrCodeMessage),
                            showCancel: false
                        });
                        // do something when code is read. For example:
                        if (qrCodeMessage) {
                            //成功扫出码qrCodeMessage为扫码内容
                            //扫码成功记得关掉摄像
                            that.action(qrCodeMessage) //对二维码逻辑处理
                            that.stopScan(); //关闭扫码功能
                        }
 
                    },
                    errorMessage => {
                        console.log('errorMessage', errorMessage);
                        if (errorMessage.includes('NotFoundException')) {
                            uni.showToast({
                                title: '未找到二维码,请重新对准',
                                icon: 'none',
                                duration: 2000
                            });
                        } else {
                            uni.showToast({
                                title: '扫码失败,请重试',
                                icon: 'none',
                                duration: 2000
                            });
                        }
 
                    }
                )
                .catch((err) => {
                    // 扫码错误信息
                    let message = "";
                    if (typeof err == "string") {
                        message = "识别失败";
                    } else {
                        if (err.name == "NotAllowedError") {
                            message = "您需要授予相机访问权限!";
                        }
                        if (err.name == "NotFoundError") {
                            message = "这个设备上没有摄像头!";
                        }
                        if (err.name == "NotSupportedError") {
                            message =
                                "摄像头访问只支持在安全的上下文中,如https或localhost!";
                        }
                        if (err.name == "NotReadableError") {
                            message = "相机被占用!";
                        }
                        if (err.name == "OverconstrainedError") {
                            message = "安装摄像头不合适!";
                        }
                        if (err.name == "StreamApiNotSupportedError") {
                            message = "此浏览器不支持流API!";
                        }
                    }
                    uni.showToast({
                        title: message,
                        icon: 'none',
                        duration: 2000
                    });
                });
        },
        action(val) {
            this.scanVal = val;
            const url = this.scanVal;
            this.checkURL(url)
 
            // plus.runtime.openURL(this.scanVal, function(res) {
            //     console.log(res);
            // });
        },
        stopScan() {
            console.log('停止扫码')
            this.isScaning = false;
            if (this.html5QrCode) {
                this.html5QrCode.stop()
                    .then((ignore) => {
                        console.log("停止扫码", ignore);
                    })
                    .catch((err) => {
                        console.log(err);
                        showToast("停止扫码失败");
                    });
            }
        },
 
    }
    }
</script>
 
<style>
    .container {
        height: 100%;
    }
 
    .reader-box {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.5);
    }
 
    .reader {
        width: 540rpx;
        height: 540rpx;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
</style>