index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <div class="agri-file" :style="{ height: `calc(100vh - ${tabBarHeight}px)` }">
  3. <!-- 天气遮罩 -->
  4. <div class="weather-mask" v-show="isExpanded" @click="handleMaskClick"></div>
  5. <!-- 天气 -->
  6. <weather-info ref="weatherInfoRef" :hasWeather="false" from="monitor" class="weather-info"
  7. @weatherExpanded="weatherExpanded" @changeGarden="changeGarden" @changeGardenTab="changeGardenTab"
  8. :isGarden="true" :gardenId="defaultGardenId">
  9. <template #types-content>
  10. <div class="type-tabs">
  11. <div class="type-item" @click="changeType(farmVarietyName)"
  12. :class="{ 'type-item-active': activeType === farmVarietyName }">{{ farmVarietyName }}</div>
  13. <!-- <div class="type-item" @click="changeType('水稻')" :class="{ 'type-item-active': activeType === '水稻' }">{{ $t('水稻') }}</div> -->
  14. <!-- <div class="type-item" @click="changeType('柑橘')" :class="{ 'type-item-active': activeType === '柑橘' }">{{ $t('柑橘') }}</div> -->
  15. <!-- <div class="type-item" @click="changeType('小麦')" :class="{ 'type-item-active': activeType === '小麦' }">{{ $t('小麦') }}</div> -->
  16. </div>
  17. </template>
  18. </weather-info>
  19. <!-- 农场列表 -->
  20. <div v-show="activeGardenTab === 'list'">
  21. <garden-list ref="gardenListRef" :garden-id="selectedGardenId" @loaded="handleGardenLoaded"
  22. @selectGarden="handleGardenSelected" />
  23. </div>
  24. <div class="file-content" v-show="activeGardenTab === 'current'">
  25. <!-- 地图图例:分类标识 + 长势等级 -->
  26. <div class="map-legend">
  27. <div
  28. v-for="item in mapLegendItems"
  29. :key="item.key"
  30. class="map-legend__item"
  31. >
  32. <span class="map-legend__pill" :class="item.pillClass"></span>
  33. <span class="map-legend__text">{{ item.label }}</span>
  34. </div>
  35. </div>
  36. <!-- <div class="map-legend-box">
  37. <div class="map-legend-box__labels">
  38. <span
  39. v-for="(label, index) in growthScaleLabels"
  40. :key="index"
  41. >{{ label }}</span>
  42. </div>
  43. <div class="map-legend-box__bar"></div>
  44. </div> -->
  45. <div class="map-tool-bar">
  46. <div
  47. v-for="(item, index) in mapToolItems"
  48. :key="item.key"
  49. class="map-tool-bar__item"
  50. :class="{ 'map-tool-bar__item--active': activeMapTool === index }"
  51. @click="changeMapTool(index)"
  52. >
  53. <img
  54. class="map-tool-bar__icon"
  55. :src="require(`@/assets/img/map/tool-${index + 1}.png`)"
  56. :alt="item.label"
  57. />
  58. <span class="map-tool-bar__label">{{ item.label }}</span>
  59. </div>
  60. </div>
  61. <div class="map-container" ref="mapContainer"></div>
  62. <file-float
  63. v-model:active-tab="activeRecordTab"
  64. v-model:active-sub-tab="activeRecordSubTab"
  65. :farm-record-data="farmRecordData"
  66. :loading="farmRecordLoading"
  67. :crop-variety="farmVarietyName"
  68. />
  69. </div>
  70. </div>
  71. </template>
  72. <script setup>
  73. import { computed, nextTick, onActivated, ref, watch } from "vue";
  74. import { useRoute } from "vue-router";
  75. import { useStore } from "vuex";
  76. import weatherInfo from "@/components/weatherInfo.vue";
  77. import gardenList from "@/components/gardenList.vue";
  78. import fileFloat from "./components/fileFloat.vue";
  79. import FileMap, { RECORD_TAB_KEYS, recordsToCenterPoint } from "./fileMap.js";
  80. import { useI18n } from "@/i18n";
  81. const { t, locale } = useI18n();
  82. const store = useStore();
  83. const route = useRoute();
  84. const tabBarHeight = computed(() => store.state.home.tabBarHeight);
  85. const isExpanded = ref(false);
  86. const weatherInfoRef = ref(null);
  87. const defaultGardenId = ref(null);
  88. const selectedGardenId = ref(null);
  89. const gardenListRef = ref(null);
  90. const activeGardenTab = ref("current");
  91. const activeType = ref(null);
  92. const MAP_LEGEND_CONFIG = [
  93. { key: "zone", labelKey: "agriFile.legendZone", type: "zone" },
  94. { key: "growth", labelKey: "agriFile.legendGrowth", type: "growth" },
  95. { key: "pest", labelKey: "agriFile.legendPest", type: "pest" },
  96. ];
  97. const GROWTH_SCALE_LABEL_KEYS = [
  98. "agriFile.scaleNormal",
  99. "agriFile.scaleGood",
  100. "agriFile.scaleExcellent",
  101. ];
  102. const mapLegendItems = computed(() =>
  103. MAP_LEGEND_CONFIG.map(({ key, labelKey, type }) => ({
  104. key,
  105. label: t(labelKey),
  106. pillClass: `map-legend__pill--${type}`,
  107. }))
  108. );
  109. const growthScaleLabels = computed(() => GROWTH_SCALE_LABEL_KEYS.map((key) => t(key)));
  110. const MAP_TOOL_ITEMS = [
  111. { key: "all", label: "底图" },
  112. { key: "habitat", label: "植被" },
  113. { key: "light", label: "水体" },
  114. { key: "water", label: "降水" },
  115. // { key: "fengshui", label: "风水" },
  116. // { key: "soil", label: "土壤" },
  117. ];
  118. const mapToolItems = MAP_TOOL_ITEMS;
  119. const activeMapTool = ref(0);
  120. const changeMapTool = (index) => {
  121. activeMapTool.value = index;
  122. };
  123. const fileMap = new FileMap();
  124. const farmRecordData = ref({});
  125. const farmRecordLoading = ref(false);
  126. const activeRecordTab = ref(0);
  127. const activeRecordSubTab = ref(0);
  128. const getActiveTabKey = () =>
  129. RECORD_TAB_KEYS[activeRecordSubTab.value] || RECORD_TAB_KEYS[0];
  130. const getActiveRecords = () => farmRecordData.value[getActiveTabKey()] || [];
  131. const syncFarmRecordMap = () => {
  132. if (activeGardenTab.value !== "current") return;
  133. nextTick(() => {
  134. fileMap.setRecordPolygons(getActiveRecords(), getActiveTabKey());
  135. });
  136. };
  137. const initAgriFileMap = async () => {
  138. await nextTick();
  139. if (!mapContainer.value) return;
  140. fileMap.initMap(recordsToCenterPoint(getActiveRecords()), mapContainer.value);
  141. };
  142. const getFarmRecord = async () => {
  143. farmRecordLoading.value = true;
  144. try {
  145. const farmData = JSON.parse(localStorage.getItem("selectedFarmData"));
  146. const res = await VE_API.monitor.getFarmRecord({
  147. farm_id: selectedGardenId.value,
  148. variety_code: farmData.farm_variety,
  149. });
  150. if (res.code === 200) {
  151. farmRecordData.value = {
  152. phenology: res.data?.phenology ?? [],
  153. abnormal: res.data?.abnormal ?? [],
  154. farming: res.data?.farming ?? [],
  155. };
  156. syncFarmRecordMap();
  157. }
  158. } finally {
  159. farmRecordLoading.value = false;
  160. }
  161. };
  162. watch(activeRecordSubTab, syncFarmRecordMap);
  163. watch(activeGardenTab, syncFarmRecordMap);
  164. const weatherExpanded = (isExpandedValue) => {
  165. isExpanded.value = isExpandedValue;
  166. };
  167. const handleMaskClick = () => {
  168. if (weatherInfoRef.value?.toggleExpand) {
  169. weatherInfoRef.value.toggleExpand();
  170. }
  171. };
  172. const changeGardenTab = (tab) => {
  173. activeGardenTab.value = tab;
  174. };
  175. const changeType = (type) => {
  176. activeType.value = type;
  177. };
  178. const handleGardenLoaded = ({ hasFarm }) => {
  179. weatherInfoRef.value?.setGardenLoaded?.(hasFarm);
  180. };
  181. const handleGardenSelected = (garden) => {
  182. selectedGardenId.value = garden?.id ?? null;
  183. weatherInfoRef.value?.setSelectedGarden?.(garden);
  184. };
  185. const farmVarietyName = ref(null);
  186. const changeGarden = (data) => {
  187. if (!data?.id) return;
  188. store.commit("home/SET_GARDEN_ID", data.id);
  189. getFarmRecord();
  190. activeType.value = data.variety_name;
  191. farmVarietyName.value = data.variety_name;
  192. };
  193. const mapContainer = ref(null);
  194. onActivated(async () => {
  195. if (route.query?.farmId) {
  196. defaultGardenId.value = route.query.farmId;
  197. await getFarmRecord();
  198. }
  199. const savedFarmId = localStorage.getItem("selectedFarmId");
  200. selectedGardenId.value = savedFarmId ? Number(savedFarmId) : null;
  201. gardenListRef.value?.refreshFarmList?.();
  202. await initAgriFileMap();
  203. });
  204. </script>
  205. <style lang="scss" scoped>
  206. .agri-file {
  207. width: 100%;
  208. height: 100%;
  209. background: #F5F7FB;
  210. box-sizing: border-box;
  211. .weather-mask {
  212. position: fixed;
  213. top: 0;
  214. left: 0;
  215. width: 100%;
  216. height: 100%;
  217. background-color: rgba(0, 0, 0, 0.52);
  218. z-index: 11;
  219. }
  220. .weather-info {
  221. width: calc(100% - 20px);
  222. position: absolute;
  223. z-index: 12;
  224. left: 10px;
  225. top: 12px;
  226. }
  227. .file-content {
  228. position: relative;
  229. height: 100%;
  230. box-sizing: border-box;
  231. $map-legend-panel-bg: rgba(0, 0, 0, 0.46);
  232. $map-legend-text-color: #ffffff;
  233. $map-legend-text-size: 12px;
  234. $map-legend-scale-gradient: linear-gradient(
  235. 90deg,
  236. #007aff 0%,
  237. #00d4aa 25%,
  238. #ffe600 50%,
  239. #ff9500 75%,
  240. #ff3b30 100%
  241. );
  242. @mixin map-legend-panel-base {
  243. position: absolute;
  244. z-index: 15;
  245. background: $map-legend-panel-bg;
  246. backdrop-filter: blur(4px);
  247. box-sizing: border-box;
  248. }
  249. .map-legend {
  250. @include map-legend-panel-base;
  251. top: 110px;
  252. right: 12px;
  253. display: flex;
  254. align-items: center;
  255. justify-content: space-around;
  256. gap: 10px;
  257. padding: 8px 10px;
  258. border-radius: 4px;
  259. &__item {
  260. display: flex;
  261. align-items: center;
  262. gap: 5px;
  263. }
  264. &__pill {
  265. width: 16px;
  266. height: 5px;
  267. border-radius: 10px;
  268. &--zone {
  269. background: #1c9e80;
  270. }
  271. &--growth {
  272. background: #ff953d;
  273. }
  274. &--pest {
  275. background: #e03131;
  276. }
  277. }
  278. &__text {
  279. font-size: $map-legend-text-size;
  280. color: $map-legend-text-color;
  281. }
  282. }
  283. .map-legend-box {
  284. @include map-legend-panel-base;
  285. top: 150px;
  286. right: 10px;
  287. width: 222px;
  288. padding: 5px 10px;
  289. border-radius: 5px;
  290. &__labels {
  291. display: flex;
  292. justify-content: space-between;
  293. align-items: center;
  294. margin-bottom: 8px;
  295. font-size: $map-legend-text-size;
  296. color: $map-legend-text-color;
  297. }
  298. &__bar {
  299. height: 8px;
  300. border-radius: 4px;
  301. background: $map-legend-scale-gradient;
  302. }
  303. }
  304. .map-tool-bar {
  305. position: absolute;
  306. left: 12px;
  307. top: 50%;
  308. transform: translateY(-78%);
  309. z-index: 15;
  310. display: flex;
  311. flex-direction: column;
  312. align-items: center;
  313. padding: 10px 6px;
  314. background: #ffffff;
  315. border-radius: 7px;
  316. box-shadow: 0px 2.3px 2.3px 0px #0000001A;
  317. box-sizing: border-box;
  318. &__item {
  319. display: flex;
  320. flex-direction: column;
  321. align-items: center;
  322. justify-content: center;
  323. padding: 10px 5px;
  324. color: #C1C1C1;
  325. .map-tool-bar__icon {
  326. width: 18px;
  327. height: 16px;
  328. margin-bottom: 3px;
  329. filter: grayscale(1);
  330. }
  331. &--active {
  332. color: #2199f8;
  333. .map-tool-bar__icon {
  334. filter: grayscale(0);
  335. }
  336. }
  337. }
  338. &__label {
  339. font-size: 12px;
  340. }
  341. }
  342. .map-container {
  343. width: 100%;
  344. height: 100%;
  345. }
  346. }
  347. .type-tabs {
  348. width: 100%;
  349. background: #FFF;
  350. display: flex;
  351. align-items: center;
  352. flex-wrap: wrap;
  353. gap: 8px;
  354. padding: 8px;
  355. .type-item {
  356. height: 28px;
  357. line-height: 28px;
  358. text-align: center;
  359. padding: 0 6px;
  360. min-width: 78px;
  361. color: #9A9A9A;
  362. background: #EFEFEF;
  363. box-sizing: border-box;
  364. border-radius: 2px;
  365. &.type-item-active {
  366. background: #2199F8;
  367. color: #fff;
  368. }
  369. }
  370. }
  371. }
  372. </style>