pestInteract.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div class="pest-interact-page">
  3. <custom-header :name="t('agriFile.pestInteract')" />
  4. <div class="pest-interact-content">
  5. <div class="map-container" ref="mapContainer"></div>
  6. <div class="search-bar">
  7. <location-search class="search-bar__search" :user-location="userLocation"
  8. @change="handleLocationChange" />
  9. </div>
  10. <div class="map-tip">{{ t("agriFile.selectAbnormalPlot") }}</div>
  11. <div class="locate-btn" @click="handleLocate">
  12. <img class="locate-btn__icon" src="@/assets/img/map/map-icon.png" alt="" />
  13. </div>
  14. </div>
  15. <div class="custom-bottom-fixed-btns">
  16. <div class="bottom-btn secondary-btn" @click="handleCancel">{{ t("agriFile.cancel") }}</div>
  17. <div class="bottom-btn primary-btn" @click="handleConfirm">{{ t("agriFile.confirmPlot") }}</div>
  18. </div>
  19. <album-upload-popup v-model:show="showUploadPopup" />
  20. </div>
  21. </template>
  22. <script setup>
  23. import { nextTick, onActivated, ref } from "vue";
  24. import { useRouter } from "vue-router";
  25. import { useStore } from "vuex";
  26. import { ElMessage } from "element-plus";
  27. import customHeader from "@/components/customHeader.vue";
  28. import locationSearch from "@/components/pageComponents/locationSearch.vue";
  29. import albumUploadPopup from "../components/albumUploadPopup.vue";
  30. import FileMap from "../fileMap";
  31. import * as util from "@/common/ol_common.js";
  32. import { useI18n } from "@/i18n";
  33. const { t } = useI18n();
  34. const router = useRouter();
  35. const store = useStore();
  36. const mapContainer = ref(null);
  37. const fileMap = new FileMap();
  38. const selectedZoneId = ref(null);
  39. const showUploadPopup = ref(false);
  40. let clickBound = false;
  41. const DEFAULT_MAP_LOCATION = "POINT(113.6142086995688 23.585836479509055)";
  42. const userLocation = ref(
  43. store.state.home.miniUserLocation ||
  44. localStorage.getItem("MINI_USER_LOCATION") ||
  45. "113.61702297075017,23.584863449735067"
  46. );
  47. const MOCK_ZONES = [
  48. { id: 1, nameKey: "agriFile.legendZone", dlng: -0.00115, dlat: 0.00055, delta: 0.00155 },
  49. ];
  50. function squarePolygonWkt(lng, lat, delta = 0.0014) {
  51. const ring = [
  52. [lng - delta, lat + delta * 0.7],
  53. [lng + delta * 0.35, lat + delta],
  54. [lng + delta, lat - delta * 0.2],
  55. [lng + delta * 0.15, lat - delta],
  56. [lng - delta * 0.85, lat - delta * 0.45],
  57. [lng - delta, lat + delta * 0.7],
  58. ];
  59. return `POLYGON((${ring.map((point) => point.join(" ")).join(", ")}))`;
  60. }
  61. function getFarmData() {
  62. try {
  63. return JSON.parse(localStorage.getItem("selectedFarmData") || "{}");
  64. } catch {
  65. return {};
  66. }
  67. }
  68. function isPointWkt(value) {
  69. return typeof value === "string" && /^POINT\s*\(/i.test(value.trim());
  70. }
  71. function wkbHexToPointWkt(hex) {
  72. const text = String(hex || "").trim();
  73. if (!/^[0-9a-fA-F]+$/.test(text) || text.length < 42) return null;
  74. const bytes = new Uint8Array(text.length / 2);
  75. for (let i = 0; i < bytes.length; i++) {
  76. bytes[i] = parseInt(text.slice(i * 2, i * 2 + 2), 16);
  77. }
  78. const view = new DataView(bytes.buffer);
  79. const little = view.getUint8(0) === 1;
  80. let type = view.getUint32(1, little);
  81. const hasSrid = (type & 0x20000000) !== 0;
  82. type &= 0xff;
  83. if (type !== 1) return null;
  84. let offset = 5;
  85. if (hasSrid) offset += 4;
  86. if (offset + 16 > bytes.length) return null;
  87. const lng = view.getFloat64(offset, little);
  88. const lat = view.getFloat64(offset + 8, little);
  89. if (!Number.isFinite(lng) || !Number.isFinite(lat)) return null;
  90. return `POINT(${lng} ${lat})`;
  91. }
  92. function toPointWkt(value) {
  93. if (!value || typeof value !== "string") return null;
  94. if (isPointWkt(value)) return value.trim();
  95. return wkbHexToPointWkt(value);
  96. }
  97. function getFarmMapLocation() {
  98. const farmData = getFarmData();
  99. const candidates = [farmData.wkt, farmData.geom_wkt, farmData.farm_location];
  100. for (const item of candidates) {
  101. const pointWkt = toPointWkt(item);
  102. if (pointWkt) return pointWkt;
  103. }
  104. return DEFAULT_MAP_LOCATION;
  105. }
  106. function getDefaultMapLocation() {
  107. return (
  108. toPointWkt(localStorage.getItem("MINI_USER_LOCATION_POINT")) ||
  109. toPointWkt(store.state.home.miniUserLocationPoint) ||
  110. getFarmMapLocation()
  111. );
  112. }
  113. function getDefaultMapCoordinate() {
  114. return util.wktCastGeom(getDefaultMapLocation()).getFirstCoordinate();
  115. }
  116. function loadPlotLayers() {
  117. const [lng, lat] = getDefaultMapCoordinate();
  118. const labels = MOCK_ZONES.map((item) => ({
  119. name: t(item.nameKey),
  120. longitude: lng + item.dlng,
  121. latitude: lat + item.dlat,
  122. }));
  123. const zoneRecords = MOCK_ZONES.map((item) => ({
  124. id: item.id,
  125. zone_name: "",
  126. polygon: squarePolygonWkt(lng + item.dlng, lat + item.dlat, item.delta),
  127. }));
  128. fileMap.setRecordPolygons(zoneRecords, "album");
  129. fileMap.setAlbumMarkers({ labels, photos: [] });
  130. fileMap.recordPolygonLayer.source.getFeatures().forEach((feature, index) => {
  131. feature.set("zone_id", MOCK_ZONES[index]?.id);
  132. });
  133. selectedZoneId.value = MOCK_ZONES[0]?.id ?? null;
  134. }
  135. function onMapClick(evt) {
  136. const feature = fileMap.kmap?.map?.forEachFeatureAtPixel(evt.pixel, (hit) => {
  137. if (hit.get("zone_id")) return hit;
  138. return undefined;
  139. });
  140. if (!feature) return;
  141. selectedZoneId.value = feature.get("zone_id");
  142. }
  143. function bindMapClick() {
  144. if (clickBound || !fileMap.kmap?.map) return;
  145. clickBound = true;
  146. fileMap.kmap.map.on("singleclick", onMapClick);
  147. }
  148. const initMap = async () => {
  149. await nextTick();
  150. if (!mapContainer.value) return;
  151. fileMap.initMap(getDefaultMapLocation(), mapContainer.value);
  152. loadPlotLayers();
  153. fileMap.kmap?.map?.updateSize?.();
  154. bindMapClick();
  155. };
  156. const handleLocationChange = (payload) => {
  157. if (!payload?.coordinateArray || !fileMap.kmap) return;
  158. fileMap.kmap.getView().animate({
  159. center: payload.coordinateArray,
  160. zoom: 16,
  161. duration: 0,
  162. });
  163. };
  164. const handleLocate = () => {
  165. if (!fileMap.kmap) return;
  166. fileMap.kmap.getView().animate({
  167. center: getDefaultMapCoordinate(),
  168. zoom: 16,
  169. duration: 0,
  170. });
  171. };
  172. const handleCancel = () => {
  173. router.back();
  174. };
  175. const handleConfirm = () => {
  176. if (!selectedZoneId.value) {
  177. ElMessage.warning(t("agriFile.pleaseSelectPlot"));
  178. return;
  179. }
  180. sessionStorage.setItem("SELECTED_ABNORMAL_PLOT", String(selectedZoneId.value));
  181. showUploadPopup.value = true;
  182. };
  183. onActivated(() => {
  184. initMap();
  185. });
  186. </script>
  187. <style lang="scss" scoped>
  188. .pest-interact-page {
  189. display: flex;
  190. flex-direction: column;
  191. width: 100%;
  192. height: 100vh;
  193. overflow: hidden;
  194. background: #fff;
  195. }
  196. .pest-interact-content {
  197. position: relative;
  198. flex: 1;
  199. min-height: 0;
  200. overflow: hidden;
  201. .map-container {
  202. width: 100%;
  203. height: 100%;
  204. }
  205. .search-bar {
  206. position: absolute;
  207. top: 12px;
  208. left: 12px;
  209. right: 12px;
  210. z-index: 2;
  211. display: flex;
  212. align-items: center;
  213. height: 40px;
  214. padding: 0 4px 0 12px;
  215. border-radius: 20px;
  216. background: rgba(0, 0, 0, 0.45);
  217. box-sizing: border-box;
  218. &__search {
  219. flex: 1;
  220. min-width: 0;
  221. :deep(.el-select__wrapper) {
  222. background: transparent;
  223. box-shadow: none;
  224. border: none;
  225. min-height: 40px;
  226. padding-left: 0;
  227. }
  228. :deep(.el-select__placeholder),
  229. :deep(.el-select__input) {
  230. color: rgba(255, 255, 255, 0.7);
  231. }
  232. :deep(.el-icon) {
  233. color: rgba(255, 255, 255, 0.85);
  234. }
  235. }
  236. }
  237. .map-tip {
  238. position: absolute;
  239. top: 64px;
  240. left: 50%;
  241. z-index: 2;
  242. transform: translateX(-50%);
  243. padding: 8px 30px;
  244. border-radius: 25px;
  245. background: rgba(0, 0, 0, 0.5);
  246. color: #7CC5FF;
  247. font-size: 12px;
  248. white-space: nowrap;
  249. }
  250. .locate-btn {
  251. position: absolute;
  252. right: 12px;
  253. bottom: 24px;
  254. z-index: 2;
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. width: 36px;
  259. height: 36px;
  260. border-radius: 8px;
  261. background: #fff;
  262. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  263. &__icon {
  264. width: 16px;
  265. height: 18px;
  266. }
  267. }
  268. }
  269. .custom-bottom-fixed-btns {
  270. position: relative;
  271. justify-content: space-between;
  272. .bottom-btn {
  273. padding: 10px 20px;
  274. border-radius: 25px;
  275. }
  276. .secondary-btn {
  277. color: #666;
  278. border: 1px solid rgba(153, 153, 153, 0.5);
  279. }
  280. .primary-btn {
  281. background: #2199f8;
  282. }
  283. }
  284. </style>