Skip to content

Commit 6123077

Browse files
committed
fix weather
1 parent 297822c commit 6123077

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

api/weather.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ export default async function handler(req, res) {
4242
return res.status(200).json(data);
4343
}
4444

45+
// 逆地理编码(location 格式: lon,lat)
46+
if (type === "regeo") {
47+
if (!key) return res.status(500).json({ error: "WEATHER_KEY not configured" });
48+
const { location } = req.query || {};
49+
if (!location) return res.status(400).json({ error: "location parameter required" });
50+
const r = await fetch(
51+
`https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${encodeURIComponent(location)}`,
52+
);
53+
const data = await r.json();
54+
res.setHeader("Access-Control-Allow-Origin", "*");
55+
return res.status(200).json(data);
56+
}
57+
4558
return res.status(400).json({ error: "invalid type" });
4659
} catch (err) {
4760
res.setHeader("Access-Control-Allow-Origin", "*");

src/api/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,18 @@ export const getOtherWeather = async () => {
9393
return await res.json();
9494
}
9595
};
96+
97+
// 逆地理编码(传入经度,纬度,返回 regeo 结果)
98+
export const getRegeo = async (key, location) => {
99+
try {
100+
const res = await fetch(`/api/weather?type=regeo&location=${encodeURIComponent(location)}`);
101+
if (res.ok) return await res.json();
102+
throw new Error("proxy failed");
103+
} catch (err) {
104+
// 回退到直接调用高德 regeo(方便本地开发或未配置 server-side key)
105+
const res = await fetch(
106+
`https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${encodeURIComponent(location)}`,
107+
);
108+
return await res.json();
109+
}
110+
};

src/components/Weather.vue

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</template>
1919

2020
<script setup>
21-
import { getAdcode, getWeather, getOtherWeather } from "@/api";
21+
import { getAdcode, getWeather, getOtherWeather, getRegeo } from "@/api";
2222
import { Error } from "@icon-park/vue-next";
2323
2424
// 高德开发者 Key
@@ -70,16 +70,61 @@ const getWeatherData = async () => {
7070
windpower: data.condition.day_wind_power,
7171
};
7272
} else {
73-
// 获取 Adcode
74-
const adCode = await getAdcode(mainKey);
75-
console.log(adCode);
76-
if (adCode.infocode !== "10000") {
77-
throw "地区查询失败";
73+
// 优先尝试浏览器定位(需要用户授权),若成功使用 regeo 获取 adcode;否则回退到 IP 定位
74+
console.log("尝试使用浏览器定位获取经纬度");
75+
const getPosition = (timeout = 8000) =>
76+
new Promise((resolve, reject) => {
77+
if (!navigator.geolocation) return reject(new Error("浏览器不支持定位"));
78+
const timer = setTimeout(() => reject(new Error("定位超时")), timeout);
79+
navigator.geolocation.getCurrentPosition(
80+
(pos) => {
81+
clearTimeout(timer);
82+
resolve(pos.coords);
83+
},
84+
(err) => {
85+
clearTimeout(timer);
86+
reject(err);
87+
},
88+
{ enableHighAccuracy: false, timeout: 8000, maximumAge: 0 },
89+
);
90+
});
91+
92+
let adCodeFromGeo = null;
93+
try {
94+
const coords = await getPosition(8000);
95+
console.log("浏览器定位经纬度:", coords);
96+
const location = `${coords.longitude},${coords.latitude}`;
97+
const regeo = await getRegeo(mainKey, location);
98+
console.log("regeo 返回:", regeo);
99+
if (regeo && regeo.status === "1" && regeo.regeocode) {
100+
const comp = regeo.regeocode.addressComponent || {};
101+
adCodeFromGeo = {
102+
city: comp.city || comp.province || comp.township || "未知地区",
103+
adcode: comp.adcode || null,
104+
};
105+
}
106+
} catch (err) {
107+
console.warn("浏览器定位或 regeo 失败,回退到 IP 定位:", err);
78108
}
79-
weatherData.adCode = {
80-
city: adCode.city,
81-
adcode: adCode.adcode,
82-
};
109+
110+
if (adCodeFromGeo && adCodeFromGeo.adcode) {
111+
weatherData.adCode = {
112+
city: adCodeFromGeo.city,
113+
adcode: adCodeFromGeo.adcode,
114+
};
115+
} else {
116+
// 回退到 IP 定位
117+
const adCode = await getAdcode(mainKey);
118+
console.log("IP 定位返回:", adCode);
119+
if (adCode.infocode !== "10000") {
120+
throw "地区查询失败";
121+
}
122+
weatherData.adCode = {
123+
city: adCode.city,
124+
adcode: adCode.adcode,
125+
};
126+
}
127+
83128
// 获取天气信息
84129
const result = await getWeather(mainKey, weatherData.adCode.adcode);
85130
// 打印返回用于排查

0 commit comments

Comments
 (0)