Procházet zdrojové kódy

fix: 天气定位信息

刘秀芳 před 1 dnem
rodič
revize
0d9a7c3bd1
1 změnil soubory, kde provedl 22 přidání a 12 odebrání
  1. 22 12
      src/components/weatherInfo.vue

+ 22 - 12
src/components/weatherInfo.vue

@@ -70,6 +70,7 @@ import weatherChart from "./weatherChart.vue";
 import { useRouter } from "vue-router";
 import { useStore } from "vuex";
 import farmInfoPopup from "@/views/old_mini/home/components/farmInfoPopup.vue";
+import { convertPointToArray } from "@/utils/index";
 const store = useStore();
 
 const props = defineProps({
@@ -96,6 +97,8 @@ const handleCommand = ({id, name}) => {
     localStorage.setItem('selectedFarmId', id);
     localStorage.setItem('selectedFarmName', name);
     localStorage.setItem('selectedFarmPoint', selectedFarm.point);
+    getLocationName();
+    getWeatherData();
     emit('changeGarden',{id, name});
 };
 
@@ -136,6 +139,9 @@ async function setFarmByGardenId(gardenIdValue) {
                     localStorage.setItem('selectedFarmId', farmId.value);
                     localStorage.setItem('selectedFarmName', farmName.value);
                     localStorage.setItem('selectedFarmPoint', targetFarm.point);
+
+                    getLocationName();
+                    getWeatherData();
                     emit('changeGarden', { id: farmId.value, name: farmName.value });
                     resolve(true);
                 } else {
@@ -191,6 +197,8 @@ function selectFarmFromList(data) {
         // 如果没有保存的选择,按优先级选择
         selectDefaultFarm(data);
     }
+    getLocationName();
+    getWeatherData();
     emit('changeGarden',{id: farmId.value, name: farmName.value});
 }
 
@@ -237,16 +245,12 @@ function selectDefaultFarm(data) {
     localStorage.setItem('selectedFarmId', farmId.value);
     localStorage.setItem('selectedFarmName', farmName.value);
     localStorage.setItem('selectedFarmPoint', data[0].point);
-}
-
-onMounted(() => {
     getLocationName();
     getWeatherData();
-})
+}
 
 onActivated(() => {
     getFarmList();
-    getWeatherData();
 });
 
 // 暴露刷新方法供父组件调用
@@ -260,12 +264,18 @@ const weatherData = ref(null);
 const currentWeather = ref({ temp: "--", text: "--", iconDay: "" });
 const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
 function getLocationName() {
-    const location = store.state.home.miniUserLocation;
-    // 将 location 从"经度,纬度"格式转换为"纬度,经度"格式
-    let formattedLocation = location;
-    if (typeof location === 'string' && location.includes(',')) {
-        const [lng, lat] = location.split(',');
-        formattedLocation = `${lat},${lng}`;
+    const farmLocation = convertPointToArray(localStorage.getItem('selectedFarmPoint'));
+    let formattedLocation = null;
+    
+    if (farmLocation) {
+        formattedLocation = `${farmLocation[1]},${farmLocation[0]}`;
+    } else {
+        const location = store.state.home.miniUserLocation;
+        // 将 location 从"经度,纬度"格式转换为"纬度,经度"格式
+        if (typeof location === 'string' && location.includes(',')) {
+            const [lng, lat] = location.split(',');
+            formattedLocation = `${lat},${lng}`;
+        }
     }
     const params = {
             key: MAP_KEY,
@@ -286,7 +296,7 @@ const handleAddGarden = () => {
 
 // 获取天气数据
 function getWeatherData() {
-    const point = store.state.home.miniUserLocationPoint;
+    const point = localStorage.getItem('selectedFarmPoint') || store.state.home.miniUserLocationPoint;
     if (!point) {
         return;
     }