口袋优选

KBLoginRequest.m 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // KBLoginRequest.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/24.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBLoginRequest.h"
  9. @implementation KBLoginRequest
  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. success(responseDict[@"rst"]);
  35. }
  36. }else{
  37. if ([responseDict[@"errno"] isEqualToString:@"2001"]) {
  38. [MBProgressHUD showMessage:@"不存在该账号"];
  39. }else if ([responseDict[@"errno"] isEqualToString:@"1005"]){
  40. [MBProgressHUD showMessage:@"请输入合法的手机号"];
  41. }else if ([responseDict[@"errno"] isEqualToString:@"1004"]){
  42. [MBProgressHUD showMessage:@"验证码错误"];
  43. }else if ([responseDict[@"errno"] isEqualToString:@"1012"]){
  44. [MBProgressHUD showMessage:@"微信绑定手机号失败"];
  45. }
  46. else{
  47. // NSString *errorStr = responseDict[@"err"];
  48. [MBProgressHUD showMessage:@"绑定失败"];
  49. }
  50. NSError *error = nil;
  51. failure(error);
  52. }
  53. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  54. if (failure) {
  55. failure(error);
  56. }
  57. }];
  58. }
  59. @end