fix(login): streamline badge login flow and router exit handling

This commit is contained in:
zm2000 2026-03-25 16:23:05 +08:00
parent 992d556083
commit 8ec1cf70b5
3 changed files with 38 additions and 30 deletions

View File

@ -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
});
}
}
}

View File

@ -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 || '工牌登录失败' });

View File

@ -33,21 +33,6 @@
</view>
</view>
<!-- 设备状态显示 -->
<view class="device-status" v-if="currentDevice">
<view class="status-card">
<view class="device-info">
<text class="device-name">{{ currentDevice.deviceName }}</text>
<text class="device-id">设备ID: {{ currentDevice.deviceId }}</text>
</view>
<u-button
type="primary"
text="确认登录"
size="small"
@click="confirmLogin"
></u-button>
</view>
</view>
</view>
<!-- 二维码登录弹窗 -->
@ -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' });
}
},