货无忧
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.
 
 
 
 
 

431 lines
10 KiB

<template>
<view class="content"></view>
<view :class="{scan_continer: true,active:details.isShowEditMask}"
:style="{top: details.btnPosition.y + 'px', left: details.btnPosition.x + 'px', zIndex: isShowScanBtn? '100000': '-1', display: isShowScanBtn ? 'flex': 'none'}"
@touchstart="(e)=> handleTouchStart(e)" @touchmove="handleTouchMove" @touchend="(e) => handleTouchEnd(e)"
id="scanBtn">
<u-icon name="scan" :color="details.isShowEditMask? '#fff':'#d3832a'" size="60" />
</view>
<view :class="{mask: true, active:details.isShowEditMask}" id="SetEditScanBtnMask" @touchmove.stop="handleTouchMove">
<scroll-view scroll-y="true" style="height: 100vh;" :enable-back-to-top="false">
<!-- 控件 -->
<view class="flex-c-sb pd20 mask_row" id="SetEditScanBtnMask_control">
<view class="btn closeBtn" @click="handleCloseScanBtnPosition">
</view>
<view class="btn primaryBtn" @click="()=> initScanBtnPosition(true)">
</view>
<view class="btn" @click="handleConfrimScanBtnPosition">
</view>
</view>
</scroll-view>
</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,
nextTick,
defineProps,
onMounted,
getCurrentInstance
} from "vue";
import file from '@/utils/log.js';
import uaePdaStore from '@/store/uaePdaStore.js';
import { storeToRefs } from 'pinia';
const pdaStore = uaePdaStore()
const { HANDLE_REFRESH } = pdaStore
const { isShowScanBtn } = storeToRefs(pdaStore)
const { writeLog, selectLogUrl, delLogFile } = file
const { root, writeTxt, mkdirs, filelist, readTxt } = handleFile
let details = reactive({
activity: null,
receiver: null,
intentFilter: null,
isstart: false,
/** 间隔时间 -- 手指按下和抬起的间隔时间 */
timer: 0,
/** 是否显示设置蒙层 */
isShowEditMask: false,
btnPosition: {
x: '100%',
y: '100%',
// 边界
leftMax: 0,
rightMax: 0,
topMax: 0,
bottomMax: 0
}
})
const instance = getCurrentInstance()
/** 扫码日志 */
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'
})
if (/http/.test(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
}
/** 获取边界 */
/** 初始化扫描位置 */
const initScanBtnPosition = async (isRefresh = false) => {
try {
await nextTick()
uni.createSelectorQuery().in(instance).select('#SetEditScanBtnMask').boundingClientRect(function (rect) {
console.log('rect :>> ', rect);
details.btnPosition.rightMax = rect.width
uni.createSelectorQuery().in(instance).select('#SetEditScanBtnMask_control').boundingClientRect(function (res) {
console.log('res :>> ', res);
details.btnPosition.topMax = Math.ceil(res.height)
}).exec();
uni.createSelectorQuery().in(instance).select('#scanBtn').boundingClientRect(function (res) {
console.log('res :>> ', res);
const x = uni.getStorageSync('ScanX')
const y = uni.getStorageSync('ScanY')
details.btnPosition.bottomMax = rect.height - Math.ceil(res.height)
details.btnPosition.rightMax = rect.width - Math.ceil(res.width)
details.btnPosition.x = x && !isRefresh ? x : (details.btnPosition.rightMax) / 2
details.btnPosition.y = y && !isRefresh ? y : details.btnPosition.bottomMax - 20
uni.setStorageSync(
'ScanX',
details.btnPosition.x
)
uni.setStorageSync(
'ScanY',
details.btnPosition.y
)
}).exec();
}).exec();
} catch (err) {
console.log('err :>> ', err);
//TODO handle the exception
}
}
onMounted(() => {
initScanBtnPosition()
})
// 点击扫描
const handleScan = () => {
HANDLE_REFRESH(false)
uni.scanCode({
success(res) {
const content = res.result
console.log('content :>> ', content);
if (!content) return uni.showToast({
title: '码值有误',
icon: 'none'
})
if (/ http /.test(content)) return uni.showToast({
title: '码值有误',
icon: 'none'
})
uni.$emit('scancodedate', content.replace(/\n/g, ""))
}
})
}
let timer = null
/** 手指按下 */
const handleTouchStart = (e) => {
console.log('start')
details.timer = new Date().getTime()
// 是否开启设置蒙层
timer = setTimeout(() => {
details.isShowEditMask = true
console.log('开启设置蒙层')
clearTimeout(timer)
timer = null
}, 1000)
}
let totolTimer = null
/** 手指移动 */
const handleTouchMove = (e) => {
e.preventDefault()
timer && clearTimeout(timer)
if (!details.isShowEditMask) return
if (totolTimer) return
totolTimer = setTimeout(() => {
// console.log('e :>> ', e);
const { leftMax, rightMax, topMax, bottomMax } = details.btnPosition
const { pageY, pageX } = e.touches[0]
details.btnPosition.x =
pageX < leftMax
? leftMax
: pageX > rightMax
? rightMax
: pageX
details.btnPosition.y =
pageY < topMax
? topMax
: pageY > bottomMax
? bottomMax
: pageY
clearTimeout(totolTimer)
totolTimer = null
}, 50)
}
/** 手指抬起 */
const handleTouchEnd = (e) => {
console.log('end')
const _newTime = new Date().getTime()
timer && clearTimeout(timer)
if (!timer || _newTime - details.timer > 300) return
handleScan()
}
/** 取消保存 */
const handleCloseScanBtnPosition = () => {
details.btnPosition.x = uni.getStorageSync('ScanX')
details.btnPosition.y = uni.getStorageSync('ScanY')
details.isShowEditMask = false
}
/** 保存按钮位置 */
const handleConfrimScanBtnPosition = () => {
uni.setStorageSync(
'ScanX',
details.btnPosition.x
)
uni.setStorageSync(
'ScanY',
details.btnPosition.y
)
details.isShowEditMask = false
}
</script>
<style lang="scss" scoped>
@import url(@/utils/style/common.scss);
// 扫描按钮
.scan_continer {
position: fixed;
padding: 20upx;
border: 14upx solid var(--subjectColor);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
z-index: 100000;
transition: all 0.03s;
&.active {
border-color: #fff;
}
}
// 设置蒙层
.mask {
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.3);
position: fixed;
top: 0;
left: -200%;
transition: all 0.5s;
z-index: 99999;
&.active {
left: 0;
}
.mask_row {
padding-top: 40px;
background: #fff;
/* #ifdef MP-WEIXIN */
padding-top: 60px;
/* #endif */
}
.btn {
font-size: 0.8rem;
line-height: 0.8rem;
padding: 20upx 40upx;
border-radius: 10upx;
background-color: var(--subjectColor);
color: #fff;
font-family: 宋体;
font-weight: bold;
&.closeBtn {
background-color: #efefef;
color: #666;
}
&.primaryBtn {
background-color: var(--primaryColor);
}
}
}
</style>