微信授权登录功能开发联调

This commit is contained in:
冯辉
2025-10-20 11:43:44 +08:00
parent ae91ded327
commit d9c1f83693
5 changed files with 784 additions and 41 deletions

34
App.vue
View File

@@ -11,24 +11,26 @@ onLaunch((options) => {
useDictStore().getDictData();
// uni.hideTabBar();
// 登录
let token = uni.getStorageSync('token') || ''; // 同步获取 缓存信息
if (token) {
useUserStore()
.loginSetToken(token)
.then(() => {
$api.msg('登录成功');
})
.catch(() => {
uni.redirectTo({
url: '/pages/login/login',
// 尝试从缓存恢复用户信息
const restored = useUserStore().restoreUserInfo();
if (restored) {
// 如果成功恢复用户信息验证token是否有效
let token = uni.getStorageSync('token') || '';
if (token) {
useUserStore()
.loginSetToken(token)
.then(() => {
console.log('用户登录状态已恢复');
})
.catch(() => {
// token无效清除缓存不跳转登录页
console.log('token已过期需要重新登录');
useUserStore().logOut(false);
});
});
} else {
// uni.redirectTo({
// url: '/pages/login/login',
// });
}
}
// 不再强制跳转到登录页,而是在需要登录时弹出授权弹窗
});
onMounted(() => {