config.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import type { TabBar } from '@uni-helper/vite-plugin-uni-pages'
  2. /**
  3. * tabbar 选择的策略,更详细的介绍见 tabbar.md 文件
  4. * 0: 'NO_TABBAR' `无 tabbar`
  5. * 1: 'NATIVE_TABBAR' `完全原生 tabbar`
  6. * 2: 'CUSTOM_TABBAR_WITH_CACHE' `有缓存自定义 tabbar`
  7. * 3: 'CUSTOM_TABBAR_WITHOUT_CACHE' `无缓存自定义 tabbar`
  8. *
  9. * 温馨提示:本文件的任何代码更改了之后,都需要重新运行,否则 pages.json 不会更新导致配置不生效
  10. */
  11. export const TABBAR_STRATEGY_MAP = {
  12. NO_TABBAR: 0,
  13. NATIVE_TABBAR: 1,
  14. CUSTOM_TABBAR_WITH_CACHE: 2,
  15. CUSTOM_TABBAR_WITHOUT_CACHE: 3,
  16. }
  17. // TODO: 1/3. 通过这里切换使用tabbar的策略
  18. // 如果是使用 NO_TABBAR(0),nativeTabbarList 和 customTabbarList 都不生效(里面的配置不用管)
  19. // 如果是使用 NATIVE_TABBAR(1),只需要配置 nativeTabbarList,customTabbarList 不生效
  20. // 如果是使用 CUSTOM_TABBAR(2,3),只需要配置 customTabbarList,nativeTabbarList 不生效
  21. export const selectedTabbarStrategy = TABBAR_STRATEGY_MAP.CUSTOM_TABBAR_WITH_CACHE
  22. type NativeTabBarItem = TabBar['list'][number]
  23. // TODO: 2/3. 使用 NATIVE_TABBAR 时,更新下面的 tabbar 配置
  24. export const nativeTabbarList: NativeTabBarItem[] = [
  25. {
  26. iconPath: 'static/tabbar/home.png',
  27. selectedIconPath: 'static/tabbar/homeHL.png',
  28. pagePath: 'pages/index/index',
  29. text: '首页',
  30. },
  31. {
  32. iconPath: 'static/tabbar/example.png',
  33. selectedIconPath: 'static/tabbar/exampleHL.png',
  34. pagePath: 'pages/about/about',
  35. text: '关于',
  36. },
  37. {
  38. iconPath: 'static/tabbar/personal.png',
  39. selectedIconPath: 'static/tabbar/personalHL.png',
  40. pagePath: 'pages/me/me',
  41. text: '个人',
  42. },
  43. ]
  44. // badge 显示一个数字或 小红点(样式可以直接在 tabbar/index.vue 里面修改)
  45. export type CustomTabBarItemBadge = number | 'dot'
  46. export interface CustomTabBarItem {
  47. text: string
  48. pagePath: string
  49. iconType: 'uniUi' | 'uiLib' | 'unocss' | 'iconfont' | 'image' // 不建议用 image 模式,需要配置2张图
  50. icon: any // 其实是 string 类型,这里是为了避免 ts 报错 (tabbar/index.vue 里面 uni-icons 那行)
  51. iconActive?: string // 只有在 image 模式下才需要,传递的是高亮的图片(PS: 不建议用 image 模式)
  52. badge?: CustomTabBarItemBadge
  53. isBulge?: boolean // 是否是中间的鼓包tabbarItem
  54. }
  55. // TODO: 3/3. 使用 CUSTOM_TABBAR(2,3) 时,更新下面的 tabbar 配置
  56. // 如果需要配置鼓包,需要在 'tabbar/store.ts' 里面设置,最后在 `tabbar/index.vue` 里面更改鼓包的图片
  57. export const customTabbarList: CustomTabBarItem[] = [
  58. {
  59. text: '首页',
  60. pagePath: 'pages/index/index',
  61. // 本框架内置了 uniapp 官方UI库 (uni-ui)的图标库
  62. // 使用方式如:<uni-icons type="home" size="30"/>
  63. // 图标列表地址:https://uniapp.dcloud.net.cn/component/uniui/uni-icons.html
  64. iconType: 'uniUi',
  65. icon: 'home',
  66. // badge: 'dot',
  67. },
  68. {
  69. text: '关于',
  70. pagePath: 'pages/about/about',
  71. // 注意 unocss 图标需要如下处理:(二选一)
  72. // 1)在fg-tabbar.vue页面上引入一下并注释掉(见tabbar/index.vue代码第2行)
  73. // 2)配置到 unocss.config.ts 的 safelist 中
  74. iconType: 'unocss',
  75. icon: 'i-carbon-code',
  76. // badge: 10,
  77. },
  78. {
  79. pagePath: 'pages/me/me',
  80. text: '我的',
  81. iconType: 'uniUi',
  82. icon: 'contact',
  83. // badge: 100,
  84. },
  85. // 其他类型演示
  86. // 1、uiLib
  87. // {
  88. // pagePath: 'pages/index/index',
  89. // text: '首页',
  90. // iconType: 'uiLib',
  91. // icon: 'home',
  92. // },
  93. // 2、iconfont
  94. // {
  95. // pagePath: 'pages/index/index',
  96. // text: '首页',
  97. // // 注意 iconfont 图标需要额外加上 'iconfont',如下
  98. // iconType: 'iconfont',
  99. // icon: 'iconfont icon-my',
  100. // },
  101. // 3、image
  102. // {
  103. // pagePath: 'pages/index/index',
  104. // text: '首页',
  105. // // 使用 ‘image’时,需要配置 icon + iconActive 2张图片
  106. // iconType: 'image',
  107. // icon: '/static/tabbar/home.png',
  108. // iconActive: '/static/tabbar/homeHL.png',
  109. // },
  110. ]
  111. /**
  112. * 是否启用 tabbar 缓存
  113. * NATIVE_TABBAR(1) 和 CUSTOM_TABBAR_WITH_CACHE(2) 时,需要tabbar缓存
  114. */
  115. export const tabbarCacheEnable
  116. = [TABBAR_STRATEGY_MAP.NATIVE_TABBAR, TABBAR_STRATEGY_MAP.CUSTOM_TABBAR_WITH_CACHE].includes(selectedTabbarStrategy)
  117. /**
  118. * 是否启用自定义 tabbar
  119. * CUSTOM_TABBAR(2,3) 时,启用自定义tabbar
  120. */
  121. export const customTabbarEnable
  122. = [TABBAR_STRATEGY_MAP.CUSTOM_TABBAR_WITH_CACHE, TABBAR_STRATEGY_MAP.CUSTOM_TABBAR_WITHOUT_CACHE].includes(selectedTabbarStrategy)
  123. /**
  124. * 是否需要隐藏原生 tabbar
  125. * CUSTOM_TABBAR_WITH_CACHE(2) 时,需要隐藏原生tabbar
  126. */
  127. export const needHideNativeTabbar = selectedTabbarStrategy === TABBAR_STRATEGY_MAP.CUSTOM_TABBAR_WITH_CACHE
  128. const _tabbarList = customTabbarEnable ? customTabbarList.map(item => ({ text: item.text, pagePath: item.pagePath })) : nativeTabbarList
  129. export const tabbarList = customTabbarEnable ? customTabbarList : nativeTabbarList
  130. const _tabbar: TabBar = {
  131. // 只有微信小程序支持 custom。App 和 H5 不生效
  132. custom: selectedTabbarStrategy === TABBAR_STRATEGY_MAP.CUSTOM_TABBAR_WITH_CACHE,
  133. color: '#999999',
  134. selectedColor: '#018d71',
  135. backgroundColor: '#F8F8F8',
  136. borderStyle: 'black',
  137. height: '50px',
  138. fontSize: '10px',
  139. iconWidth: '24px',
  140. spacing: '3px',
  141. list: _tabbarList as unknown as TabBar['list'],
  142. }
  143. // 0和1 需要显示底部的tabbar的各种配置,以利用缓存
  144. export const tabBar = tabbarCacheEnable ? _tabbar : undefined