之前講過前端生成二維碼,那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>