store.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type { CustomTabBarItem } from './config'
  2. import { tabbarList as _tabbarList, customTabbarEnable } from './config'
  3. // TODO 1/2: 中间的鼓包tabbarItem的开关
  4. const BULGE_ENABLE = false
  5. /** tabbarList 里面的 path 从 pages.config.ts 得到 */
  6. const tabbarList: CustomTabBarItem[] = _tabbarList.map(item => ({
  7. ...item,
  8. pagePath: item.pagePath.startsWith('/') ? item.pagePath : `/${item.pagePath}`,
  9. }))
  10. if (customTabbarEnable && BULGE_ENABLE) {
  11. if (tabbarList.length % 2) {
  12. console.error('有鼓包时 tabbar 数量必须是偶数,否则样式很奇怪!!')
  13. }
  14. tabbarList.splice(tabbarList.length / 2, 0, {
  15. isBulge: true,
  16. } as CustomTabBarItem)
  17. }
  18. export function isPageTabbar(path: string) {
  19. return tabbarList.some(item => item.pagePath === path)
  20. }
  21. /**
  22. * 自定义 tabbar 的状态管理,原生 tabbar 无需关注本文件
  23. * tabbar 状态,增加 storageSync 保证刷新浏览器时在正确的 tabbar 页面
  24. * 使用reactive简单状态,而不是 pinia 全局状态
  25. */
  26. const tabbarStore = reactive({
  27. curIdx: uni.getStorageSync('app-tabbar-index') || 0,
  28. prevIdx: uni.getStorageSync('app-tabbar-index') || 0,
  29. setCurIdx(idx: number) {
  30. this.curIdx = idx
  31. uni.setStorageSync('app-tabbar-index', idx)
  32. },
  33. setAutoCurIdx(path: string) {
  34. const index = tabbarList.findIndex(item => item.pagePath === path)
  35. console.log('index:', index, path)
  36. // console.log('tabbarList:', tabbarList)
  37. if (index === -1) {
  38. this.setCurIdx(0)
  39. }
  40. else {
  41. this.setCurIdx(index)
  42. }
  43. },
  44. restorePrevIdx() {
  45. if (this.prevIdx === this.curIdx)
  46. return
  47. this.setCurIdx(this.prevIdx)
  48. this.prevIdx = uni.getStorageSync('app-tabbar-index') || 0
  49. },
  50. })
  51. export { tabbarList, tabbarStore }