//
//  NewsNetModel.m
//  Open
//
//  Created by gao on 2017/1/23.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "NewsNetModel.h"
#import "homePageNewsModel.h"
@implementation NewsNetModel
+(id)getMoreNewsForThisPath:(NSString *)path nssing:(NSString*)str completionHandle:(void (^)(newsModel *, NSError *))complete
{
    
    NSLog(@"---------str:%@",str);
    

    
    NSString *appcode = @"7247dcdb95e2c8ec1ca69821a0823906";
    NSString *host = @"http://v.juhe.cn";
    NSString *paths = @"/toutiao/index";
    NSString *method = @"GET";
    NSString *querys = [NSString stringWithFormat:@"?type=%@&key=%@",str,appcode];
    NSString *url = [NSString stringWithFormat:@"%@%@%@",  host,  paths , querys];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]  cachePolicy:1  timeoutInterval:  5];
    request.HTTPMethod  =  method;

    NSURLSession *requestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
     NSURLSessionDataTask *task = [requestSession dataTaskWithRequest:request
                                                   completionHandler:^(NSData * _Nullable body , NSURLResponse * _Nullable response, NSError * _Nullable error) {
                           
                                                   
                                                           //打印应答中的body
                                                           NSLog(@"Response body: %@" , body);
                                                           NSLog(@"Response object: %@" , response);
                                                           NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
                                                           //打印应答中的body
                                                           NSLog(@"Response body: %@" , bodyString);
                                                       if (body) {
                                                           NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableLeaves error:&error];
                                                           NSLog(@"%@",weatherDic);
                                                           complete( [newsModel mj_objectWithKeyValues:weatherDic],error);
                                                       }
             
                                                   }];
    
    [task resume];
    return task;

    
    
    
    
}

+(id)acquirePicture:(NSString*)address andlocationCoordinate:(NSString*)locationCoordinate andint:(NSInteger)intShowType complete:(void(^)(homePageNewsModel* , NSError *))complete
{
    
    NSLog(@"%@",address);
    
    if ([NSString blankString:address]) {
        address =[NSNull null];
    }
    //@"versionId":[NSNumber numberWithInt:2001]
    NSDictionary *dictNew =@{
                          @"showType":@(intShowType),
                             @"address":address,
                           @"versionId":KKLifeVersionCode,
                           @"fromSys":@"2",
                           @"fromApp":@"kklife"
                             };
    NSLog(@"dictNew:%@",dictNew);
  
    NSString * pathNew_1 =[IPDIZHI stringByAppendingString:@"/socialsecurity/kankan/getNews"];
    NSString * pathNew =  [pathNew_1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    return [self  POST:pathNew parameters:dictNew completionHandler:^(homePageNewsModel* responseObj, NSError *error) {
        
        NSLog(@"%@",responseObj);
        
        complete( [homePageNewsModel mj_objectWithKeyValues:responseObj],error);
    }];
}
+ (NSString *)toJSONString:(NSDictionary *)str {
    NSData *data = [NSJSONSerialization dataWithJSONObject:str
                                                   options:NSJSONReadingMutableLeaves | NSJSONReadingAllowFragments
                                                     error:nil];
     
    if (data == nil) {
        return nil;
    }
     
    NSString *string = [[NSString alloc] initWithData:data
                                             encoding:NSUTF8StringEncoding];
    return string;
}

@end

