解决微信小程序首页空白问题。
This commit is contained in:
@@ -63,12 +63,32 @@ function setupRequestInterceptor() {
|
||||
console.error('获取 tenantId 失败:', e)
|
||||
}
|
||||
|
||||
// 检查是否需要登录:如果不是登录接口且租户ID为空,则跳转到登录页
|
||||
// 检查是否需要登录:如果不是登录接口且租户ID为空
|
||||
// 只在页面完全加载后才跳转,避免扫码进入时页面空白
|
||||
if (!isLoginApi(url) && !tenantId) {
|
||||
console.warn('[Request Interceptor] 租户ID为空,跳转到登录页面')
|
||||
redirectToLogin()
|
||||
// 返回一个被拒绝的Promise,阻止请求继续执行
|
||||
return Promise.reject(new Error('未登录,请先登录'))
|
||||
// 获取当前页面栈,检查是否页面已完全加载
|
||||
const pages = getCurrentPages()
|
||||
const hasLoadedPages = pages.length > 0 && pages[pages.length - 1].route
|
||||
|
||||
// 如果页面已加载,检查是否在首页或登录相关页面
|
||||
if (hasLoadedPages) {
|
||||
const currentPage = pages[pages.length - 1]
|
||||
const currentRoute = '/' + currentPage.route
|
||||
|
||||
// 在首页或登录相关页面时,不立即跳转,让页面先加载
|
||||
const isHomePage = currentRoute.includes('/pages/furniture_reception/furniture_reception')
|
||||
const isLoginRelated = currentRoute.includes('/uni_modules/uni-id-pages/pages/login') ||
|
||||
currentRoute.includes('/uni_modules/uni-id-pages/pages/register') ||
|
||||
currentRoute.includes('/uni_modules/uni-id-pages/pages/retrieve')
|
||||
|
||||
if (!isHomePage && !isLoginRelated) {
|
||||
console.warn('[Request Interceptor] 租户ID为空,跳转到登录页面')
|
||||
redirectToLogin()
|
||||
// 返回一个被拒绝的Promise,阻止请求继续执行
|
||||
return Promise.reject(new Error('未登录,请先登录'))
|
||||
}
|
||||
}
|
||||
// 如果页面还没加载完成,不立即跳转,避免扫码进入时空白
|
||||
}
|
||||
|
||||
// 构建新的 header(保留原有的 header,避免覆盖)
|
||||
@@ -163,9 +183,16 @@ export default async function() {
|
||||
|
||||
// uniStarterConfig挂载到getApp().globalData.config
|
||||
setTimeout(() => {
|
||||
getApp({
|
||||
allowDefault: true
|
||||
}).globalData.config = uniStarterConfig;
|
||||
try {
|
||||
const app = getApp({
|
||||
allowDefault: true
|
||||
});
|
||||
if (app && app.globalData) {
|
||||
app.globalData.config = uniStarterConfig;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[appInit] 挂载配置失败:', e);
|
||||
}
|
||||
}, 1)
|
||||
|
||||
|
||||
|
||||
@@ -11,15 +11,17 @@ const config = {
|
||||
let hostUserInfo = null
|
||||
|
||||
function getHostUserInfo() {
|
||||
if (hostUserInfo === null) {
|
||||
// 检查 uni 是否可用
|
||||
if (typeof uni !== 'undefined' && uni.getStorageSync) {
|
||||
hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo') || {}
|
||||
} else {
|
||||
hostUserInfo = {}
|
||||
// 总是从存储中读取最新值,确保登录状态的实时性
|
||||
// 特别是在微信小程序中,页面跳转可能导致模块重新加载
|
||||
try {
|
||||
if (typeof uni !== 'undefined' && uni && typeof uni.getStorageSync === 'function') {
|
||||
const storedUserInfo = uni.getStorageSync('uni-id-pages-userInfo') || {};
|
||||
return storedUserInfo;
|
||||
}
|
||||
} catch (e) {
|
||||
// 在模块初始化时,uni可能还未完全初始化,静默失败
|
||||
}
|
||||
return hostUserInfo
|
||||
return {};
|
||||
}
|
||||
|
||||
const data = {
|
||||
@@ -48,14 +50,34 @@ export const mutations = {
|
||||
// #endif
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 从本地存储中重新加载用户信息到store
|
||||
* 用于解决store初始化时uni未准备好导致的问题
|
||||
*/
|
||||
syncUserInfoFromStorage() {
|
||||
try {
|
||||
if (typeof uni !== 'undefined' && uni && typeof uni.getStorageSync === 'function') {
|
||||
const storedUserInfo = uni.getStorageSync('uni-id-pages-userInfo') || {};
|
||||
if (Object.keys(storedUserInfo).length > 0) {
|
||||
this.setUserInfo(storedUserInfo, { cover: true });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 静默失败
|
||||
}
|
||||
return false;
|
||||
},
|
||||
setUserInfo(data, {cover}={cover:false}) {
|
||||
// console.log('set-userInfo', data);
|
||||
let userInfo = cover?data:Object.assign(store.userInfo,data)
|
||||
store.userInfo = Object.assign({},userInfo)
|
||||
store.hasLogin = Object.keys(store.userInfo).length != 0
|
||||
// console.log('store.userInfo', store.userInfo);
|
||||
if (typeof uni !== 'undefined' && uni.setStorageSync) {
|
||||
uni.setStorageSync('uni-id-pages-userInfo', store.userInfo)
|
||||
if (typeof uni !== 'undefined' && uni && typeof uni.setStorageSync === 'function') {
|
||||
try {
|
||||
uni.setStorageSync('uni-id-pages-userInfo', store.userInfo)
|
||||
} catch (e) {
|
||||
// 静默失败
|
||||
}
|
||||
}
|
||||
return data
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user