更新
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
<template>
|
||||
<view class="un-pages-container">
|
||||
<!-- 顶部导航栏 -->
|
||||
<uni-nav-bar v-if="showNavBar" :fixed="true" :background-color="navBarBgColor" :color="navBarColor"
|
||||
:status-bar="true" :border="navBarBorder" :left-icon="showBack ? 'left' : ''" :title="navBarTitle"
|
||||
@click-left="handleBack">
|
||||
<slot name="navbar-right" slot="right"></slot>
|
||||
</uni-nav-bar>
|
||||
|
||||
<!-- 中间内容区域 -->
|
||||
<scroll-view class="content-scroll" scroll-y :style="{ height: scrollHeight }" :show-scrollbar="showScrollbar">
|
||||
<view class="content-wrapper">
|
||||
<slot></slot>
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<!-- 底部tabbar -->
|
||||
<un-tab-bar v-if="showTabBar" :current-path="currentPath" @change="handleTabChange"></un-tab-bar>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'UnPages',
|
||||
props: {
|
||||
// 是否显示顶部导航栏
|
||||
showNavBar: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
// 导航栏标题
|
||||
navBarTitle: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
// 导航栏背景色
|
||||
navBarBgColor: {
|
||||
type: String,
|
||||
default: '#fff'
|
||||
},
|
||||
// 导航栏文字颜色
|
||||
navBarColor: {
|
||||
type: String,
|
||||
default: '#333'
|
||||
},
|
||||
// 导航栏是否显示边框
|
||||
navBarBorder: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
// 是否显示返回按钮
|
||||
showBack: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 是否显示底部tabbar
|
||||
showTabBar: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 是否显示滚动条
|
||||
showScrollbar: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
scrollHeight: 'calc(100vh - 44px)',
|
||||
systemInfo: null,
|
||||
currentPath: ''
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.calculateHeight()
|
||||
// 获取当前页面路径
|
||||
this.getCurrentPath()
|
||||
// 监听窗口变化
|
||||
uni.onWindowResize(() => {
|
||||
this.calculateHeight()
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* 计算滚动区域高度
|
||||
*/
|
||||
calculateHeight() {
|
||||
try {
|
||||
const systemInfo = uni.getSystemInfoSync()
|
||||
const statusBarHeight = systemInfo.statusBarHeight || 0
|
||||
const navBarHeight = this.showNavBar ? 44 : 0
|
||||
const tabBarHeight = this.showTabBar ? 50 : 0 // tabbar高度约50px
|
||||
|
||||
let height = systemInfo.windowHeight - navBarHeight - tabBarHeight
|
||||
|
||||
// 如果有tabbar,加上安全区域
|
||||
if (this.showTabBar && systemInfo.safeAreaInsets) {
|
||||
height -= systemInfo.safeAreaInsets.bottom
|
||||
}
|
||||
|
||||
this.scrollHeight = height + 'px'
|
||||
} catch (e) {
|
||||
console.error('计算高度失败:', e)
|
||||
this.scrollHeight = 'calc(100vh - 44px)'
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理返回按钮点击
|
||||
*/
|
||||
handleBack() {
|
||||
uni.navigateBack({
|
||||
delta: 1,
|
||||
fail: () => {
|
||||
// 如果无法返回,跳转到首页
|
||||
uni.switchTab({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前页面路径
|
||||
*/
|
||||
getCurrentPath() {
|
||||
try {
|
||||
const pages = getCurrentPages()
|
||||
if (pages && pages.length > 0) {
|
||||
const currentPage = pages[pages.length - 1]
|
||||
this.currentPath = '/' + currentPage.route
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取当前页面路径失败:', e)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理tab切换
|
||||
*/
|
||||
handleTabChange(path) {
|
||||
this.$emit('tab-change', path)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.un-pages-container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: #F8F8F8;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.content-scroll {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.content-wrapper {
|
||||
width: 100%;
|
||||
min-height: 100%;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,348 @@
|
||||
<template>
|
||||
<view class="tab-bar-container" :style="{ paddingBottom: safeAreaBottom }">
|
||||
<view
|
||||
class="tab-bar-item"
|
||||
v-for="(item, index) in tabbarList"
|
||||
:key="index"
|
||||
:class="{ 'active': currentPath === item.pagePath }"
|
||||
@tap="switchTab(index, item.pagePath)"
|
||||
>
|
||||
<view class="icon-wrapper">
|
||||
<uni-icons
|
||||
:type="currentPath === item.pagePath ? item.activeIcon : item.icon"
|
||||
:size="iconSize"
|
||||
:color="currentPath === item.pagePath ? activeColor : inactiveColor"
|
||||
:customStyle="{ transition: 'all 0.3s' }"
|
||||
></uni-icons>
|
||||
<view class="badge-wrapper">
|
||||
<!-- 数字徽章 -->
|
||||
<view class="badge badge-num" v-if="item.badge > 0">
|
||||
{{ item.badge > 99 ? '99+' : item.badge }}
|
||||
</view>
|
||||
<!-- 红点徽章 -->
|
||||
<view class="badge badge-dot" v-else-if="item.dot"></view>
|
||||
</view>
|
||||
</view>
|
||||
<text class="tab-text" :style="{ color: currentPath === item.pagePath ? activeColor : inactiveColor }">
|
||||
{{ item.text }}
|
||||
</text>
|
||||
<!-- 激活状态下划线 -->
|
||||
<view class="active-line" v-if="currentPath === item.pagePath" :style="{ backgroundColor: activeColor }"></view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import config from '@/config/index.js'
|
||||
|
||||
export default {
|
||||
name: 'TabBar',
|
||||
props: {
|
||||
// 当前页面路径
|
||||
currentPath: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
// 图标大小
|
||||
iconSize: {
|
||||
type: Number,
|
||||
default: 24
|
||||
},
|
||||
// 激活状态颜色
|
||||
activeColor: {
|
||||
type: String,
|
||||
default: '#667eea'
|
||||
},
|
||||
// 未激活状态颜色
|
||||
inactiveColor: {
|
||||
type: String,
|
||||
default: '#999'
|
||||
},
|
||||
// 是否显示激活状态下划线
|
||||
showActiveLine: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
tabbarList: [],
|
||||
safeAreaBottom: '0px'
|
||||
}
|
||||
},
|
||||
created() {
|
||||
// 从配置文件加载tabbar配置
|
||||
this.tabbarList = config.tabbarList || []
|
||||
// 获取安全区域底部高度
|
||||
this.getSafeAreaBottom()
|
||||
},
|
||||
mounted() {
|
||||
// 监听页面显示,重新获取安全区域
|
||||
uni.onWindowResize(() => {
|
||||
this.getSafeAreaBottom()
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* 切换tab
|
||||
* @param {Number} index - tab索引
|
||||
* @param {String} path - 页面路径
|
||||
*/
|
||||
switchTab(index, path) {
|
||||
// 如果点击的是当前页面,不执行跳转
|
||||
if (this.currentPath === path) return
|
||||
|
||||
// 发送切换事件
|
||||
this.$emit('change', path)
|
||||
|
||||
// 触发点击反馈
|
||||
this.playClickFeedback()
|
||||
|
||||
// 使用reLaunch跳转到对应页面(关闭所有页面,打开新页面)
|
||||
this.navigateToPage(path)
|
||||
},
|
||||
|
||||
/**
|
||||
* 跳转到页面
|
||||
* @param {String} path - 页面路径
|
||||
*/
|
||||
navigateToPage(path) {
|
||||
// 使用reLaunch跳转,因为这是自定义tabbar
|
||||
uni.reLaunch({
|
||||
url: path,
|
||||
success: () => {
|
||||
console.log('跳转成功:', path)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('跳转失败:', err)
|
||||
this.handleNavigationError()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理跳转失败的情况
|
||||
*/
|
||||
handleNavigationError() {
|
||||
uni.showToast({
|
||||
title: '页面跳转失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 点击反馈动画
|
||||
*/
|
||||
playClickFeedback() {
|
||||
// 可以添加震动反馈
|
||||
if (uni.vibrateShort) {
|
||||
uni.vibrateShort({
|
||||
success: () => {},
|
||||
fail: () => {}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取安全区域底部高度
|
||||
*/
|
||||
getSafeAreaBottom() {
|
||||
const systemInfo = uni.getSystemInfoSync()
|
||||
this.safeAreaBottom = systemInfo.safeAreaInsets
|
||||
? systemInfo.safeAreaInsets.bottom + 'px'
|
||||
: '0px'
|
||||
},
|
||||
|
||||
/**
|
||||
* 设置数字徽章
|
||||
* @param {Number} index - tab索引
|
||||
* @param {Number} count - 徽章数量
|
||||
*/
|
||||
setBadge(index, count) {
|
||||
if (this.tabbarList[index]) {
|
||||
this.$set(this.tabbarList, index, {
|
||||
...this.tabbarList[index],
|
||||
badge: count,
|
||||
dot: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 设置红点徽章
|
||||
* @param {Number} index - tab索引
|
||||
* @param {Boolean} show - 是否显示红点
|
||||
*/
|
||||
setDot(index, show) {
|
||||
if (this.tabbarList[index]) {
|
||||
this.$set(this.tabbarList, index, {
|
||||
...this.tabbarList[index],
|
||||
dot: show,
|
||||
badge: 0
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 清除所有徽章
|
||||
*/
|
||||
clearAllBadges() {
|
||||
this.tabbarList.forEach((item, index) => {
|
||||
this.$set(this.tabbarList, index, {
|
||||
...item,
|
||||
badge: 0,
|
||||
dot: false
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取tabbar项
|
||||
* @param {Number} index - tab索引
|
||||
* @returns {Object} tabbar项
|
||||
*/
|
||||
getTabItem(index) {
|
||||
return this.tabbarList[index] || null
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.tab-bar-container {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: 999;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
height: 100rpx;
|
||||
background: #fff;
|
||||
border-top: 1rpx solid #eee;
|
||||
box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.tab-bar-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex: 1;
|
||||
height: 100%;
|
||||
position: relative;
|
||||
transition: all 0.3s;
|
||||
cursor: pointer;
|
||||
|
||||
&:active {
|
||||
opacity: 0.7;
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
&.active {
|
||||
.tab-text {
|
||||
font-weight: 500;
|
||||
transform: translateY(-2rpx);
|
||||
}
|
||||
|
||||
.icon-wrapper {
|
||||
transform: translateY(-2rpx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.icon-wrapper {
|
||||
position: relative;
|
||||
width: 48rpx;
|
||||
height: 48rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 6rpx;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.badge-wrapper {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: -8rpx;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.badge {
|
||||
position: absolute;
|
||||
top: -8rpx;
|
||||
right: -8rpx;
|
||||
min-width: 32rpx;
|
||||
height: 32rpx;
|
||||
padding: 0 6rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #fff;
|
||||
font-size: 20rpx;
|
||||
font-weight: 500;
|
||||
line-height: 1;
|
||||
box-shadow: 0 2rpx 8rpx rgba(245, 108, 108, 0.4);
|
||||
animation: badgeIn 0.3s ease-out;
|
||||
|
||||
&.badge-num {
|
||||
background: linear-gradient(135deg, #f56c6c 0%, #f89898 100%);
|
||||
border-radius: 16rpx;
|
||||
border: 2rpx solid #fff;
|
||||
}
|
||||
|
||||
&.badge-dot {
|
||||
min-width: 16rpx;
|
||||
height: 16rpx;
|
||||
background: #f56c6c;
|
||||
border-radius: 50%;
|
||||
border: 2rpx solid #fff;
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes badgeIn {
|
||||
0% {
|
||||
transform: scale(0);
|
||||
opacity: 0;
|
||||
}
|
||||
50% {
|
||||
transform: scale(1.2);
|
||||
}
|
||||
100% {
|
||||
transform: scale(1);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.tab-text {
|
||||
font-size: 22rpx;
|
||||
color: #999;
|
||||
transition: all 0.3s;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
.active-line {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 40rpx;
|
||||
height: 4rpx;
|
||||
border-radius: 2rpx;
|
||||
animation: lineIn 0.3s ease-out;
|
||||
}
|
||||
|
||||
@keyframes lineIn {
|
||||
0% {
|
||||
width: 0;
|
||||
}
|
||||
100% {
|
||||
width: 40rpx;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user