using LY.App.Extensions.DI; using Newtonsoft.Json; namespace LY.App.Service { [ServiceInjection(InjectionType.Scoped)] public class WeatherService { private IHttpClientFactory _httpClientFactory; private IConfiguration _configuration; public WeatherService(IHttpClientFactory httpClientFactory, IConfiguration configuration) { _httpClientFactory = httpClientFactory; _configuration = configuration; } /// /// 爬气象局的天气数据% /// /// public async Task GetWeather() { try { var client = _httpClientFactory.CreateClient(); var url = _configuration.GetSection("Weather").Value; if (!string.IsNullOrEmpty(url)) { var resp = await client.GetAsync(url); if (resp.IsSuccessStatusCode) { var data = await resp.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(data); return result?.data; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return null; } public class Location { /// /// 54511 /// public string id { get; set; } /// /// 北京 /// public string name { get; set; } /// /// 中国, 北京, 北京 /// public string path { get; set; } } public class Now { /// /// Precipitation /// public double precipitation { get; set; } /// /// Temperature /// public double temperature { get; set; } /// /// Pressure /// public double pressure { get; set; } /// /// Humidity /// public double humidity { get; set; } /// /// 东北风 /// public string windDirection { get; set; } /// /// WindDirectionDegree /// public double windDirectionDegree { get; set; } /// /// WindSpeed /// public double windSpeed { get; set; } /// /// 微风 /// public string windScale { get; set; } } /// /// /// public class Data { /// /// Location /// public Location location { get; set; } /// /// Now /// public Now now { get; set; } /// /// Alarm /// public List alarm { get; set; } /// /// 2024/01/15 10:05 /// public DateTime lastUpdate { get; set; } } public class Root { /// /// success /// public string msg { get; set; } /// /// Code /// public int code { get; set; } /// /// Data /// public Data data { get; set; } } } }