口袋优选

KBSendCodeRequest.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // KBSendCodeRequest.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/24.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBSendCodeRequest.h"
  9. @implementation KBSendCodeRequest
  10. + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
  11. // 1.创建请求管理者
  12. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  13. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  14. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  15. mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  16. mgr.requestSerializer.timeoutInterval = 15;
  17. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  18. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  19. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  20. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  21. if ([AccountTool isLogin]) {
  22. NSString *token = [AccountTool account].token;
  23. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  24. }
  25. NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  26. [mgr.requestSerializer setValue:sex forHTTPHeaderField:@"sex"];
  27. // 2.发送请求
  28. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  29. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  30. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  31. if (!responseDict[@"rst"]) {
  32. success(@{});
  33. }else{
  34. [SVProgressHUD showSuccessWithStatus:@"发送成功"];
  35. success(responseDict[@"rst"]);
  36. }
  37. }else{
  38. if ([responseDict[@"errno"] isEqualToString:@"1005"]) {
  39. [MBProgressHUD showMessage:@"请输入合法的手机号"];
  40. }else if ([responseDict[@"errno"] isEqualToString:@"1007"]){
  41. [MBProgressHUD showMessage:@"签名不合法"];
  42. }else if ([responseDict[@"errno"] isEqualToString:@"1008"]){
  43. [MBProgressHUD showMessage:@"请求时间不合法 "];
  44. }else{
  45. NSString *errorStr = responseDict[@"err"];
  46. if (errorStr) {
  47. [MBProgressHUD showMessage:errorStr];
  48. return ;
  49. }
  50. [MBProgressHUD showMessage:@"发送失败"];
  51. }
  52. NSError *error = nil;
  53. failure(error);
  54. }
  55. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  56. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  57. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  58. NSLog(@"服务器的错误原因:%@",str);
  59. if (failure) {
  60. failure(error);
  61. }
  62. }];
  63. }
  64. @end