pestInteract.vue 10 KB

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