浏览代码

fix: 创建农场

lxf 4 天之前
父节点
当前提交
484928aa47
共有 1 个文件被更改,包括 12 次插入7 次删除
  1. 12 7
      src/views/old_mini/create_farm/index.vue

+ 12 - 7
src/views/old_mini/create_farm/index.vue

@@ -13,7 +13,7 @@
                     <el-option v-for="(item, index) in locationOptions.list" :key="index" :label="item.title"
                         :value="{ value: item.point, item }">
                         <span>{{ item.title }}</span>
-                        <span class="sub-title">{{ item.province }}{{ item.city }}{{ item.district }}</span>
+                        <span class="sub-title">{{ item.province }}{{ item.city }}{{ item.district || '' }}</span>
                     </el-option>
                 </el-select>
             </div>
@@ -130,7 +130,7 @@ import customHeader from "@/components/customHeader.vue";
 import IndexMap from "./map/index.js";
 import { useRoute, useRouter } from "vue-router";
 import { mapLocation } from "./map/index.js";
-import { onMounted, ref, reactive, watch, onActivated, nextTick } from "vue";
+import { onMounted, ref, reactive, watch, onActivated, nextTick,onDeactivated } from "vue";
 import { useStore } from "vuex";
 import { convertPointToArray } from "@/utils/index";
 import { ElMessage } from "element-plus";
@@ -275,9 +275,10 @@ onMounted(() => {
 const polygonArr = ref(null);
 const paramsType = ref(null);
 onActivated(() => {
+    const centerPointVal = sessionStorage.getItem('centerPoint') ? JSON.parse(sessionStorage.getItem('centerPoint')) : null;
     paramsType.value = route.query.type;
 
-    centerPoint.value = store.state.home.miniUserLocationPoint;
+    centerPoint.value = centerPointVal || store.state.home.miniUserLocationPoint;
     const arr = convertPointToArray(centerPoint.value);
     getLocationName(`${arr[1]},${arr[0]}`);
     // 仅在携带 isReload 标记、且不是编辑/小程序回流场景时,认为是一次全新创建,重置表单和地块,
@@ -315,6 +316,10 @@ onActivated(() => {
     });
 });
 
+onDeactivated(() => {
+    sessionStorage.setItem('centerPoint', JSON.stringify(centerPoint.value));
+});
+
 // 处理地图更新的逻辑
 function handleMapUpdate() {
     if (route.query.isReload) {
@@ -436,9 +441,9 @@ const handleSearchRes = (v) => {
     indexMap.setMapPosition(coordinateArray);
     centerPoint.value = `POINT (${coordinateArray[0]} ${coordinateArray[1]})`;
     ruleForm.address = v.item?.title || v.item?.address;
-    pointAddress.value = v.item?.province + v.item?.city + v.item?.district;
+    pointAddress.value = v.item?.province + v.item?.city + (v.item?.district || '');
     // 更新farmCity以便后续更新农场名称
-    farmCity.value = v.item?.city + v.item?.district || "";
+    farmCity.value = v.item?.city + (v.item?.district || "");
     // 地址修改后,如果满足条件则自动更新农场名称
     updateFarmNameIfNeeded();
     getSpecieList();
@@ -691,7 +696,7 @@ function getLocationName(location) {
         const add = result.formatted_addresses?.recommend ? result.formatted_addresses.recommend : result.address + "";
         ruleForm.address = add;
         pointAddress.value = result.address;
-        farmCity.value = result.address_component?.city + result.address_component?.district || "";
+        farmCity.value = result.address_component?.city + (result.address_component?.district || "");
         // 地址修改后,如果满足条件则自动更新农场名称
         updateFarmNameIfNeeded();
     });
@@ -915,7 +920,7 @@ function populateEditData() {
     if (editData.district) {
         try {
             const districtInfo = JSON.parse(editData.district);
-            pointAddress.value = districtInfo.province + districtInfo.city + districtInfo.district;
+            pointAddress.value = districtInfo.province + districtInfo.city + (districtInfo?.district || '');
         } catch (e) {
             console.warn("解析地址信息失败:", e);
         }