tabbar.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //管理导航栏状态和导航逻辑
  2. import { defineStore } from 'pinia'
  3. import Taro from '@tarojs/taro'
  4. // TabBar 项目的接口定义
  5. export interface TabBarItem {
  6. pagePath: string
  7. text: string
  8. icon: any
  9. }
  10. export const useTabBarStore = defineStore('tabbar', {
  11. state: () => ({
  12. currentIndex: 0,
  13. switching: false,
  14. tabList: [
  15. {
  16. pagePath: '/pages/index/index',
  17. text: '游戏广场',
  18. icon: 'Home'
  19. },
  20. {
  21. pagePath: '/pages/room/join/index',
  22. text: '房间',
  23. icon: 'Find'
  24. },
  25. {
  26. pagePath: '/pages/history/index',
  27. text: '历史记录',
  28. icon: 'Clock'
  29. },
  30. {
  31. pagePath: '/pages/profile/index',
  32. text: '我的',
  33. icon: 'My'
  34. }
  35. ]
  36. }),
  37. actions: {
  38. // 更新当前选中的 tab
  39. updateSelected() {
  40. const pages = Taro.getCurrentPages()
  41. if (!pages || pages.length === 0) return
  42. const currentPage = pages[pages.length - 1]
  43. if (!currentPage || !currentPage.route) return
  44. const url = `/${currentPage.route}`
  45. console.log('当前页面路径:', url)
  46. // 查找当前页面在哪个 tab
  47. const index = this.tabList.findIndex(item => item.pagePath === url)
  48. // 只有确实匹配到 tab 时才更新状态
  49. if (index !== -1) {
  50. console.log('更新选中 tab 为:', index)
  51. this.currentIndex = index
  52. } else {
  53. console.log('当前页面不在 tab 列表中:', url)
  54. }
  55. },
  56. /**
  57. * 封装页面跳转逻辑
  58. * @param url 目标页面路径
  59. * @param options 配置项
  60. */
  61. navigate(url: string, options: { params?: Record<string, any>; isTab?: boolean } = {}) {
  62. const { params, isTab } = options
  63. // 拼接参数
  64. let fullUrl = url
  65. if (params) {
  66. const queryString = Object.entries(params)
  67. .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
  68. .join('&')
  69. fullUrl = `${url}?${queryString}`
  70. }
  71. console.log('fullUrl:', fullUrl)
  72. // 判断是否为 TabBar 页面
  73. const tabIndex = this.tabList.findIndex((item) => item.pagePath === url)
  74. if (isTab || tabIndex !== -1) {
  75. // 如果是 TabBar 页面,切换 TabBar
  76. this.currentIndex = tabIndex
  77. this.switchTab(fullUrl, tabIndex)
  78. } else {
  79. // 非 TabBar 页面,普通跳转
  80. Taro.navigateTo({
  81. url: fullUrl
  82. }).catch((err) => {
  83. console.error('navigateTo 错误:', err)
  84. })
  85. }
  86. },
  87. /**
  88. * 切换到房间 Tab 的逻辑
  89. * @param subPage 子页面路径
  90. * @param options 带有 params 的选项对象
  91. */
  92. switchToRoomTab(subPage = '/pages/room/join/index', options: { params?: Record<string, any> } = {}) {
  93. const isSubPage = !this.tabList.some((item) => item.pagePath === subPage)
  94. if (isSubPage) {
  95. // 如果是房间的子页面,更新 TabBar 状态并跳转
  96. this.currentIndex = 1 // 房间 Tab 的索引
  97. this.navigate(subPage, options) // 正确传递整个 options 对象
  98. } else {
  99. // 否则直接切换到 `房间` Tab
  100. this.navigate('/pages/room/join/index', {
  101. isTab: true
  102. })
  103. }
  104. },
  105. // 切换 tab
  106. switchTab(url: string, index: number) {
  107. if (this.switching) return // 防止重复点击
  108. console.log('尝试切换到:', url, index)
  109. this.switching = true
  110. // 先更新 UI 状态
  111. this.currentIndex = index
  112. // 执行页面跳转
  113. Taro.reLaunch({
  114. url,
  115. success: () => {
  116. console.log('跳转成功到:', url)
  117. this.currentIndex = index
  118. },
  119. fail: (error) => {
  120. console.error('跳转失败:', error)
  121. },
  122. complete: () => {
  123. this.switching = false
  124. }
  125. })
  126. },
  127. // 在应用启动时初始化监听
  128. setupListeners() {
  129. // 绑定页面显示事件
  130. Taro.eventCenter.on('taroPageShow', this.updateSelected.bind(this))
  131. // 绑定路由变化事件
  132. Taro.eventCenter.on('routeChange', this.updateSelected.bind(this))
  133. // 监听小程序显示事件
  134. Taro.onAppShow(this.updateSelected.bind(this))
  135. },
  136. // 移除监听
  137. removeListeners() {
  138. Taro.eventCenter.off('taroPageShow', this.updateSelected.bind(this))
  139. Taro.eventCenter.off('routeChange', this.updateSelected.bind(this))
  140. Taro.offAppShow(this.updateSelected.bind(this))
  141. }
  142. }
  143. })