You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
85 lines
1.8 KiB
<template> |
|
<view class="content"></view> |
|
</template> |
|
|
|
<script lang="ts"> |
|
import { |
|
onLoad, |
|
onShow, |
|
onHide, |
|
onUnload |
|
} from '@dcloudio/uni-app' |
|
import { |
|
reactive, |
|
defineProps |
|
} from "vue"; |
|
export default { |
|
setup() { |
|
let details = reactive({ |
|
activity: null, |
|
receiver: null, |
|
intentFilter: null, |
|
isstart:false, |
|
// ishidestop:false, |
|
}) |
|
const props=defineProps<{ishidestop?:boolean}>() |
|
onLoad(() => { |
|
initScan() |
|
startScan(); |
|
}) |
|
onShow(()=>{ |
|
startScan(); |
|
}) |
|
onHide(() => { |
|
if(props?.ishidestop)return |
|
stopScan(); |
|
}) |
|
onUnload(() => { |
|
stopScan(); |
|
uni.$off('scancodedate') |
|
}) |
|
|
|
function initScan() { |
|
// #ifdef APP |
|
if (plus) { |
|
details.activity = plus.android.runtimeMainActivity(); //获取activity |
|
var IntentFilter = plus.android.importClass('android.content.IntentFilter'); |
|
details.intentFilter = new IntentFilter(); |
|
details.intentFilter.addAction("com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED") // 换你的广播动作 |
|
details.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', { |
|
onReceive: function(context, intent) { |
|
plus.android.importClass(intent); |
|
let content = intent.getStringExtra('data'); // 换你的广播标签 |
|
if (!_details.sfhd) { |
|
uni.$emit('scancodedate', content.replace(/\n/g, "")) |
|
} |
|
} |
|
}); |
|
} |
|
// #endif |
|
} |
|
|
|
function startScan() { |
|
// #ifdef APP |
|
if(details.isstart) return |
|
details.isstart=true |
|
details.activity.registerReceiver(details.receiver, details.intentFilter); |
|
// #endif |
|
} |
|
function stopScan() { |
|
// #ifdef APP |
|
details.isstart=false |
|
details.activity.unregisterReceiver(details.receiver); |
|
// #endif |
|
} |
|
return { |
|
startScan, |
|
stopScan, |
|
initScan, |
|
} |
|
} |
|
} |
|
</script> |
|
|
|
<style> |
|
</style> |