App.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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="/growth_report">
  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. <tabbar-item replace to="/youwei_trace">
  61. <span>有味溯源</span>
  62. <template #icon="props">
  63. <img
  64. :src="
  65. props.active
  66. ? require('@/assets/img/tab_bar/farm-active.png')
  67. : require('@/assets/img/tab_bar/farm.png')
  68. "
  69. />
  70. </template>
  71. </tabbar-item>
  72. <!-- </template> -->
  73. <!-- 普通农户:首页、农事服务、用户管理、个人中心(保留原逻辑) -->
  74. <!-- <template v-else-if="userType == 1">
  75. <tabbar-item replace to="/home" v-if="curRole == 0 || curRole == 2">
  76. <span>首页</span>
  77. <template #icon="props">
  78. <img
  79. :src="
  80. props.active
  81. ? require('@/assets/img/tab_bar/home-active.png')
  82. : require('@/assets/img/tab_bar/home.png')
  83. "
  84. />
  85. </template>
  86. </tabbar-item>
  87. <tabbar-item replace to="/task_condition">
  88. <span>农事服务</span>
  89. <template #icon="props">
  90. <img
  91. :src="
  92. props.active
  93. ? require('@/assets/img/tab_bar/service-active.png')
  94. : require('@/assets/img/tab_bar/service.png')
  95. "
  96. />
  97. </template>
  98. </tabbar-item>
  99. <tabbar-item replace to="/user">
  100. <span>用户管理</span>
  101. <template #icon="props">
  102. <img
  103. :src="
  104. props.active
  105. ? require('@/assets/img/tab_bar/user-active.png')
  106. : require('@/assets/img/tab_bar/user.png')
  107. "
  108. />
  109. </template>
  110. </tabbar-item>
  111. <tabbar-item replace to="/mine">
  112. <span>个人中心</span>
  113. <template #icon="props">
  114. <img
  115. :src="
  116. props.active
  117. ? require('@/assets/img/tab_bar/mine-active.png')
  118. : require('@/assets/img/tab_bar/mine.png')
  119. "
  120. />
  121. </template>
  122. </tabbar-item>
  123. </template> -->
  124. </Tabbar>
  125. <!-- 开启底部安全区适配 -->
  126. <number-keyboard safe-area-inset-bottom />
  127. </template>
  128. <script setup>
  129. import { NumberKeyboard } from "vant";
  130. import { Tabbar, TabbarItem } from "vant";
  131. import { nextTick, watch, onMounted, ref, computed } from "vue";
  132. import { useRoute, useRouter } from "vue-router";
  133. import { useStore } from "vuex";
  134. import { NH, NZ, EXPERT } from "@/common/user_role";
  135. const store = useStore();
  136. const route = useRoute();
  137. const router = useRouter();
  138. // 首页loading加载完才显示底部导航栏
  139. const showTab = ref(false);
  140. // 0: 农户, 1: 专家, 2:农资农服
  141. const curRole = ref(0);
  142. // USER_TYPE: 1 普通农户,2 托管农户
  143. const userType = ref("2");
  144. // 获取用户是否为托管农户,并缓存 USER_TYPE(与 dev_login.vue 逻辑一致)
  145. const fetchUserType = async () => {
  146. userType.value = "2";
  147. localStorage.setItem("USER_TYPE", "2");
  148. // // 检查是否有 token,没有 token 则不调用接口
  149. // const token = store.state.app.token || localStorage.getItem("token");
  150. // if (!token) {
  151. // // 没有 token 时使用缓存的值
  152. // userType.value = localStorage.getItem("USER_TYPE") || "1";
  153. // return;
  154. // }
  155. // try {
  156. // const { data } = await VE_API.farm.userFarmSelectOption({ userType: 2 });
  157. // if (Array.isArray(data) && data.length > 0) {
  158. // // 有托管农户数据
  159. // userType.value = "2";
  160. // localStorage.setItem("USER_TYPE", "2");
  161. // } else {
  162. // userType.value = "1";
  163. // localStorage.setItem("USER_TYPE", "1");
  164. // }
  165. // } catch (error) {
  166. // // 请求失败时保留默认值
  167. // userType.value = localStorage.getItem("USER_TYPE") || "1";
  168. // }
  169. };
  170. let tabBarHeight = 0;
  171. // 监听 token 变化,当有 token 时调用接口获取用户类型
  172. watch(
  173. () => store.state.app.token,
  174. (newToken) => {
  175. if (newToken) {
  176. fetchUserType();
  177. }
  178. },
  179. { immediate: true }
  180. );
  181. onMounted(() => {
  182. setTimeout(() => {
  183. curRole.value = store.state.app.curRole;
  184. if (route.meta.showTabbar) {
  185. showTab.value = true;
  186. }
  187. nextTick(() => {
  188. const tabBarElement = document.querySelector(".van-tabbar");
  189. if (tabBarElement) {
  190. tabBarHeight = tabBarElement.offsetHeight;
  191. localStorage.setItem("tabBarHeight", tabBarHeight);
  192. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  193. }
  194. });
  195. }, 500);
  196. });
  197. watch(
  198. () => route.name,
  199. (newName, oldName) => {
  200. // 在这里执行你的自定义逻辑
  201. showTab.value = false;
  202. if (route.meta.showTabbar) {
  203. showTab.value = true;
  204. }
  205. nextTick(() => {
  206. const tabBarElement = document.querySelector(".van-tabbar");
  207. if (tabBarElement) {
  208. tabBarHeight = tabBarElement.offsetHeight;
  209. localStorage.setItem("tabBarHeight", tabBarHeight);
  210. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  211. }
  212. curRole.value = store.state.app.curRole
  213. });
  214. },
  215. { immediate: false }
  216. );
  217. </script>
  218. <style lang="scss" scoped>
  219. .tabbar {
  220. position: fixed;
  221. pointer-events: all;
  222. z-index: 9999;
  223. }
  224. </style>