Browse Source

仅中文环境查询天气

pull/701/head
cKey 3 years ago
parent
commit
0650969cf1
  1. 28
      apps/vue/src/store/modules/weather.ts

28
apps/vue/src/store/modules/weather.ts

@ -1,6 +1,7 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { Position, WeatherInfo } from '/@/api/weather/model'; import { Position, WeatherInfo } from '/@/api/weather/model';
import { getPosition, getWeather } from '/@/api/weather'; import { getPosition, getWeather } from '/@/api/weather';
import { useLocaleStoreWithOut } from './locale';
interface WeatherState { interface WeatherState {
currentCity?: Position; currentCity?: Position;
@ -17,20 +18,23 @@ export const useWeatherStore = defineStore({
}), }),
actions: { actions: {
async updateWeather() { async updateWeather() {
const hour = new Date().getHours(); const localeStore = useLocaleStoreWithOut();
if (hour - this.lastUpdateHour != 0) { // 中文环境天气api有效
const position = await getPosition(); if (localeStore.getLocale.includes('zh')) {
const weatherResult = await getWeather(position.code); const hour = new Date().getHours();
if (weatherResult.code !== 0) { if (hour - this.lastUpdateHour != 0) {
return Promise.reject(weatherResult.msg); const position = await getPosition();
const weatherResult = await getWeather(position.code);
if (weatherResult.code !== 0) {
return Promise.reject(weatherResult.msg);
}
this.currentCity = position;
this.weather = weatherResult.data;
this.lastUpdateHour = hour;
return Promise.resolve(this.weather);
} }
this.currentCity = position;
this.weather = weatherResult.data;
this.lastUpdateHour = hour;
return Promise.resolve(this.weather);
} else {
return Promise.resolve(this.weather);
} }
return Promise.resolve(this.weather);
} }
} }
}); });

Loading…
Cancel
Save