App.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!--
  2. * @Author: your name
  3. * @Date: 2021-01-07 09:49:29
  4. * @LastEditTime: 2021-11-01 14:08:55
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \vue3-element-admin\src\App.vue
  8. -->
  9. <template>
  10. <!-- <router-view v-slot="{ Component }">
  11. <keep-alive exclude="GardenReport">
  12. <component :is="Component" />
  13. </keep-alive>
  14. </router-view> -->
  15. <el-config-provider :locale="elementLocale">
  16. <!-- keep-alive 必须常驻;用内部 component 的 v-if 区分是否缓存,避免切到非缓存页时整棵 keep-alive 被销毁 -->
  17. <router-view v-slot="{ Component }">
  18. <keep-alive>
  19. <component :is="Component" v-if="Component && route.meta.keepAlive" :key="route.name" />
  20. </keep-alive>
  21. <component :is="Component" v-if="Component && !route.meta.keepAlive" :key="route.name" />
  22. </router-view>
  23. <Tabbar class="tabbar" route fixed v-show="showTab" active-color="#FFD489" inactive-color="#B5B5B5">
  24. <tabbar-item replace to="/growth_report">
  25. <span>{{ t("tabbar.situationAssessment") }}</span>
  26. <template #icon="props">
  27. <img :class="props.active ? 'active-icon' : 'inactive-icon'"
  28. src="@/assets/img/tab_bar/home-active.png" />
  29. </template>
  30. </tabbar-item>
  31. <tabbar-item replace to="/diagnosis_report">
  32. <span>诊断报告</span>
  33. <template #icon="props">
  34. <img :class="props.active ? 'active-icon' : 'inactive-icon'"
  35. src="@/assets/img/tab_bar/report-active.png" />
  36. </template>
  37. </tabbar-item>
  38. <tabbar-item replace to="/agri_file">
  39. <span>{{ t("tabbar.agriFile") }}</span>
  40. <template #icon="props">
  41. <img :class="props.active ? 'active-icon' : 'inactive-icon'"
  42. src="@/assets/img/tab_bar/tree-active.png" />
  43. </template>
  44. </tabbar-item>
  45. <tabbar-item replace to="/work_execute">
  46. <span>{{ t("tabbar.schedule") }}</span>
  47. <template #icon="props">
  48. <img :class="props.active ? 'active-icon' : 'inactive-icon'"
  49. src="@/assets/img/tab_bar/task-active.png" />
  50. </template>
  51. </tabbar-item>
  52. </Tabbar>
  53. <!-- 开启底部安全区适配 -->
  54. <number-keyboard safe-area-inset-bottom />
  55. </el-config-provider>
  56. </template>
  57. <script setup>
  58. import { NumberKeyboard } from "vant";
  59. import { Tabbar, TabbarItem } from "vant";
  60. import { nextTick, watch, onMounted, ref, computed } from "vue";
  61. import { useRoute, useRouter } from "vue-router";
  62. import { useStore } from "vuex";
  63. import { NH, NZ, EXPERT } from "@/common/user_role";
  64. import zhCn from "element-plus/es/locale/lang/zh-cn";
  65. import en from "element-plus/es/locale/lang/en";
  66. import { useI18n } from "@/i18n";
  67. const { t, locale } = useI18n();
  68. const store = useStore();
  69. const elementLocale = computed(() => (locale.value === "en" ? en : zhCn));
  70. const route = useRoute();
  71. const router = useRouter();
  72. // 首页loading加载完才显示底部导航栏
  73. const showTab = ref(false);
  74. // 0: 农户, 1: 专家, 2:农资农服
  75. const curRole = ref(0);
  76. // USER_TYPE: 1 普通农户,2 托管农户
  77. const userType = ref("2");
  78. // 获取用户是否为托管农户,并缓存 USER_TYPE(与 dev_login.vue 逻辑一致)
  79. const fetchUserType = async () => {
  80. userType.value = "2";
  81. localStorage.setItem("USER_TYPE", "2");
  82. // // 检查是否有 token,没有 token 则不调用接口
  83. // const token = store.state.app.token || localStorage.getItem("token");
  84. // if (!token) {
  85. // // 没有 token 时使用缓存的值
  86. // userType.value = localStorage.getItem("USER_TYPE") || "1";
  87. // return;
  88. // }
  89. // try {
  90. // const { data } = await VE_API.farm.userFarmSelectOption({ userType: 2 });
  91. // if (Array.isArray(data) && data.length > 0) {
  92. // // 有托管农户数据
  93. // userType.value = "2";
  94. // localStorage.setItem("USER_TYPE", "2");
  95. // } else {
  96. // userType.value = "1";
  97. // localStorage.setItem("USER_TYPE", "1");
  98. // }
  99. // } catch (error) {
  100. // // 请求失败时保留默认值
  101. // userType.value = localStorage.getItem("USER_TYPE") || "1";
  102. // }
  103. };
  104. let tabBarHeight = 0;
  105. // 监听 token 变化,当有 token 时调用接口获取用户类型
  106. watch(
  107. () => store.state.app.token,
  108. (newToken) => {
  109. if (newToken) {
  110. fetchUserType();
  111. }
  112. },
  113. { immediate: true }
  114. );
  115. onMounted(() => {
  116. setTimeout(() => {
  117. curRole.value = store.state.app.curRole;
  118. if (route.meta.showTabbar) {
  119. showTab.value = true;
  120. }
  121. nextTick(() => {
  122. const tabBarElement = document.querySelector(".van-tabbar");
  123. if (tabBarElement) {
  124. tabBarHeight = tabBarElement.offsetHeight;
  125. localStorage.setItem("tabBarHeight", tabBarHeight);
  126. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  127. }
  128. });
  129. }, 500);
  130. });
  131. watch(
  132. () => route.name,
  133. (newName, oldName) => {
  134. // 在这里执行你的自定义逻辑
  135. showTab.value = false;
  136. if (route.meta.showTabbar) {
  137. showTab.value = true;
  138. }
  139. nextTick(() => {
  140. const tabBarElement = document.querySelector(".van-tabbar");
  141. if (tabBarElement) {
  142. tabBarHeight = tabBarElement.offsetHeight;
  143. localStorage.setItem("tabBarHeight", tabBarHeight);
  144. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  145. }
  146. curRole.value = store.state.app.curRole
  147. });
  148. },
  149. { immediate: false }
  150. );
  151. </script>
  152. <style lang="scss" scoped>
  153. .tabbar {
  154. position: fixed;
  155. pointer-events: all;
  156. z-index: 9999;
  157. --van-tabbar-background: #1D1D1D;
  158. --van-tabbar-item-active-background: #1D1D1D;
  159. .inactive-icon {
  160. filter: grayscale(100%);
  161. }
  162. &::after {
  163. border: none;
  164. }
  165. }
  166. </style>
  167. <style lang="scss">
  168. .van-overlay {
  169. backdrop-filter: blur(4px);
  170. }
  171. </style>