更新天气获取逻辑

This commit is contained in:
2025-12-09 20:31:19 +08:00
parent 5375e41b7e
commit 2066dfe8c1
2 changed files with 21 additions and 7 deletions

View File

@@ -46,7 +46,6 @@ import { get } from '@/utils/request';
import chartContainer from './components/chartContainer.vue';
import { initTop5 } from './ts/initTop5';
import AMapLoader from "@amap/amap-jsapi-loader";
import { log } from 'echarts/types/src/util/log.js';
// 全局状态
const cityGDPStore = useCityGDPStore();
// dom节点
@@ -86,16 +85,25 @@ const getLocation = async (lng: number, lat: number, key: string) => {
const now = new Date().getTime();
const cd = 1000 * 60
//会话存储取出json
const location = JSON.parse(sessionStorage.getItem('location')!).time;
if (now - location > cd) {
const location: { time: number, citycode: string } = JSON.parse(sessionStorage.getItem('location')!);
if (!location) {
console.log("开始请求");
return true
}
console.log(`剩余cd${(cd - (now - location)) / 1000}s`);
const _time = now - location.time
if (_time > cd) {
console.log("开始请求");
return true
}
console.log(_time, now, location);
console.log(`剩余cd${(cd - _time) / 1000}s`);
return false
}
if (lng && lat && checkTime()) {
const res = await get<any>(`${url}/v3/geocode/regeo?key=${key}&location=${lng},${lat}`);
console.log(res);
if (res.status === '1') {
const time = new Date().getTime()
let data = {}
@@ -109,6 +117,11 @@ const getLocation = async (lng: number, lat: number, key: string) => {
console.log("地理逆编码错误");
console.log(res);
}
let adcode: any = sessionStorage.getItem("location")
adcode = JSON.parse(adcode)
adcode = adcode.citycode
const weather = await get(`${url}/v3/weather/weatherInfo?key=${key}&city=${adcode}`)
console.log(weather);
}
return null;
};
@@ -127,6 +140,7 @@ const initAMap = async (key1: string, key2: string, key3: string) => {
console.log("定位成功!");
lng = data.position.lng;
lat = data.position.lat;
// 调用逆地理编码获取位置详情
getLocation(lng, lat, key3).then(() => {
@@ -141,7 +155,7 @@ const initAMap = async (key1: string, key2: string, key3: string) => {
AMap.plugin('AMap.Geolocation', function () {
const geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
timeout: 1000000,
buttonPosition: "RB",
});
geolocation.getCurrentPosition(function (status: any, result: any) {
@@ -160,8 +174,8 @@ const initAMap = async (key1: string, key2: string, key3: string) => {
onMounted(async () => {
//地图传入key安全密钥定位服务key
// await initAMap("fe533a0fc1658158460a31b3ae0add51", "43029a44482fc7cdf66b0b590d138278", "48da806104e4bf77be4eeecf83f440aa"); // 初始化高德地图安全配置
getLocation(120.126893, 30.276614, "48da806104e4bf77be4eeecf83f440aa")
await initAMap("fe533a0fc1658158460a31b3ae0add51", "43029a44482fc7cdf66b0b590d138278", "48da806104e4bf77be4eeecf83f440aa"); // 初始化高德地图安全配置
// getLocation(120.126893, 30.276614, "48da806104e4bf77be4eeecf83f440aa")
await getData(); // 获取数据
calcGDP(); // 计算GDP
await initChinaMap(chinaMapRef.value!); // 初始化地图

Binary file not shown.