由于直接调用微信的scanCode,无法自定义界面,所以在此使用原生组件camera
https://developers.weixin.qq.com/miniprogram/dev/component/camera.html
<block wx:if="{{showCamera}}">
<camera flash="{{flash}}" binderror="error" bindscancode="scancode">
在原生camera组件上,覆盖需要用到cover-view和cover-image
<cover-view class='border_writh'></cover-view>
</camera>
</block>
scancode(e){
// 校验扫描结果,并处理
let res = e.detail.result
if(this.data.canScan){
this.setDate({canScan:false})
...
}
}
使用camera 需用户授权使用相机
若用户拒绝可通过opensetting打开设置 设置允许后返回可能会无法重新调用摄像头因此可通过if控制节点 在页面onShow中getSetting查看scope.camera是否授权
wx.showLoading({
title: '加载中',
mask:true
})
wx.getSetting({
success(res) {
if (!res.authSetting['scope.camera']) {
setTimeout(() => {
that.setDate({ShowCamera:false})
}, 300)
} else {
that.setDate({ShowCamera:false})
}
},
complete() {
wx.hideLoading()
}
})
使用相机扫码会持续一直扫描,对应的会一直执行bindscancode
可通过变量控制扫码结果 canScan
扫描成功后canScan为false下次扫描的结果判断后就不会执行
当然也可用此模拟扫码枪 (但还是建议使用专业扫码枪)
可通过 flash控制扫码时 手电筒的开启关闭