新增扫描工牌登录
This commit is contained in:
parent
705c6f3854
commit
c8a229fb32
@ -2,13 +2,14 @@
|
|||||||
<view class="page">
|
<view class="page">
|
||||||
<view class="login-card">
|
<view class="login-card">
|
||||||
<view class="visual-side">
|
<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>
|
<text class="visual-desc">仓库管理系统工作台</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="form-side">
|
<view class="form-side">
|
||||||
<view class="header">
|
<view class="header">
|
||||||
<text class="title">登录</text>
|
<text class="title">登录</text>
|
||||||
|
<text class="scan-hint">支持扫描工牌登录</text>
|
||||||
<text class="subtitle">欢迎回来,请登录您的账户</text>
|
<text class="subtitle">欢迎回来,请登录您的账户</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
@ -24,11 +25,16 @@
|
|||||||
<view v-if="loginMode === 'scan'" class="panel">
|
<view v-if="loginMode === 'scan'" class="panel">
|
||||||
<text class="scan-tip-top">打开微信,扫描二维码登录</text>
|
<text class="scan-tip-top">打开微信,扫描二维码登录</text>
|
||||||
<view class="scan-box">
|
<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>
|
<text v-else class="scan-placeholder">{{ scanCode || '二维码区域' }}</text>
|
||||||
</view>
|
</view>
|
||||||
<text class="scan-tip-bottom">{{ scanStatus }}</text>
|
<text class="scan-tip-bottom">{{ scanStatus }}</text>
|
||||||
<button class="primary-btn scan-login-btn" @click="handleScanLogin">扫描枪扫描工牌登录</button>
|
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view v-else class="panel">
|
<view v-else class="panel">
|
||||||
@ -71,6 +77,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<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 {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -78,21 +88,51 @@ export default {
|
|||||||
showPassword: false,
|
showPassword: false,
|
||||||
focusedField: '',
|
focusedField: '',
|
||||||
scanLoading: false,
|
scanLoading: false,
|
||||||
|
scanBadgeSubmitting: false,
|
||||||
|
scanBadgeLastSubmittedCode: '',
|
||||||
|
scanBadgeLastSubmittedAt: 0,
|
||||||
scanClientCode: '',
|
scanClientCode: '',
|
||||||
|
scanGlobalKeyBuffer: '',
|
||||||
|
scanGlobalKeyTimer: null,
|
||||||
|
scanGlobalKeyListener: null,
|
||||||
scanCode: '',
|
scanCode: '',
|
||||||
scanQrImage: '',
|
scanQrImage: '',
|
||||||
|
scanMiniProgramUrl: '',
|
||||||
scanStatus: '等待扫码...',
|
scanStatus: '等待扫码...',
|
||||||
|
scanPollTimer: null,
|
||||||
|
scanPollInterval: 1500,
|
||||||
|
scanRequestTimeout: 120000,
|
||||||
form: {
|
form: {
|
||||||
username: '',
|
username: '',
|
||||||
password: '',
|
password: '',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onUnload() {
|
||||||
|
this.stopScanLongPolling()
|
||||||
|
this.removeGlobalKeyboardListener()
|
||||||
|
this.scanBadgeSubmitting = false
|
||||||
|
},
|
||||||
|
onReady() {
|
||||||
|
this.attachGlobalKeyboardListener()
|
||||||
|
},
|
||||||
|
onShow() {
|
||||||
|
this.attachGlobalKeyboardListener()
|
||||||
|
},
|
||||||
|
onHide() {
|
||||||
|
this.removeGlobalKeyboardListener()
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
switchMode(mode) {
|
switchMode(mode) {
|
||||||
this.loginMode = mode
|
this.loginMode = mode
|
||||||
|
if (mode !== 'scan') {
|
||||||
|
this.stopScanLongPolling()
|
||||||
|
this.scanBadgeSubmitting = false
|
||||||
|
}
|
||||||
if (mode === 'scan' && !this.scanCode && !this.scanQrImage) {
|
if (mode === 'scan' && !this.scanCode && !this.scanQrImage) {
|
||||||
this.handleScanLogin()
|
this.handleScanLogin()
|
||||||
|
} else if (mode === 'scan' && this.scanClientCode) {
|
||||||
|
this.startScanLongPolling()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
togglePassword() {
|
togglePassword() {
|
||||||
@ -102,46 +142,183 @@ export default {
|
|||||||
this.form[field] = ''
|
this.form[field] = ''
|
||||||
this.focusedField = 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) {
|
handleFocus(field) {
|
||||||
this.focusedField = field
|
this.focusedField = field
|
||||||
},
|
},
|
||||||
handleBlur() {
|
handleBlur() {
|
||||||
this.focusedField = ''
|
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) {
|
if (this.scanLoading) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isPolling) {
|
||||||
|
this.stopScanLongPolling()
|
||||||
|
}
|
||||||
|
|
||||||
this.scanLoading = true
|
this.scanLoading = true
|
||||||
this.scanStatus = '正在获取二维码...'
|
if (!isPolling) {
|
||||||
|
this.scanStatus = '正在获取二维码...'
|
||||||
|
}
|
||||||
|
|
||||||
const query = [`method=user.getLoginWechatCode`, `v=1.0`, `timestamp=${Date.now()}`]
|
const query = [`method=user.getLoginWechatCode`, `v=1.0`, `timestamp=${Date.now()}`]
|
||||||
if (this.scanClientCode) {
|
const requestData = this.scanClientCode ? { clientCode: this.scanClientCode } : {}
|
||||||
query.push(`clientCode=${encodeURIComponent(this.scanClientCode)}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
uni.request({
|
uni.request({
|
||||||
url: `http://192.168.11.56:6060/system?${query.join('&')}`,
|
url: `http://192.168.11.56:6060/system?${query.join('&')}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
timeout: this.scanRequestTimeout,
|
||||||
header: {
|
header: {
|
||||||
|
accept: 'application/json, text/plain, */*',
|
||||||
|
'content-type': 'application/json',
|
||||||
platform: 'workerstation',
|
platform: 'workerstation',
|
||||||
project: 'warehouse',
|
project: 'warehouse',
|
||||||
},
|
},
|
||||||
data: this.scanClientCode ? { clientCode: this.scanClientCode } : {},
|
data: requestData,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
const body = res?.data || {}
|
const body = res?.data || {}
|
||||||
const payload = body?.data || body?.result || body
|
const payload = body?.data || body?.result || body
|
||||||
|
|
||||||
const clientCode = payload?.clientCode || body?.clientCode || ''
|
const clientCode =
|
||||||
const qrImage = payload?.qrCodeUrl || payload?.qrcodeUrl || payload?.codeUrl || payload?.wechatCodeUrl || payload?.image || ''
|
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 loginCode = payload?.loginWechatCode || payload?.wechatCode || payload?.qrCode || payload?.code || ''
|
||||||
|
const miniProgramPageUrl = this.buildMiniProgramUrlByCode(loginCode)
|
||||||
|
const qrImageFromTemplate = this.buildQrImageUrlByPageUrl(miniProgramPageUrl)
|
||||||
|
|
||||||
if (clientCode) {
|
if (clientCode) {
|
||||||
this.scanClientCode = clientCode
|
this.scanClientCode = clientCode
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scanQrImage = qrImage || ''
|
this.scanMiniProgramUrl = miniProgramPageUrl || ''
|
||||||
|
this.scanQrImage = qrImageFromTemplate || qrImageFromApi || ''
|
||||||
this.scanCode = loginCode || ''
|
this.scanCode = loginCode || ''
|
||||||
|
|
||||||
if (this.scanQrImage || this.scanCode) {
|
if (this.scanQrImage || this.scanCode) {
|
||||||
@ -150,11 +327,86 @@ export default {
|
|||||||
this.scanStatus = body?.message || body?.msg || '二维码获取失败'
|
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 = '网络异常,请稍后重试'
|
this.scanStatus = '网络异常,请稍后重试'
|
||||||
},
|
},
|
||||||
complete: () => {
|
complete: () => {
|
||||||
this.scanLoading = false
|
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×tamp=${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;
|
color: #666;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.scan-hint {
|
||||||
|
display: block;
|
||||||
|
font-size: 26rpx;
|
||||||
|
color: #2fbf71;
|
||||||
|
margin-bottom: 10rpx;
|
||||||
|
}
|
||||||
|
|
||||||
.switch-tabs {
|
.switch-tabs {
|
||||||
display: flex;
|
display: flex;
|
||||||
background-color: #f5f7fb;
|
background-color: #f5f7fb;
|
||||||
@ -347,6 +606,15 @@ export default {
|
|||||||
margin-bottom: 16rpx;
|
margin-bottom: 16rpx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.scan-hidden-input {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
opacity: 0;
|
||||||
|
left: -9999px;
|
||||||
|
top: -9999px;
|
||||||
|
}
|
||||||
|
|
||||||
.scan-placeholder {
|
.scan-placeholder {
|
||||||
color: #999;
|
color: #999;
|
||||||
font-size: 28rpx;
|
font-size: 28rpx;
|
||||||
|
|||||||
BIN
src/static/images/1.png
Normal file
BIN
src/static/images/1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
Loading…
x
Reference in New Issue
Block a user