Files
hazardousWaste_app/pages/components/Navigation.vue

156 lines
3.1 KiB
Vue
Raw Normal View History

2026-04-03 08:38:34 +08:00
<template>
<!-- 状态栏占位 -->
2026-04-14 08:46:29 +08:00
<!-- <view class="nav-placeholder" :style="{ height: 35 + 'px' }"></view> -->
2026-04-03 08:38:34 +08:00
2026-04-14 08:46:29 +08:00
<view class="navbar" :style="{ height: navHeight + 'rpx' }">
2026-04-23 14:35:54 +08:00
<view class="left" @tap="goBack">
2026-04-14 08:46:29 +08:00
<uni-icons type="left" size="18" v-if="showBack" />
2026-04-03 08:38:34 +08:00
</view>
<!-- 标题 -->
2026-04-03 08:38:34 +08:00
<view class="title">{{ title }}</view>
<!-- 右侧插槽 -->
2026-04-03 08:38:34 +08:00
<view class="right">
<slot name="right"></slot>
</view>
</view>
2026-04-14 08:46:29 +08:00
<!-- <view class="contentBox"></view> -->
2026-04-03 08:38:34 +08:00
</template>
<script setup>
import { ref, onMounted, toRefs } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { getTabBarList } from '../until'
2026-04-14 08:46:29 +08:00
import { findIndex } from 'lodash'
2026-04-03 08:38:34 +08:00
const props = defineProps({
title: {
type: String,
default: ''
},
backUrl: {
type: String,
default: ''
},
showBack: {
type: Boolean,
default: true
}
2026-04-03 08:38:34 +08:00
})
const { backUrl, title, showBack } = toRefs(props)
// 状态栏 + 导航栏高度
2026-04-03 08:38:34 +08:00
const statusBarHeight = ref(0)
const navHeight = ref(0)
// 返回目标路由
const backTarget = ref('')
const isTabBar = ref('')
// 获取系统信息
2026-04-03 08:38:34 +08:00
onMounted(() => {
try {
const res = uni.getSystemInfoSync()
2026-04-14 08:46:29 +08:00
statusBarHeight.value = res.statusBarHeight || 0
2026-04-14 08:46:29 +08:00
console.log(statusBarHeight, 'statusBarHeight==>');
// 标准导航栏高度 44px + 状态栏 = 总高度
2026-04-14 08:46:29 +08:00
navHeight.value = statusBarHeight.value + 60
} catch (e) {
console.warn('获取系统信息失败', e)
}
2026-04-03 08:38:34 +08:00
})
//页面加载时获取上一页路由
2026-04-03 08:38:34 +08:00
onLoad(() => {
const tabList = getTabBarList()
const isTabBarNav = findIndex(tabList, (i) => i.pagePath == backUrl.value) != -1
2026-04-14 08:46:29 +08:00
console.log(tabList, backUrl, isTabBarNav);
isTabBar.value = isTabBarNav
2026-04-03 08:38:34 +08:00
if (backUrl.value) {
backTarget.value = backUrl.value
return
2026-04-03 08:38:34 +08:00
}
try {
const pages = getCurrentPages()
if (pages && pages.length >= 2) {
const prevPage = pages[pages.length - 2]
backTarget.value = prevPage.route || ''
}
} catch (e) {
console.warn('获取页面栈失败', e)
}
2026-04-03 08:38:34 +08:00
})
// 返回逻辑
2026-04-03 08:38:34 +08:00
const goBack = () => {
if (!showBack.value) return;
if (backTarget.value) {
const url = backTarget.value.startsWith('/') ? backTarget.value : `/${backTarget.value}`;
2026-04-03 08:38:34 +08:00
if (isTabBar.value) uni.switchTab({ url: url })
else uni.navigateTo({ url: url })
2026-04-03 08:38:34 +08:00
} else {
// 无目标路由 正常返回
2026-04-03 08:38:34 +08:00
uni.navigateBack()
}
}
</script>
<style scoped lang="scss">
.nav-placeholder {
width: 100%;
}
/* 导航栏主体fixed 固定在顶部 */
2026-04-03 08:38:34 +08:00
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #fff;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20rpx;
box-sizing: border-box;
z-index: 999;
}
.left {
display: flex;
align-items: center;
justify-content: center;
width: 40rpx;
height: 100%;
2026-04-03 08:38:34 +08:00
}
.title {
2026-04-14 08:46:29 +08:00
font-size: 16px;
font-weight: 600;
text-align: center;
color: #333;
max-width: 400rpx;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
2026-04-03 08:38:34 +08:00
}
.right {
min-width: 60rpx;
display: flex;
align-items: center;
justify-content: flex-end;
}
.content {
2026-04-03 08:38:34 +08:00
width: 100%;
box-sizing: border-box;
2026-04-03 08:38:34 +08:00
}
</style>