index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <div class="growth-report-page" :style="{ height: `calc(100vh - ${tabBarHeight}px)` }">
  3. <!-- 天气遮罩 -->
  4. <div class="weather-mask" v-show="isExpanded" @click="handleMaskClick"></div>
  5. <!-- 头部 -->
  6. <div
  7. class="growth-report-header"
  8. :style="activeGardenTab === 'current' ? headerMotionStyle : undefined"
  9. >
  10. <weather-info ref="weatherInfoRef" :hasWeather="false" from="growth_report" class="weather-info"
  11. @weatherExpanded="weatherExpanded" @changeGarden="changeGarden" @changeGardenTab="changeGardenTab"
  12. @reportTabClick="handleReportTabClick" :isGarden="true" :gardenId="defaultGardenId" />
  13. </div>
  14. <!-- 农场列表 -->
  15. <div v-show="activeGardenTab === 'list'">
  16. <garden-list ref="gardenListRef" :garden-id="selectedGardenId" @loaded="handleGardenLoaded"
  17. @selectGarden="handleGardenSelected" />
  18. </div>
  19. <div class="report-content" v-show="activeGardenTab === 'current'">
  20. <div class="map-legend" :class="{ 'map-legend--en': locale === 'en' }">
  21. <div
  22. v-for="item in mapLegendItems"
  23. :key="item.key"
  24. class="map-legend__item"
  25. >
  26. <span class="map-legend__pill" :class="item.pillClass"></span>
  27. <span class="map-legend__text">{{ item.label }}</span>
  28. </div>
  29. </div>
  30. <div class="map-container" ref="mapContainer"></div>
  31. </div>
  32. <risk-report-panel
  33. v-show="activeGardenTab === 'current'"
  34. :view-type="panelViewType"
  35. :plot-detail="plotDetail"
  36. @expand-progress="panelExpandProgress = $event"
  37. @close-plot-detail="handleClosePlotDetail"
  38. />
  39. </div>
  40. </template>
  41. <script setup>
  42. import { computed, nextTick, onActivated, ref } from "vue";
  43. import { useRoute, useRouter } from "vue-router";
  44. import { useStore } from "vuex";
  45. import { useI18n } from "@/i18n";
  46. import weatherInfo from "@/components/weatherInfo.vue";
  47. import gardenList from "@/components/gardenList.vue";
  48. import GrowthReportMap from "./growthReportMap.js";
  49. import RiskReportPanel from "./components/RiskReportPanel.vue";
  50. import * as util from "@/common/ol_common.js";
  51. const DEFAULT_FARM_POINT = "POINT(113.6142086995688 23.585836479509055)";
  52. function isPointWkt(value) {
  53. return typeof value === "string" && /^POINT\s*\(/i.test(value.trim());
  54. }
  55. function isPolygonWkt(value) {
  56. return typeof value === "string" && /^(MULTI)?POLYGON\s*\(/i.test(value.trim());
  57. }
  58. function resolveFarmPolygonWkt(farm) {
  59. if (!farm || typeof farm === "string") return null;
  60. const polygon = farm.farm_polygon;
  61. return isPolygonWkt(polygon) ? polygon : null;
  62. }
  63. function resolveFarmLocationWkt(farm, polygonWkt) {
  64. if (typeof farm === "string" && isPointWkt(farm)) return farm;
  65. if (!farm || typeof farm === "string") return null;
  66. const candidates = [farm.wkt, farm.geom_wkt, farm.farm_location];
  67. for (const candidate of candidates) {
  68. if (isPointWkt(candidate)) return candidate;
  69. }
  70. if (polygonWkt) {
  71. const geom = util.wktCastGeom(polygonWkt);
  72. const extent = geom.getExtent();
  73. return `POINT(${(extent[0] + extent[2]) / 2} ${(extent[1] + extent[3]) / 2})`;
  74. }
  75. return null;
  76. }
  77. const store = useStore();
  78. const { t, locale } = useI18n();
  79. const route = useRoute();
  80. const router = useRouter();
  81. const tabBarHeight = computed(() => store.state.home.tabBarHeight);
  82. const isExpanded = ref(false);
  83. const weatherInfoRef = ref(null);
  84. const defaultGardenId = ref(null);
  85. const selectedGardenId = ref(null);
  86. const gardenListRef = ref(null);
  87. const activeGardenTab = ref("current");
  88. const panelExpandProgress = ref(0);
  89. const panelViewType = ref("risk");
  90. const plotDetail = ref(createDefaultPlotDetail());
  91. const HEADER_FADE_START = 0.68;
  92. function createDefaultPlotDetail() {
  93. const defaultImage = require("@/assets/img/home/banner.png");
  94. return {
  95. name: "地块名称",
  96. area: "289亩",
  97. categories: "荔枝、水稻",
  98. startTime: "2026/06/05",
  99. harvestTime: "2026/06/05",
  100. description: "当前处于物候期,有什么风险,做了什么农事,或者正在执行中,当前处于物候期,有什么风险",
  101. images: [defaultImage, defaultImage, defaultImage, defaultImage],
  102. };
  103. }
  104. function buildPlotDetailByTab(item) {
  105. const tabLabelMap = {
  106. soilImprovement: "土壤改良地块",
  107. rotationAdvice: "轮作建议地块",
  108. };
  109. return {
  110. ...createDefaultPlotDetail(),
  111. name: tabLabelMap[item.key] || "地块名称",
  112. };
  113. }
  114. const handleClosePlotDetail = () => {
  115. panelViewType.value = "risk";
  116. };
  117. const headerMotionStyle = computed(() => {
  118. const progress = panelExpandProgress.value;
  119. const fade = progress <= HEADER_FADE_START
  120. ? 0
  121. : (progress - HEADER_FADE_START) / (1 - HEADER_FADE_START);
  122. const opacity = 1 - fade;
  123. return {
  124. opacity,
  125. transform: `translateY(${-14 * fade}px) scale(${1 - fade * 0.04})`,
  126. pointerEvents: opacity < 0.15 ? "none" : "auto",
  127. };
  128. });
  129. const currentFarmName = ref("");
  130. const currentFarmVariety = ref(null);
  131. const mapContainer = ref(null);
  132. const growthReportMap = new GrowthReportMap();
  133. const syncMapByFarm = async (farm) => {
  134. const polygonWkt = resolveFarmPolygonWkt(farm);
  135. const location = resolveFarmLocationWkt(farm, polygonWkt) || DEFAULT_FARM_POINT;
  136. await nextTick();
  137. if (!mapContainer.value) return;
  138. if (growthReportMap.kmap) {
  139. growthReportMap.kmap.map?.updateSize?.();
  140. if (polygonWkt) {
  141. growthReportMap.setAreaGeometry([polygonWkt]);
  142. const coordinate = util.wktCastGeom(location).getFirstCoordinate();
  143. growthReportMap.setMapPoint(coordinate);
  144. growthReportMap.scheduleFitView();
  145. } else {
  146. growthReportMap.clearLayer();
  147. const coordinate = util.wktCastGeom(location).getFirstCoordinate();
  148. growthReportMap.setMapPosition(coordinate);
  149. }
  150. return;
  151. }
  152. growthReportMap.initMap(location, mapContainer.value);
  153. if (polygonWkt) {
  154. growthReportMap.setAreaGeometry([polygonWkt]);
  155. growthReportMap.scheduleFitView();
  156. }
  157. };
  158. const initGrowthReportMap = async () => {
  159. await syncMapByFarm();
  160. };
  161. const mapLegendItems = computed(() => [
  162. { key: "zone", label: t("agriFile.legendZone"), pillClass: "map-legend__pill--zone" },
  163. { key: "growth", label: t("agriFile.legendGrowth"), pillClass: "map-legend__pill--growth" },
  164. { key: "pest", label: t("agriFile.legendPest"), pillClass: "map-legend__pill--pest" },
  165. ]);
  166. const handleReportTabClick = (item) => {
  167. if (item.key === "historyRisk") {
  168. router.push(
  169. `/history_risk_report?farmVariety=${currentFarmVariety.value ?? ""}&currentFarmName=${currentFarmName.value ?? ""}`
  170. );
  171. return;
  172. }
  173. plotDetail.value = buildPlotDetailByTab(item);
  174. panelViewType.value = "plot";
  175. };
  176. const weatherExpanded = (isExpandedValue) => {
  177. isExpanded.value = isExpandedValue;
  178. };
  179. const handleMaskClick = () => {
  180. if (weatherInfoRef.value?.toggleExpand) {
  181. weatherInfoRef.value.toggleExpand();
  182. }
  183. };
  184. const changeGardenTab = (tab) => {
  185. activeGardenTab.value = tab;
  186. if (tab !== "current") {
  187. panelExpandProgress.value = 0;
  188. panelViewType.value = "risk";
  189. return;
  190. }
  191. nextTick(() => {
  192. growthReportMap.kmap?.map?.updateSize?.();
  193. growthReportMap.scheduleFitView?.();
  194. });
  195. };
  196. const handleGardenLoaded = ({ hasFarm }) => {
  197. weatherInfoRef.value?.setGardenLoaded?.(hasFarm);
  198. };
  199. const handleGardenSelected = (garden) => {
  200. selectedGardenId.value = garden?.id ?? null;
  201. syncMapByFarm(garden);
  202. weatherInfoRef.value?.setSelectedGarden?.(garden);
  203. };
  204. const changeGarden = (data) => {
  205. if (!data?.id) return;
  206. store.commit("home/SET_GARDEN_ID", data.id);
  207. selectedGardenId.value = data.id;
  208. currentFarmName.value = data.name ?? "";
  209. currentFarmVariety.value = data.farm_variety ?? null;
  210. syncMapByFarm(data);
  211. };
  212. onActivated(async () => {
  213. if (route.query?.farmId) {
  214. defaultGardenId.value = route.query.farmId;
  215. }
  216. const savedFarmId = localStorage.getItem("selectedFarmId");
  217. selectedGardenId.value = savedFarmId ? Number(savedFarmId) : null;
  218. gardenListRef.value?.refreshFarmList?.();
  219. await initGrowthReportMap();
  220. });
  221. </script>
  222. <style lang="scss" scoped>
  223. .growth-report-page {
  224. width: 100%;
  225. height: 100%;
  226. background: #F5F7FB;
  227. box-sizing: border-box;
  228. .weather-mask {
  229. position: fixed;
  230. top: 0;
  231. left: 0;
  232. width: 100%;
  233. height: 100%;
  234. background-color: rgba(0, 0, 0, 0.52);
  235. z-index: 11;
  236. }
  237. .growth-report-header {
  238. position: absolute;
  239. z-index: 12;
  240. left: 10px;
  241. top: 12px;
  242. width: calc(100% - 20px);
  243. will-change: transform, opacity;
  244. transform-origin: center top;
  245. .weather-info {
  246. width: 100%;
  247. position: relative;
  248. left: auto;
  249. top: auto;
  250. :deep(.garden-tabs) {
  251. .garden-item.left-item.active .current-name {
  252. color: #2199F8;
  253. font-weight: 600;
  254. }
  255. }
  256. }
  257. }
  258. .report-content {
  259. position: relative;
  260. height: 100%;
  261. box-sizing: border-box;
  262. .map-legend {
  263. position: absolute;
  264. top: 110px;
  265. right: 10px;
  266. z-index: 15;
  267. display: flex;
  268. align-items: center;
  269. gap: 10px;
  270. padding: 4px 10px;
  271. background: rgba(0, 0, 0, 0.46);
  272. backdrop-filter: blur(4px);
  273. border-radius: 4px;
  274. box-sizing: border-box;
  275. &--en {
  276. flex-direction: column;
  277. align-items: flex-start;
  278. gap: 6px;
  279. padding: 8px 10px;
  280. background: rgba(0, 0, 0, 0.72);
  281. border-radius: 6px;
  282. .map-legend__item {
  283. gap: 6px;
  284. }
  285. .map-legend__pill {
  286. width: 18px;
  287. height: 4px;
  288. flex-shrink: 0;
  289. }
  290. .map-legend__text {
  291. font-size: 11px;
  292. line-height: 1.2;
  293. white-space: nowrap;
  294. }
  295. }
  296. &__item {
  297. display: flex;
  298. align-items: center;
  299. gap: 5px;
  300. }
  301. &__pill {
  302. width: 16px;
  303. height: 5px;
  304. border-radius: 10px;
  305. &--zone {
  306. background: #13a27f;
  307. }
  308. &--growth {
  309. background: #ff9138;
  310. }
  311. &--pest {
  312. background: #e62e2d;
  313. }
  314. }
  315. &__text {
  316. font-size: 12px;
  317. color: #fff;
  318. }
  319. }
  320. .map-container {
  321. width: 100%;
  322. height: 100%;
  323. }
  324. }
  325. }
  326. </style>