feat(scan-diag): flush 记录本次扫码按键数与最后按键;打包页 plus 链路对 1~3 字符做最小长度拦截

This commit is contained in:
liufumingren 2026-09-25 01:46:19 +08:00
parent ffd42fca33
commit 1f11cc05d2
2 changed files with 52 additions and 5 deletions

View File

@ -60,6 +60,10 @@ let buffer = ''
let startedAt = 0
let lastAt = 0
let timer = null
// 本次扫码已处理的按键数 + 最近一个按键(诊断用:区分"只收到 1 个字符"与"按键中途停止投递")
let burstKeyCount = 0
let lastPlusKeyCode = 0
let lastDomKey = ''
let domListenerAttached = false
let plusAttached = false
@ -98,6 +102,7 @@ function resetBuffer() {
buffer = ''
startedAt = 0
lastAt = 0
burstKeyCount = 0
}
/** 当前已缓冲字符的尾部:丢弃时用它回答"扫的码已经收到多少了" */
@ -111,8 +116,11 @@ function bufferTail(max = 40) {
function flush(trigger, source) {
clearTimer()
const code = buffer.trim()
const keyCount = burstKeyCount
const lastKey = source === 'plus' ? lastPlusKeyCode : lastDomKey
const durationMs = startedAt ? Math.max(0, (lastAt || Date.now()) - startedAt) : 0
buffer = ''
burstKeyCount = 0
startedAt = 0
lastAt = 0
@ -122,13 +130,22 @@ function flush(trigger, source) {
// 少于最小长度的码直接丢弃(可能是键盘误按/短码)
if (code.length < minLength) {
diagDrop('too-short', { scope: receiverScope(), code, length: code.length, minLength, trigger, source })
diagDrop('too-short', {
scope: receiverScope(),
code,
length: code.length,
keys: keyCount,
lastKey,
minLength,
trigger,
source,
})
debug('code-dropped', { reason: 'too-short', code, length: code.length, minLength, trigger, source })
return
}
// 记录完整条码:事后要能回答"这条到底扫到了什么"
diagFlush({ scope: receiverScope(), trigger, source, code, length: code.length, durationMs })
// 记录完整条码与按键数:事后要能回答"这条到底扫到了什么"、"按键是否中途停了"
diagFlush({ scope: receiverScope(), trigger, source, code, length: code.length, durationMs, keys: keyCount, lastKey })
debug('flush-buffer', { code, length: code.length, durationMs, trigger, source })
if (receiver && typeof receiver.onScanCode === 'function') {
@ -166,6 +183,14 @@ function markKeyArrival(source, extra) {
const gap = lastAnyKeyAt ? now - lastAnyKeyAt : 0
lastAnyKeyAt = now
rearmedForIdle = false
if (extra) {
if (extra.keyCode) {
lastPlusKeyCode = extra.keyCode
}
if (extra.key) {
lastDomKey = String(extra.key)
}
}
diagKey(source, extra)
return gap
}
@ -190,6 +215,10 @@ function onDomKeydown(event) {
}
debug('key-captured', { key, paused })
if (isPrintable) {
if (!buffer) {
burstKeyCount = 0
}
burstKeyCount += 1
appendChar(key)
return
}
@ -365,7 +394,9 @@ function handlePlusKeyEvent(event, type) {
const now = Date.now()
if (!buffer) {
startedAt = now
burstKeyCount = 0
}
burstKeyCount += 1
lastAt = now
buffer += text
clearTimer()

View File

@ -350,6 +350,9 @@ import {
const SCAN_IDLE_TIMEOUT = 300
// 自动提交所需的最短内容长度
const SCAN_MIN_LENGTH = 2
// 真机 plus 链路的最小码长:运单/商品码都是 8~13 位、工牌 64 位,
// 1~3 字符必然是误触/输入法/枪配置异常,不得当运单提交
const SCAN_CODE_MIN_LENGTH = 4
// 手动输入识别:相邻按键间隔超过该毫秒数视为手动输入,不自动提交
const SCAN_BURST_MAX_GAP = 100
// Android KeyEvent 符号键映射(KEYCODE_* → 字符)。
@ -420,6 +423,8 @@ export default {
plusKeyTimer: null,
plusKeyStartedAt: 0,
plusKeyLastAt: 0,
// 本次扫码已处理的按键数(诊断用:区分"只收到 1 个字符"与"整个码都收到了")
plusKeyBurstCount: 0,
// 字母大小写策略:'auto' 跟踪 Shift 键 / 'upper' 全大写 / 'lower' 全小写
// 注意:plus.key 只给 keyCode、无 Shift 状态;仅当扫码枪会额外上报
// Android Shift 键(KEYCODE_SHIFT_LEFT=59 / SHIFT_RIGHT=60)时 'auto' 才能还原大小写。
@ -2358,7 +2363,10 @@ export default {
const now = Date.now()
if (!this.plusKeyBuffer) {
this.plusKeyStartedAt = now
// 新的一次扫码:按键数重新计数
this.plusKeyBurstCount = 0
}
this.plusKeyBurstCount += 1
this.plusKeyLastAt = now
this.plusKeyBuffer += text
this.clearPlusKeyTimer()
@ -2395,18 +2403,26 @@ export default {
return
}
const code = (this.plusKeyBuffer || '').trim()
const keyCount = this.plusKeyBurstCount
const durationMs = this.plusKeyStartedAt
? Math.max(0, (this.plusKeyLastAt || Date.now()) - this.plusKeyStartedAt)
: 0
this.plusKeyBuffer = ''
this.plusKeyBurstCount = 0
this.plusKeyStartedAt = 0
this.plusKeyLastAt = 0
this.clearPlusKeyTimer()
if (!code) {
return
}
diagFlush({ trigger, source: 'plus', code, length: code.length, durationMs })
console.log('[packaging][scan] plus-buffer trigger =', trigger, 'code =', code)
// 过短输入(误触/输入法/枪异常)不当运单:记录收到的内容与按键数,便于事后区分
// "本来就只有 1 个字符"与"扫了完整码但按键中途停止投递"
if (code.length < SCAN_CODE_MIN_LENGTH) {
diagDrop('too-short', { source: 'plus', code, length: code.length, keys: keyCount, trigger })
return
}
diagFlush({ trigger, source: 'plus', code, length: code.length, durationMs, keys: keyCount })
console.log('[packaging][scan] plus-buffer trigger =', trigger, 'code =', code, 'keys =', keyCount)
this.submitScanCode(code, 'plus')
},
handleHiddenScanFocus() {