HomeWeatherBoard.m 1.49 KB
Newer Older
Sandy's avatar
Sandy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//
//  HomeWeatherBoard.m
//  HDMall
//
//  Created by Javen on 2017/7/28.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "HomeWeatherBoard.h"

@implementation HomeWeatherBoard

- (void)awakeFromNib {
    [super awakeFromNib];
    WS(weakSelf);
    [[LocationHelper shareInstance] getCityNameSuccess:^(NSString *string) {
        [weakSelf httpGetWeather:string];
    }
                                                failed:nil];

}

- (void)configWithModel:(WeatherModel *)model {
    if (model.city.length > 0) {
        self.city.text = model.city;
        [self.img sd_setImageWithURL:[NSURL URLWithString:model.dayPictureUrl]];
        self.detail.text = [NSString stringWithFormat:@"%@ %@ %@", model.weather, model.wind, model.temperature];
    }
}

/** 根据城市名称获取天气 */
- (void)httpGetWeather:(NSString *)cityName
{
    if (nil != self.weatherModel) {
        return;
    }
    
    NSDictionary *myDictionary = @{ @"ak" : BAIDU_AK, @"location" : cityName, @"output" : @"json" };
    WS(weakSelf);
    
    [ZJHttpManager GET:WEATHER_SERVER_URL parameters:myDictionary complete:^(id responseObject, NSError *error) {
        //更新天气信息
        weakSelf.weatherModel = [[WeatherModel alloc] init];
        weakSelf.weatherModel.city = myDictionary[@"location"];
        [weakSelf.weatherModel setValuesForKeysWithDictionary:responseObject[@"results"][0][@"weather_data"][0]];
        [weakSelf configWithModel:weakSelf.weatherModel];
    }];
    
}

@end