修改平板端登录页、首页扫描工牌登录问题

This commit is contained in:
zm2000 2026-05-24 18:22:20 +08:00
parent 3a6bb410c5
commit 1c948df084
3 changed files with 616 additions and 14 deletions

View File

@ -6,6 +6,8 @@
class="search-input"
v-model="keyword"
placeholder="请输入快递号/商品条码/拣货车/数量等关键词搜索订单"
:focus="false"
:adjust-position="false"
confirm-type="search"
@confirm="handleSearch"
/>

View File

@ -57,6 +57,22 @@ import { changeUserRole, fetchUserInfo, logout } from '@/api/index'
import { hasMenuView, normalizeMenuPath, resolveMenuView } from '@/router/menuRoutes'
export default {
onReady() {
uni.hideKeyboard?.()
;[80, 180, 320, 520].forEach((delay) => {
setTimeout(() => {
uni.hideKeyboard?.()
}, delay)
})
},
onShow() {
uni.hideKeyboard?.()
;[80, 180, 320, 520].forEach((delay) => {
setTimeout(() => {
uni.hideKeyboard?.()
}, delay)
})
},
onLoad(options) {
const token = uni.getStorageSync('token')
if (!token) {

View File

@ -75,6 +75,19 @@
</view>
</view>
</view>
<input
class="scan-hidden-input"
v-if="enableHiddenInputCapture"
v-model="scanHiddenValue"
:focus="scanInputFocus"
:adjust-position="false"
confirm-type="done"
@focus="handleHiddenScanFocus"
@input="handleHiddenScanInput"
@confirm="handleHiddenScanConfirm"
@blur="handleHiddenScanBlur"
/>
</view>
</template>
@ -99,6 +112,23 @@ export default {
scanGlobalKeyBuffer: '',
scanGlobalKeyTimer: null,
scanGlobalKeyListener: null,
scanGlobalKeyTargets: [],
scanGlobalKeyStartedAt: 0,
scanGlobalKeyLastAt: 0,
scanPlusKeyListener: null,
scanPlusKeyBuffer: '',
scanPlusKeyTimer: null,
scanPlusKeyStartedAt: 0,
scanPlusKeyLastAt: 0,
enableHiddenInputCapture: false,
scanInputFocus: false,
scanHiddenValue: '',
scanHiddenTimer: null,
scanHiddenRefocusTimer: null,
scanFocusResumeTimer: null,
scanKeyboardSuppressTimers: [],
scanHiddenFocused: false,
pageActive: true,
scanCode: '',
scanQrImage: '',
scanMiniProgramUrl: '',
@ -113,21 +143,51 @@ export default {
}
},
onUnload() {
this.pageActive = false
this.stopScanLongPolling()
this.removePlusKeyboardListener()
this.removeGlobalKeyboardListener()
this.clearScanFocusResumeTimer()
this.clearKeyboardSuppressTimers()
this.clearHiddenRefocusTimer()
this.scanBadgeSubmitting = false
},
onReady() {
this.pageActive = true
this.setupScanCaptureMode()
this.attachPlusKeyboardListener()
this.attachGlobalKeyboardListener()
this.suppressSoftKeyboardBurst()
this.scheduleScanInputFocus(260)
},
onShow() {
this.pageActive = true
this.setupScanCaptureMode()
this.attachPlusKeyboardListener()
this.attachGlobalKeyboardListener()
this.suppressSoftKeyboardBurst()
this.scheduleScanInputFocus(320)
},
onHide() {
this.pageActive = false
this.removePlusKeyboardListener()
this.removeGlobalKeyboardListener()
this.clearScanFocusResumeTimer()
this.clearKeyboardSuppressTimers()
this.clearHiddenScanTimer()
this.clearPlusKeyTimer()
this.clearHiddenRefocusTimer()
this.scanHiddenFocused = false
this.scanInputFocus = false
},
methods: {
isPageActive() {
return this.pageActive !== false
},
switchMode(mode) {
if (!this.isPageActive()) {
return
}
this.loginMode = mode
if (mode !== 'scan') {
this.stopScanLongPolling()
@ -138,6 +198,9 @@ export default {
} else if (mode === 'scan' && this.scanClientCode) {
this.startScanLongPolling()
}
this.suppressSoftKeyboardBurst()
this.scheduleScanInputFocus(280)
},
togglePassword() {
this.showPassword = !this.showPassword
@ -152,25 +215,393 @@ export default {
this.scanGlobalKeyTimer = null
}
},
clearPlusKeyTimer() {
if (this.scanPlusKeyTimer) {
clearTimeout(this.scanPlusKeyTimer)
this.scanPlusKeyTimer = null
}
},
clearHiddenScanTimer() {
if (this.scanHiddenTimer) {
clearTimeout(this.scanHiddenTimer)
this.scanHiddenTimer = null
}
},
clearHiddenRefocusTimer() {
if (this.scanHiddenRefocusTimer) {
clearTimeout(this.scanHiddenRefocusTimer)
this.scanHiddenRefocusTimer = null
}
},
clearScanFocusResumeTimer() {
if (this.scanFocusResumeTimer) {
clearTimeout(this.scanFocusResumeTimer)
this.scanFocusResumeTimer = null
}
},
clearKeyboardSuppressTimers() {
if (!Array.isArray(this.scanKeyboardSuppressTimers)) {
this.scanKeyboardSuppressTimers = []
return
}
this.scanKeyboardSuppressTimers.forEach((timer) => {
clearTimeout(timer)
})
this.scanKeyboardSuppressTimers = []
},
logScanDebug(...args) {
console.log('[scan-login]', ...args)
},
suppressSoftKeyboardBurst() {
if (!this.isPageActive()) {
return
}
this.clearKeyboardSuppressTimers()
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()) {
return
}
this.clearScanFocusResumeTimer()
this.scanFocusResumeTimer = setTimeout(() => {
if (!this.isPageActive()) {
return
}
this.ensureScanInputFocus()
}, delay)
},
hasPlusKeyboardSupport() {
// #ifdef APP-PLUS
return typeof plus !== 'undefined' && !!plus?.key?.addEventListener
// #endif
// #ifndef APP-PLUS
return false
// #endif
},
hasDomKeyboardSupport() {
return typeof window !== 'undefined' || typeof document !== 'undefined'
},
setupScanCaptureMode() {
this.enableHiddenInputCapture = !this.hasDomKeyboardSupport()
this.logScanDebug('scan-capture-mode', {
plus: this.hasPlusKeyboardSupport(),
dom: this.hasDomKeyboardSupport(),
hiddenInput: this.enableHiddenInputCapture,
})
},
parsePlusKeyCodeToChar(keyCode) {
if (keyCode >= 48 && keyCode <= 57) {
return String.fromCharCode(keyCode)
}
if (keyCode >= 65 && keyCode <= 90) {
return String.fromCharCode(keyCode)
}
if (keyCode >= 96 && keyCode <= 105) {
return String(keyCode - 96)
}
if (keyCode === 189 || keyCode === 109) {
return '-'
}
if (keyCode === 190 || keyCode === 110) {
return '.'
}
if (keyCode === 191 || keyCode === 111) {
return '/'
}
return ''
},
flushPlusKeyBuffer(trigger = 'timer') {
if (!this.isPageActive()) {
return
}
const code = (this.scanPlusKeyBuffer || '').trim()
const durationMs = this.scanPlusKeyStartedAt
? Math.max(0, (this.scanPlusKeyLastAt || Date.now()) - this.scanPlusKeyStartedAt)
: 0
this.scanPlusKeyBuffer = ''
this.scanPlusKeyStartedAt = 0
this.scanPlusKeyLastAt = 0
this.clearPlusKeyTimer()
if (!code) {
return
}
this.logScanDebug('plus-buffer', {
trigger,
code,
length: code.length,
durationMs,
})
this.handleScanBadgeLogin(code)
},
attachPlusKeyboardListener() {
if (!this.hasPlusKeyboardSupport()) {
return
}
if (this.scanPlusKeyListener) {
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')
}
}
// #ifdef APP-PLUS
plus.key.addEventListener('keydown', this.scanPlusKeyListener)
// #endif
this.logScanDebug('attach-plus-listener-done')
},
removePlusKeyboardListener() {
this.clearPlusKeyTimer()
this.scanPlusKeyBuffer = ''
this.scanPlusKeyStartedAt = 0
this.scanPlusKeyLastAt = 0
if (!this.scanPlusKeyListener) {
return
}
// #ifdef APP-PLUS
if (this.hasPlusKeyboardSupport()) {
plus.key.removeEventListener('keydown', this.scanPlusKeyListener)
}
// #endif
this.scanPlusKeyListener = null
this.logScanDebug('remove-plus-listener-done')
},
ensureScanInputFocus() {
if (!this.isPageActive()) {
return
}
if (!this.enableHiddenInputCapture) {
this.scanInputFocus = false
this.suppressSoftKeyboardBurst()
return
}
if (this.focusedField === 'username' || this.focusedField === 'password') {
return
}
this.clearHiddenRefocusTimer()
if (!this.scanInputFocus) {
this.scanInputFocus = true
}
},
processHiddenScanValue(trigger = 'timer') {
if (!this.isPageActive()) {
return
}
const code = (this.scanHiddenValue || '').trim()
this.clearHiddenScanTimer()
if (!code) {
return
}
this.logScanDebug('hidden-input-captured', {
trigger,
code,
length: code.length,
})
this.scanHiddenValue = ''
this.dismissSoftKeyboard()
this.handleScanBadgeLogin(code)
this.ensureScanInputFocus()
},
handleHiddenScanFocus() {
if (!this.isPageActive()) {
return
}
if (!this.enableHiddenInputCapture) {
this.scanInputFocus = false
return
}
this.logScanDebug('hidden-input-focus')
this.scanHiddenFocused = true
this.scanInputFocus = true
this.clearHiddenRefocusTimer()
this.suppressSoftKeyboardBurst()
},
handleHiddenScanBlur() {
if (!this.isPageActive()) {
return
}
if (!this.enableHiddenInputCapture) {
return
}
this.logScanDebug('hidden-input-blur')
this.scanHiddenFocused = false
this.scanInputFocus = false
this.clearHiddenRefocusTimer()
if (this.focusedField === 'username' || this.focusedField === 'password') {
return
}
this.scanHiddenRefocusTimer = setTimeout(() => {
if (!this.isPageActive()) {
return
}
if (!this.scanHiddenFocused && this.focusedField !== 'username' && this.focusedField !== 'password') {
this.scheduleScanInputFocus(220)
}
}, 500)
},
handleHiddenScanInput(event) {
if (!this.isPageActive()) {
return
}
if (!this.enableHiddenInputCapture) {
return
}
this.scanHiddenValue = event?.detail?.value || ''
this.logScanDebug('hidden-input-typing', {
length: this.scanHiddenValue.length,
})
this.clearHiddenScanTimer()
this.scanHiddenTimer = setTimeout(() => {
if (!this.isPageActive()) {
return
}
this.processHiddenScanValue('timer')
}, 320)
},
handleHiddenScanConfirm(event) {
if (!this.isPageActive()) {
return
}
if (!this.enableHiddenInputCapture) {
return
}
const value = event?.detail?.value || this.scanHiddenValue || ''
this.scanHiddenValue = value
this.processHiddenScanValue('confirm')
},
dismissSoftKeyboard() {
if (!this.isPageActive()) {
return
}
this.focusedField = ''
if (typeof document !== 'undefined') {
document.activeElement?.blur?.()
}
uni.hideKeyboard?.()
},
isLikelyScannerCode(code = '', durationMs = 0, byEnter = false) {
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.scanGlobalKeyBuffer || '').trim()
const durationMs = this.scanGlobalKeyStartedAt
? Math.max(0, (this.scanGlobalKeyLastAt || Date.now()) - this.scanGlobalKeyStartedAt)
: 0
this.scanGlobalKeyBuffer = ''
this.scanGlobalKeyStartedAt = 0
this.scanGlobalKeyLastAt = 0
this.clearGlobalKeyTimer()
if (!code) {
return
}
this.handleScanBadgeLogin(code)
this.logScanDebug('flush-buffer', {
code,
length: code.length,
durationMs,
})
if (this.isLikelyScannerCode(code, durationMs)) {
this.dismissSoftKeyboard()
this.handleScanBadgeLogin(code)
} else {
this.logScanDebug('flush-buffer-skipped', {
reason: 'not-likely-scanner-code',
code,
length: code.length,
durationMs,
})
}
},
attachGlobalKeyboardListener() {
if (this.scanGlobalKeyListener) {
this.logScanDebug('attach-listener-skip', { reason: 'already-attached' })
return
}
if (typeof window === 'undefined' || typeof document === 'undefined') {
const hasWindow = typeof window !== 'undefined'
const hasDocument = typeof document !== 'undefined'
this.logScanDebug('attach-listener-start', { hasWindow, hasDocument })
const targets = []
if (hasWindow) {
targets.push(window)
}
if (hasDocument) {
targets.push(document)
}
if (!targets.length) {
this.logScanDebug('attach-listener-failed', { reason: 'no-window-or-document' })
return
}
this.scanGlobalKeyTargets = targets
this.scanGlobalKeyListener = (event) => {
if (this.loginMode !== 'account' && this.loginMode !== 'scan') {
@ -181,50 +612,123 @@ export default {
const isPrintable = key.length === 1
const isEnter = key === 'Enter'
// 当前在账号输入框/密码输入框时,不接管正常键入,避免影响手工输入
if (this.focusedField === 'username' || this.focusedField === 'password') {
return
if (isPrintable || isEnter) {
this.logScanDebug('key-captured', {
key,
loginMode: this.loginMode,
focusedField: this.focusedField,
})
}
if (isPrintable) {
const now = Date.now()
if (!this.scanGlobalKeyBuffer) {
this.scanGlobalKeyStartedAt = now
}
this.scanGlobalKeyLastAt = now
this.scanGlobalKeyBuffer += key
this.clearGlobalKeyTimer()
this.scanGlobalKeyTimer = setTimeout(() => {
if (!this.isPageActive()) {
return
}
this.flushGlobalScanBuffer()
}, 80)
}, 380)
return
}
if (isEnter) {
event.preventDefault?.()
this.flushGlobalScanBuffer()
const code = (this.scanGlobalKeyBuffer || '').trim()
const durationMs = this.scanGlobalKeyStartedAt
? Math.max(0, (this.scanGlobalKeyLastAt || Date.now()) - this.scanGlobalKeyStartedAt)
: 0
this.scanGlobalKeyBuffer = ''
this.scanGlobalKeyStartedAt = 0
this.scanGlobalKeyLastAt = 0
this.clearGlobalKeyTimer()
if (!code) {
return
}
this.logScanDebug('enter-detected', {
code,
length: code.length,
durationMs,
})
if (this.isLikelyScannerCode(code, durationMs, true)) {
event.preventDefault?.()
event.stopPropagation?.()
this.dismissSoftKeyboard()
this.handleScanBadgeLogin(code)
} else {
this.logScanDebug('enter-skipped', {
reason: 'not-likely-scanner-code',
code,
length: code.length,
durationMs,
})
}
}
}
window.addEventListener('keydown', this.scanGlobalKeyListener, true)
this.scanGlobalKeyTargets.forEach((target) => {
target.addEventListener('keydown', this.scanGlobalKeyListener, true)
})
this.logScanDebug('attach-listener-done', {
targetCount: this.scanGlobalKeyTargets.length,
})
},
removeGlobalKeyboardListener() {
this.clearGlobalKeyTimer()
this.clearHiddenScanTimer()
this.scanGlobalKeyBuffer = ''
this.scanGlobalKeyStartedAt = 0
this.scanGlobalKeyLastAt = 0
this.logScanDebug('remove-listener-start', {
targetCount: this.scanGlobalKeyTargets.length,
hasListener: !!this.scanGlobalKeyListener,
})
if (typeof window === 'undefined' || typeof document === 'undefined') {
this.scanGlobalKeyListener = null
this.scanGlobalKeyTargets = []
this.logScanDebug('remove-listener-failed', { reason: 'no-window-or-document' })
return
}
if (this.scanGlobalKeyListener) {
window.removeEventListener('keydown', this.scanGlobalKeyListener, true)
this.scanGlobalKeyTargets.forEach((target) => {
target.removeEventListener('keydown', this.scanGlobalKeyListener, true)
})
this.scanGlobalKeyListener = null
}
this.scanGlobalKeyTargets = []
this.logScanDebug('remove-listener-done')
},
handleScanCodeInput() {
// no-op: kept for compatibility if template bindings remain elsewhere
},
handleFocus(field) {
if (!this.isPageActive()) {
return
}
this.focusedField = field
this.clearScanFocusResumeTimer()
this.clearHiddenRefocusTimer()
this.scanInputFocus = false
this.scanHiddenFocused = false
this.clearHiddenScanTimer()
},
handleBlur() {
if (!this.isPageActive()) {
return
}
this.focusedField = ''
this.scheduleScanInputFocus(220)
},
buildMiniProgramUrlByCode(code) {
if (!code) {
@ -248,6 +752,9 @@ export default {
return `${qrFallbackBaseUrl}${encodeURIComponent(pageUrl)}`
},
handleScanImageError() {
if (!this.isPageActive()) {
return
}
const fallbackUrl = this.buildFallbackQrImageUrlByPageUrl(this.scanMiniProgramUrl)
if (fallbackUrl && this.scanQrImage !== fallbackUrl) {
this.scanQrImage = fallbackUrl
@ -257,12 +764,18 @@ export default {
this.scanStatus = '二维码加载失败,请重试'
},
startScanLongPolling() {
if (!this.isPageActive()) {
return
}
if (!this.scanClientCode) {
return
}
this.stopScanLongPolling()
this.scanPollTimer = setTimeout(() => {
if (!this.isPageActive()) {
return
}
this.handleScanLogin(true)
}, this.scanPollInterval)
},
@ -273,6 +786,9 @@ export default {
}
},
handleScanLogin(isPolling = false) {
if (!this.isPageActive()) {
return
}
if (this.scanLoading) {
return
}
@ -290,6 +806,9 @@ export default {
getLoginWechatCode(requestData, { timeout: this.scanRequestTimeout })
.then((body) => {
if (!this.isPageActive()) {
return
}
const payload = body?.data || body?.result || body
const clientCode =
@ -319,6 +838,9 @@ export default {
}
})
.catch((err) => {
if (!this.isPageActive()) {
return
}
const errMsg = err?.errMsg || ''
if (errMsg.includes('timeout') || errMsg.includes('abort') || errMsg.includes('cancel')) {
this.scanStatus = '请求超时,正在继续轮询...'
@ -328,6 +850,9 @@ export default {
this.scanStatus = '网络异常,请稍后重试'
})
.finally(() => {
if (!this.isPageActive()) {
return
}
this.scanLoading = false
if (this.loginMode === 'scan' && this.scanClientCode) {
this.startScanLongPolling()
@ -335,8 +860,12 @@ export default {
})
},
handleScanBadgeLogin(codeParam = '') {
if (!this.isPageActive()) {
return
}
const code = (codeParam || '').trim()
if (!code) {
this.logScanDebug('scan-login-abort', { reason: 'empty-code' })
return
}
@ -345,6 +874,10 @@ export default {
this.scanBadgeSubmitting ||
(this.scanBadgeLastSubmittedCode === code && now - this.scanBadgeLastSubmittedAt < 1200)
) {
this.logScanDebug('scan-login-skipped', {
reason: this.scanBadgeSubmitting ? 'submitting' : 'duplicate-in-1200ms',
code,
})
return
}
@ -352,17 +885,38 @@ export default {
this.scanBadgeLastSubmittedCode = code
this.scanBadgeLastSubmittedAt = now
this.logScanDebug('scan-login-request-send', {
api: 'loginByCode',
code,
})
loginByCode(code)
.then((body) => {
if (!this.isPageActive()) {
return
}
this.logScanDebug('scan-login-response', {
ok: true,
code,
body,
})
const token = body?.token || body?.data?.token || body?.result?.token
if (token) {
this.scanInputFocus = false
this.clearScanFocusResumeTimer()
this.clearHiddenRefocusTimer()
this.clearHiddenScanTimer()
this.suppressSoftKeyboardBurst()
uni.setStorageSync('token', token)
uni.showToast({
title: '登录成功',
icon: 'success',
})
setTimeout(() => {
if (!this.isPageActive()) {
return
}
uni.reLaunch({
url: '/pages/index/index',
})
@ -370,24 +924,38 @@ export default {
return
}
this.focusScanInput()
this.dismissSoftKeyboard()
uni.showToast({
title: body?.msg || body?.message || '工牌登录失败',
icon: 'none',
})
})
.catch(() => {
this.focusScanInput()
.catch((err) => {
if (!this.isPageActive()) {
return
}
this.logScanDebug('scan-login-response', {
ok: false,
code,
err,
})
this.dismissSoftKeyboard()
uni.showToast({
title: '网络异常,请稍后重试',
icon: 'none',
})
})
.finally(() => {
if (!this.isPageActive()) {
return
}
this.scanBadgeSubmitting = false
})
},
handleAccountLogin() {
if (!this.isPageActive()) {
return
}
if (!this.form.username || !this.form.password) {
uni.showToast({
title: '请输入账号和密码',
@ -398,15 +966,26 @@ export default {
loginByAccount(this.form.username, this.form.password)
.then((body) => {
if (!this.isPageActive()) {
return
}
const token = body?.token || body?.data?.token || body?.result?.token
if (token) {
this.scanInputFocus = false
this.clearScanFocusResumeTimer()
this.clearHiddenRefocusTimer()
this.clearHiddenScanTimer()
this.suppressSoftKeyboardBurst()
uni.setStorageSync('token', token)
uni.showToast({
title: '登录成功',
icon: 'success',
})
setTimeout(() => {
if (!this.isPageActive()) {
return
}
uni.reLaunch({
url: '/pages/index/index',
})
@ -420,6 +999,9 @@ export default {
})
})
.catch(() => {
if (!this.isPageActive()) {
return
}
uni.showToast({
title: '网络异常,请稍后重试',
icon: 'none',
@ -439,7 +1021,7 @@ export default {
justify-content: center;
box-sizing: border-box;
padding: 48rpx;
background-color: #f8f8f8;
background-color: #f2f2f2;
}
.login-card {
@ -448,6 +1030,8 @@ export default {
min-width: 920px;
background-color: #fff;
border-radius: 20rpx;
box-shadow: 0 18rpx 44rpx rgba(0, 0, 0, 0.10), 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
transform: translateY(-3rpx);
display: flex;
overflow: hidden;
}