新增扫描工牌登录

This commit is contained in:
zm2000 2026-05-15 18:18:03 +08:00
parent 705c6f3854
commit c8a229fb32
2 changed files with 281 additions and 13 deletions

View File

@ -2,13 +2,14 @@
<view class="page">
<view class="login-card">
<view class="visual-side">
<image class="visual-logo" src="http://192.168.11.56:5174/public/logo/1.png" mode="widthFix"></image>
<image class="visual-logo" src="/static/images/1.png" mode="widthFix"></image>
<text class="visual-desc">仓库管理系统工作台</text>
</view>
<view class="form-side">
<view class="header">
<text class="title">登录</text>
<text class="scan-hint">支持扫描工牌登录</text>
<text class="subtitle">欢迎回来,请登录您的账户</text>
</view>
@ -24,11 +25,16 @@
<view v-if="loginMode === 'scan'" class="panel">
<text class="scan-tip-top">打开微信,扫描二维码登录</text>
<view class="scan-box">
<image v-if="scanQrImage" class="scan-image" :src="scanQrImage" mode="aspectFit"></image>
<image
v-if="scanQrImage"
class="scan-image"
:src="scanQrImage"
mode="aspectFit"
@error="handleScanImageError"
></image>
<text v-else class="scan-placeholder">{{ scanCode || '二维码区域' }}</text>
</view>
<text class="scan-tip-bottom">{{ scanStatus }}</text>
<button class="primary-btn scan-login-btn" @click="handleScanLogin">扫描枪扫描工牌登录</button>
</view>
<view v-else class="panel">
@ -71,6 +77,10 @@
</template>
<script>
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'
export default {
data() {
return {
@ -78,21 +88,51 @@ export default {
showPassword: false,
focusedField: '',
scanLoading: false,
scanBadgeSubmitting: false,
scanBadgeLastSubmittedCode: '',
scanBadgeLastSubmittedAt: 0,
scanClientCode: '',
scanGlobalKeyBuffer: '',
scanGlobalKeyTimer: null,
scanGlobalKeyListener: null,
scanCode: '',
scanQrImage: '',
scanMiniProgramUrl: '',
scanStatus: '等待扫码...',
scanPollTimer: null,
scanPollInterval: 1500,
scanRequestTimeout: 120000,
form: {
username: '',
password: '',
},
}
},
onUnload() {
this.stopScanLongPolling()
this.removeGlobalKeyboardListener()
this.scanBadgeSubmitting = false
},
onReady() {
this.attachGlobalKeyboardListener()
},
onShow() {
this.attachGlobalKeyboardListener()
},
onHide() {
this.removeGlobalKeyboardListener()
},
methods: {
switchMode(mode) {
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()
}
},
togglePassword() {
@ -102,46 +142,183 @@ export default {
this.form[field] = ''
this.focusedField = field
},
clearGlobalKeyTimer() {
if (this.scanGlobalKeyTimer) {
clearTimeout(this.scanGlobalKeyTimer)
this.scanGlobalKeyTimer = null
}
},
flushGlobalScanBuffer() {
const code = (this.scanGlobalKeyBuffer || '').trim()
this.scanGlobalKeyBuffer = ''
this.clearGlobalKeyTimer()
if (!code) {
return
}
this.handleScanBadgeLogin(code)
},
attachGlobalKeyboardListener() {
if (this.scanGlobalKeyListener) {
return
}
if (typeof window === 'undefined' || typeof document === 'undefined') {
return
}
this.scanGlobalKeyListener = (event) => {
if (this.loginMode !== 'account' && this.loginMode !== 'scan') {
return
}
const key = event?.key || ''
const isPrintable = key.length === 1
const isEnter = key === 'Enter'
// 当前在账号输入框/密码输入框时,不接管正常键入,避免影响手工输入
if (this.focusedField === 'username' || this.focusedField === 'password') {
return
}
if (isPrintable) {
this.scanGlobalKeyBuffer += key
this.clearGlobalKeyTimer()
this.scanGlobalKeyTimer = setTimeout(() => {
this.flushGlobalScanBuffer()
}, 80)
return
}
if (isEnter) {
event.preventDefault?.()
this.flushGlobalScanBuffer()
}
}
window.addEventListener('keydown', this.scanGlobalKeyListener, true)
},
removeGlobalKeyboardListener() {
this.clearGlobalKeyTimer()
this.scanGlobalKeyBuffer = ''
if (typeof window === 'undefined' || typeof document === 'undefined') {
this.scanGlobalKeyListener = null
return
}
if (this.scanGlobalKeyListener) {
window.removeEventListener('keydown', this.scanGlobalKeyListener, true)
this.scanGlobalKeyListener = null
}
},
handleScanCodeInput() {
// no-op: kept for compatibility if template bindings remain elsewhere
},
handleFocus(field) {
this.focusedField = field
},
handleBlur() {
this.focusedField = ''
},
handleScanLogin() {
buildMiniProgramUrlByCode(code) {
if (!code) {
return ''
}
return loginLinkTemplate.replace('${code}', code)
},
buildQrImageUrlByPageUrl(pageUrl) {
if (!pageUrl) {
return ''
}
return `${qrBaseUrl}${encodeURIComponent(pageUrl)}`
},
buildFallbackQrImageUrlByPageUrl(pageUrl) {
if (!pageUrl) {
return ''
}
return `${qrFallbackBaseUrl}${encodeURIComponent(pageUrl)}`
},
handleScanImageError() {
const fallbackUrl = this.buildFallbackQrImageUrlByPageUrl(this.scanMiniProgramUrl)
if (fallbackUrl && this.scanQrImage !== fallbackUrl) {
this.scanQrImage = fallbackUrl
this.scanStatus = '主二维码通道受限,已切换备用通道'
return
}
this.scanStatus = '二维码加载失败,请重试'
},
startScanLongPolling() {
if (!this.scanClientCode) {
return
}
this.stopScanLongPolling()
this.scanPollTimer = setTimeout(() => {
this.handleScanLogin(true)
}, this.scanPollInterval)
},
stopScanLongPolling() {
if (this.scanPollTimer) {
clearTimeout(this.scanPollTimer)
this.scanPollTimer = null
}
},
handleScanLogin(isPolling = false) {
if (this.scanLoading) {
return
}
if (!isPolling) {
this.stopScanLongPolling()
}
this.scanLoading = true
this.scanStatus = '正在获取二维码...'
if (!isPolling) {
this.scanStatus = '正在获取二维码...'
}
const query = [`method=user.getLoginWechatCode`, `v=1.0`, `timestamp=${Date.now()}`]
if (this.scanClientCode) {
query.push(`clientCode=${encodeURIComponent(this.scanClientCode)}`)
}
const requestData = this.scanClientCode ? { clientCode: this.scanClientCode } : {}
uni.request({
url: `http://192.168.11.56:6060/system?${query.join('&')}`,
method: 'POST',
timeout: this.scanRequestTimeout,
header: {
accept: 'application/json, text/plain, */*',
'content-type': 'application/json',
platform: 'workerstation',
project: 'warehouse',
},
data: this.scanClientCode ? { clientCode: this.scanClientCode } : {},
data: requestData,
success: (res) => {
const body = res?.data || {}
const payload = body?.data || body?.result || body
const clientCode = payload?.clientCode || body?.clientCode || ''
const qrImage = payload?.qrCodeUrl || payload?.qrcodeUrl || payload?.codeUrl || payload?.wechatCodeUrl || payload?.image || ''
const clientCode =
payload?.clientCode ||
payload?.client_code ||
payload?.client?.code ||
body?.clientCode ||
body?.client_code ||
''
const qrImageFromApi = payload?.qrCodeUrl || payload?.qrcodeUrl || payload?.codeUrl || payload?.wechatCodeUrl || payload?.image || ''
const loginCode = payload?.loginWechatCode || payload?.wechatCode || payload?.qrCode || payload?.code || ''
const miniProgramPageUrl = this.buildMiniProgramUrlByCode(loginCode)
const qrImageFromTemplate = this.buildQrImageUrlByPageUrl(miniProgramPageUrl)
if (clientCode) {
this.scanClientCode = clientCode
}
this.scanQrImage = qrImage || ''
this.scanMiniProgramUrl = miniProgramPageUrl || ''
this.scanQrImage = qrImageFromTemplate || qrImageFromApi || ''
this.scanCode = loginCode || ''
if (this.scanQrImage || this.scanCode) {
@ -150,11 +327,86 @@ export default {
this.scanStatus = body?.message || body?.msg || '二维码获取失败'
}
},
fail: () => {
fail: (err) => {
const errMsg = err?.errMsg || ''
if (errMsg.includes('timeout') || errMsg.includes('abort') || errMsg.includes('cancel')) {
this.scanStatus = '请求超时,正在继续轮询...'
return
}
this.scanStatus = '网络异常,请稍后重试'
},
complete: () => {
this.scanLoading = false
if (this.loginMode === 'scan' && this.scanClientCode) {
this.startScanLongPolling()
}
},
})
},
handleScanBadgeLogin(codeParam = '') {
const code = (codeParam || '').trim()
if (!code) {
return
}
const now = Date.now()
if (
this.scanBadgeSubmitting ||
(this.scanBadgeLastSubmittedCode === code && now - this.scanBadgeLastSubmittedAt < 1200)
) {
return
}
this.scanBadgeSubmitting = true
this.scanBadgeLastSubmittedCode = code
this.scanBadgeLastSubmittedAt = now
uni.request({
url: `http://192.168.11.56:6060/system?method=user.loginByCode&v=1.0&timestamp=${Date.now()}`,
method: 'POST',
header: {
accept: 'application/json, text/plain, */*',
'content-type': 'application/json',
platform: 'workerstation',
project: 'warehouse',
},
data: {
code,
},
success: (res) => {
const body = res?.data || {}
const token = body?.token || body?.data?.token || body?.result?.token
if (token) {
uni.setStorageSync('token', token)
uni.showToast({
title: '登录成功',
icon: 'success',
})
setTimeout(() => {
uni.reLaunch({
url: '/pages/index/index',
})
}, 300)
return
}
this.focusScanInput()
uni.showToast({
title: body?.msg || body?.message || '工牌登录失败',
icon: 'none',
})
},
fail: () => {
this.focusScanInput()
uni.showToast({
title: '网络异常,请稍后重试',
icon: 'none',
})
},
complete: () => {
this.scanBadgeSubmitting = false
},
})
},
@ -287,6 +539,13 @@ export default {
color: #666;
}
.scan-hint {
display: block;
font-size: 26rpx;
color: #2fbf71;
margin-bottom: 10rpx;
}
.switch-tabs {
display: flex;
background-color: #f5f7fb;
@ -347,6 +606,15 @@ export default {
margin-bottom: 16rpx;
}
.scan-hidden-input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
left: -9999px;
top: -9999px;
}
.scan-placeholder {
color: #999;
font-size: 28rpx;

BIN
src/static/images/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB