diff --git a/src/custom-tab-bar/index.tsx b/src/custom-tab-bar/index.tsx index 7ed003d..940097d 100644 --- a/src/custom-tab-bar/index.tsx +++ b/src/custom-tab-bar/index.tsx @@ -5,7 +5,6 @@ import PublishPopup from '@/components/PublishPopup' import type { PublishOption } from '@/components/PublishPopup' import './index.less' -/** Tab 项配置(顺序与 app.config.ts tabBar.list 一致) */ interface TabItem { key: string label: string @@ -21,13 +20,32 @@ const TAB_LIST: TabItem[] = [ ] interface CustomTabBarProps { - /** 当前激活的 tab key,由页面传入 */ - activeKey: string + /** 当前激活的 tab key。H5 由页面传入;小程序由 Taro 自动渲染,props 为空 */ + activeKey?: string +} + +/** + * 从小程序路由推导激活 tab(Taro 自动渲染 custom-tab-bar 时不传 props) + */ +function deriveActiveKey(): string { + try { + const pages = Taro.getCurrentPages() + if (pages.length > 0) { + const route = pages[pages.length - 1]?.route || '' + for (const tab of TAB_LIST) { + if (route.includes(tab.key + '/index')) return tab.key + } + } + } catch { /* ignore */ } + return 'index' } /** 获取底部安全距离 */ function getSafeBottom(): number { try { + if(process.env.TARO_ENV === 'h5') { + return 0 + } const info = Taro.getSystemInfoSync() if (info.safeArea) { return info.screenHeight - info.safeArea.bottom @@ -36,7 +54,9 @@ function getSafeBottom(): number { return 0 } -export default function CustomTabBar({ activeKey }: CustomTabBarProps) { +export default function CustomTabBar({ activeKey: propKey }: CustomTabBarProps) { + // H5 走 props,小程序自动渲染时无 props,从路由推导 + const activeKey = propKey || deriveActiveKey() const safeBottom = getSafeBottom() const [publishVisible, setPublishVisible] = useState(false) diff --git a/src/pages/index/index.config.ts b/src/pages/index/index.config.ts index 577e9a9..a707860 100644 --- a/src/pages/index/index.config.ts +++ b/src/pages/index/index.config.ts @@ -2,4 +2,5 @@ export default definePageConfig({ navigationBarTitleText: '首页', navigationStyle: 'custom', enablePullDownRefresh: false, + usingComponents: {}, }) diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index 61a42aa..3466b8e 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -165,7 +165,7 @@ export default function Index() { - + {/**/} ) } diff --git a/src/pages/messages/index.config.ts b/src/pages/messages/index.config.ts index 4bbd8f2..c039c90 100644 --- a/src/pages/messages/index.config.ts +++ b/src/pages/messages/index.config.ts @@ -1,4 +1,4 @@ export default definePageConfig({ navigationBarTitleText: '消息', - navigationStyle: 'custom', + usingComponents: {}, }) diff --git a/src/pages/messages/index.tsx b/src/pages/messages/index.tsx index a12fbc9..00a1a6f 100644 --- a/src/pages/messages/index.tsx +++ b/src/pages/messages/index.tsx @@ -1,7 +1,6 @@ import Taro from '@tarojs/taro' import { View, Text, Image, ScrollView } from '@tarojs/components' import { Badge } from '@antmjs/vantui' -import CustomTabBar from '@/custom-tab-bar' import { chatList } from '@/mock/messages' import type { ChatItem } from '@/types/message' import './index.less' @@ -86,7 +85,7 @@ export default function Messages() { {/* 未读数 */} {msg.unread ? ( - + ) : null} ))} @@ -94,8 +93,6 @@ export default function Messages() { - - ) } diff --git a/src/pages/profile/index.config.ts b/src/pages/profile/index.config.ts index 977ab5f..4b2eeb0 100644 --- a/src/pages/profile/index.config.ts +++ b/src/pages/profile/index.config.ts @@ -1,4 +1,5 @@ export default definePageConfig({ navigationBarTitleText: '我的', navigationStyle: 'custom', + usingComponents: {}, }) diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index 221ae66..4fe22f3 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -2,7 +2,6 @@ import { useMemo } from 'react' import Taro from '@tarojs/taro' import { View, Text, Image, ScrollView } from '@tarojs/components' import { Cell, CellGroup, Icon } from '@antmjs/vantui' -import CustomTabBar from '@/custom-tab-bar' import './index.less' /** 状态栏高度 */ @@ -155,9 +154,6 @@ export default function Profile() { 校园通 v1.0.0 - - {/* ========== TabBar ========== */} - ) } diff --git a/src/pages/tasks/index.config.ts b/src/pages/tasks/index.config.ts index 1c4a702..16c4c1a 100644 --- a/src/pages/tasks/index.config.ts +++ b/src/pages/tasks/index.config.ts @@ -1,5 +1,4 @@ export default definePageConfig({ navigationBarTitleText: '校园任务', - navigationStyle: 'custom', - "usingComponents": {} + usingComponents: {}, }) diff --git a/src/pages/tasks/index.tsx b/src/pages/tasks/index.tsx index f03a19e..c9c53c9 100644 --- a/src/pages/tasks/index.tsx +++ b/src/pages/tasks/index.tsx @@ -1,11 +1,9 @@ import { useState, useMemo } from 'react' import Taro from '@tarojs/taro' import { View, ScrollView } from '@tarojs/components' -import { Search, Tabs, Tab } from '@antmjs/vantui' +import { Search } from '@antmjs/vantui' import TaskCard from '@/components/TaskCard' -import CustomTabBar from '@/custom-tab-bar' import { tasks } from '@/mock/tasks' -import type { TaskType } from '@/types/task' import './index.less' /** 筛选标签 */ @@ -100,8 +98,6 @@ export default function Tasks() { - - ) }