文件
fenleixinxi/uniapp/components/fc-navbar/fc-navbar.vue
MiAn 25a3a3d52f feat: 新增安全导航工具并全量替换原生路由调用,解决页面连击、栈超限与白屏问题
新增全局安全导航工具 safeNavigateTo/safeRedirectTo/safeNavigateBack,实现防连击锁、页面栈超限降级、无历史页平滑重定向逻辑。全量替换所有页面及组件中的 uni 原生路由调用,包括充值页、商家详情页、优惠券页、收藏页、订单页、会员中心、首页、登录页等多个页面,同时更新 tabbar 跳转逻辑杜绝 reLaunch 白屏,新增路由工具单元测试,优化分类页空数据兜底提示,提升页面跳转稳定性与用户体验。
2026-11-04 10:59:49 +08:00

216 行
4.8 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="fc-navbar" :class="[`fc-navbar--${theme}`]">
<!-- 系统状态栏占位 -->
<view class="fc-navbar__status" :style="{ height: statusBarHeight + 'px' }"></view>
<!-- 导航内容条:精确对齐胶囊高度并避让右侧胶囊 -->
<view
class="fc-navbar__bar"
:style="{
height: barHeight + 'px',
paddingRight: capsulePadding ? (capsulePadding + 'px') : '24rpx',
}"
>
<!-- 返回按钮(层级置顶,保证可点击,增加点击热区) -->
<view v-if="showBack" class="fc-navbar__back" @click.stop="onBack">
<text class="fc-navbar__back-icon">‹</text>
</view>
<!-- 标题区域:默认居中显示 -->
<view class="fc-navbar__title" :class="{ 'fc-navbar__title--center': center, 'fc-navbar__title--noback': !showBack }">
<slot>
<text class="fc-navbar__title-text fc-ellipsis">{{ title }}</text>
</slot>
</view>
<!-- 右侧功能区(位于胶囊左侧) -->
<view class="fc-navbar__right">
<slot name="right"></slot>
</view>
</view>
</view>
</template>
<script>
import { getNavMetrics } from '@/common/utils/navbar.js'
import { safeNavigateBack } from '@/common/utils/nav.js'
export default {
name: 'FcNavbar',
props: {
/** 导航标题文字 */
title: { type: String, default: '' },
/** 风格主题:gradient(主色渐变白字) | white(白底黑字) | transparent(透明底黑字) */
theme: { type: String, default: 'gradient' },
/** 是否展示左侧返回按钮 */
showBack: { type: Boolean, default: false },
/** 标题文字是否居中,默认 true(居中排列) */
center: { type: Boolean, default: true },
/** 自定义返回兜底跳转路径(默认返回首页 /pages/index/index) */
fallbackUrl: { type: String, default: '' },
},
data() {
return {
statusBarHeight: 0,
barHeight: 44,
capsulePadding: 0,
}
},
created() {
this.initMetrics()
},
mounted() {
this.syncStatusBarStyle()
},
methods: {
/**
* 初始化获取设备状态栏和胶囊参数
*/
initMetrics() {
const m = getNavMetrics()
this.statusBarHeight = m.statusBarHeight
this.barHeight = m.navBarHeight
this.capsulePadding = m.capsulePadding
this.syncStatusBarStyle()
},
/**
* 同步原生系统状态栏文字与胶囊颜色
*/
syncStatusBarStyle() {
try {
if (typeof uni !== 'undefined' && uni.setNavigationBarColor) {
uni.setNavigationBarColor({
frontColor: '#000000',
backgroundColor: this.theme === 'gradient' ? '#eafbf1' : '#ffffff',
})
}
} catch (e) {}
},
/**
* 点击返回上一页
* 统一通过 safeNavigateBack 执行(页面栈 > 1 正常回退,无页面栈平滑重定向兜底)
* 彻底移除 switchTab 报错与 reLaunch 重建
*/
onBack() {
this.$emit('back')
const target = this.fallbackUrl || '/pages/index/index'
safeNavigateBack(target, 1)
},
},
}
</script>
<style lang="scss" scoped>
.fc-navbar {
position: sticky;
top: 0;
z-index: 99;
&--gradient {
background: $fc-gradient-hero;
.fc-navbar__title-text {
color: $fc-text;
}
.fc-navbar__back-icon {
color: $fc-text;
}
}
&--white {
background: #ffffff;
border-bottom: 2rpx solid $fc-border;
.fc-navbar__title-text {
color: $fc-text;
}
.fc-navbar__back-icon {
color: $fc-text;
}
}
&--transparent {
background: transparent;
.fc-navbar__title-text {
color: #0c2b1c;
font-weight: 600;
}
.fc-navbar__back-icon {
color: #0c2b1c;
}
}
}
.fc-navbar__bar {
display: flex;
align-items: center;
padding-left: 12rpx;
box-sizing: border-box;
position: relative;
width: 100%;
}
.fc-navbar__back {
width: 96rpx;
height: 100%;
min-height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
cursor: pointer;
position: relative;
z-index: 25;
transition: opacity 0.15s ease, transform 0.15s ease;
&:active {
transform: scale(0.92);
opacity: 0.75;
}
}
.fc-navbar--white .fc-navbar__back:active {
opacity: 0.7;
}
.fc-navbar__back-icon {
font-size: 54rpx;
line-height: 1;
font-weight: 400;
margin-top: -6rpx;
pointer-events: none;
}
.fc-navbar__title {
flex: 1;
min-width: 0;
display: flex;
align-items: center;
padding-left: 4rpx;
&--noback {
padding-left: 20rpx;
}
&--center {
position: absolute;
left: 80rpx;
right: 80rpx;
top: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
pointer-events: none;
padding: 0 80rpx;
box-sizing: border-box;
z-index: 1;
}
}
.fc-navbar__title-text {
font-size: 34rpx;
font-weight: 700;
letter-spacing: 0.5rpx;
max-width: 100%;
text-align: center;
}
.fc-navbar__right {
position: relative;
z-index: 3;
display: flex;
align-items: center;
flex-shrink: 0;
min-width: 68rpx;
justify-content: flex-end;
}
</style>