113 lines
3.6 KiB
JavaScript
113 lines
3.6 KiB
JavaScript
|
|
const scanBarcode = {
|
|||
|
|
scanCode: function (option) {
|
|||
|
|
if(!option){
|
|||
|
|
option = {
|
|||
|
|
onlyFromCamera: true,
|
|||
|
|
success:function (code){
|
|||
|
|
uni.showModal({
|
|||
|
|
title: "扫码结果",
|
|||
|
|
content: code,
|
|||
|
|
showCancel: false,
|
|||
|
|
confirmText: '确定'
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
fail:function (error){
|
|||
|
|
console.log(e)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
let isFlash = false;
|
|||
|
|
|
|||
|
|
// 创建一个空的webview
|
|||
|
|
const webview = plus.webview.open(
|
|||
|
|
'',
|
|||
|
|
'webview', {
|
|||
|
|
top: '0px',
|
|||
|
|
width: '100%',
|
|||
|
|
height: '100%',
|
|||
|
|
backButtonAutoControl: "close",
|
|||
|
|
titleNView: {
|
|||
|
|
backgroundColor: 'rgba(0, 0, 0, 0.5)',
|
|||
|
|
autoBackButton: true,
|
|||
|
|
titleColor: '#ffffff',
|
|||
|
|
titleText: "扫码"
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 创建barcode
|
|||
|
|
const barcode = plus.barcode.create(
|
|||
|
|
'barcode',
|
|||
|
|
[plus.barcode.QR,plus.barcode.EAN13,plus.barcode.EAN8,plus.barcode.AZTEC,plus.barcode.DATAMATRIX,plus.barcode.UPCA,plus.barcode.UPCE,plus.barcode.CODABAR,plus.barcode.CODE39,plus.barcode.CODE93,plus.barcode.CODE128,plus.barcode.ITF,plus.barcode.PDF417], // https://www.html5plus.org/doc/zh_cn/barcode.html
|
|||
|
|
{
|
|||
|
|
top: 'auto',
|
|||
|
|
left: 'auto',
|
|||
|
|
width: '100%',
|
|||
|
|
height: '100%',
|
|||
|
|
position: 'static',
|
|||
|
|
scanbarColor: '#f1c01f',
|
|||
|
|
frameColor: '#c0ff01'
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 创建按钮“闪光灯”
|
|||
|
|
const bFlash = new plus.nativeObj.View('nbutton', {
|
|||
|
|
bottom: '20px',
|
|||
|
|
left: option.onlyFromCamera ? '30%' : '5%',
|
|||
|
|
width: '40%',
|
|||
|
|
height: '44px'
|
|||
|
|
}, [{
|
|||
|
|
tag: 'rect',
|
|||
|
|
id: 'rect',
|
|||
|
|
rectStyles: {
|
|||
|
|
radius: '8px',
|
|||
|
|
color: 'rgba(0,0,0,0.5)'
|
|||
|
|
}
|
|||
|
|
}, {
|
|||
|
|
tag: 'font',
|
|||
|
|
id: 'text',
|
|||
|
|
text: '闪光灯',
|
|||
|
|
textStyles: {
|
|||
|
|
color: '#FFFFFF'
|
|||
|
|
}
|
|||
|
|
}]);
|
|||
|
|
|
|||
|
|
// 控制闪光灯按钮样式
|
|||
|
|
bFlash.addEventListener('touchend', (e) => {
|
|||
|
|
isFlash = !isFlash;
|
|||
|
|
isFlash && bFlash.drawText("闪光灯", null, {color: "#00ccff"}, 'text')
|
|||
|
|
!isFlash && bFlash.drawText("闪光灯", null, {color: "#ffffff"}, 'text')
|
|||
|
|
barcode?.setFlash(isFlash);
|
|||
|
|
}, false);
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 监听webview关闭,将相关控件都进行关闭
|
|||
|
|
webview.addEventListener("close", () => {
|
|||
|
|
barcode?.close();
|
|||
|
|
bFlashm?.close();
|
|||
|
|
bAlbum?.close();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 扫码的成功回调:关闭webview,并执行success方法
|
|||
|
|
barcode.onmarked = async (type, code, file, charset) => {
|
|||
|
|
webview.close();
|
|||
|
|
await option.success(code);
|
|||
|
|
};
|
|||
|
|
barcode.onerror = async (error) => {
|
|||
|
|
webview.close();
|
|||
|
|
await option.fail(error);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 将相关控件添加至webview中
|
|||
|
|
plus.webview.getWebviewById('webview').append(barcode)
|
|||
|
|
plus.webview.getWebviewById('webview').append(bFlash)
|
|||
|
|
!option.onlyFromCamera && plus.webview.getWebviewById('webview').append(bAlbum)
|
|||
|
|
|
|||
|
|
// start
|
|||
|
|
barcode.start({
|
|||
|
|
vibrate: true
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default scanBarcode;
|