App.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // USER_TYPE: 1 普通农户,2 托管农户
  131. const userType = ref(localStorage.getItem("USER_TYPE") || "1");
  132. // 获取用户是否为托管农户,并缓存 USER_TYPE(与 dev_login.vue 逻辑一致)
  133. const fetchUserType = async () => {
  134. // 检查是否有 token,没有 token 则不调用接口
  135. const token = store.state.app.token || localStorage.getItem("token");
  136. if (!token) {
  137. // 没有 token 时使用缓存的值
  138. userType.value = localStorage.getItem("USER_TYPE") || "1";
  139. return;
  140. }
  141. try {
  142. const { data } = await VE_API.farm.userFarmSelectOption({ userType: 2 });
  143. if (Array.isArray(data) && data.length > 0) {
  144. // 有托管农户数据
  145. userType.value = "2";
  146. localStorage.setItem("USER_TYPE", "2");
  147. } else {
  148. userType.value = "1";
  149. localStorage.setItem("USER_TYPE", "1");
  150. }
  151. } catch (error) {
  152. // 请求失败时保留默认值
  153. userType.value = localStorage.getItem("USER_TYPE") || "1";
  154. }
  155. };
  156. let tabBarHeight = 0;
  157. // 监听 token 变化,当有 token 时调用接口获取用户类型
  158. watch(
  159. () => store.state.app.token,
  160. (newToken) => {
  161. if (newToken) {
  162. fetchUserType();
  163. }
  164. },
  165. { immediate: true }
  166. );
  167. onMounted(() => {
  168. setTimeout(() => {
  169. curRole.value = store.state.app.curRole;
  170. if (route.meta.showTabbar) {
  171. showTab.value = true;
  172. }
  173. nextTick(() => {
  174. const tabBarElement = document.querySelector(".van-tabbar");
  175. if (tabBarElement) {
  176. tabBarHeight = tabBarElement.offsetHeight;
  177. localStorage.setItem("tabBarHeight", tabBarHeight);
  178. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  179. }
  180. });
  181. }, 500);
  182. });
  183. watch(
  184. () => route.name,
  185. (newName, oldName) => {
  186. // 在这里执行你的自定义逻辑
  187. showTab.value = false;
  188. if (route.meta.showTabbar) {
  189. showTab.value = true;
  190. }
  191. nextTick(() => {
  192. const tabBarElement = document.querySelector(".van-tabbar");
  193. if (tabBarElement) {
  194. tabBarHeight = tabBarElement.offsetHeight;
  195. localStorage.setItem("tabBarHeight", tabBarHeight);
  196. store.commit("home/SET_TAB_BAR_HEIGHT", tabBarHeight);
  197. }
  198. curRole.value = store.state.app.curRole
  199. });
  200. },
  201. { immediate: false }
  202. );
  203. </script>
  204. <style lang="scss" scoped>
  205. .tabbar {
  206. position: fixed;
  207. pointer-events: all;
  208. z-index: 9999;
  209. }
  210. </style>