From 8ec1cf70b571e9136b1d7f29ebc779cfa3353d99 Mon Sep 17 00:00:00 2001 From: zm2000 <18386612143@163.com> Date: Wed, 25 Mar 2026 16:23:05 +0800 Subject: [PATCH] fix(login): streamline badge login flow and router exit handling --- core/router/index.js | 18 +++++++++++------- core/store/modules/user.js | 25 +++++++++++++++++++------ pages/login/index.vue | 25 ++++++++----------------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/core/router/index.js b/core/router/index.js index bf3a47b..f55d918 100644 --- a/core/router/index.js +++ b/core/router/index.js @@ -19,8 +19,6 @@ const router = createRouter({ }) => { switch (type) { case 3: // APP退出应用 - - case 2: // #ifdef APP-PLUS router.$lockStatus = false; uni.showModal({ @@ -34,6 +32,9 @@ const router = createRouter({ }); // #endif break; + case 2: + router.$lockStatus = false; + break; case 0: router.$lockStatus = false; break; @@ -88,14 +89,13 @@ router.beforeEach(async (to, from, next) => { } else { console.log("没有用户信息"); - await store.dispatch('getUserInfo'); - console.log("获取完成,去哪里",to) try { + await store.dispatch('getUserInfo'); + console.log("获取完成,去哪里",to) await store.dispatch('floatWindowManager/is_visible'); - // 关键修改:等待获取用户信息 - if (to.path === '/pages/index/index'){ + if (to.path === '/pages/index/index') { next(); - }else{ + } else { next({ path: to.fullPath, NAVTYPE: 'SwitchTab' @@ -103,6 +103,10 @@ router.beforeEach(async (to, from, next) => { } } catch (error) { await store.dispatch('logout'); + next({ + path: '/pages/login/index', + replace: true + }); } } } diff --git a/core/store/modules/user.js b/core/store/modules/user.js index d9d8499..4fea215 100644 --- a/core/store/modules/user.js +++ b/core/store/modules/user.js @@ -69,7 +69,8 @@ const actions = { }); dispatch('logout'); console.log("无权登录"); - return false; + reject({ msg: '无权登录' }); + return; } const user = data @@ -188,8 +189,8 @@ const actions = { }) }, loginByCode({ - commit, - state + state, + dispatch }, payload) { const code = String(payload?.code || payload?.uuid || '').trim(); @@ -203,7 +204,7 @@ const actions = { code, device: state.app.device, project: state.app.project - }).then(response => { + }).then(async response => { if (response && response.flag === 'failure') { reject({ msg: response.message || '工牌登录失败' }); return; @@ -217,12 +218,24 @@ const actions = { response?.data?.accessToken || response?.accessToken; - if (!token) { + const authToken = token || getToken(); + + if (!authToken) { reject({ msg: response?.message || '登录成功但未返回token' }); return; } - setToken(token); + if (token) { + setToken(token); + } + + try { + await dispatch('getUserInfo', authToken); + } catch (e) { + reject({ msg: e?.msg || e?.message || '用户信息加载失败' }); + return; + } + resolve(response); }).catch(error => { reject({ msg: error?.msg || error?.message || error?.data?.message || '工牌登录失败' }); diff --git a/pages/login/index.vue b/pages/login/index.vue index 59616c8..6e75cf6 100644 --- a/pages/login/index.vue +++ b/pages/login/index.vue @@ -33,21 +33,6 @@ - - - - - {{ currentDevice.deviceName }} - 设备ID: {{ currentDevice.deviceId }} - - - - @@ -98,7 +83,6 @@ import { setToken, getToken } from '@/core/utils/auth' -import { goHome } from '@/core/utils/auth-guard' export default { data() { @@ -128,6 +112,7 @@ export default { const storageBg = uni.getStorageSync("bg"); const bgUrl = storageBg || "/static/loginbg.jpg"; this.bgStyle = `--bg-url: url(${bgUrl})`; + this.scanning = true; uni.hideTabBar(); this.$store.dispatch('broadcast/setCallbacks', { onResult: this.handleBroadcastResult @@ -135,6 +120,7 @@ export default { this.$store.dispatch('broadcast/start'); }, onShow() { + this.scanning = true; this.$store.dispatch('broadcast/resume'); }, onHide() { @@ -276,8 +262,11 @@ export default { throw new Error('请先扫描有效工牌') } await this.$store.dispatch('loginByCode', { code }) - await goHome() + uni.switchTab({ url: '/pages/index/index' }) } catch (error) { + this.scanning = true; + this.currentDevice = null; + this.$store.dispatch('broadcast/resume'); uni.showToast({ title: error?.msg || error?.message || '工牌认证登录失败', icon: 'none' @@ -304,9 +293,11 @@ export default { // 自动登录 this.confirmLogin(); } catch (e) { + this.scanning = true; this.deviceCode = ''; this.scanUuid = ''; this.currentDevice = null; + this.$store.dispatch('broadcast/resume'); uni.showToast({ title: e?.message || '扫码数据不合法', icon: 'none' }); } },