🐞 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

8
.env
View File

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

View File

@ -10,7 +10,7 @@ const identifyInput = (input) => {
* @type {RegExp} * @type {RegExp}
*/ */
const urlRegex = 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 = 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]?)$/; /^(?:(?: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]?)$/;