修复bug

This commit is contained in:
zhouyanli
2026-08-01 18:10:56 +08:00
parent f74fb33e11
commit 2010c63d70
7 changed files with 107 additions and 47 deletions

View File

@@ -183,7 +183,42 @@
const noticeKey = ref(0)
const noticeId = ref('')
const lastUserId = ref('')
const dataLoaded = ref(false)
const pageReady = ref(false)
// ======= 首页数据缓存(冷启动秒开) =======
const HOME_CACHE_KEY = 'home_cache'
function loadHomeCache() {
try {
const raw = uni.getStorageSync(HOME_CACHE_KEY)
if (!raw) return false
const c = JSON.parse(raw)
if (c.community && c.community !== '请绑定房屋') currentCommunity.value = c.community
if (c.villageId) {
currentVillageId.value = c.villageId
uni.setStorageSync('currentVillageId', c.villageId)
}
if (c.bannerList && c.bannerList.length) bannerList.value = c.bannerList
if (c.bannerRaw && c.bannerRaw.length) bannerRawData.value = c.bannerRaw
if (c.notice && c.notice !== '暂无公告') noticeText.value = c.notice
if (c.noticeId) noticeId.value = c.noticeId
if (c.activityList && c.activityList.length) activityList.value = c.activityList
bannerSwiperKey.value++
return true
} catch (e) { return false }
}
function saveHomeCache() {
try {
uni.setStorageSync(HOME_CACHE_KEY, JSON.stringify({
community: currentCommunity.value,
villageId: currentVillageId.value,
bannerList: bannerList.value,
bannerRaw: bannerRawData.value,
notice: noticeText.value,
noticeId: noticeId.value,
activityList: activityList.value
}))
} catch (e) {}
}
// =======方法=======
const formatDate = (date) => {
@@ -207,6 +242,7 @@
bannerList.value = newList
await nextTick()
bannerSwiperKey.value++
saveHomeCache()
}
}
} catch (err) {
@@ -235,6 +271,7 @@
getCurrentVillage().then(res => {
if (res.code === 200 && res.data && res.data.villageId) {
currentCommunity.value = res.data.villageName || '未命名小区'
saveHomeCache()
currentVillageId.value = res.data.villageId
uni.setStorageSync('currentVillageId', res.data.villageId)
} else {
@@ -256,7 +293,7 @@
if (res.code === 200) {
if (res.data) {
noticeText.value = res.data.noticeTitle || ''
noticeId.value = res.data.noticeId || ''
noticeId.value = res.data.noticeId || ''; saveHomeCache()
} else {
noticeText.value = '暂无公告'
noticeId.value = ''
@@ -270,20 +307,19 @@
const refreshAllData = async () => {
const token = uni.getStorageSync('token') || ''
dataLoaded.value = true
const needReload = uni.getStorageSync('needReloadUserInfo')
if (token && needReload) {
try {
const res = await getReloadUserInfo()
uni.removeStorageSync('needReloadUserInfo')
getReloadUserInfo().then(res => {
if (res.code === 200 && res.data) {
if (res.data.userId) uni.setStorageSync('userId', res.data.userId)
const reloadedVillageId = res.data.currentVillageId || 0
uni.setStorageSync('currentVillageId', reloadedVillageId)
}
} catch (err) {
}).catch(err => {
console.error('重新加载用户信息失败', err)
} finally {
uni.removeStorageSync('needReloadUserInfo')
}
})
}
getCurrentCommunityData()
getNoticeData()
@@ -314,16 +350,34 @@
onShow(() => {
const currentUserId = uni.getStorageSync('userId') || ''
if (lastUserId.value && lastUserId.value !== currentUserId) {
const userIdChanged = lastUserId.value && lastUserId.value !== currentUserId
refreshing.value = false
if (userIdChanged) {
resetPageData()
refreshAllData()
} else {
} else if (!dataLoaded.value) {
// 首次进入且之前因无 token 跳过了 API现在有 token 则补拉
refreshAllData()
}
lastUserId.value = currentUserId
})
onLoad(() => {
// 新用户(未看过引导页且未登录)跳转引导页
var hasShowGuide = uni.getStorageSync('hasShowGuide')
var token = uni.getStorageSync('token')
if (!hasShowGuide && !token) {
uni.reLaunch({ url: '/pages/navigation/navigation' })
return
}
// 优先展示缓存数据,实现冷启动秒开
loadHomeCache()
// 关闭原生 splash避免 WebView 初始化期间一直转圈
// #ifdef APP-PLUS
try { plus.navigator.closeSplashscreen() } catch (e) {}
// #endif
// 后台静默刷新最新数据
refreshAllData()
uni.$on('confirm', slelctHomeData)
lastUserId.value = uni.getStorageSync('userId') || ''
})
@@ -358,7 +412,7 @@
try {
const res = await getActivitylatest({ villageId: uni.getStorageSync('currentVillageId') })
if (res.code === 200) {
activityList.value = res.data || []
activityList.value = res.data || []; saveHomeCache()
} else {
activityList.value = []
}
@@ -634,6 +688,9 @@
color: #333;
margin-bottom: 12px;
line-height: 1.4;
white-space: normal;
word-break: break-all;
overflow-wrap: break-word;
}
.activity-time {

View File

@@ -148,8 +148,16 @@
complete: () => {}
});
}
let lastUserIdMy = ''
onShow(() =>{
getUserInfoData()
const token = uni.getStorageSync('token')
if (!token) return
const currentUserId = uni.getStorageSync('userId') || ''
// 仅登录状态变化时刷新,切后台回来直接用缓存
if (currentUserId !== lastUserIdMy) {
lastUserIdMy = currentUserId
getUserInfoData()
}
})
</script>

View File

@@ -73,30 +73,17 @@
<script setup>
import { ref, onMounted } from 'vue';
// 守卫:已登录或已看过引导页的用户不展示引导页,直接跳转
const hasShowGuide = uni.getStorageSync('hasShowGuide')
const tokenSave = uni.getStorageSync('token')
// 是否需要重定向(老用户跳过引导页)
const needRedirect = hasShowGuide || tokenSave
if (needRedirect) {
// 已登录或已看过引导页:直接进首页
uni.reLaunch({ url: '/pages/index/index' })
}
// ========== 响应式数据 ==========
// ========== 响应式数据 ==========
// 当前引导步骤0/1/2
const currentStep = ref(0);
const mode = ref("default")
// 防重复跳转锁
const isNavigating = ref(false)
// 控制是否展示引导内容(初始为 false避免重定向用户看到闪烁
// 控制是否展示引导内容
const showContent = ref(false)
// 页面挂载后延迟显示,让过渡更平滑
onMounted(() => {
// 需要重定向的用户不展示引导内容
if (needRedirect) return
setTimeout(() => {
showContent.value = true
}, 100)
@@ -110,16 +97,12 @@ const handleSwiperChange = (e) => {
// 点击按钮操作
const handleBtnClick = () => {
// 前两步:点击下一步切换
if (currentStep.value < 2) {
currentStep.value++;
} else {
// 防重复跳转:已触发过跳转则直接返回
if (isNavigating.value) return;
isNavigating.value = true;
// 记录引导已完成
uni.setStorageSync('hasShowGuide', true);
// 直接进入首页(未登录用户可正常浏览首页,需登录的功能由点击拦截处理)
uni.switchTab({
url: '/pages/index/index'
})