新增搜索课程和加载组件页面,签到页面添加遮罩防重复扫码,添加 request 便捷方法(get/post/put/delete)
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
<!-- components/TabBar.vue -->
|
||||
<template>
|
||||
<view class="tab-bar">
|
||||
<view
|
||||
v-for="(tab, index) in tabs"
|
||||
:key="tab.path"
|
||||
:class="['tab-item', { active: currentIndex === index }]"
|
||||
:class="['tab-item', { active: currentActiveIndex === index }]"
|
||||
hover-class="tab-item--hover"
|
||||
@tap.stop="onTabTap(index)"
|
||||
>
|
||||
<image
|
||||
:src="currentIndex === index ? tab.iconActive : tab.icon"
|
||||
:src="currentActiveIndex === index ? tab.iconActive : tab.icon"
|
||||
mode="aspectFit"
|
||||
class="tab-icon"
|
||||
/>
|
||||
@@ -18,7 +19,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue'
|
||||
import {
|
||||
PAGE,
|
||||
TAB_ROUTES,
|
||||
@@ -31,6 +32,76 @@ const props = defineProps({
|
||||
activeTab: { type: Number, default: -1 }
|
||||
})
|
||||
|
||||
const emit = defineEmits(['update:active', 'tab-change'])
|
||||
|
||||
// 当前激活的索引 - 默认从路由获取
|
||||
const currentActiveIndex = ref(-1)
|
||||
|
||||
// 从路由获取当前激活的 tab
|
||||
function getActiveIndexFromRoute() {
|
||||
const routePath = getCurrentRoutePath()
|
||||
const index = getTabIndexByRoute(routePath)
|
||||
console.log('从路由获取索引:', routePath, '->', index)
|
||||
return index >= 0 ? index : 0
|
||||
}
|
||||
|
||||
// 同步激活状态(高优先级:路由 > props)
|
||||
function syncActiveState() {
|
||||
// 优先从路由获取(最准确)
|
||||
const routeIndex = getActiveIndexFromRoute()
|
||||
if (routeIndex >= 0) {
|
||||
currentActiveIndex.value = routeIndex
|
||||
return
|
||||
}
|
||||
|
||||
// 其次使用 props
|
||||
if (props.active >= 0) {
|
||||
currentActiveIndex.value = props.active
|
||||
} else if (props.activeTab >= 0) {
|
||||
currentActiveIndex.value = props.activeTab
|
||||
} else {
|
||||
currentActiveIndex.value = 0
|
||||
}
|
||||
}
|
||||
|
||||
// 监听路由变化(页面切换时自动同步)
|
||||
let routeWatcher = null
|
||||
|
||||
onMounted(() => {
|
||||
// 初始同步
|
||||
syncActiveState()
|
||||
|
||||
// 监听路由变化(App 端)
|
||||
// #ifdef APP-PLUS
|
||||
routeWatcher = plus.globalEvent.addEventListener('newintent', () => {
|
||||
setTimeout(syncActiveState, 50)
|
||||
})
|
||||
// #endif
|
||||
|
||||
// 监听页面显示(跨端通用)
|
||||
uni.onAppRoute ? uni.onAppRoute(() => {
|
||||
setTimeout(syncActiveState, 50)
|
||||
}) : null
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
// 清理监听
|
||||
// #ifdef APP-PLUS
|
||||
if (routeWatcher) {
|
||||
plus.globalEvent.removeEventListener('newintent', routeWatcher)
|
||||
}
|
||||
// #endif
|
||||
})
|
||||
|
||||
// 监听 props 变化
|
||||
watch(() => props.active, () => {
|
||||
// 只有当 props 主动变化且不是来自路由同步时才更新
|
||||
const routeIndex = getActiveIndexFromRoute()
|
||||
if (routeIndex !== currentActiveIndex.value) {
|
||||
syncActiveState()
|
||||
}
|
||||
})
|
||||
|
||||
const tabs = [
|
||||
{
|
||||
path: PAGE.INDEX,
|
||||
@@ -64,19 +135,51 @@ const tabs = [
|
||||
}
|
||||
]
|
||||
|
||||
const currentIndex = computed(() => {
|
||||
if (props.active >= 0) return props.active
|
||||
if (props.activeTab >= 0) return props.activeTab
|
||||
return getTabIndexByRoute(getCurrentRoutePath())
|
||||
})
|
||||
let isSwitching = false
|
||||
|
||||
function onTabTap(index) {
|
||||
if (index === currentIndex.value) return
|
||||
const path = TAB_ROUTES[index]
|
||||
uni.reLaunch({
|
||||
url: path,
|
||||
fail: () => {
|
||||
uni.showToast({ title: '页面跳转失败', icon: 'none' })
|
||||
if (isSwitching) return
|
||||
|
||||
const targetPath = TAB_ROUTES[index]
|
||||
const currentPath = TAB_ROUTES[currentActiveIndex.value]
|
||||
|
||||
if (targetPath === currentPath) return
|
||||
|
||||
console.log('Tab 点击:', index, targetPath)
|
||||
|
||||
// 1. 立即更新 UI 高亮
|
||||
currentActiveIndex.value = index
|
||||
|
||||
// 2. 通知父组件
|
||||
emit('update:active', index)
|
||||
emit('tab-change', index)
|
||||
|
||||
// 3. 显示 loading(可选)
|
||||
let timer = setTimeout(() => {
|
||||
uni.showLoading({ title: '加载中...', mask: true })
|
||||
}, 50)
|
||||
|
||||
isSwitching = true
|
||||
|
||||
// 4. 执行跳转
|
||||
uni.switchTab({
|
||||
url: targetPath,
|
||||
success: () => {
|
||||
console.log('switchTab 成功:', targetPath)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('switchTab 失败:', err)
|
||||
// 降级
|
||||
uni.reLaunch({ url: targetPath })
|
||||
},
|
||||
complete: () => {
|
||||
clearTimeout(timer)
|
||||
uni.hideLoading()
|
||||
setTimeout(() => {
|
||||
isSwitching = false
|
||||
// 跳转完成后,再次同步确保高亮正确
|
||||
syncActiveState()
|
||||
}, 100)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -106,6 +209,11 @@ function onTabTap(index) {
|
||||
align-items: center;
|
||||
gap: 8rpx;
|
||||
padding: 12rpx 24rpx;
|
||||
transition: all 0.1s ease;
|
||||
}
|
||||
|
||||
.tab-item:active {
|
||||
transform: scale(0.95);
|
||||
}
|
||||
|
||||
.tab-icon {
|
||||
@@ -122,4 +230,4 @@ function onTabTap(index) {
|
||||
color: #f97316;
|
||||
font-weight: 600;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
Reference in New Issue
Block a user