mirror of
https://github.com/caojiezi2003/Snavigation.git
synced 2024-11-23 10:59:46 +00:00
parent
9f9854ceb6
commit
2edc0cfd62
6
.env
6
.env
@ -16,3 +16,9 @@ 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"
|
@ -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" },
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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("地区查询失败");
|
||||||
|
}
|
||||||
|
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));
|
localStorage.setItem("lastWeatherData", JSON.stringify(lastWeatherData));
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("天气获取失败:" + error);
|
|
||||||
$message.warning("天气获取失败", {
|
|
||||||
duration: 1500,
|
|
||||||
});
|
|
||||||
weatherShow.value = false;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
console.log("从缓存中读取天气数据");
|
console.log("从缓存中读取天气数据:", lastWeatherData);
|
||||||
weatherData.value = lastWeatherData.data;
|
weatherData.value = lastWeatherData.data;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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]?)$/;
|
||||||
|
Loading…
Reference in New Issue
Block a user