🐞 fix: 天气 API 切换至高德 #32

- 修复捷径无法添加 ip 地址 #31
This commit is contained in:
imsyy 2024-01-17 10:54:53 +08:00
parent 9f9854ceb6
commit 2edc0cfd62
No known key found for this signature in database
GPG Key ID: 5D959EAB73CA095D
4 changed files with 56 additions and 35 deletions

6
.env
View File

@ -16,3 +16,9 @@ VITE_INPUT_TIP = "想要搜点什么"
# ICP 备案号
## 若不需要,请设为空即可
VITE_ICP = "豫ICP备2022018134号-1"
# 天气 Key
## 请前往 高德开放平台注册 Web服务 Key
## 请注意不是 Web端 (JS API),免费申请,每日上限 5000 次
## 此处提供的服务可能会超量从而无法访问,请自行申请!请自行申请!请自行申请!
VITE_WEATHER_KEY = "6c13af6fc30868bee488faf2cc652ab4"

View File

@ -3,12 +3,23 @@ import fetchJsonp from "fetch-jsonp";
/**
* 获取天气
* https://api.oioweb.cn/doc/weather/GetWeather
* https://lbs.amap.com/api/webservice/guide/api/weatherinfo
*/
export const getWeather = () => {
// 获取高德地理位置信息
export const getAdcode = async (key) => {
return axios({
method: "GET",
url: "https://api.oioweb.cn/api/weather/GetWeather",
url: "https://restapi.amap.com/v3/ip",
params: { key },
});
};
// 获取高德地理天气信息
export const getWeather = async (key, city) => {
return axios({
method: "GET",
url: "https://restapi.amap.com/v3/weather/weatherInfo",
params: { key, city, extensions: "base" },
});
};

View File

@ -42,11 +42,11 @@
<span class="day">{{ timeData.day ?? "0" }}</span>
<span class="weekday">{{ timeData.weekday ?? "星期八" }}</span>
</div>
<div v-if="weatherShow && set.showWeather" class="weather">
<span class="status">{{ weatherData.condition ?? "N/A" }}</span>
<span class="temperature">{{ weatherData.temp ?? "N/A" }} </span>
<span class="wind">{{ weatherData.windDir ?? "N/A" }}</span>
<span v-if="weatherData.windLevel" class="wind-level"> {{ weatherData.windLevel }} </span>
<div v-if="set.showWeather" class="weather">
<span class="status">{{ weatherData?.condition ?? "N/A" }}</span>
<span class="temperature">{{ weatherData?.temp ?? "N/A" }} </span>
<span class="wind">{{ weatherData?.windDir ?? "N/A" }}</span>
<span v-if="weatherData?.windLevel" class="wind-level"> {{ weatherData.windLevel }} </span>
</div>
</div>
</template>
@ -55,7 +55,7 @@
import { getCurrentTime } from "@/utils/timeTools";
import { ref, onMounted, onBeforeUnmount, watch } from "vue";
import { statusStore, setStore } from "@/stores";
import { getWeather } from "@/api";
import { getAdcode, getWeather } from "@/api";
const set = setStore();
const status = statusStore();
@ -65,8 +65,8 @@ const timeData = ref({});
const timeInterval = ref(null);
//
const weatherShow = ref(true);
const weatherData = ref({});
const weatherData = ref(null);
const weatherKey = import.meta.env.VITE_WEATHER_KEY;
//
const updateTimeData = () => {
@ -74,7 +74,10 @@ const updateTimeData = () => {
};
//
const getWeatherData = () => {
const getWeatherData = async () => {
if (!weatherKey) {
return $message.warning("请配置天气 Key");
}
//
const currentTime = Date.now();
//
@ -84,28 +87,29 @@ const getWeatherData = () => {
};
//
const timeDifference = currentTime - lastWeatherData.lastFetchTime;
// 2
if (timeDifference >= 2 * 60 * 1000) {
getWeather()
.then((res) => {
console.log(res);
weatherData.value = res.result.condition;
lastWeatherData = {
data: res.result.condition,
lastFetchTime: currentTime,
// 5
if (timeDifference >= 5 * 60 * 1000) {
const adCodeResult = await getAdcode(weatherKey);
if (adCodeResult.infocode !== "10000") {
return $message.error("地区查询失败");
}
//
const weatherResult = await getWeather(weatherKey, adCodeResult.adcode);
if (weatherResult.infocode !== "10000") {
return $message.error("地区查询失败");
}
const data = weatherResult.lives[0];
weatherData.value = {
condition: data.weather,
temp: data.temperature,
windDir: data.winddirection + "风",
windLevel: data.windpower,
};
// localStorage
lastWeatherData = { data: weatherData.value, lastFetchTime: currentTime };
//
localStorage.setItem("lastWeatherData", JSON.stringify(lastWeatherData));
})
.catch((error) => {
console.error("天气获取失败:" + error);
$message.warning("天气获取失败", {
duration: 1500,
});
weatherShow.value = false;
});
} else {
console.log("从缓存中读取天气数据");
console.log("从缓存中读取天气数据:", lastWeatherData);
weatherData.value = lastWeatherData.data;
}
};

View File

@ -10,7 +10,7 @@ const identifyInput = (input) => {
* @type {RegExp}
*/
const urlRegex =
/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/;
/^(?:(?:(?:https?|ftp):)?\/\/)?(?:\d{1,3}(?:\.\d{1,3}){3}|[a-z0-9-]+\.[a-z]{2,})(?::\d+)?(?:\/[^?\s]*)?$/i;
const ipv4Regex =
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;