细节优化
This commit is contained in:
@@ -93,6 +93,27 @@ interface WeatherApiResponse {
|
||||
data: DataOuter;
|
||||
}
|
||||
|
||||
interface NewWeatherData {
|
||||
temperature: string;
|
||||
weather: string;
|
||||
humidity: string;
|
||||
windDirection: string;
|
||||
windPower: string;
|
||||
visibility: string;
|
||||
rainfall?: string;
|
||||
pm25?: string;
|
||||
}
|
||||
|
||||
interface NewWeatherApiResponseData {
|
||||
region: string;
|
||||
weatherData: NewWeatherData;
|
||||
}
|
||||
|
||||
interface NewWeatherApiResponse {
|
||||
status: string;
|
||||
data: NewWeatherApiResponseData;
|
||||
}
|
||||
|
||||
export default class WidgetFormAbility extends FormExtensionAbility {
|
||||
async onCreate(want: Want): Promise<formBindingData.FormBindingData> {
|
||||
console.info('[WidgetFormAbility] onCreate');
|
||||
@@ -268,8 +289,16 @@ export default class WidgetFormAbility extends FormExtensionAbility {
|
||||
|
||||
private async fetchWeather(): Promise<WeatherData | null> {
|
||||
try {
|
||||
// 检查本地缓存
|
||||
const cachedWeather = await this.getCachedWeather();
|
||||
if (cachedWeather) {
|
||||
console.info('[WidgetFormAbility] Using cached weather data');
|
||||
return cachedWeather;
|
||||
}
|
||||
|
||||
// 调用新的 API 获取天气数据
|
||||
const httpRequest = http.createHttp();
|
||||
const response = await httpRequest.request('https://yy.vogov.cn/api/tq/api.php', {
|
||||
const response = await httpRequest.request('https://v2.jinrishici.com/user/info', {
|
||||
method: http.RequestMethod.GET,
|
||||
connectTimeout: 10000,
|
||||
readTimeout: 10000
|
||||
@@ -278,15 +307,18 @@ export default class WidgetFormAbility extends FormExtensionAbility {
|
||||
httpRequest.destroy();
|
||||
|
||||
if (response.responseCode === 200) {
|
||||
const result = JSON.parse(response.result as string) as WeatherApiResponse;
|
||||
if (result.status === 'success' && result.data && result.data.data) {
|
||||
const weatherInfo = result.data.data.cityDZ.weatherinfo;
|
||||
const result = JSON.parse(response.result as string) as NewWeatherApiResponse;
|
||||
if (result.status === 'success' && result.data && result.data.weatherData) {
|
||||
const weatherData: WeatherData = {
|
||||
city: weatherInfo.city,
|
||||
weather: weatherInfo.weather,
|
||||
temp: weatherInfo.temp
|
||||
city: result.data.region || '未知城市',
|
||||
weather: result.data.weatherData.weather || '未知',
|
||||
temp: result.data.weatherData.temperature || '0'
|
||||
};
|
||||
console.info('[WidgetFormAbility] Fetched weather: ' + JSON.stringify(weatherData));
|
||||
|
||||
// 缓存天气数据
|
||||
await this.cacheWeather(weatherData);
|
||||
|
||||
return weatherData;
|
||||
}
|
||||
}
|
||||
@@ -297,6 +329,48 @@ export default class WidgetFormAbility extends FormExtensionAbility {
|
||||
}
|
||||
}
|
||||
|
||||
private async getCachedWeather(): Promise<WeatherData | null> {
|
||||
try {
|
||||
const preference = await preferences.getPreferences(this.context, 'widget_weather_cache');
|
||||
const cachedData = await preference.get('weatherData', '') as string;
|
||||
const lastFetchTime = await preference.get('lastFetchTime', 0) as number;
|
||||
const fetchCount = await preference.get('fetchCount', 0) as number;
|
||||
|
||||
// 检查是否已经请求过2次以上,并且缓存时间在5分钟内
|
||||
if (fetchCount >= 2 && cachedData) {
|
||||
const currentTime = Date.now();
|
||||
const fiveMinutes = 5 * 60 * 1000;
|
||||
|
||||
if (currentTime - lastFetchTime < fiveMinutes) {
|
||||
const weatherData = JSON.parse(cachedData) as WeatherData;
|
||||
return weatherData;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('[WidgetFormAbility] getCachedWeather error: ' + JSON.stringify(error));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async cacheWeather(weatherData: WeatherData): Promise<void> {
|
||||
try {
|
||||
const preference = await preferences.getPreferences(this.context, 'widget_weather_cache');
|
||||
const currentTime = Date.now();
|
||||
const fetchCount = await preference.get('fetchCount', 0) as number;
|
||||
|
||||
await preference.put('weatherData', JSON.stringify(weatherData));
|
||||
await preference.put('lastFetchTime', currentTime);
|
||||
await preference.put('fetchCount', fetchCount + 1);
|
||||
await preference.flush();
|
||||
|
||||
console.info('[WidgetFormAbility] Weather data cached');
|
||||
} catch (error) {
|
||||
console.error('[WidgetFormAbility] cacheWeather error: ' + JSON.stringify(error));
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchRandomQuote(): Promise<QuoteResult | null> {
|
||||
try {
|
||||
const httpRequest = http.createHttp();
|
||||
|
||||
Reference in New Issue
Block a user