吉安感知网项目-前端
罗广辉
2026-01-26 beb95fb5fc166804056abafd70fc01ac27de7621
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
<template>
    <div class="container">
        <div id="reader"></div>
 
        <div class="scan-tip">将二维码对准扫描框</div>
    </div>
</template>
 
<script setup>
document.title = '扫码'
 
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { Html5Qrcode } from 'html5-qrcode'
 
let cameraId = ref('')
let devicesInfo = ref('')
let html5QrCode = ref(null)
const router = useRouter()
 
onMounted(() => {
    getCameras()
})
 
onUnmounted(() => {
    stop()
 
    document.title = ''
})
 
const getCameras = () => {
    Html5Qrcode.getCameras()
        .then(devices => {
            console.log('摄像头信息', devices)
            if (devices && devices.length) {
                // 如果有2个摄像头,1为前置的
                if (devices.length > 1) {
                    cameraId.value = devices[1].id
                } else {
                    cameraId.value = devices[0].id
                }
                devicesInfo.value = devices
                // start开始扫描
                start()
            }
        })
        .catch(err => {
            // handle err
            console.log('获取设备信息失败', err) // 获取设备信息失败
        })
}
 
function removeParamFromUrl (url, paramToRemove) {
    // 分割URL和查询参数部分
    const [baseUrl, queryString] = url.split('?')
 
    // 如果没有查询参数,直接返回原URL
    if (!queryString) return url
 
    // 用 & 分割所有参数
    const params = queryString.split('&')
 
    // 过滤掉要删除的参数
    const filteredParams = params.filter(param => {
        // 获取参数名(分割第一个=之前的部分)
        const paramName = param.split('=')[0]
        return paramName !== paramToRemove
    })
 
    // 重新组合URL
    if (filteredParams.length === 0) {
        return baseUrl // 如果没有参数了,只返回基础URL
    } else {
        return baseUrl + '?' + filteredParams.join('&')
    }
}
 
const start = () => {
    html5QrCode = new Html5Qrcode('reader')
    html5QrCode
        .start(
            // retreived in the previous step.
            cameraId.value,
            {
                fps: 10, // 设置每秒多少帧
                qrbox: { width: 250, height: 250 }, // 设置取景范围
                aspectRatio: 1, // 宽高比
                disableFlip: false, // 允许翻转扫描
            },
            (decodedText, decodedResult) => {
                if (decodedText.includes('ztzf')) {
                    const newUrl = removeParamFromUrl(decodedText, 'from')
 
                    const transmitData = { data: { type: 'browser', data: { url: newUrl } } }
                    wx.miniProgram.navigateTo({ url: "/subPackages/browser/index?url=" + encodeURIComponent(newUrl) })
                    wx.miniProgram.postMessage(transmitData)
                    uni.postMessage(transmitData)
                }
            },
            errorMessage => {
                // parse error, ideally ignore it. For example:
                // console.log(`QR Code no longer in front of camera.`);
                console.log('暂无额扫描结果', errorMessage)
            }
        )
        .catch(err => {
            // Start failed, handle it. For example,
            console.log(`Unable to start scanning, error: ${err}`)
        })
}
const stop = () => {
    html5QrCode
        .stop()
        .then(ignore => {
            // QR Code scanning is stopped.
            console.log('QR Code scanning stopped.', ignore)
        })
        .catch(err => {
            // Stop failed, handle it.
            console.log('Unable to stop scanning.', err)
        })
}
</script>
 
<style lang="scss" scoped>
.container {
    position: fixed;
    width: 100%;
    height: 100%;
    background: #000;
    z-index: 9999;
}
 
#reader {
    top: 50%;
    left: 0;
    transform: translateY(-50%);
}
 
/* 扫描提示文字 */
.scan-tip {
    width: 100%;
    position: absolute;
    bottom: 44px;
    left: 50%;
    transform: translateX(-50%);
    color: white;
    font-size: 14px;
    text-align: center;
}
</style>