App.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. <router-view v-slot="{ Component }">
  16. <keep-alive v-if="route.meta && route.meta.keepAlive">
  17. <component :is="Component" />
  18. </keep-alive>
  19. <component v-else :is="Component" />
  20. </router-view>
  21. <Tabbar class="tabbar" route fixed v-show="showTab" active-color="#2199F8" inactive-color="#898989">
  22. <!-- 托管农户:首页、作物档案、农事记录 -->
  23. <template v-if="userType == 2">
  24. <tabbar-item replace to="/home">
  25. <span>首页</span>
  26. <template #icon="props">
  27. <img
  28. :src="
  29. props.active
  30. ? require('@/assets/img/tab_bar/home-active.png')
  31. : require('@/assets/img/tab_bar/home.png')
  32. "
  33. />
  34. </template>
  35. </tabbar-item>
  36. <tabbar-item replace to="/monitor">
  37. <span>作物档案</span>
  38. <template #icon="props">
  39. <img
  40. :src="
  41. props.active
  42. ? require('@/assets/img/tab_bar/tree-active.png')
  43. : require('@/assets/img/tab_bar/tree.png')
  44. "
  45. />
  46. </template>
  47. </tabbar-item>
  48. <tabbar-item replace to="/agri_record">
  49. <span>农事记录</span>
  50. <template #icon="props">
  51. <img
  52. :src="
  53. props.active
  54. ? require('@/assets/img/tab_bar/task-active.png')
  55. : require('@/assets/img/tab_bar/task.png')
  56. "
  57. />
  58. </template>
  59. </tabbar-item>
  60. </template>
  61. <!-- 普通农户:首页、农事服务、用户管理、个人中心(保留原逻辑) -->
  62. <template v-else-if="userType == 1">
  63. <tabbar-item replace to="/home" v-if="curRole == 0 || curRole == 2">
  64. <span>首页</span>
  65. <template #icon="props">
  66. <img
  67. :src="
  68. props.active
  69. ? require('@/assets/img/tab_bar/home-active.png')
  70. : require('@/assets/img/tab_bar/home.png')
  71. "
  72. />
  73. </template>
  74. </tabbar-item>
  75. <tabbar-item replace to="/task_condition">
  76. <span>农事服务</span>
  77. <template #icon="props">
  78. <img
  79. :src="
  80. props.active
  81. ? require('@/assets/img/tab_bar/service-active.png')
  82. : require('@/assets/img/tab_bar/service.png')
  83. "
  84. />
  85. </template>
  86. </tabbar-item>
  87. <tabbar-item replace to="/user">
  88. <span>用户管理</span>
  89. <template #icon="props">
  90. <img
  91. :src="
  92. props.active
  93. ? require('@/assets/img/tab_bar/user-active.png')
  94. : require('@/assets/img/tab_bar/user.png')
  95. "
  96. />
  97. </template>
  98. </tabbar-item>
  99. <tabbar-item replace to="/mine">
  100. <span>个人中心</span>
  101. <template #icon="props">
  102. <img
  103. :src="
  104. props.active
  105. ? require('@/assets/img/tab_bar/mine-active.png')
  106. : require('@/assets/img/tab_bar/mine.png')
  107. "
  108. />
  109. </template>
  110. </tabbar-item>
  111. </template>
  112. </Tabbar>
  113. <!-- 开启底部安全区适配 -->
  114. <number-keyboard safe-area-inset-bottom />
  115. </template>
  116. <script setup>
  117. import { NumberKeyboard } from "vant";
  118. import { Tabbar, TabbarItem } from "vant";
  119. import { nextTick, watch, onMounted, ref, computed } from "vue";
  120. import { useRoute, useRouter } from "vue-router";
  121. import { useStore } from "vuex";
  122. import { NH, NZ, EXPERT } from "@/common/user_role";
  123. const store = useStore();
  124. const route = useRoute();
  125. const router = useRouter();
  126. // 首页loading加载完才显示底部导航栏
  127. const showTab = ref(false);
  128. // 0: 农户, 1: 专家, 2:农资农服
  129. const curRole = ref(0);
  130. let tabBarHeight = 0;
  131. // USER_TYPE: 1 普通农户,2 托管农户
  132. const userType = ref(localStorage.getItem("USER_TYPE") || null);
  133. // 防止重复请求的标志
  134. const isFetchingUserType = ref(false);
  135. // 获取用户是否为托管农户,并缓存 USER_TYPE
  136. const fetchUserType = async () => {
  137. if (isFetchingUserType.value) return;
  138. isFetchingUserType.value = true;
  139. try {
  140. const { data } = await VE_API.farm.userFarmSelectOption({ userType: 2 });
  141. if (Array.isArray(data) && data.length > 0) {
  142. localStorage.setItem("USER_TYPE", "2");
  143. userType.value = "2";
  144. } else {
  145. localStorage.setItem("USER_TYPE", "1");
  146. userType.value = "1";
  147. }
  148. } catch (error) {
  149. // 获取用户类型失败时的错误处理
  150. } finally {
  151. isFetchingUserType.value = false;
  152. }
  153. };
  154. // 监听 token 变化,当有 token 时获取用户类型
  155. const token = computed(() => store.getters.token);
  156. watch(token, async (newToken) => {
  157. if (newToken) {
  158. await fetchUserType();
  159. }
  160. }, { immediate: true });
  161. onMounted(() => {
  162. setTimeout(() => {
  163. curRole.value = store.state.app.curRole;
  164. if (route.meta.showTabbar) {
  165. showTab.value = true;
  166. }
  167. nextTick(() => {
  168. const tabBarElement = document.querySelector(".van-tabbar");
  169. if (tabBarElement) {
  170. tabBarHeight = tabBarElement.offsetHeight;
  171. localStorage.setItem("tabBarHeight", tabBarHeight);
  172. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  173. }
  174. });
  175. }, 500);
  176. });
  177. watch(
  178. () => route.name,
  179. (newName, oldName) => {
  180. // 在这里执行你的自定义逻辑
  181. showTab.value = false;
  182. if (route.meta.showTabbar) {
  183. showTab.value = true;
  184. }
  185. nextTick(() => {
  186. const tabBarElement = document.querySelector(".van-tabbar");
  187. if (tabBarElement) {
  188. tabBarHeight = tabBarElement.offsetHeight;
  189. localStorage.setItem("tabBarHeight", tabBarHeight);
  190. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  191. }
  192. curRole.value = store.state.app.curRole
  193. });
  194. },
  195. { immediate: false }
  196. );
  197. </script>
  198. <style lang="scss" scoped>
  199. .tabbar {
  200. position: fixed;
  201. pointer-events: all;
  202. z-index: 9999;
  203. }
  204. </style>