From 7b3e6f1f797da7abe5b2b8ff3f388ee5b3e9d613 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 25 Sep 2026 01:36:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(scan-diag):=20=E8=AE=B0=E5=BD=95=E6=89=AB?= =?UTF-8?q?=E7=A0=81=E8=B5=B7=E5=A7=8B=E6=8C=89=E9=94=AE=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?keyCode/type=E3=80=81=E4=B8=BB=E7=BA=BF=E7=A8=8B=E5=8D=A1?= =?UTF-8?q?=E9=A1=BF(stall)=E6=A3=80=E6=B5=8B=E3=80=81=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=B8=A6=E9=A1=B5=E9=9D=A2scope?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/scan-diag.js | 65 +++++++++++++++++++++++++++------ src/core/scan.js | 68 +++++++++++++++++++++++------------ src/pages/login/login.vue | 45 +++++++++++++++++++++++ src/pages/packaging/index.vue | 17 ++++----- src/pages/returns/index.vue | 1 + 5 files changed, 154 insertions(+), 42 deletions(-) diff --git a/src/core/scan-diag.js b/src/core/scan-diag.js index 253b4bf..dbf2fdc 100644 --- a/src/core/scan-diag.js +++ b/src/core/scan-diag.js @@ -12,9 +12,10 @@ * 约定:本模块只做记录,不做任何业务决策,任何异常都不得向上抛。 */ +import { BASE_URL } from '../../env.js' + const STORAGE_KEY = 'scanDiagLog:v1' -const LOGCAT_TAG = 'WMSScan' -// 内存中保留的事件条数 +const LOGCAT_TAG = 'WMSScan'// 内存中保留的事件条数 const MAX_EVENTS = 300 // 持久化到 storage 的事件条数(控制体积,约 10KB) const MAX_PERSIST_EVENTS = 160 @@ -29,6 +30,10 @@ const CONTENT_LOG_MIN_INTERVAL_MS = 1000 const MAX_VALUE_LEN = 220 // 超过该空闲时长后恢复的第一下按键单独记一条 key-resume const KEY_RESUME_GAP_MS = 60 * 1000 +// 大于该间隔视为"新的一次扫码",记录起始按键的原始 keyCode/type +const KEY_BURST_GAP_MS = 500 +// 心跳定时器迟到判定:实际间隔 > 设定周期 * 该倍数 → 主线程被卡住 +const STALL_FACTOR = 2.5 const state = { scope: 'unknown', @@ -45,6 +50,9 @@ const state = { healthDrops: 0, healthHeals: 0, dropLoggedAt: {}, + lastTickAt: 0, + maxTickGapMs: 0, + stallCount: 0, loaded: false, persistedAt: 0, persistTimer: null, @@ -97,6 +105,15 @@ function appVersion() { } } +/** 当前构建指向的接口主机:现场确认"这台机器连的是哪个后端" */ +function apiHost() { + try { + return String(BASE_URL || '').replace(/^https?:\/\//, '').replace(/\/.*$/, '') || 'unknown' + } catch (e) { + return 'unknown' + } +} + function fmtDuration(ms) { const totalSec = Math.max(0, Math.floor(ms / 1000)) if (totalSec < 60) { @@ -231,7 +248,7 @@ function push(kind, message, data) { if (state.events.length > MAX_EVENTS) { state.events.splice(0, state.events.length - MAX_EVENTS) } - // 按键逐条记录会刷屏,只计数(key-resume 会单独 push) + // 按键逐条记录会刷屏,只计数(key-resume / key-start 会单独 push) if (kind !== 'key') { logcat(`${fmtStamp(entry.t)} [${kind}] ${entry.msg}`) schedulePersist() @@ -266,7 +283,7 @@ export function diagSetGates(gates) { * 记录一次按键到达(在任何门控判断之前调用)。 * 用于区分「按键根本没到 JS」和「到了 JS 但被门控丢掉」。 */ -export function diagKey(source) { +export function diagKey(source, extra) { const now = Date.now() const gap = state.lastKeyAt ? now - state.lastKeyAt : 0 state.lastKeyAt = now @@ -276,6 +293,9 @@ export function diagKey(source) { if (gap > KEY_RESUME_GAP_MS) { // 长时间无按键后的第一下:定位"卡住→恢复"的时间点 push('key', `key-resume source=${key} 空闲=${fmtDuration(gap)}`) + } else if (gap > KEY_BURST_GAP_MS) { + // 一次扫码的起始按键:记下原始 keyCode/type——用于识别输入法注入/合成事件/异常按键流 + push('key-start', `source=${key}`, extra) } } @@ -334,23 +354,44 @@ export function diagSetContext(patch) { } } -/** 健康心跳:默认 60s 一条,含区间按键数/丢弃增量/门控快照 */ -export function diagHeartbeat(force) { +/** + * 健康心跳 + * - 每次调用(包括被节流跳过的)都会检查"定时器迟到":页面看门狗/自愈心跳是固定周期调的, + * 实际间隔远超周期 → JS 主线程被长任务/GC/其它进程卡住,这是"全局卡住"的直接证据; + * - 每 intervalMs 输出一条 health,含区间按键/丢弃增量与门控快照。 + * @param options { force?: boolean, intervalMs?: number } + */ +export function diagHeartbeat(options) { + const opts = options && typeof options === 'object' ? options : { force: !!options } const now = Date.now() - if (!force && now - state.lastHealthAt < HEALTH_INTERVAL_MS) { + const intervalMs = Number(opts.intervalMs) > 0 ? Number(opts.intervalMs) : HEALTH_INTERVAL_MS + const tickGap = state.lastTickAt ? now - state.lastTickAt : 0 + state.lastTickAt = now + if (tickGap && tickGap > intervalMs * STALL_FACTOR) { + state.stallCount += 1 + state.maxTickGapMs = Math.max(state.maxTickGapMs, tickGap) + push('stall', `主线程疑似卡顿 ${tickGap}ms(定时器周期 ${intervalMs}ms)`, { + gapMs: tickGap, + intervalMs, + keys: state.counters.keys, + gates: state.gates, + }) + } + if (!opts.force && now - state.lastHealthAt < HEALTH_INTERVAL_MS) { return } const keys = state.counters.keys - state.healthKeys const drops = totalDrops() - state.healthDrops const heals = state.counters.heals - state.healthHeals - const idleAgo = state.lastKeyAt ? fmtDuration(now - state.lastKeyAt) : 'never' + const idleAgo = state.lastKeyAt ? `${fmtDuration(now - state.lastKeyAt)}前` : 'never' state.lastHealthAt = now state.healthKeys = state.counters.keys state.healthDrops = totalDrops() state.healthHeals = state.counters.heals push( 'health', - `keys=${keys} drops=${drops} heals=${heals} flush累计=${state.counters.flushes} 最近按键=${idleAgo}前`, + `keys=${keys} drops=${drops} heals=${heals} flush累计=${state.counters.flushes} ` + + `最近按键=${idleAgo} tick=${tickGap || intervalMs}ms 最大tick=${state.maxTickGapMs}ms 卡顿=${state.stallCount}`, state.gates ) } @@ -379,6 +420,8 @@ export function diagSnapshot() { gates: Object.assign({}, state.gates), context: Object.assign({}, state.context), lastKeyAgoMs: state.lastKeyAt ? Date.now() - state.lastKeyAt : null, + maxTickGapMs: state.maxTickGapMs, + stallCount: state.stallCount, events: state.events.slice(), } } @@ -390,7 +433,7 @@ export function diagText() { const version = appVersion() lines.push( `【扫码诊断】${fmtFull(Date.now())} scope=${snapshot.scope}` + - `${version ? ` 版本=${version}` : ''} 已运行=${fmtDuration(snapshot.uptimeMs)}` + `${version ? ` 版本=${version}` : ''} 接口=${apiHost()} 已运行=${fmtDuration(snapshot.uptimeMs)}` ) const ctxKeys = Object.keys(snapshot.context) if (ctxKeys.length) { @@ -404,7 +447,7 @@ export function diagText() { : `(最近一次 ${fmtDuration(snapshot.lastKeyAgoMs)} 前)`) ) lines.push(`按键来源=${JSON.stringify(snapshot.counters.keysBySource)}`) - lines.push(`成功识别=${snapshot.counters.flushes} 自愈=${snapshot.counters.heals}`) + lines.push(`成功识别=${snapshot.counters.flushes} 自愈=${snapshot.counters.heals} 主线程卡顿=${snapshot.stallCount}(最大${snapshot.maxTickGapMs}ms)`) const dropKeys = Object.keys(snapshot.counters.drops) lines.push( `丢弃统计=${ diff --git a/src/core/scan.js b/src/core/scan.js index 187fb64..8097ab1 100644 --- a/src/core/scan.js +++ b/src/core/scan.js @@ -122,13 +122,13 @@ function flush(trigger, source) { // 少于最小长度的码直接丢弃(可能是键盘误按/短码) if (code.length < minLength) { - diagDrop('too-short', { code, length: code.length, minLength, trigger, source }) + diagDrop('too-short', { scope: receiverScope(), code, length: code.length, minLength, trigger, source }) debug('code-dropped', { reason: 'too-short', code, length: code.length, minLength, trigger, source }) return } // 记录完整条码:事后要能回答"这条到底扫到了什么" - diagFlush({ trigger, source, code, length: code.length, durationMs }) + diagFlush({ scope: receiverScope(), trigger, source, code, length: code.length, durationMs }) debug('flush-buffer', { code, length: code.length, durationMs, trigger, source }) if (receiver && typeof receiver.onScanCode === 'function') { @@ -151,31 +151,41 @@ function appendChar(ch) { timer = setTimeout(() => flush('timer', 'dom'), 380) } +/** 当前接收者所属页面(诊断日志用):页面注册时传 scope,未传则为 scan */ +function receiverScope() { + return (receiver && receiver.scope) || 'scan' +} + /** * 记录「按键到达 JS」这一事实(必须在任何门控判断之前调用)。 + * extra 用于扫码起始按键采样(keyCode/type/key),便于识别输入法注入与异常按键流。 * 返回距上一次按键的空闲时长,供事件类型锁自愈使用。 */ -function markKeyArrival(source) { +function markKeyArrival(source, extra) { const now = Date.now() const gap = lastAnyKeyAt ? now - lastAnyKeyAt : 0 lastAnyKeyAt = now rearmedForIdle = false - diagKey(source) + diagKey(source, extra) return gap } // ---------- DOM(H5/Electron) ---------- function onDomKeydown(event) { - markKeyArrival('dom') + markKeyArrival('dom', { key: (event && event.key) || '' }) if (!receiver || paused) { - diagDrop(receiver ? 'paused' : 'no-receiver', { source: 'dom', buffer: bufferTail() }) + diagDrop(receiver ? 'paused' : 'no-receiver', { + scope: receiverScope(), + source: 'dom', + buffer: bufferTail(), + }) return } const key = event?.key || '' const isPrintable = key.length === 1 const isEnter = key === 'Enter' if (!isPrintable && !isEnter) { - diagDrop('unmapped-dom-key', { key, buffer: bufferTail() }) + diagDrop('unmapped-dom-key', { scope: receiverScope(), key, buffer: bufferTail() }) return } debug('key-captured', { key, paused }) @@ -295,18 +305,22 @@ function isPlusEnterKey(keyCode, text) { } function handlePlusKeyEvent(event, type) { - const gap = markKeyArrival('plus') + const keyCode = Number((event && event.keyCode) || 0) + const gap = markKeyArrival('plus', { keyCode, type }) // 空闲后重新锁定实际生效的事件类型:自愈「锁错就永久静默」 if (plusKeyEventType && gap > IDLE_LOCK_RESET_MS) { const locked = plusKeyEventType plusKeyEventType = null - diagHeal('event-type-lock-idle-reset', { locked, gapMs: gap }) + diagHeal('event-type-lock-idle-reset', { scope: receiverScope(), locked, gapMs: gap }) } if (!receiver || paused) { - diagDrop(receiver ? 'paused' : 'no-receiver', { source: 'plus', buffer: bufferTail() }) + diagDrop(receiver ? 'paused' : 'no-receiver', { + scope: receiverScope(), + source: 'plus', + buffer: bufferTail(), + }) return } - const keyCode = Number((event && event.keyCode) || 0) // 跟踪 Shift 修饰键(Android KEYCODE_SHIFT_LEFT=59 / SHIFT_RIGHT=60) if (keyCode === 59 || keyCode === 60) { @@ -327,7 +341,7 @@ function handlePlusKeyEvent(event, type) { plusShiftPrev = plusShiftDown const isEnter = isPlusEnterKey(keyCode, text) if (!text && !isEnter) { - diagDrop('unmapped-keycode', { keyCode, type, buffer: bufferTail() }) + diagDrop('unmapped-keycode', { scope: receiverScope(), keyCode, type, buffer: bufferTail() }) return } @@ -338,6 +352,7 @@ function handlePlusKeyEvent(event, type) { if (plusKeyEventType !== type) { // 这一类丢弃最致命(整串码会丢),必须带上已收到的内容 diagDrop('event-type-locked', { + scope: receiverScope(), keyCode, type, locked: plusKeyEventType, @@ -377,7 +392,7 @@ function ensurePlusListener() { plus.key.addEventListener('keyup', onPlusKeyUp) plusAttached = true diagNote('plus-listener-attached') - diagSetGates({ plusAttached: true }) + diagSetGates({ scope: receiverScope(), plusAttached: true }) debug('plus-listener-attached') // #endif } @@ -415,8 +430,8 @@ export function registerScanReceiver(next) { if (receiver) { ensureDomListener() ensurePlusListener() - diagSetGates({ receiver: true, minLength }) - diagNote('receiver-registered') + diagSetGates({ scope: receiverScope(), receiver: true, minLength }) + diagNote('receiver-registered', { scope: receiverScope() }) debug('receiver-registered') } } @@ -426,13 +441,14 @@ export function unregisterScanReceiver(target) { if (target && receiver !== target) { return } + const scope = receiverScope() receiver = null resetBuffer() removeDomListener() removePlusListener() setScanPaused(false) - diagSetGates({ receiver: false, paused: false, buffer: 0 }) - diagNote('receiver-unregistered') + diagSetGates({ scope, receiver: false, paused: false, buffer: 0 }) + diagNote('receiver-unregistered', { scope }) } /** 暂停/恢复捕获(账号/密码等输入框聚焦时暂停,并清空已缓冲的按键) */ @@ -440,7 +456,7 @@ export function setScanPaused(value) { const next = !!value if (paused !== next) { // 暂停标志卡住是最常见的"扫码没反应"原因,必须留痕 - diagNote('setScanPaused', { value: next }) + diagNote('setScanPaused', { scope: receiverScope(), value: next }) } paused = next diagSetGates({ paused }) @@ -497,23 +513,29 @@ export function startScanSelfHeal(intervalMs) { selfHealStartedAt = Date.now() selfHealTimer = setInterval(() => { // 从未收到过按键时以心跳启动时刻为基准,否则"一直收不到"的情况永远不会触发重挂 - const idleMs = Date.now() - (lastAnyKeyAt || selfHealStartedAt) + const now = Date.now() + const idleForRearmMs = now - (lastAnyKeyAt || selfHealStartedAt) diagSetGates({ + scope: receiverScope(), paused, receiver: !!receiver, plusAttached, eventTypeLock: plusKeyEventType || 'none', - idleSec: Math.round(idleMs / 1000), + idleSec: lastAnyKeyAt ? Math.round((now - lastAnyKeyAt) / 1000) : 'never', }) - if (receiver && idleMs > LISTENER_REARM_IDLE_MS && !rearmedForIdle) { + if (receiver && idleForRearmMs > LISTENER_REARM_IDLE_MS && !rearmedForIdle) { rearmedForIdle = true removePlusListener() removeDomListener() ensurePlusListener() ensureDomListener() - diagHeal('listener-rearmed-idle', { idleMs, hint: '长时间无按键,强制重挂监听' }) + diagHeal('listener-rearmed-idle', { + scope: receiverScope(), + idleMs: idleForRearmMs, + hint: '长时间无按键,强制重挂监听', + }) } - diagHeartbeat() + diagHeartbeat({ intervalMs: period }) }, period) } diff --git a/src/pages/login/login.vue b/src/pages/login/login.vue index ccfa362..1210314 100644 --- a/src/pages/login/login.vue +++ b/src/pages/login/login.vue @@ -107,7 +107,15 @@ > {{ workbenchBindCode || '绑定码生成中...' }} + + + 绑定码 + {{ workbenchBindCode || '—' }} + {{ workbenchStatus }} + + 刷新绑定码 + @@ -548,6 +556,7 @@ export default { // 注册/注销通用全局扫码接收(捕获与最小长度过滤由 src/core/scan.js 统一处理) registerScanCapture() { registerScanReceiver({ + scope: 'login', onScanCode: (code, meta) => { if (!this.isPageActive()) { return @@ -2313,6 +2322,42 @@ export default { margin-bottom: 0; } +/* 绑定码文本(工作台面板):与二维码内容一致,供二维码扫不出时人工读取/输入 */ +.wb-code-box { + display: flex; + align-items: center; + justify-content: center; + margin-bottom: 0.205rem; +} + +.wb-code-label { + font-size: 0.563rem; + color: #666; + margin-right: 0.205rem; +} + +.wb-code-value { + font-size: 0.717rem; + font-weight: bold; + color: #2f7dff; + letter-spacing: 0.051rem; + font-family: Consolas, Menlo, monospace; + max-width: 5.6rem; + word-break: break-all; + text-align: center; + user-select: text; +} + +.wb-refresh-link { + display: flex; + align-items: center; + justify-content: center; + text-align: center; + padding: 0.205rem 0 0; + font-size: 0.563rem; + color: #2f7dff; +} + .scan-hidden-input { position: absolute; width: 0.02rem; diff --git a/src/pages/packaging/index.vue b/src/pages/packaging/index.vue index f816831..724dd6d 100644 --- a/src/pages/packaging/index.vue +++ b/src/pages/packaging/index.vue @@ -1889,7 +1889,7 @@ export default { * 回车且有内容时立即提交;无回车结束符则静默防抖自动提交。 */ handleGlobalKeydown(event) { - this.markScanKeyArrived('dom') + this.markScanKeyArrived('dom', { key: (event && event.key) || '' }) if (!this.isPageActive()) { diagDrop('page-inactive', { source: 'dom' }) return @@ -2039,15 +2039,16 @@ export default { this.ensureScanInputFocus() this.maybeRearmScanListenersWhenIdle() diagSetGates({ + scope: 'packaging', pageActive: this.pageActive !== false, plusListener: !!(this.plusKeyListener || this.plusKeyUpListener), domListener: !!this.globalKeyListener, eventTypeLock: this._plusKeyEventType || 'none', idleSec: this.lastScanKeyArrivedAt ? Math.round((Date.now() - this.lastScanKeyArrivedAt) / 1000) - : 0, + : 'never', }) - diagHeartbeat() + diagHeartbeat({ intervalMs: 2000 }) }, 2000) }, stopScanWatchdog() { @@ -2057,12 +2058,12 @@ export default { } }, /** 记录「按键到达 JS」(任何门控之前调用),返回距上次按键的空闲时长 */ - markScanKeyArrived(source) { + markScanKeyArrived(source, extra) { const now = Date.now() const gap = this.lastScanKeyArrivedAt ? now - this.lastScanKeyArrivedAt : 0 this.lastScanKeyArrivedAt = now this.scanRearmedForIdle = false - diagKey(source) + diagKey(source, extra) return gap }, /** @@ -2085,7 +2086,7 @@ export default { this.removeGlobalKeyboardListener() this.attachPlusKeyboardListener() this.attachGlobalKeyboardListener() - diagHeal('listener-rearmed-idle', { idleMs, hint: '长时间无按键,强制重挂监听' }) + diagHeal('listener-rearmed-idle', { scope: 'packaging', idleMs, hint: '长时间无按键,强制重挂监听' }) console.log('[packaging][scan] 已重挂扫码监听(空闲)', idleMs) }, /** 诊断面板隐藏入口:3 秒内连点标题 5 次(操作员不会误触) */ @@ -2302,7 +2303,8 @@ export default { // 统一处理 plus.key 按键(keydown / keyup) handlePlusKeyEvent(event, type) { // 先记录「按键到达 JS」这一事实(任何门控之前),用于区分「按键根本没到」与「到了被门控丢掉」 - const gap = this.markScanKeyArrived('plus') + const keyCode = Number((event && event.keyCode) || 0) + const gap = this.markScanKeyArrived('plus', { keyCode, type }) // 空闲后重新锁定实际生效的事件类型:自愈「锁错就永久静默」 if (this._plusKeyEventType && gap > SCAN_IDLE_LOCK_RESET_MS) { const locked = this._plusKeyEventType @@ -2313,7 +2315,6 @@ export default { diagDrop('page-inactive', { source: 'plus' }) return } - const keyCode = Number((event && event.keyCode) || 0) // 跟踪 Shift 修饰键(Android KEYCODE_SHIFT_LEFT=59 / SHIFT_RIGHT=60) if (keyCode === 59 || keyCode === 60) { diff --git a/src/pages/returns/index.vue b/src/pages/returns/index.vue index 88d6bfb..2f2653e 100644 --- a/src/pages/returns/index.vue +++ b/src/pages/returns/index.vue @@ -890,6 +890,7 @@ export default { registerScanCapture() { setScanMinLength(SCAN_MIN_LENGTH) registerScanReceiver({ + scope: 'returns', onScanCode: (code, meta) => { if (!this.isPageActive()) { diagDrop('page-inactive-receiver', { source: 'receiver' })