如何實現掃碼功能?


之前講過前端生成二維碼,那H5 APP如何實現掃碼功能;

掃碼功能的實現

(一)、使用H5 API barcode實現掃碼功能
# 模板
    <template>
        <div>
            <div class="scan">
                <div id="bcid"></div>
                <footer>
                    <button v-if="Start" @click="startRecognize">掃 碼 加 油</button>
                </footer>
            </div>
        </div>
    </template>

    <script>
        // 掃描對象
        var scan = null;
        data(){
            return {
                Begin:true
            }
        },
        methods:{
            startRecognize(){
                const that = this;
                this.Begin = false;
                if(!window.plus) return;
                scan = new plus.barcode.Barcode('bcid');
                // 開始掃描
                scan.start();
                // 掃碼成功之后的回調函數 type是類型 result 結果
                scan.onmarked = function(type,result){
                    const text = '';
                    switch(type){
                        case plus.barcode.QR:
                            type = 'QR';
                            break;
                        case plus.barcode.EAN13:
                            type = "EAN13";
                            break;
                        case plus.barcode.EAN8:
                            type = "EAN8";
                            break;
                        default:
                            type = "其它" + type;
                            break;
                    }
                    result = result.replace(/\n/g,"");
                    const id = result.match(/\d+/)[0];
                    const Token = localStorage.getItem('token');
                    that.$router.push({
                        name:'OrderDetail',
                        params: {
                            id:id,
                            token:token
                        }
                    });
                    scan.close();
                }
            },
            // 關閉掃碼識別控件 
            closeScan(){
                if(!window.plus) return;
                scan.close();
                this.$router.push('./qrcode');
            }
        }
    </script>

    <style lang="less" scoped>
    .scan {
        height: 96vh;
        background-color: #EFEFEF;

        #bcid {
            width: 100vw;
            height: 96vh;
            text-align: center;
            color: #fff;
        }

        footer {
            width: 100%;
            height: 8rem;
            line-height: 2rem;
            font-size: 14px;
            position: absolute;
            left: 0;
            bottom: 10rem;
            z-index: 999;

            button {
                width: 8rem;
                height: 8rem;
                border: 4px solid #1f4ba5;
                border-radius: 50%;
                font-size: 16px;
                font-family: PingFangSC-Medium;
                background-color: #fff;
            }   
    }
}

    </style>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM