// // KBLoginRequest.m // YouHuiProject // // Created by 小花 on 2018/1/24. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBLoginRequest.h" @implementation KBLoginRequest + (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{ success(responseDict[@"rst"]); } }else{ if ([responseDict[@"errno"] isEqualToString:@"2001"]) { [MBProgressHUD showMessage:@"不存在该账号"]; }else if ([responseDict[@"errno"] isEqualToString:@"1005"]){ [MBProgressHUD showMessage:@"请输入合法的手机号"]; }else if ([responseDict[@"errno"] isEqualToString:@"1004"]){ [MBProgressHUD showMessage:@"验证码错误"]; }else if ([responseDict[@"errno"] isEqualToString:@"1012"]){ [MBProgressHUD showMessage:@"微信绑定手机号失败"]; } else{ // NSString *errorStr = responseDict[@"err"]; [MBProgressHUD showMessage:@"绑定失败"]; } NSError *error = nil; failure(error); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }]; } @end