fix: 修复教书先生API接口调用失败 #224

This commit is contained in:
imsyy 2023-12-11 16:11:22 +08:00
parent b28f92b99f
commit a8b1d66330
2 changed files with 56 additions and 64 deletions

View File

@ -45,23 +45,22 @@ const hitokotoData = reactive({
}); });
// //
const getHitokotoData = () => { const getHitokotoData = async () => {
getHitokoto() try {
.then((res) => { const result = await getHitokoto();
hitokotoData.text = res.hitokoto; hitokotoData.text = result.hitokoto;
hitokotoData.from = res.from; hitokotoData.from = result.from;
}) } catch (error) {
.catch(() => { ElMessage({
ElMessage({ message: "一言获取失败",
message: "一言获取失败", icon: h(Error, {
icon: h(Error, { theme: "filled",
theme: "filled", fill: "#efefef",
fill: "#efefef", }),
}),
});
hitokotoData.text = "这里应该显示一句话";
hitokotoData.from = "無名";
}); });
hitokotoData.text = "这里应该显示一句话";
hitokotoData.from = "無名";
}
}; };
// //

View File

@ -39,54 +39,47 @@ const weatherData = reactive({
}); });
// //
const getWeatherData = () => { const getWeatherData = async () => {
// try {
if (!mainKey) { //
getOtherWeather() if (!mainKey) {
.then((res) => { console.log("未配置,使用备用天气接口");
console.log(res); const result = await getOtherWeather();
const data = res.result; console.log(result);
weatherData.adCode = { const data = result.result;
city: data.city.name, weatherData.adCode = {
adcode: data.city.cityId, city: data.city.city_name || "未知地区",
}; // adcode: data.city.cityId,
weatherData.weather = { };
weather: data.condition.condition, weatherData.weather = {
temperature: data.condition.temp, weather: data.condition.condition,
winddirection: data.condition.windDir, temperature: data.condition.temp,
windpower: data.condition.windLevel, winddirection: data.condition.windDir,
}; windpower: data.condition.windLevel,
}) };
.catch((err) => { } else {
console.error("天气信息获取失败:" + err); // Adcode
onError("天气信息获取失败"); const adCode = await getAdcode(mainKey);
}); console.log(adCode);
} else { if (adCode.infocode !== "10000") {
getAdcode(mainKey) throw "地区查询失败";
.then((res) => { }
weatherData.adCode = { weatherData.adCode = {
city: res.city, city: adCode.city,
adcode: res.adcode, adcode: adCode.adcode,
}; };
// //
getWeather(mainKey, weatherData.adCode.adcode) const result = await getWeather(mainKey, weatherData.adCode.adcode);
.then((res) => { weatherData.weather = {
weatherData.weather = { weather: result.lives[0].weather,
weather: res.lives[0].weather, temperature: result.lives[0].temperature,
temperature: res.lives[0].temperature, winddirection: result.lives[0].winddirection,
winddirection: res.lives[0].winddirection, windpower: result.lives[0].windpower,
windpower: res.lives[0].windpower, };
}; }
}) } catch (error) {
.catch((err) => { console.error("天气信息获取失败:" + error);
console.error("天气信息获取失败:" + err); onError("天气信息获取失败");
onError("天气信息获取失败");
});
})
.catch((err) => {
console.error("地理位置获取失败:" + err);
onError("地理位置获取失败");
});
} }
}; };