App.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <script setup lang="ts">
  2. import { onHide, onLaunch, onShow } from '@dcloudio/uni-app'
  3. import { watch, onMounted } from 'vue'
  4. import { usePageAuth } from '@/hooks/usePageAuth'
  5. import { useConfigStore } from '@/store'
  6. import { t } from '@/i18n'
  7. import { useLangStore } from '@/store/lang'
  8. import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
  9. usePageAuth()
  10. const configStore = useConfigStore()
  11. const langStore = useLangStore()
  12. onLaunch(() => {
  13. console.log('App Launch')
  14. // 获取公共配置
  15. configStore.fetchPublicConfig().catch((error) => {
  16. console.error('获取公共配置失败:', error)
  17. })
  18. })
  19. onShow(() => {
  20. console.log('App Show')
  21. // 使用setTimeout延迟执行,确保tabBar已经初始化
  22. setTimeout(() => {
  23. updateTabBarText()
  24. }, 100)
  25. })
  26. // 动态更新tabBar文本
  27. function updateTabBarText() {
  28. try {
  29. // 设置首页tabBar文本
  30. uni.setTabBarItem({
  31. index: 0,
  32. text: t('tabBar.home'),
  33. success: () => {},
  34. fail: (err) => {
  35. console.log('设置首页tabBar文本失败:', err)
  36. }
  37. })
  38. // 设置配网tabBar文本
  39. uni.setTabBarItem({
  40. index: 1,
  41. text: t('tabBar.deviceConfig'),
  42. success: () => {},
  43. fail: (err) => {
  44. console.log('设置配网tabBar文本失败:', err)
  45. }
  46. })
  47. // 设置系统tabBar文本
  48. uni.setTabBarItem({
  49. index: 2,
  50. text: t('tabBar.settings'),
  51. success: () => {},
  52. fail: (err) => {
  53. console.log('设置系统tabBar文本失败:', err)
  54. }
  55. })
  56. } catch (error) {
  57. console.log('更新tabBar文本时出错:', error)
  58. }
  59. }
  60. // 监听语言切换事件
  61. onMounted(() => {
  62. // 监听语言变化,当语言改变时自动更新tabBar文本
  63. watch(() => langStore.currentLang, () => {
  64. console.log('语言已切换,更新tabBar文本')
  65. // 语言切换后立即更新tabBar文本
  66. updateTabBarText()
  67. })
  68. })
  69. onHide(() => {
  70. console.log('App Hide')
  71. })
  72. </script>
  73. <style lang="scss">
  74. swiper,
  75. scroll-view {
  76. flex: 1;
  77. height: 100%;
  78. overflow: hidden;
  79. }
  80. image {
  81. width: 100%;
  82. height: 100%;
  83. vertical-align: middle;
  84. }
  85. </style>