feat: 新增安全导航工具并全量替换原生路由调用,解决页面连击、栈超限与白屏问题
新增全局安全导航工具 safeNavigateTo/safeRedirectTo/safeNavigateBack,实现防连击锁、页面栈超限降级、无历史页平滑重定向逻辑。全量替换所有页面及组件中的 uni 原生路由调用,包括充值页、商家详情页、优惠券页、收藏页、订单页、会员中心、首页、登录页等多个页面,同时更新 tabbar 跳转逻辑杜绝 reLaunch 白屏,新增路由工具单元测试,优化分类页空数据兜底提示,提升页面跳转稳定性与用户体验。
这个提交包含在:
@@ -32,6 +32,7 @@
|
||||
|
||||
<script>
|
||||
import { getNavMetrics } from '@/common/utils/navbar.js'
|
||||
import { safeNavigateBack } from '@/common/utils/nav.js'
|
||||
|
||||
export default {
|
||||
name: 'FcNavbar',
|
||||
@@ -86,58 +87,13 @@ export default {
|
||||
},
|
||||
/**
|
||||
* 点击返回上一页
|
||||
* 优先 navigateBack;若无上一页历史(如分享直达、热重载、单页直开)或返回失败,则立即执行安全降级,绝不卡死在原地
|
||||
* 统一通过 safeNavigateBack 执行(页面栈 > 1 正常回退,无页面栈平滑重定向兜底)
|
||||
* 彻底移除 switchTab 报错与 reLaunch 重建
|
||||
*/
|
||||
onBack() {
|
||||
this.$emit('back')
|
||||
let pages = []
|
||||
try {
|
||||
pages = (typeof getCurrentPages === 'function') ? getCurrentPages() : []
|
||||
} catch (e) {
|
||||
pages = []
|
||||
}
|
||||
|
||||
// 页面栈深度 > 1 时,执行系统级返回
|
||||
if (pages && pages.length > 1) {
|
||||
uni.navigateBack({
|
||||
delta: 1,
|
||||
fail: () => {
|
||||
this.fallbackNavigation()
|
||||
},
|
||||
})
|
||||
} else {
|
||||
// 页面栈仅有 1 页(分享直达、编译刷新),直接执行降级跳转,避免微信底层静默丢弃导致卡死原地
|
||||
this.fallbackNavigation()
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 导航失败或无页面栈时的降级逻辑(优先降级至指定 fallbackUrl,否则平滑回到同城首页)
|
||||
*/
|
||||
fallbackNavigation() {
|
||||
const target = this.fallbackUrl || '/pages/index/index'
|
||||
const tabPages = ['/pages/index/index', '/pages/category/index', '/pages/post/create', '/pages/message/index', '/pages/mine/index']
|
||||
const isTabBar = tabPages.includes(target)
|
||||
|
||||
if (isTabBar) {
|
||||
uni.switchTab({
|
||||
url: target,
|
||||
fail: () => {
|
||||
uni.reLaunch({ url: target })
|
||||
},
|
||||
})
|
||||
} else {
|
||||
uni.redirectTo({
|
||||
url: target,
|
||||
fail: () => {
|
||||
uni.reLaunch({
|
||||
url: target,
|
||||
fail: () => {
|
||||
uni.switchTab({ url: '/pages/index/index' })
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
safeNavigateBack(target, 1)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@
|
||||
<view
|
||||
class="fc-post-card"
|
||||
:class="{ 'fc-post-card--compact': compact }"
|
||||
hover-class="fc-post-card--active"
|
||||
:hover-stay-time="80"
|
||||
@click="$emit('click', post)"
|
||||
>
|
||||
<!-- 缩略图:有真实实拍图展示实拍大图;无图时展示分类图标微名片(分类名统一由下方分类标签承载,避免重复) -->
|
||||
<view class="fc-post-card__thumb" :class="{ 'fc-post-card__thumb--badge': !thumbUrl }">
|
||||
<image v-if="thumbUrl" class="fc-post-card__thumb-img" :src="thumbUrl" mode="aspectFill" />
|
||||
<image v-if="thumbUrl" class="fc-post-card__thumb-img" :src="thumbUrl" mode="aspectFill" :lazy-load="true" />
|
||||
<view v-else class="fc-post-card__deco-badge">
|
||||
<view class="fc-post-card__deco-halo">
|
||||
<image class="fc-post-card__deco-icon" :src="categoryIconUrl(post.categoryId)" mode="aspectFit" />
|
||||
<image class="fc-post-card__deco-icon" :src="categoryIconUrl(post.categoryId)" mode="aspectFit" :lazy-load="true" />
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -132,8 +134,10 @@ export default {
|
||||
&:hover {
|
||||
box-shadow: 0 8rpx 24rpx rgba(4, 48, 30, 0.1);
|
||||
}
|
||||
&:active {
|
||||
&:active,
|
||||
&--active {
|
||||
transform: scale(0.985);
|
||||
opacity: 0.92;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,11 @@
|
||||
import { iconUrl } from '@/common/store/ui.js'
|
||||
import messageApi from '@/common/api/message.js'
|
||||
import { useUserStore } from '@/common/store/index.js'
|
||||
import { safeNavigateTo, safeRedirectTo } from '@/common/utils/nav.js'
|
||||
|
||||
// 未读消息请求节流时间戳(毫秒)
|
||||
let lastFetchUnreadAt = 0
|
||||
let cachedUnreadCount = 0
|
||||
|
||||
export default {
|
||||
name: 'FcTabbar',
|
||||
@@ -98,26 +103,39 @@ export default {
|
||||
*/
|
||||
iconUrl,
|
||||
/**
|
||||
* 静默拉取当前用户未读消息总数
|
||||
* 静默拉取当前用户未读消息总数(带 10 秒防抖节流,避免高频切页重复刷接口)
|
||||
* @param {boolean} force 是否强制跳过缓存刷新
|
||||
*/
|
||||
async fetchUnread() {
|
||||
async fetchUnread(force = false) {
|
||||
let logged = false
|
||||
try {
|
||||
logged = !!useUserStore().isLogin.value
|
||||
} catch (e) {}
|
||||
if (!logged) {
|
||||
this.unreadCount = 0
|
||||
cachedUnreadCount = 0
|
||||
return
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
if (!force && now - lastFetchUnreadAt < 10000) {
|
||||
this.unreadCount = cachedUnreadCount
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await messageApi.unreadCount()
|
||||
this.unreadCount = Number((res && res.count) || 0)
|
||||
const count = Number((res && res.count) || 0)
|
||||
this.unreadCount = count
|
||||
cachedUnreadCount = count
|
||||
lastFetchUnreadAt = now
|
||||
} catch (e) {
|
||||
this.unreadCount = 0
|
||||
this.unreadCount = cachedUnreadCount || 0
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 切换底部导航目标页面
|
||||
* 杜绝 reLaunch 全局冷重启白屏,常驻 Tab 统一采用 safeRedirectTo 平滑切换
|
||||
* @param {Object} item 目标 Tab 配置对象
|
||||
* @param {number} idx 目标 Tab 索引
|
||||
*/
|
||||
@@ -126,22 +144,12 @@ export default {
|
||||
|
||||
// 中间突出加号(发布):作为独立模态表单页推入,保留前序页面栈,支持原路返回
|
||||
if (item.primary) {
|
||||
uni.navigateTo({ url: item.path })
|
||||
safeNavigateTo(item.path)
|
||||
return
|
||||
}
|
||||
|
||||
// 其余常驻 Tab 页面平滑切换:H5 环境优先 redirectTo 避免 reLaunch 闪屏
|
||||
// #ifdef H5
|
||||
uni.redirectTo({
|
||||
url: item.path,
|
||||
fail: () => {
|
||||
uni.reLaunch({ url: item.path })
|
||||
},
|
||||
})
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.reLaunch({ url: item.path })
|
||||
// #endif
|
||||
// 其余常驻 Tab 页面平滑切换:杜绝 reLaunch 造成的整个小程序销毁与白屏
|
||||
safeRedirectTo(item.path)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户