解决微信小程序首页空白问题。

This commit is contained in:
cst61
2026-02-01 09:21:11 +08:00
parent 80d6d46182
commit cc01c4dc47
454 changed files with 404 additions and 62235 deletions

138
App.vue
View File

@@ -1,7 +1,14 @@
<template>
<view id="app">
<!-- 加载状态 -->
<view v-if="showLoading" class="loading-container">
<view class="loading-spinner">
<view class="spinner"></view>
<text class="loading-text">正在加载...</text>
</view>
</view>
<!-- 页面内容 -->
<router-view />
<router-view v-else />
</view>
</template>
@@ -19,7 +26,7 @@
// uni-agree 已在子包中,主包不再引用(避免跨包引用问题)
// import checkIsAgree from '@/pages-subpackage/uni-agree/utils/uni-agree.js';
import uniIdPageInit from '@/common/uni-id-pages-init.js';
import { store } from '@/common/store.js';
import { store, mutations } from '@/common/store.js';
export default {
globalData: {
@@ -31,6 +38,7 @@
},
data() {
return {
showLoading: true // 初始显示加载状态
};
},
onLaunch: async function() {
@@ -81,21 +89,41 @@
this.setupRouteInterceptor()
// #ifdef MP-WEIXIN
// 微信小程序:先快速检查登录状态,如果未登录则立即跳转,避免首页闪烁
// 延迟一小段时间确保页面栈已初始化
// 微信小程序:等待页面完全加载后再检查登录状态,避免扫码进入时空白页面
// 延迟检查,确保页面栈已初始化且页面已完全加载
setTimeout(() => {
if (!this.isLoggedIn()) {
this.redirectToLogin()
} else {
// 已登录,检查当前页面
this.checkCurrentPageLogin()
// 检查页面是否已完全加载
const checkPageLoaded = () => {
const pages = getCurrentPages()
if (pages.length > 0 && pages[pages.length - 1].route) {
// 页面已加载,检查登录状态
if (!this.isLoggedIn()) {
this.showLoading = false // 隐藏加载状态
this.redirectToLogin()
} else {
// 已登录,检查当前页面
this.checkCurrentPageLogin()
// 延迟隐藏加载状态,确保页面渲染完成
setTimeout(() => {
this.showLoading = false
}, 200)
}
} else {
// 页面还没加载完成,继续等待
setTimeout(checkPageLoaded, 100)
}
}
}, 300)
checkPageLoaded()
}, 500)
// #endif
// #ifndef MP-WEIXIN
// 其他平台:延迟检查登录状态
setTimeout(() => {
this.checkCurrentPageLogin()
// 延迟隐藏加载状态,确保页面渲染完成
setTimeout(() => {
this.showLoading = false
}, 200)
}, 100)
// #endif
@@ -122,6 +150,12 @@
},
onShow: function() {
console.log('App Show')
// 每次应用显示时同步用户信息解决store初始化时uni未准备好导致的问题
try {
mutations.syncUserInfoFromStorage();
} catch (e) {
console.warn('[App] 同步用户信息失败:', e);
}
// 每次应用显示时也检查当前页面是否需要登录
this.checkCurrentPageLogin()
},
@@ -265,6 +299,35 @@
},
/**
* 更新当前页面路径(用于更新 tabBar 等)
*/
updateCurrentPath() {
try {
const pages = getCurrentPages();
if (pages.length > 0) {
const currentPage = pages[pages.length - 1];
if (currentPage && currentPage.route) {
const currentPath = `/${currentPage.route}`;
// 更新 tabBar 的当前路径(如果存在)
try {
const app = getApp({ allowDefault: true });
if (app && app.globalData) {
const tabBar = app.globalData.tabBarInstance;
if (tabBar && typeof tabBar.updateCurrentPath === 'function') {
tabBar.updateCurrentPath(currentPath);
}
}
} catch (e) {
// 静默失败
}
}
}
} catch (e) {
// 静默失败,不影响应用启动
}
},
/**
* 检查当前页面是否需要登录
*/
@@ -276,11 +339,18 @@
this.globalData.roles = tokenData.roles;
this.globalData.roleName = tokenData.roleName || '';
// 通知自定义 tabBar 更新
const tabBar = getApp().globalData.tabBarInstance;
if (tabBar) {
tabBar.refreshRole();
// 通知自定义 tabBar 更新
try {
const app = getApp({ allowDefault: true });
if (app && app.globalData) {
const tabBar = app.globalData.tabBarInstance;
if (tabBar) {
tabBar.refreshRole();
}
}
} catch (e) {
// 静默失败
}
}
} catch (e) {
console.warn('[App] Failed to set global role info:', e);
@@ -337,4 +407,44 @@
<style>
/*每个页面公共css */
/* 加载状态样式 */
.loading-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #f8f8f8;
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.loading-spinner {
display: flex;
flex-direction: column;
align-items: center;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #e0e0e0;
border-top: 4px solid #007AFF;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 16px;
}
.loading-text {
color: #666;
font-size: 14px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>