136 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			136 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
								 | 
							
								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;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 爬气象局的天气数据%
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task<Data> 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<Root>(data);
							 | 
						|||
| 
								 | 
							
								                        return result?.data;
							 | 
						|||
| 
								 | 
							
								                    }
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            catch (Exception ex)
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								                Console.WriteLine(ex.Message);
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return null;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        public class Location
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// 54511
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public string id { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// 北京
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public string name { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// 中国, 北京, 北京
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public string path { get; set; }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        public class Now
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Precipitation
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public double precipitation { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Temperature
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public double temperature { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Pressure
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public double pressure { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Humidity
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public double humidity { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// 东北风
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public string windDirection { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// WindDirectionDegree
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public double windDirectionDegree { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// WindSpeed
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public double windSpeed { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// 微风
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public string windScale { get; set; }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        public class Data
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Location
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public Location location { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Now
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public Now now { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Alarm
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public List<object> alarm { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// 2024/01/15 10:05
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public DateTime lastUpdate { get; set; }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        public class Root
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// success
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public string msg { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Code
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public int code { get; set; }
							 | 
						|||
| 
								 | 
							
								            /// <summary>
							 | 
						|||
| 
								 | 
							
								            /// Data
							 | 
						|||
| 
								 | 
							
								            /// </summary>
							 | 
						|||
| 
								 | 
							
								            public Data data { get; set; }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |