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.
143 lines
3.8 KiB
143 lines
3.8 KiB
<template> |
|
<view class="content"></view> |
|
</template> |
|
|
|
<script lang="ts" setup> |
|
import { |
|
onLoad, |
|
onShow, |
|
onHide, |
|
onUnload |
|
} from '@dcloudio/uni-app' |
|
// import { storeToRefs } from 'pinia'; |
|
import handleFile from '@/utils/handleFile.js'; |
|
import { |
|
reactive, |
|
defineProps |
|
} from "vue"; |
|
|
|
import file from '@/utils/log.js'; |
|
|
|
const { writeLog, selectLogUrl, delLogFile } = file |
|
|
|
const { root, writeTxt, mkdirs, filelist, readTxt } = handleFile |
|
|
|
let details = reactive({ |
|
activity: null, |
|
receiver: null, |
|
intentFilter: null, |
|
isstart: false, |
|
|
|
}) |
|
|
|
/** 扫码日志 */ |
|
const scanLog = reactive<string[]>([]) |
|
|
|
/** 将扫描的码添加在日志中 */ |
|
const handleScanLog = (scancode : string) => { |
|
// 当日志数超过3000时, 去除末尾 |
|
if (scanLog.length > 3000) scanLog.pop() |
|
scanLog.unshift(scancode) |
|
} |
|
|
|
const props = defineProps<{ ishidestop ?: boolean }>() |
|
onLoad(async () => { |
|
// 从本地获取日志文件 |
|
initScan() |
|
startScan(); |
|
console.log('扫描的--------------onLoad'); |
|
}) |
|
onShow(() => { |
|
startScan(); |
|
console.log('扫描的--------------onShow'); |
|
}) |
|
onHide(() => { |
|
// 将日志文件写入本地 |
|
console.log('props?.ishidestop :>> ', props?.ishidestop); |
|
if (props?.ishidestop) return |
|
stopScan(); |
|
console.log('扫描的--------------onHide'); |
|
}) |
|
onUnload(() => { |
|
stopScan(); |
|
uni.$off('scancodedate') |
|
console.log('扫描的--------------onUnload'); |
|
// 清除三天外的日志 |
|
// #ifdef APP |
|
delLogFile() |
|
// #endif |
|
}) |
|
|
|
// uni.getSystemInfoSync().deviceBrand |
|
function initScan() { |
|
const pdaBroadcastList = { |
|
'seuic': { |
|
// 广播动作 |
|
broadcastAction: 'com.android.server.scannerservice.broadcast', |
|
// 广播标签 |
|
broadcastTag: 'scannerdata' |
|
}, |
|
'ubx': { |
|
// 广播动作 |
|
broadcastAction: 'android.intent.ACTION_DECODE_DATA', |
|
// 广播标签 |
|
broadcastTag: 'barcode_string' |
|
} |
|
} |
|
// #ifdef APP |
|
console.log('广播') |
|
if (plus) { |
|
console.log('getDeviceBrand() :>> ', uni.getSystemInfoSync()); |
|
const { brand } = uni.getSystemInfoSync() |
|
console.log('brand :>> ', brand); |
|
console.log('进入') |
|
details.activity = plus.android.runtimeMainActivity(); //获取activity |
|
console.log('details.activity :>> ', details.activity); |
|
var IntentFilter = (plus.android.importClass('android.content.IntentFilter') as any); |
|
// console.log('IntentFilter :>> ', IntentFilter); |
|
details.intentFilter = new IntentFilter(); |
|
// 检测PDA厂家信息, 配置不同的广播动作 |
|
details.intentFilter.addAction(pdaBroadcastList[brand].broadcastAction) // 换你的广播动作 |
|
// details.intentFilter.addAction('com.android.server.scannerservice.broadcast') // 换你的广播动作 |
|
details.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', { |
|
onReceive: function (context, intent) { |
|
console.log("intent", intent) |
|
plus.android.importClass(intent); |
|
let content = intent.getStringExtra(pdaBroadcastList[brand].broadcastTag); // 换你的广播标签 |
|
// 将扫描 |
|
// let content = intent.getStringExtra('scannerdata'); // 换你的广播标签 |
|
console.log('content :>> ', content); |
|
if (!content) return uni.showToast({ |
|
title: '码值有误', |
|
icon: 'none' |
|
}) |
|
// handleScanLog(content) |
|
|
|
// #ifdef APP |
|
writeLog(content + '>>>>>' + content.replace(/\n/g, ""), false) |
|
// #endif |
|
|
|
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 |
|
} |
|
</script> |
|
|
|
<style> |
|
</style> |