Esempio n. 1
0
        /// <summary>
        /// 获取天气字符串
        /// </summary>
        /// <param name="cityName"></param>
        /// <returns></returns>
        private async Task <string> GetWeatherString(string cityId)
        {
            IGetWeatherRequest request       = GetWeatherRequestFactory.CreateGetWeatherRequest(GetWeatherMode.CityId, cityId);
            string             requestUrl    = request.GetRequestUrl();
            string             resposeString = await Weather.Utils.HttpHelper.GetUrlResposeAsnyc(requestUrl);

            return(resposeString.Replace("HeWeather data service 3.0", "result"));
        }
Esempio n. 2
0
        /// <summary>
        /// 获取天气数据
        /// </summary>
        /// <param name="cityId"></param>
        private async Task GetWeather(string cityId, int isRefresh)
        {
            Model.UserCity userCity = string.IsNullOrEmpty(cityId) ? userCityRespose.UserCities.FirstOrDefault(x => x.IsDefault == 1) : userCityRespose.UserCities.FirstOrDefault(x => x.CityId == cityId);

            IGetWeatherRequest weatherRequest = GetWeatherRequestFactory.CreateGetWeatherRequest(GetWeatherMode.CityId, userCity.CityId);

            //有网络
            if (NetHelper.IsNetworkAvailable())
            {
                if (userRespose.UserConfig.IsWifiUpdate == 0)
                {
                    weatherRespose = await GetWeatherAsync(isRefresh, userCity, weatherRequest);
                }
                else
                {
                    if (NetHelper.IsWifiConnection())
                    {
                        weatherRespose = await GetWeatherAsync(isRefresh, userCity, weatherRequest);
                    }
                    else
                    {
                        weatherRespose.result = null;
                        NotifyUser("Wifi未启动");
                    }
                }
            }
            else
            {
                weatherRespose = await weatherService.GetWeatherByClientAsync(userCity.CityId.ToString());

                NotifyUser("请开启网络,以更新最新天气数据");
            }

            if (weatherRespose != null)
            {
                var respose = weatherRespose.result.FirstOrDefault();


                var aqi             = respose.aqi;
                var now             = respose.now;
                var basic           = respose.basic;
                var daily_forecast  = respose.daily_forecast;
                var hourly_forecast = respose.hourly_forecast;
                if (aqi != null)
                {
                    homePageModel.Aqi = "空气质量: " + aqi.city.qlty;
                }
                homePageModel.City        = basic.city;
                homePageModel.Daytmp      = daily_forecast.FirstOrDefault().tmp.min + "° / " + daily_forecast.FirstOrDefault().tmp.max + "°";
                homePageModel.Hum         = "湿度:  " + now.hum + " %";
                homePageModel.Pres        = now.pres + " hPa";
                homePageModel.Tmp         = now.tmp + "°";
                homePageModel.Txt         = now.cond.txt;
                homePageModel.Update      = basic.update.loc + " 发布";
                homePageModel.Vis         = "能见度:  " + now.vis + " km";
                homePageModel.Wind        = now.wind.dir + " " + now.wind.sc + " 级";
                homePageModel.WeatherType = weatherTypeRespose.WeatherTypes.FirstOrDefault(x => x.Code == now.cond.code);

                List <ViewModel.DailyItem> dailyList = new List <ViewModel.DailyItem>();
                foreach (var item in daily_forecast)
                {
                    DailyItem daily = new DailyItem()
                    {
                        Date  = GetDateStr(item.date),
                        Image = weatherTypeRespose.WeatherTypes.FirstOrDefault(x => x.Code == item.cond.code_d).TileWidePic,
                        Tmp   = item.tmp.min + "° / " + item.tmp.max + "°",
                        Txt   = item.cond.txt_d
                    };
                    dailyList.Add(daily);
                }

                homePageModel.DailyList = dailyList;

                List <ViewModel.HourlyItem> hourlyList = new List <ViewModel.HourlyItem>();
                foreach (var item in hourly_forecast)
                {
                    HourlyItem hourly = new HourlyItem()
                    {
                        Date = DateTime.Parse(item.date).ToString("HH:mm"),
                        Hum  = item.hum + " %",
                        Tmp  = item.tmp + "°",
                        Wind = item.wind.dir + " " + item.wind.sc
                    };
                    hourlyList.Add(hourly);
                }
                homePageModel.HourlyList = hourlyList;
                LayoutRoot.DataContext   = null;
                LayoutRoot.DataContext   = homePageModel;
            }
            else
            {
                NotifyUser("天气数据获取失败");
            }
        }