custom-tabbar.uvue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <view class="custom-tabbar">
  3. <view v-for="(item, index) in visibleTabList" :key="index" class="tab-item" @click="handleTabClick(index)">
  4. <view class="tab-icon-container">
  5. <image class="tab-icon" :src="currentVisibleIndex == index ? item.selectedIconPath : item.iconPath" mode="aspectFit"></image>
  6. <view v-if="item.badge != null && item.badge > 0" class="badge">
  7. <text class="badge-text">{{ item.badge }}</text>
  8. </view>
  9. </view>
  10. <text :class="['tab-text', currentVisibleIndex == index ? 'tab-text-active' : '']">
  11. {{ item.text }}
  12. </text>
  13. </view>
  14. </view>
  15. </template>
  16. <script setup lang="uts">
  17. import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
  18. import { getMessageList } from '../../api/message/index'
  19. // Props
  20. type Props = {
  21. current?: number
  22. hide0:number
  23. hide1:number
  24. hide2:number
  25. hide3:number
  26. }
  27. const props = withDefaults(defineProps<Props>(), {
  28. current: 0,
  29. hide0:1,
  30. hide1:1,
  31. hide2:1,
  32. hide3:1
  33. })
  34. // Tab 列表配置
  35. type TabItem = {
  36. pagePath: string
  37. text: string
  38. iconPath: string
  39. selectedIconPath: string
  40. badge?: number
  41. }
  42. const tabList = ref<TabItem[]>([
  43. {
  44. pagePath: '/pages/index/index',
  45. text: '首页',
  46. iconPath: '/static/images/custom-tabbar/11.png',
  47. selectedIconPath: '/static/images/custom-tabbar/22.png'
  48. },
  49. {
  50. pagePath: '/pages/workspace/index',
  51. text: '工作台',
  52. iconPath: '/static/images/custom-tabbar/33.png',
  53. selectedIconPath: '/static/images/custom-tabbar/44.png'
  54. },
  55. {
  56. pagePath: '/pages/message/index',
  57. text: '消息',
  58. iconPath: '/static/images/custom-tabbar/77.png',
  59. selectedIconPath: '/static/images/custom-tabbar/88.png',
  60. badge: 0
  61. },
  62. {
  63. pagePath: '/pages/profile/index',
  64. text: '我的',
  65. iconPath: '/static/images/custom-tabbar/55.png',
  66. selectedIconPath: '/static/images/custom-tabbar/66.png'
  67. }
  68. ])
  69. // 计算可见的 tab 列表
  70. const visibleTabList = computed<TabItem[]>(() => {
  71. const hideValues = [props.hide0, props.hide1, props.hide2, props.hide3]
  72. return tabList.value.filter((item: TabItem, index: number) => {
  73. return index < hideValues.length && hideValues[index] === 1
  74. })
  75. })
  76. // 计算当前选中项在可见列表中的索引
  77. const currentVisibleIndex = computed<number>(() => {
  78. const hideValues = [props.hide0, props.hide1, props.hide2, props.hide3]
  79. let visibleIndex = 0
  80. for (let i = 0; i < props.current; i++) {
  81. // 如果前面的 tab 是可见的,则可见索引增加
  82. if (i < hideValues.length && hideValues[i] === 1) {
  83. visibleIndex++
  84. }
  85. }
  86. // 检查当前 tab 是否可见
  87. if (props.current >= hideValues.length || hideValues[props.current] !== 1) {
  88. // 如果当前 tab 不可见,返回第一个可见的 tab 索引
  89. return 0
  90. }
  91. return visibleIndex
  92. })
  93. // 定时器
  94. let refreshTimer: number | null = null
  95. // 获取未读消息数量
  96. const getUnreadMessageCount = async (): Promise<void> => {
  97. try {
  98. const result = await getMessageList(1, 1, '', 'UNREAD')
  99. const resultObj = result as UTSJSONObject
  100. const total = resultObj['total'] as number
  101. // 更新消息标签的徽章
  102. const messageTabIndex = tabList.value.findIndex(item => item.pagePath === '/pages/message/index')
  103. if (messageTabIndex !== -1) {
  104. tabList.value[messageTabIndex].badge = total
  105. }
  106. } catch (e) {
  107. console.error('获取未读消息数量失败:', e)
  108. }
  109. }
  110. // 启动定时器
  111. const startRefreshTimer = (): void => {
  112. // 每30秒更新一次未读消息数量
  113. refreshTimer = setInterval(() => {
  114. getUnreadMessageCount()
  115. }, 30000)
  116. }
  117. // 停止定时器
  118. const stopRefreshTimer = (): void => {
  119. if (refreshTimer != null) {
  120. clearInterval(refreshTimer as number)
  121. refreshTimer = null
  122. }
  123. }
  124. // Tab 点击事件
  125. const handleTabClick = (index: number): void => {
  126. // 将可见索引转换回原始索引
  127. const hideValues = [props.hide0, props.hide1, props.hide2, props.hide3]
  128. let originalIndex = 0
  129. let visibleCount = 0
  130. for (let i = 0; i < tabList.value.length; i++) {
  131. if (i < hideValues.length && hideValues[i] === 1) {
  132. if (visibleCount === index) {
  133. originalIndex = i
  134. break
  135. }
  136. visibleCount++
  137. }
  138. }
  139. if (originalIndex == props.current) {
  140. return
  141. }
  142. const item = tabList.value[originalIndex]
  143. uni.redirectTo({
  144. url: item.pagePath
  145. })
  146. }
  147. // 监听登录状态变化
  148. const onAuthStateChange = (res: UTSJSONObject): void => {
  149. if (res != null && res['isLoggedIn'] === false) {
  150. stopRefreshTimer()
  151. } else if (res != null && res['isLoggedIn'] === true) {
  152. // 重新登录后重新启动timer
  153. startRefreshTimer()
  154. getUnreadMessageCount()
  155. }
  156. }
  157. // 组件挂载时
  158. onMounted(() => {
  159. // 初始获取未读消息数量
  160. getUnreadMessageCount()
  161. // 启动定时更新
  162. startRefreshTimer()
  163. // 监听未读消息数量更新事件
  164. uni.$on('updateUnreadCount', getUnreadMessageCount)
  165. // 监听登录状态变化,清除timer
  166. uni.$on('onAuthStateChange', onAuthStateChange)
  167. })
  168. // 组件卸载时
  169. onBeforeUnmount(() => {
  170. // 停止定时器
  171. stopRefreshTimer()
  172. // 移除事件监听
  173. uni.$off('updateUnreadCount', getUnreadMessageCount)
  174. uni.$off('onAuthStateChange', onAuthStateChange)
  175. })
  176. </script>
  177. <style lang="scss">
  178. .custom-tabbar {
  179. position: fixed;
  180. bottom: 0;
  181. left: 0;
  182. right: 0;
  183. z-index: 1000;
  184. height: 150rpx;
  185. background-color: #ffffff;
  186. border-top: 1rpx solid #e5e5e5;
  187. flex-direction: row;
  188. justify-content: space-around;
  189. align-items: center;
  190. padding-bottom: env(safe-area-inset-bottom);
  191. }
  192. .tab {
  193. &-item {
  194. flex: 1;
  195. justify-content: center;
  196. align-items: center;
  197. }
  198. &-icon-container {
  199. position: relative;
  200. display: flex;
  201. align-items: center;
  202. justify-content: flex-start;
  203. margin-bottom: 8rpx;
  204. width: 80rpx;
  205. height: 70rpx;
  206. padding-left: 10rpx;
  207. }
  208. &-icon {
  209. width: 48rpx;
  210. height: 48rpx;
  211. }
  212. &-text {
  213. font-size: 24rpx;
  214. color: #666666;
  215. &-active {
  216. color: #0081ff;
  217. }
  218. }
  219. }
  220. .badge {
  221. position: absolute;
  222. top: 0rpx;
  223. right: 0rpx;
  224. min-width: 36rpx;
  225. height: 36rpx;
  226. background-color: #ff3b30;
  227. border-radius: 18rpx;
  228. display: flex;
  229. align-items: center;
  230. justify-content: center;
  231. padding: 0 10rpx;
  232. z-index: 1;
  233. .badge-text {
  234. font-size: 20rpx;
  235. color: #ffffff;
  236. font-weight: bold;
  237. }
  238. }
  239. </style>