98 lines
3.5 KiB
Vue
98 lines
3.5 KiB
Vue
<template>
|
||
<!-- 该组件无实际 DOM 结构 -->
|
||
<div style="display: none"></div>
|
||
</template>
|
||
|
||
<script setup>
|
||
import { onMounted } from 'vue'
|
||
// 定义自定义事件
|
||
const emit = defineEmits(['scanSuccessBox', 'scanErrorBox'])
|
||
|
||
// 扫描二维码方法
|
||
const scanQrCode = () => {
|
||
console.log('扫一扫')
|
||
console.log('window对象', window)
|
||
|
||
try {
|
||
document.addEventListener('deviceready', () => {
|
||
window.cordova.plugins.zxingScan.startScan(
|
||
(successResult) => {
|
||
console.log('🚀 ~ 二维码 ~ successResult:', successResult)
|
||
if (successResult.data == '操作已取消') return
|
||
emit('scanSuccessBox', successResult)
|
||
},
|
||
(error) => {
|
||
console.log('🚀 ~ 二维码 ~ error:', error)
|
||
emit('scanErrorBox', new Error(`扫描失败: ${error}`))
|
||
},
|
||
{
|
||
isPlayBeep: true, //是否播放扫描声音
|
||
isShake: true, //是否震动
|
||
fullScreenScan: true, //是否全屏
|
||
},
|
||
)
|
||
})
|
||
} catch (error) {
|
||
console.log('🚀 ~ scanQrCode ~ error:', error)
|
||
}
|
||
|
||
// document.addEventListener('deviceready', onDeviceReady.bind(this), false)
|
||
|
||
function onDeviceReady() {
|
||
window.cordova.plugins.zxingScan.startScan(
|
||
(successResult) => {
|
||
console.log('🚀 ~ 二维码 ~ successResult:', successResult)
|
||
emit('scanSuccessBox', successResult)
|
||
},
|
||
(error) => {
|
||
console.log('🚀 ~ 二维码 ~ error:', error)
|
||
emit('scanErrorBox', new Error(`扫描失败: ${error}`))
|
||
},
|
||
{
|
||
isPlayBeep: true, //是否播放扫描声音
|
||
isShake: true, //是否震动
|
||
fullScreenScan: true, //是否全屏
|
||
},
|
||
)
|
||
// Cordova is now initialized. Have fun!
|
||
// console.log('Running cordova-' + window.cordova.platformId + '@' + window.cordova.version)
|
||
// window.cordova.plugins.barcodeScanner.scan(
|
||
// function (result) {
|
||
// emit('scanSuccessBox', result)
|
||
// console.log('success')
|
||
// console.log(result)
|
||
// }.bind(this),
|
||
// function (error) {
|
||
// emit('scanErrorBox', new Error(`扫描失败: ${error}`))
|
||
// console.log('error')
|
||
// console.log(error)
|
||
// },
|
||
// {
|
||
// preferFrontCamera: false, // 是否默认使用前置摄像头(同时支持 iOS 和 Android)
|
||
// showFlipCameraButton: true, // 是否显示前后摄像头切换按钮(同时支持 iOS 和 Android)
|
||
// showTorchButton: true, // 是否显示打开关闭闪光灯按钮(同时支持 iOS 和 Android)
|
||
// torchOn: false, // 是否默认打开闪关灯(仅支持 Android)
|
||
// saveHistory: true, // 是否记录扫码历史(仅支持 Android)
|
||
// prompt: '请扫描二维码', // 扫码界面下方提示语(仅支持 Android)
|
||
// resultDisplayDuration: 500, // 扫码文本在扫码界面上显示多少毫秒。0完全禁用。默认1500(仅Android)
|
||
// formats: 'QR_CODE,PDF_417', // 支持的格式,默认为除PDF_417和RSS_EXPANDED之外的所有格式
|
||
// orientation: 'portrait', // 设置为横向或纵向(portrait|landscape),默认跟随手机旋转(仅Android)
|
||
// disableAnimations: true, // 禁用动画(仅支持 iOS)
|
||
// disableSuccessBeep: false, // 禁用成功蜂鸣声(同时支持 iOS 和 Android)
|
||
// },
|
||
// )
|
||
}
|
||
}
|
||
|
||
// 暴露扫描方法
|
||
defineExpose({
|
||
scanQrCode,
|
||
})
|
||
|
||
onMounted(() => {
|
||
if (!window.cordova) {
|
||
console.warn('Cordova 未加载,二维码扫描功能可能不可用')
|
||
}
|
||
})
|
||
</script>
|