From 9330c71e757a5c877c6c12ca0557ecce374fc1ec Mon Sep 17 00:00:00 2001 From: zm2000 <18386612143@163.com> Date: Fri, 18 Sep 2026 18:48:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E5=8C=85=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E3=80=81=E7=99=BB=E5=BD=95=E3=80=81=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env.js | 3 +- src/App.vue | 70 ++- src/api/request.ts | 25 +- src/components/top-nav.vue | 78 +-- src/core/scanKey.js | 2 + src/pages/index/index.vue | 43 +- src/pages/login/login.vue | 715 ++++++++++++++++++--------- src/pages/order/index.vue | 599 +++++++++++----------- src/pages/packaging/index.vue | 903 +++++++++++++++++++++++----------- src/pages/returns/index.vue | 37 +- src/pages/weighing/index.vue | 37 +- 11 files changed, 1647 insertions(+), 865 deletions(-) create mode 100644 src/core/scanKey.js diff --git a/env.js b/env.js index 92e1006..c33f58a 100644 --- a/env.js +++ b/env.js @@ -1,3 +1,4 @@ -export const BASE_URL = 'http://192.168.11.56:6060' //后台根域名 https://demo.shopro.top +// export const BASE_URL = 'http://qimen.wms.gztbyc.com' //生产环境接口地址 +export const BASE_URL = 'http://192.168.11.56:6060' //开发环境接口地址 export const API_URL = `${BASE_URL}/system` //后台接口域名 export const IMG_URL = 'https://ye4s.file.gzxunren.com' //全局网络图片地址变量,css背景图片地址变量在uni.scss diff --git a/src/App.vue b/src/App.vue index 44b2f27..d30fe64 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,10 +1,74 @@ + + diff --git a/src/pages/login/login.vue b/src/pages/login/login.vue index e898996..01c25ff 100644 --- a/src/pages/login/login.vue +++ b/src/pages/login/login.vue @@ -9,7 +9,7 @@ 登录 - 支持扫描工牌登录 + 支持扫描工牌登录 欢迎回来,请登录您的账户 @@ -70,10 +70,7 @@ × - - - - + @@ -94,23 +91,18 @@ - 工作台尚未绑定,请在PDA使用绑定工作台界面扫码绑定 - + 工作台尚未绑定,请在PDA使用绑定工作台界面扫码绑定 + - {{ workbenchBindCode }} - 绑定码生成中... + {{ workbenchBindCode || '绑定码生成中...' }} - 绑定码:{{ workbenchBindCode }} - {{ workbenchStatus }} - + {{ workbenchStatus }} @@ -138,12 +130,35 @@ import { getLoginWechatCode, loginByCode, loginByAccount, getWorkbenchBindCode } const qrBaseUrl = 'https://ye4s.wms.gztbyc.com/grcode/build?text=' const qrFallbackBaseUrl = 'https://api.qrserver.com/v1/create-qr-code/?size=280x280&data=' const loginLinkTemplate = 'https://ye4s.wms.gztbyc.com/mini/?spm=0.4.${code}.1.4' +// Android KeyEvent 符号键映射(KEYCODE_* → 字符)。 +// 这些 keyCode 与 ASCII 字母/数字区间重叠,必须优先于 ASCII 分支判断, +// 否则 KEYCODE_EQUALS=70 会被 ASCII 的 [65..90] 分支误读为 'F'。 +const ANDROID_PUNCT_MAP = { + 55: ',', // KEYCODE_COMMA + 56: '.', // KEYCODE_PERIOD + 62: ' ', // KEYCODE_SPACE + 68: '`', // KEYCODE_GRAVE + 69: '-', // KEYCODE_MINUS + 70: '=', // KEYCODE_EQUALS + 71: '[', // KEYCODE_LEFT_BRACKET + 72: ']', // KEYCODE_RIGHT_BRACKET + 73: '\\', // KEYCODE_BACKSLASH + 74: ';', // KEYCODE_SEMICOLON + 75: "'", // KEYCODE_APOSTROPHE + 76: '/', // KEYCODE_SLASH + 77: '@', // KEYCODE_AT + 81: '+', // KEYCODE_PLUS +} export default { data() { return { loginMode: 'account', showPassword: false, + eyeOpenSvg: + '', + eyeOffSvg: + '', focusedField: '', scanLoading: false, scanBadgeSubmitting: false, @@ -157,10 +172,13 @@ export default { scanGlobalKeyStartedAt: 0, scanGlobalKeyLastAt: 0, scanPlusKeyListener: null, + scanPlusKeyUpListener: null, scanPlusKeyBuffer: '', scanPlusKeyTimer: null, scanPlusKeyStartedAt: 0, scanPlusKeyLastAt: 0, + plusLetterCase: 'auto', + plusLetterCaseLag: 1, enableHiddenInputCapture: false, scanInputFocus: false, scanHiddenValue: '', @@ -177,6 +195,7 @@ export default { scanPollTimer: null, scanPollInterval: 1500, scanRequestTimeout: 120000, + scanRequestAbort: null, form: { username: '', password: '', @@ -191,6 +210,7 @@ export default { workbenchPollTimer: null, workbenchPollInterval: 1500, workbenchRequestTimeout: 120000, + workbenchRequestAbort: null, autoEnterPackTimer: null, } }, @@ -198,6 +218,10 @@ export default { this.pageActive = false this.stopScanLongPolling() this.stopWorkbenchLongPolling() + this.abortScanRequest() + this.abortWorkbenchRequest() + this.scanLoading = false + this.workbenchLoading = false this.removePlusKeyboardListener() this.removeGlobalKeyboardListener() this.clearScanFocusResumeTimer() @@ -227,7 +251,12 @@ export default { }, onHide() { this.pageActive = false + this.stopScanLongPolling() this.stopWorkbenchLongPolling() + this.abortScanRequest() + this.abortWorkbenchRequest() + this.scanLoading = false + this.workbenchLoading = false this.removePlusKeyboardListener() this.removeGlobalKeyboardListener() this.clearScanFocusResumeTimer() @@ -239,6 +268,27 @@ export default { this.scanHiddenFocused = false this.scanInputFocus = false }, + beforeUnmount() { + this.pageActive = false + this.stopScanLongPolling() + this.stopWorkbenchLongPolling() + this.abortScanRequest() + this.abortWorkbenchRequest() + this.scanLoading = false + this.workbenchLoading = false + this.removePlusKeyboardListener() + this.removeGlobalKeyboardListener() + this.clearScanFocusResumeTimer() + this.clearKeyboardSuppressTimers() + this.clearHiddenRefocusTimer() + this.clearHiddenScanTimer() + this.clearPlusKeyTimer() + this.clearGlobalKeyTimer() + this.clearAutoEnterPackTimer() + this.scanBadgeSubmitting = false + this.scanHiddenFocused = false + this.scanInputFocus = false + }, methods: { isPageActive() { return this.pageActive !== false @@ -248,21 +298,42 @@ export default { return } this.loginMode = mode - if (mode !== 'scan') { - this.stopScanLongPolling() - this.scanBadgeSubmitting = false - } - if (mode === 'scan' && !this.scanCode && !this.scanQrImage) { - this.handleScanLogin() - } else if (mode === 'scan' && this.scanClientCode) { - this.startScanLongPolling() - } - if (mode !== 'workbench') { + if (mode === 'account') { + // 回到账号登录:关闭所有二维码轮询与扫码缓冲定时器、中断挂起请求 + this.stopScanLongPolling() this.stopWorkbenchLongPolling() - } - if (mode === 'workbench') { - this.resumeWorkbenchFlow() + this.abortScanRequest() + this.abortWorkbenchRequest() + this.scanLoading = false + this.workbenchLoading = false + this.scanBadgeSubmitting = false + this.clearGlobalKeyTimer() + this.clearPlusKeyTimer() + this.clearHiddenScanTimer() + this.scanGlobalKeyBuffer = '' + this.scanPlusKeyBuffer = '' + this.scanHiddenValue = '' + } else if (mode === 'scan') { + this.stopWorkbenchLongPolling() + this.abortWorkbenchRequest() + this.workbenchLoading = false + this.scanBadgeSubmitting = false + // 切到扫码登录:清空旧码,直接强制加载一次二维码 + this.scanClientCode = '' + this.scanCode = '' + this.scanQrImage = '' + this.scanMiniProgramUrl = '' + this.handleScanLogin(false) + } else if (mode === 'workbench') { + this.stopScanLongPolling() + this.abortScanRequest() + this.scanLoading = false + this.scanBadgeSubmitting = false + // 切到工作台:直接强制加载一次绑定码 + if (!this.hasStoredWorkbench && !this.workbenchLoggedIn) { + this.handleWorkbenchRefresh() + } } this.suppressSoftKeyboardBurst() @@ -318,21 +389,51 @@ export default { logScanDebug(...args) { console.log('[scan-login]', ...args) }, + // 窗口级软键盘模式设为 ALWAYS_HIDDEN:input 聚焦也不会自动弹软键盘。 + // 真机上不要用 uni.hideKeyboard()——它会断开输入框的按键连接, + // 导致“扫一次后就不再接收输入”;用 Android 窗口软键盘模式彻底压制。 + setWindowSoftInputHidden() { + // #ifdef APP-PLUS + try { + if (typeof plus !== 'undefined' && plus.android && plus.android.runtimeMainActivity) { + const main = plus.android.runtimeMainActivity() + const win = plus.android.invoke(main, 'getWindow') + // SOFT_INPUT_STATE_ALWAYS_HIDDEN = 3(数值直传,避免静态字段访问失败导致设置无效) + plus.android.invoke(win, 'setSoftInputMode', 3) + console.log('[scan-login][kb] setSoftInputMode=ALWAYS_HIDDEN(3)') + } else { + console.log('[scan-login][kb] plus.android unavailable') + } + } catch (e) { + console.log('[scan-login][kb] setSoftInputMode failed:', e && e.message) + } + // #endif + // 隐藏当前已显示的软键盘;此后窗口级 ALWAYS_HIDDEN 阻止再弹,不会形成闪烁循环 + if (typeof uni !== 'undefined' && typeof uni.hideKeyboard === 'function') { + uni.hideKeyboard() + } + }, suppressSoftKeyboardBurst() { if (!this.isPageActive()) { return } this.clearKeyboardSuppressTimers() + // 真机:窗口级 ALWAYS_HIDDEN,input 聚焦也不会弹软键盘(不打断扫码输入) + if (this.isAppPlus()) { + this.setWindowSoftInputHidden() + // 软键盘可能晚于页面显示弹出,多时间点重设窗口模式 + 收键盘覆盖 + ;[80, 180, 320].forEach((delay) => { + const timer = setTimeout(() => { + if (!this.isPageActive()) { + return + } + this.setWindowSoftInputHidden() + }, delay) + this.scanKeyboardSuppressTimers.push(timer) + }) + return + } uni.hideKeyboard?.() - ;[80, 180, 320].forEach((delay) => { - const timer = setTimeout(() => { - if (!this.isPageActive()) { - return - } - uni.hideKeyboard?.() - }, delay) - this.scanKeyboardSuppressTimers.push(timer) - }) }, scheduleScanInputFocus(delay = 0) { if (!this.isPageActive()) { @@ -346,6 +447,14 @@ export default { this.ensureScanInputFocus() }, delay) }, + isAppPlus() { + // #ifdef APP-PLUS + return true + // #endif + // #ifndef APP-PLUS + return false + // #endif + }, hasPlusKeyboardSupport() { // #ifdef APP-PLUS return typeof plus !== 'undefined' && !!plus?.key?.addEventListener @@ -358,6 +467,14 @@ export default { return typeof window !== 'undefined' || typeof document !== 'undefined' }, setupScanCaptureMode() { + // 真机(APP-PLUS)逻辑层无 DOM:window/document 均为 undefined, + // hasDomKeyboardSupport() 返回 false 会误开启隐藏 input, + // 而聚焦隐藏 input 会反复弹出软键盘(无操作弹键盘根因)。 + // plus.key 已按 Android 原生 keycode 完整捕获扫码,真机彻底关闭隐藏 input。 + if (this.isAppPlus()) { + this.enableHiddenInputCapture = false + return + } this.enableHiddenInputCapture = !this.hasDomKeyboardSupport() this.logScanDebug('scan-capture-mode', { plus: this.hasPlusKeyboardSupport(), @@ -366,13 +483,36 @@ export default { }) }, parsePlusKeyCodeToChar(keyCode) { + // Android Enter=66 在 ASCII 里是 'B',必须先排除,交给 isPlusEnterKey 处理 + if (keyCode === 66) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return '' + } + // ---- Android 原生 keycode ---- + if (keyCode >= 7 && keyCode <= 16) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return String(keyCode - 7) // 0-9 + } + if (keyCode >= 29 && keyCode <= 54) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return String.fromCharCode(keyCode - 29 + 65) // A-Z + } + // Android 符号键:必须放在 ASCII 分支之前(防止 70('=') → 'F' 这类误读) + if (this._plusKeyStyle !== 'ascii' && ANDROID_PUNCT_MAP[keyCode] !== undefined) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return ANDROID_PUNCT_MAP[keyCode] + } + // ---- ASCII/DOM 风格(其它设备兑底)---- if (keyCode >= 48 && keyCode <= 57) { + this._plusKeyStyle = this._plusKeyStyle || 'ascii' return String.fromCharCode(keyCode) } if (keyCode >= 65 && keyCode <= 90) { + this._plusKeyStyle = this._plusKeyStyle || 'ascii' return String.fromCharCode(keyCode) } if (keyCode >= 96 && keyCode <= 105) { + this._plusKeyStyle = this._plusKeyStyle || 'ascii' return String(keyCode - 96) } if (keyCode === 189 || keyCode === 109) { @@ -386,6 +526,33 @@ export default { } return '' }, + // 应用字母大小写策略(仅对 A-Z 生效) + applyLetterCase(ch, shifted) { + if (!ch || ch.length !== 1 || ch < 'A' || ch > 'Z') { + return ch + } + const mode = this.plusLetterCase || 'upper' + if (mode === 'lower') { + return ch.toLowerCase() + } + if (mode === 'auto') { + return shifted ? ch : ch.toLowerCase() + } + return ch // 'upper' + }, + // 判断是否为 Enter:Android 原生 Enter=66;ASCII 风格为 13 + isPlusEnterKey(keyCode, text) { + if (text) { + return false + } + if (keyCode === 66) { + return true + } + if (keyCode === 13) { + return this._plusKeyStyle !== 'android' + } + return false + }, flushPlusKeyBuffer(trigger = 'timer') { if (!this.isPageActive()) { return @@ -417,64 +584,112 @@ export default { if (!this.hasPlusKeyboardSupport()) { return } - if (this.scanPlusKeyListener) { + if (this.scanPlusKeyListener || this.scanPlusKeyUpListener) { + return + } + this.scanPlusKeyListener = (event) => this.handlePlusKeyEvent(event, 'keydown') + this.scanPlusKeyUpListener = (event) => this.handlePlusKeyEvent(event, 'keyup') + // #ifdef APP-PLUS + // 同时监听 keydown 与 keyup:本机实测只触发 keyup(keydown 不触发), + // 由 handlePlusKeyEvent 里的类型锁定避免两者都触发时重复采集 + plus.key.addEventListener('keydown', this.scanPlusKeyListener) + plus.key.addEventListener('keyup', this.scanPlusKeyUpListener) + // #endif + this.logScanDebug('attach-plus-listener-done') + }, + // 统一处理 plus.key 按键(keydown / keyup) + handlePlusKeyEvent(event, type) { + if (!this.isPageActive()) { + return + } + if (this.loginMode !== 'account' && this.loginMode !== 'scan') { + return + } + const keyCode = Number((event && event.keyCode) || 0) + + // 跟踪 Shift 修饰键(Android KEYCODE_SHIFT_LEFT=59 / SHIFT_RIGHT=60) + if (keyCode === 59 || keyCode === 60) { + if (type === 'keydown') { + this._plusShiftDown = true + } else if (type === 'keyup') { + this._plusShiftDown = false + } return } - this.scanPlusKeyListener = (event) => { - if (!this.isPageActive()) { - return - } - if (this.loginMode !== 'account' && this.loginMode !== 'scan') { - return - } - - const keyCode = Number(event?.keyCode || 0) - const isEnter = keyCode === 13 || keyCode === 66 - const text = this.parsePlusKeyCodeToChar(keyCode) - - if (text) { - const now = Date.now() - if (!this.scanPlusKeyBuffer) { - this.scanPlusKeyStartedAt = now - } - this.scanPlusKeyLastAt = now - this.scanPlusKeyBuffer += text - this.clearPlusKeyTimer() - this.scanPlusKeyTimer = setTimeout(() => { - this.flushPlusKeyBuffer('timer') - }, 420) - return - } - - if (isEnter) { - this.flushPlusKeyBuffer('enter') - } + const rawChar = this.parsePlusKeyCodeToChar(keyCode) + // Shift 状态滞后补偿:本机实测 Shift 状态比字符滞后 1 个字符 + const lag = Number(this.plusLetterCaseLag) || 0 + const isFirstChar = !this.scanPlusKeyBuffer + const shifted = lag > 0 && !isFirstChar ? !!this._plusShiftPrev : !!this._plusShiftDown + const text = this.applyLetterCase(rawChar, shifted) + this._plusShiftPrev = this._plusShiftDown + const isEnter = this.isPlusEnterKey(keyCode, text) + if (!text && !isEnter) { + return } - // #ifdef APP-PLUS - plus.key.addEventListener('keydown', this.scanPlusKeyListener) - // #endif - this.logScanDebug('attach-plus-listener-done') + // 锁定实际生效的事件类型,避免 keydown/keyup 都触发时把一个键记两次 + if (!this._plusKeyEventType) { + this._plusKeyEventType = type + } + if (this._plusKeyEventType !== type) { + return + } + + if (text) { + const now = Date.now() + if (!this.scanPlusKeyBuffer) { + this.scanPlusKeyStartedAt = now + } + this.scanPlusKeyLastAt = now + this.scanPlusKeyBuffer += text + this.clearPlusKeyTimer() + this.scanPlusKeyTimer = setTimeout(() => { + this.flushPlusKeyBuffer('timer') + }, 420) + return + } + this.flushPlusKeyBuffer('enter') }, removePlusKeyboardListener() { this.clearPlusKeyTimer() this.scanPlusKeyBuffer = '' this.scanPlusKeyStartedAt = 0 this.scanPlusKeyLastAt = 0 + this._plusKeyEventType = null + this._plusShiftDown = false + this._plusShiftPrev = false - if (!this.scanPlusKeyListener) { + if (!this.scanPlusKeyListener && !this.scanPlusKeyUpListener) { return } // #ifdef APP-PLUS if (this.hasPlusKeyboardSupport()) { - plus.key.removeEventListener('keydown', this.scanPlusKeyListener) + if (this.scanPlusKeyListener) { + plus.key.removeEventListener('keydown', this.scanPlusKeyListener) + } + if (this.scanPlusKeyUpListener) { + plus.key.removeEventListener('keyup', this.scanPlusKeyUpListener) + } } // #endif this.scanPlusKeyListener = null + this.scanPlusKeyUpListener = null this.logScanDebug('remove-plus-listener-done') }, + // 离开页面前释放全局键盘/扫码监听,确保下一页面能重新“切换”为唯一监听者 + releaseScanListenersBeforeLeave() { + this.removePlusKeyboardListener() + this.removeGlobalKeyboardListener() + this.clearScanFocusResumeTimer() + this.clearKeyboardSuppressTimers() + this.clearHiddenRefocusTimer() + this.clearHiddenScanTimer() + this.clearPlusKeyTimer() + this.clearGlobalKeyTimer() + }, ensureScanInputFocus() { if (!this.isPageActive()) { return @@ -589,6 +804,10 @@ export default { return } this.focusedField = '' + if (this.isAppPlus()) { + this.setWindowSoftInputHidden() + return + } if (typeof document !== 'undefined') { document.activeElement?.blur?.() } @@ -874,6 +1093,26 @@ export default { this.workbenchPollTimer = null } }, + abortScanRequest() { + if (typeof this.scanRequestAbort === 'function') { + try { + this.scanRequestAbort() + } catch (err) { + // 忽略中断异常 + } + } + this.scanRequestAbort = null + }, + abortWorkbenchRequest() { + if (typeof this.workbenchRequestAbort === 'function') { + try { + this.workbenchRequestAbort() + } catch (err) { + // 忽略中断异常 + } + } + this.workbenchRequestAbort = null + }, resumeWorkbenchFlow() { if (!this.isPageActive()) { return @@ -952,7 +1191,11 @@ export default { this.workbenchLoading = true const requestData = this.workbenchClientCode ? { clientCode: this.workbenchClientCode } : {} - getWorkbenchBindCode(requestData, { timeout: this.workbenchRequestTimeout }) + this.abortWorkbenchRequest() + const workbenchRequest = getWorkbenchBindCode(requestData, { timeout: this.workbenchRequestTimeout }) + this.workbenchRequestAbort = typeof workbenchRequest?.abort === 'function' ? workbenchRequest.abort : null + + workbenchRequest .then((body) => { if (!this.isPageActive()) { return @@ -995,6 +1238,7 @@ export default { this.workbenchStatus = '网络异常,请稍后重试' }) .finally(() => { + this.workbenchRequestAbort = null this.workbenchLoading = false if ( this.isPageActive() && @@ -1032,6 +1276,7 @@ export default { if (!this.isPageActive()) { return } + this.releaseScanListenersBeforeLeave() uni.reLaunch({ url: '/pages/packaging/index', }) @@ -1097,6 +1342,7 @@ export default { if (!this.isPageActive()) { return } + this.releaseScanListenersBeforeLeave() uni.reLaunch({ url: '/pages/packaging/index', }) @@ -1142,7 +1388,11 @@ export default { const requestData = this.scanClientCode ? { clientCode: this.scanClientCode } : {} - getLoginWechatCode(requestData, { timeout: this.scanRequestTimeout }) + this.abortScanRequest() + const scanRequest = getLoginWechatCode(requestData, { timeout: this.scanRequestTimeout }) + this.scanRequestAbort = typeof scanRequest?.abort === 'function' ? scanRequest.abort : null + + scanRequest .then((body) => { if (!this.isPageActive()) { return @@ -1188,6 +1438,7 @@ export default { this.scanStatus = '网络异常,请稍后重试' }) .finally(() => { + this.scanRequestAbort = null if (!this.isPageActive()) { return } @@ -1255,6 +1506,7 @@ export default { if (!this.isPageActive()) { return } + this.releaseScanListenersBeforeLeave() uni.reLaunch({ url: '/pages/index/index', }) @@ -1324,6 +1576,7 @@ export default { if (!this.isPageActive()) { return } + this.releaseScanListenersBeforeLeave() uni.reLaunch({ url: '/pages/index/index', }) @@ -1350,26 +1603,50 @@ export default { } + + \ No newline at end of file diff --git a/src/pages/order/index.vue b/src/pages/order/index.vue index 45fbbbb..056a3dd 100644 --- a/src/pages/order/index.vue +++ b/src/pages/order/index.vue @@ -3150,6 +3150,31 @@ export default { } + + diff --git a/src/pages/packaging/index.vue b/src/pages/packaging/index.vue index 48e4a59..37db6ea 100644 --- a/src/pages/packaging/index.vue +++ b/src/pages/packaging/index.vue @@ -3,7 +3,11 @@ - {{ modeTitle }} + + {{ workbenchInfo.name }} + + 打包模式 + + 已扫 {{ scannedKindCount }}/{{ totalKindCount }} 种 + 共{{ detailList.length }}项 暂无商品明细 @@ -358,6 +363,25 @@ const SCAN_IDLE_TIMEOUT = 300 const SCAN_MIN_LENGTH = 2 // 手动输入识别:相邻按键间隔超过该毫秒数视为手动输入,不自动提交 const SCAN_BURST_MAX_GAP = 100 +// Android KeyEvent 符号键映射(KEYCODE_* → 字符)。 +// 重要:这些 keyCode 与 ASCII 字母/数字区间重叠,必须优先于 ASCII 分支判断, +// 否则 KEYCODE_EQUALS=70 会被 ASCII 的 [65..90] 分支误读为 'F'。 +const ANDROID_PUNCT_MAP = { + 55: ',', // KEYCODE_COMMA + 56: '.', // KEYCODE_PERIOD + 62: ' ', // KEYCODE_SPACE + 68: '`', // KEYCODE_GRAVE + 69: '-', // KEYCODE_MINUS + 70: '=', // KEYCODE_EQUALS + 71: '[', // KEYCODE_LEFT_BRACKET + 72: ']', // KEYCODE_RIGHT_BRACKET + 73: '\\', // KEYCODE_BACKSLASH + 74: ';', // KEYCODE_SEMICOLON + 75: "'", // KEYCODE_APOSTROPHE + 76: '/', // KEYCODE_SLASH + 77: '@', // KEYCODE_AT + 81: '+', // KEYCODE_PLUS +} // 已绑定操作员本地缓存 key(后端暂未提供查询成员接口时,刷新后据此恢复显示) const MEMBER_STORAGE_KEY = 'workbenchMember' // 今日打包统计本地缓存 key(前端自行记录,按天存储,跨天重置) @@ -391,12 +415,26 @@ export default { lastScanKeyTime: 0, scanBurstConfirmed: false, plusKeyListener: null, + plusKeyUpListener: null, plusKeyBuffer: '', plusKeyTimer: null, plusKeyStartedAt: 0, plusKeyLastAt: 0, + // 字母大小写策略:'auto' 跟踪 Shift 键 / 'upper' 全大写 / 'lower' 全小写 + // 注意:plus.key 只给 keyCode、无 Shift 状态;仅当扫码枪会额外上报 + // Android Shift 键(KEYCODE_SHIFT_LEFT=59 / SHIFT_RIGHT=60)时 'auto' 才能还原大小写。 + plusLetterCase: 'auto', + // Shift 状态滞后字符数(本机实测滞后 1 个字符): + // 只投递 keyup 的设备上,Shift 的 keydown 会比字符更早进入事件流, + // 导致判断时 Shift 状态“超前”一位,用滞后 1 位补偿。0 = 不补偿。 + plusLetterCaseLag: 1, globalKeyListener: null, globalKeyTargets: [], + globalKeyBuffer: '', + globalKeyTimer: null, + globalKeyStartedAt: 0, + globalKeyLastAt: 0, + scanWatchdogTimer: null, enableHiddenInputCapture: false, scanInputFocus: false, scanHiddenValue: '', @@ -457,10 +495,6 @@ export default { } }, computed: { - modeTitle() { - const name = this.workbenchInfo?.name - return name ? `打包模式 - ${name}` : '打包模式' - }, workbenchTypeText() { const type = this.workbenchInfo?.type if (type === 'group') { @@ -555,12 +589,19 @@ export default { this.loadWorkbenchInfo() }, onShow() { - this.pageActive = true - this.attachPlusKeyboardListener() - this.attachGlobalKeyboardListener() + try { + this.pageActive = true + this.attachPlusKeyboardListener() + this.attachGlobalKeyboardListener() + this.startScanWatchdog() + this.suppressSoftKeyboardBurst() + } catch (e) { + console.error('[packaging] onShow 异常:', e) + } }, onHide() { this.pageActive = false + this.stopScanWatchdog() this.removePlusKeyboardListener() this.removeGlobalKeyboardListener() this.clearScanTimers() @@ -1324,6 +1365,10 @@ export default { }, handleSearchFocus() { this.searchFocused = true + // 真机:输入框被聚焦时立即压制软键盘(扫码走 plus.key,不依赖软键盘) + if (this.isAppPlus()) { + this.setWindowSoftInputHidden() + } }, handleSearchBlur() { this.searchFocused = false @@ -1332,15 +1377,16 @@ export default { this.scheduleScanInputFocus(150) } }, - activateSearchInput() { - this.searchFocus = true - }, handleSearchInput() { // 真机(APP-PLUS)扫码枪走 IME 注入文本、无 DOM keydown 字符, - // 停止输入 1000ms 视为一次完整扫码自动查询(@confirm 仍可即时查询) + // 停止输入 1000ms 视为一次完整扫码自动查询(@confirm 仍可即时查询)。 + // plus.key 可用时由原生链路捕获,此处不兜底,避免同一码双触发。 if (!this.isPageActive() || !this.isAppPlus()) { return } + if (this.hasPlusKeyboardSupport()) { + return + } const code = String(this.keyword || '').trim() this.clearScanInputIdleTimer() if (!code) { @@ -1373,10 +1419,11 @@ export default { if (!this.isPageActive()) { return } - const key = event.key + const key = event.key || '' const activeEl = typeof document !== 'undefined' ? document.activeElement : null const inInput = !!(activeEl && (activeEl.tagName === 'INPUT' || activeEl.tagName === 'TEXTAREA')) + // 焦点在输入框:字符由输入框自身接收(@confirm/@input 处理),这里只记录节奏 if (inInput) { this.touchScanInput() return @@ -1388,18 +1435,11 @@ export default { return } - const isPrintable = !!key && key.length === 1 - const isPrefixKey = key === 'Tab' || key === 'Enter' - if (!isPrintable && !isPrefixKey) { - return - } - - event.preventDefault() - // 绑定操作员弹窗打开时:扫码内容进入弹窗临时码输入框,不进入主搜索框 if (this.operatorDialogVisible) { + const isPrintableOp = !!key && key.length === 1 if (!this.workbenchMember) { - if (isPrintable) { + if (isPrintableOp) { this.operatorCode = (this.operatorCode || '') + key } this.operatorInputFocus = true @@ -1414,17 +1454,48 @@ export default { return } + const isPrintable = !!key && key.length === 1 + const isEnter = key === 'Enter' + if (!isPrintable && !isEnter) { + return + } + + if (event && typeof event.preventDefault === 'function') { + event.preventDefault() + } + if (isPrintable) { - this.keyword = (this.keyword || '') + key + const now = Date.now() + if (!this.globalKeyBuffer) { + this.globalKeyStartedAt = now + } + this.globalKeyLastAt = now + this.globalKeyBuffer += key + this.clearGlobalKeyTimer() + this.globalKeyTimer = setTimeout(() => { + if (!this.isPageActive()) { + return + } + this.flushGlobalScanBuffer() + }, 380) + return } - this.searchFocus = true - - if (key === 'Enter' && (this.keyword || '').trim()) { - this.handlePackSearch() + // Enter:结束符立即提交 + const code = (this.globalKeyBuffer || '').trim() + const durationMs = this.globalKeyStartedAt + ? Math.max(0, (this.globalKeyLastAt || Date.now()) - this.globalKeyStartedAt) + : 0 + this.globalKeyBuffer = '' + this.globalKeyStartedAt = 0 + this.globalKeyLastAt = 0 + this.clearGlobalKeyTimer() + if (!code) { + return + } + if (this.isLikelyScannerCode(code, durationMs, true)) { + this.submitScanCode(code, 'dom') } - - this.touchScanInput() }, touchScanInput() { const now = Date.now() @@ -1476,10 +1547,36 @@ export default { return typeof window !== 'undefined' || typeof document !== 'undefined' }, setupScanCaptureMode() { - // 真机扫码枪走 IME 注入文本(无 DOM keydown 字符)、隐藏 input 会丢字, - // 且隐藏 input 聚焦会反复弹出软键盘;改为可见搜索框直接接收扫码(与登录页一致) + // 真机(APP-PLUS)逻辑层无 DOM:window/document 均为 undefined, + // hasDomKeyboardSupport() 会返回 false,导致 !hasDomKeyboardSupport() 误开启隐藏 input, + // 而看门狗每 2s 反复聚焦隐藏 input → 软键盘反复弹出(闪烁根因)。 + // 现在 plus.key 已按 Android 原生 keycode 完整捕获扫码,真机彻底关闭隐藏 input。 + if (this.isAppPlus()) { + this.enableHiddenInputCapture = false + return + } + // H5/Electron:有 DOM → 关闭隐藏捕获;小程序等无 DOM 且无 plus.key 平台 → 开启兜底 this.enableHiddenInputCapture = !this.hasDomKeyboardSupport() }, + // 看门狗:即使 onShow 异常未触发也能自愈恢复扫码。 + // App/H5 页面隐藏或后台时收不到按键,所以 pageActive=true 不会误处理。 + startScanWatchdog() { + if (this.scanWatchdogTimer) { + return + } + this.scanWatchdogTimer = setInterval(() => { + this.pageActive = true + this.attachPlusKeyboardListener() + this.attachGlobalKeyboardListener() + this.ensureScanInputFocus() + }, 2000) + }, + stopScanWatchdog() { + if (this.scanWatchdogTimer) { + clearInterval(this.scanWatchdogTimer) + this.scanWatchdogTimer = null + } + }, isRealDevice() { // #ifdef APP-PLUS return true @@ -1508,6 +1605,47 @@ export default { } return false }, + clearGlobalKeyTimer() { + if (this.globalKeyTimer) { + clearTimeout(this.globalKeyTimer) + this.globalKeyTimer = null + } + }, + // 判断是否为扫码枪码(DOM 全局捕获用): + // 回车结束符 / 长度>=8 / 输入总时长与平均间隔足够快 → 视为扫码枪;否则视为手动输入忽略 + isLikelyScannerCode(code, durationMs, byEnter) { + const normalizedCode = (code || '').replace(/\s+/g, '') + if (!normalizedCode || normalizedCode.length < 4) { + return false + } + if (byEnter) { + return true + } + if (!durationMs || durationMs <= 0) { + return normalizedCode.length >= 8 + } + const avgInterval = durationMs / Math.max(normalizedCode.length - 1, 1) + return normalizedCode.length >= 8 || (durationMs <= 1800 && avgInterval <= 120) + }, + flushGlobalScanBuffer() { + if (!this.isPageActive()) { + return + } + const code = (this.globalKeyBuffer || '').trim() + const durationMs = this.globalKeyStartedAt + ? Math.max(0, (this.globalKeyLastAt || Date.now()) - this.globalKeyStartedAt) + : 0 + this.globalKeyBuffer = '' + this.globalKeyStartedAt = 0 + this.globalKeyLastAt = 0 + this.clearGlobalKeyTimer() + if (!code) { + return + } + if (this.isLikelyScannerCode(code, durationMs, false)) { + this.submitScanCode(code, 'dom') + } + }, attachGlobalKeyboardListener() { if (this.globalKeyListener) { return @@ -1530,7 +1668,13 @@ export default { }) }, removeGlobalKeyboardListener() { + this.clearGlobalKeyTimer() + this.globalKeyBuffer = '' + this.globalKeyStartedAt = 0 + this.globalKeyLastAt = 0 if (!this.globalKeyTargets || !this.globalKeyListener) { + this.globalKeyTargets = [] + this.globalKeyListener = null return } this.globalKeyTargets.forEach((target) => { @@ -1540,13 +1684,36 @@ export default { this.globalKeyListener = null }, parsePlusKeyCodeToChar(keyCode) { + // Android Enter=66 在 ASCII 里是 'B',必须先排除,交给 isPlusEnterKey 处理 + if (keyCode === 66) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return '' + } + // ---- Android 原生 keycode ---- + if (keyCode >= 7 && keyCode <= 16) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return String(keyCode - 7) // 0-9 + } + if (keyCode >= 29 && keyCode <= 54) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return String.fromCharCode(keyCode - 29 + 65) // A-Z + } + // Android 符号键:必须放在 ASCII 分支之前(防止 70('=') → 'F' 这类误读) + if (this._plusKeyStyle !== 'ascii' && ANDROID_PUNCT_MAP[keyCode] !== undefined) { + this._plusKeyStyle = this._plusKeyStyle || 'android' + return ANDROID_PUNCT_MAP[keyCode] + } + // ---- ASCII/DOM 风格(其它设备兑底)---- if (keyCode >= 48 && keyCode <= 57) { + this._plusKeyStyle = this._plusKeyStyle || 'ascii' return String.fromCharCode(keyCode) } if (keyCode >= 65 && keyCode <= 90) { + this._plusKeyStyle = this._plusKeyStyle || 'ascii' return String.fromCharCode(keyCode) } if (keyCode >= 96 && keyCode <= 105) { + this._plusKeyStyle = this._plusKeyStyle || 'ascii' return String(keyCode - 96) } if (keyCode === 189 || keyCode === 109) { @@ -1560,53 +1727,121 @@ export default { } return '' }, + // 应用字母大小写策略(仅对 A-Z 生效) + applyLetterCase(ch, shifted) { + if (!ch || ch.length !== 1 || ch < 'A' || ch > 'Z') { + return ch + } + const mode = this.plusLetterCase || 'upper' + if (mode === 'lower') { + return ch.toLowerCase() + } + if (mode === 'auto') { + return shifted ? ch : ch.toLowerCase() + } + return ch // 'upper' + }, + // 判断是否为 Enter:Android 原生 Enter=66;ASCII 风格为 13 + // (13 在 Android 风格下是数字键 '6',所以用 text 能否解析来消歧) + isPlusEnterKey(keyCode, text) { + if (text) { + return false + } + if (keyCode === 66) { + return true + } + if (keyCode === 13) { + return this._plusKeyStyle !== 'android' + } + return false + }, attachPlusKeyboardListener() { if (!this.hasPlusKeyboardSupport()) { return } - if (this.plusKeyListener) { + if (this.plusKeyListener || this.plusKeyUpListener) { return } - this.plusKeyListener = (event) => { - if (!this.isPageActive()) { - return - } - const keyCode = Number(event?.keyCode || 0) - const isEnter = keyCode === 13 || keyCode === 66 - const text = this.parsePlusKeyCodeToChar(keyCode) - if (text) { - const now = Date.now() - if (!this.plusKeyBuffer) { - this.plusKeyStartedAt = now - } - this.plusKeyLastAt = now - this.plusKeyBuffer += text - this.clearPlusKeyTimer() - this.plusKeyTimer = setTimeout(() => this.flushPlusKeyBuffer('timer'), 420) - return - } - if (isEnter) { - this.flushPlusKeyBuffer('enter') - } - } + this.plusKeyListener = (event) => this.handlePlusKeyEvent(event, 'keydown') + this.plusKeyUpListener = (event) => this.handlePlusKeyEvent(event, 'keyup') // #ifdef APP-PLUS + // 同时监听 keydown 与 keyup:本机实测只触发 keyup(keydown 不触发), + // 由 handlePlusKeyEvent 里的类型锁定避免两者都触发时重复采集 plus.key.addEventListener('keydown', this.plusKeyListener) + plus.key.addEventListener('keyup', this.plusKeyUpListener) // #endif }, + // 统一处理 plus.key 按键(keydown / keyup) + handlePlusKeyEvent(event, type) { + if (!this.isPageActive()) { + return + } + const keyCode = Number((event && event.keyCode) || 0) + + // 跟踪 Shift 修饰键(Android KEYCODE_SHIFT_LEFT=59 / SHIFT_RIGHT=60) + if (keyCode === 59 || keyCode === 60) { + if (type === 'keydown') { + this._plusShiftDown = true + } else if (type === 'keyup') { + this._plusShiftDown = false + } + return + } + + const rawChar = this.parsePlusKeyCodeToChar(keyCode) + // Shift 状态滞后补偿:本机实测 Shift 状态比字符滞后 1 个字符 + const lag = Number(this.plusLetterCaseLag) || 0 + const isFirstChar = !this.plusKeyBuffer + const shifted = lag > 0 && !isFirstChar ? !!this._plusShiftPrev : !!this._plusShiftDown + const text = this.applyLetterCase(rawChar, shifted) + this._plusShiftPrev = this._plusShiftDown + const isEnter = this.isPlusEnterKey(keyCode, text) + if (!text && !isEnter) { + return + } + + // 锁定实际生效的事件类型,避免 keydown/keyup 都触发时把一个键记两次 + if (!this._plusKeyEventType) { + this._plusKeyEventType = type + } + if (this._plusKeyEventType !== type) { + return + } + + if (text) { + const now = Date.now() + if (!this.plusKeyBuffer) { + this.plusKeyStartedAt = now + } + this.plusKeyLastAt = now + this.plusKeyBuffer += text + this.clearPlusKeyTimer() + this.plusKeyTimer = setTimeout(() => this.flushPlusKeyBuffer('timer'), 420) + return + } + this.flushPlusKeyBuffer('enter') + }, removePlusKeyboardListener() { this.clearPlusKeyTimer() this.plusKeyBuffer = '' this.plusKeyStartedAt = 0 this.plusKeyLastAt = 0 - if (!this.plusKeyListener) { + this._plusKeyEventType = null + if (!this.plusKeyListener && !this.plusKeyUpListener) { return } // #ifdef APP-PLUS if (this.hasPlusKeyboardSupport()) { - plus.key.removeEventListener('keydown', this.plusKeyListener) + if (this.plusKeyListener) { + plus.key.removeEventListener('keydown', this.plusKeyListener) + } + if (this.plusKeyUpListener) { + plus.key.removeEventListener('keyup', this.plusKeyUpListener) + } } // #endif this.plusKeyListener = null + this.plusKeyUpListener = null }, flushPlusKeyBuffer(trigger = 'timer') { if (!this.isPageActive()) { @@ -1620,6 +1855,7 @@ export default { if (!code) { return } + console.log('[packaging][scan] plus-buffer trigger =', trigger, 'code =', code) this.submitScanCode(code, 'plus') }, handleHiddenScanFocus() { @@ -1757,18 +1993,60 @@ export default { this.ensureScanInputFocus() }, delay) }, + // 窗口级软键盘模式设为 ALWAYS_HIDDEN:input 聚焦也不会自动弹软键盘。 + // 真机上不要用 uni.hideKeyboard()——它会断开输入框的按键连接, + // 导致“扫一次后就不再接收输入”;用 Android 窗口软键盘模式彻底压制。 + setWindowSoftInputHidden() { + // #ifdef APP-PLUS + try { + if (typeof plus !== 'undefined' && plus.android && plus.android.runtimeMainActivity) { + const main = plus.android.runtimeMainActivity() + const win = plus.android.invoke(main, 'getWindow') + // SOFT_INPUT_STATE_ALWAYS_HIDDEN = 3(数值直传,避免静态字段访问失败导致设置无效) + plus.android.invoke(win, 'setSoftInputMode', 3) + console.log('[packaging][kb] setSoftInputMode=ALWAYS_HIDDEN(3)') + } else { + console.log('[packaging][kb] plus.android unavailable') + } + } catch (e) { + console.log('[packaging][kb] setSoftInputMode failed:', e && e.message) + } + // #endif + // 隐藏当前已显示的软键盘;此后窗口级 ALWAYS_HIDDEN 阻止再弹,不会形成闪烁循环 + if (typeof uni !== 'undefined' && typeof uni.hideKeyboard === 'function') { + uni.hideKeyboard() + } + }, suppressSoftKeyboardBurst() { if (!this.isPageActive()) { return } this.clearKeyboardSuppressTimers() - // 仅聚焦时隐藏一次软键盘,避免扫描过程中反复 hideKeyboard 打断输入 + // 真机:窗口级 ALWAYS_HIDDEN,input 聚焦也不会弹软键盘(不打断扫码输入) + if (this.isAppPlus()) { + this.setWindowSoftInputHidden() + // 软键盘可能晚于页面显示弹出,多时间点重设窗口模式 + 收键盘覆盖 + ;[80, 180, 320].forEach((delay) => { + const timer = setTimeout(() => { + if (!this.isPageActive()) { + return + } + this.setWindowSoftInputHidden() + }, delay) + this.scanKeyboardSuppressTimers.push(timer) + }) + return + } uni.hideKeyboard?.() }, dismissSoftKeyboard() { if (!this.isPageActive()) { return } + if (this.isAppPlus()) { + this.setWindowSoftInputHidden() + return + } // 不主动 blur,避免隐藏输入框失焦导致下一次扫码首字符丢失 uni.hideKeyboard?.() }, @@ -2347,6 +2625,11 @@ export default { if (wbToken && wbToken === mainToken) { uni.removeStorageSync('token') } + // 离开前释放全局扫码监听,避免登录页重新注册时被本页残留监听干扰 + this.stopScanWatchdog() + this.removePlusKeyboardListener() + this.removeGlobalKeyboardListener() + this.clearScanTimers() setTimeout(() => { uni.reLaunch({ url: '/pages/login/login', @@ -2381,22 +2664,22 @@ export default { }, }, mounted() { - // 广播扫描:真机/H5 双链路(隐藏 input + plus.key + DOM 全局监听) - this.setupScanCaptureMode() - this.attachPlusKeyboardListener() - this.attachGlobalKeyboardListener() - this.suppressSoftKeyboardBurst() - this.scheduleScanInputFocus(260) - this.initNativeTts() - // 页面进入即聚焦搜索框(H5 无隐藏捕获时) - this.$nextTick(() => { - if (!this.enableHiddenInputCapture) { - this.activateSearchInput() - } - }) + try { + // 广播扫描:plus.key(Android 原生 keycode)+ DOM 全局 keydown + 隐藏 input(关闭) + this.setupScanCaptureMode() + this.attachPlusKeyboardListener() + this.attachGlobalKeyboardListener() + this.startScanWatchdog() + this.suppressSoftKeyboardBurst() + this.scheduleScanInputFocus(260) + this.initNativeTts() + } catch (e) { + console.error('[packaging] mounted 异常:', e) + } }, beforeUnmount() { this.pageActive = false + this.stopScanWatchdog() this.shutdownNativeTts() this.destroyVoiceAudio() this.removePlusKeyboardListener() @@ -2406,10 +2689,35 @@ export default { } + + diff --git a/src/pages/returns/index.vue b/src/pages/returns/index.vue index f7d8579..86981af 100644 --- a/src/pages/returns/index.vue +++ b/src/pages/returns/index.vue @@ -30,6 +30,31 @@ export default { } + +