你的位置:首页 > 操作系统

[操作系统]封装的数据请求加上风火轮的效果


把数据请求和风火轮的效果封装成一个方法,用的时候直接调用这个方法就可以

+ (void)startRequest:(NSString*)method

             baseurl:(NSString*)baseurl

               param:(NSDictionary*)params

             success:(DKSuccess)success

             failure:(DKFailure)failure

        showProgress:(BOOL)progress{

    NSAssert(params, @"params is nil.");

    NSDictionary *par = @{@"reqData":[self dictionaryToString:params]};

    if (progress) {

        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];

    }

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    if ([method isEqualToString:Get]) {

        [manager GET:baseurl parameters:par progress:^(NSProgress * _Nonnull downloadProgress) {

            

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

            [SVProgressHUD dismiss];

            NSAssert(responseObject, @"responseObject is nil.");

            success(task, [self decryptData:responseObject]);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            [SVProgressHUD dismiss];

            failure(task, error);

        }];

    }else{

        [manager POST:baseurl parameters:par progress:^(NSProgress * _Nonnull uploadProgress) {

            

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

            [SVProgressHUD dismiss];

            NSAssert(responseObject, @"responseObject is nil.");

            success(task, [self decryptData:responseObject]);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            [SVProgressHUD dismiss];

            failure(task, error);

        }];

    }

}