// // DRSendCodeRequest.m // YouHuiProject // // Created by 小花 on 2018/1/24. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRSendCodeRequest.h" @implementation DRSendCodeRequest + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure { // 1.创建请求管理者 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data mgr.responseSerializer=[AFHTTPResponseSerializer serializer]; mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil]; mgr.requestSerializer.timeoutInterval = 15; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"]; [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"]; if ([AccountTool isLogin]) { NSString *token = [AccountTool account].token; [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"]; } NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey]; [mgr.requestSerializer setValue:sex forHTTPHeaderField:@"sex"]; // 2.发送请求 [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; if ([responseDict[@"errno"] isEqualToString:@"0"]) { if (!responseDict[@"rst"]) { success(@{}); }else{ [SVProgressHUD showSuccessWithStatus:@"发送成功"]; success(responseDict[@"rst"]); } }else{ if ([responseDict[@"errno"] isEqualToString:@"1005"]) { [MBProgressHUD showMessage:@"请输入合法的手机号"]; }else if ([responseDict[@"errno"] isEqualToString:@"1007"]){ [MBProgressHUD showMessage:@"签名不合法"]; }else if ([responseDict[@"errno"] isEqualToString:@"1008"]){ [MBProgressHUD showMessage:@"请求时间不合法 "]; }else{ NSString *errorStr = responseDict[@"err"]; if (errorStr) { [MBProgressHUD showMessage:errorStr]; return ; } [MBProgressHUD showMessage:@"发送失败"]; } NSError *error = nil; failure(error); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"]; NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"服务器的错误原因:%@",str); if (failure) { failure(error); } }]; } @end