CNLiveCMineInteViewModel.m 11.6 KB
//
//  CNLiveCMineInteViewModel.m
//  CNLiveCMineModule
//
//  Created by open on 2021/12/27.
//

#import "CNLiveCMineInteViewModel.h"
#import "CNLiveCMineInteHeaderCustomView.h"
@implementation CNLiveCMineInteViewModel

+(void)requestWithIntegraTaskWithSignMedthodForCompleterBlock:(void (^)(NSMutableArray * , NSString *))completerBlock
{
    if (![CNLiveNetworking isNetworking]) {
        NSMutableArray * tempArray01 = [[NSMutableArray alloc] initWithArray:@[@"",@"",@"",@""]];
        CNLiveCMineInteTaskListModel * listModel = (CNLiveCMineInteTaskListModel *)[CNLiveRespCacheManager objectCacheWithFileName:NSStringFromClass(CNLiveCMineInteTaskListModel.class)];
        if (listModel) {
            [tempArray01 replaceObjectAtIndex:1 withObject:listModel];
        }
        CNLiveCMineInteListModel * spListModel = (CNLiveCMineInteListModel *)[CNLiveRespCacheManager objectCacheWithFileName:NSStringFromClass(CNLiveCMineInteListModel.class)];
        if (!spListModel) {
            spListModel = [[CNLiveCMineInteListModel alloc] init];
            NSMutableArray * tempArray = [[NSMutableArray alloc] init];
            CNLiveCMineInteListSubModel * subModel = [[CNLiveCMineInteListSubModel alloc] init];
            subModel.contentId = @"21";
            subModel.poster = @"c_mine_content_item_task";
            subModel.title = @"做任务";
            subModel.subTitle = @"赚取更多积分";
            subModel.isEmpty = @"1";
            [tempArray addObject:subModel];
            spListModel.list = tempArray;
        }
        if (spListModel) {
            [tempArray01 replaceObjectAtIndex:3 withObject:spListModel];
        }
        completerBlock(tempArray01,@"请求失败");
        [QMUITips showWithText:@"无法连接网络" inView:AppKeyWindow hideAfterDelay:1.5];
        return;
    }
    [[CNLiveCMineService shareMineProtocol] pushCMineInteContentForCMainUMengStatisticaMedthod];
    NSDictionary * param = @{
        @"sid":[CNUserInfoManager manager].userInfoModel.uid
    };
    
    NSDictionary * param01 = @{
        @"channelId":@"21",
        @"modelId":@"",
        @"page":@"1",
        @"pageSize":@"10"
    };
    
    NSDictionary * param02 = @{
        @"blockId":@"21",
        @"userId":[CNUserInfoManager manager].userInfoModel.uid,
        @"type":@"",
        @"page":@"1",
        @"pageSize":@"10"
    };
    
    __block CNLiveNetworking * signInNetworking = [CNLiveNetworking createBatchOrChainNetworkWithMethod:CNLiveRequestMethodPOST URLString:cn_API_Host(@"/Daren/integral/checkIn.action") Param:param];
    signInNetworking.tag = 1000;
    signInNetworking.isShowDataResult = YES;
    signInNetworking.allowRequestDefaultArgument = YES;
    signInNetworking.resultResponseAsynchronously = NO;
    
    __block CNLiveNetworking * inteGetNetworking = [CNLiveNetworking createBatchOrChainNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Host(@"/Daren/integral/getTasks.action") Param:param];
    inteGetNetworking.tag = 1001;
    inteGetNetworking.isShowDataResult = YES;
    inteGetNetworking.allowRequestDefaultArgument = YES;
    inteGetNetworking.resultResponseAsynchronously = NO;
    
    __block CNLiveNetworking * spListNetworking = [CNLiveNetworking createBatchOrChainNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/contentApi/getList") Param:param01];
    spListNetworking.tag = 1002;
    spListNetworking.isShowDataResult = YES;
    spListNetworking.allowRequestDefaultArgument = YES;
    spListNetworking.resultResponseAsynchronously = NO;
    
    __block CNLiveNetworking * blockNetworking = [CNLiveNetworking createBatchOrChainNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/contentApi/getBlockContents") Param:param02];
    blockNetworking.tag = 1003;
    blockNetworking.isShowDataResult = YES;
    blockNetworking.allowRequestDefaultArgument = YES;
    blockNetworking.resultResponseAsynchronously = NO;

    [CNLiveNetworkManager requestBatchWithArray:@[signInNetworking,inteGetNetworking,spListNetworking,blockNetworking] CompletionBlock:^(NSArray *resultArray) {
        @try {
            NSMutableArray * tempArray = [[NSMutableArray alloc] init];
            for (CNLiveNetworking * networking in resultArray) {
                if (networking.tag == 1000 && ![tempArray containsObject:signInNetworking]) {
                    [tempArray addObject:networking];
                }
                if (networking.tag == 1001 && ![tempArray containsObject:inteGetNetworking]) {
                    [tempArray addObject:networking];
                }
                if (networking.tag == 1002 && ![tempArray containsObject:spListNetworking]) {
                    [tempArray addObject:networking];
                }
                if (networking.tag == 1003 && ![tempArray containsObject:blockNetworking]) {
                    [tempArray addObject:networking];
                }
            }
            NSMutableArray * tempArray01 = [[NSMutableArray alloc] initWithArray:@[@"",@"",@"",@""]];
            for (CNLiveNetworking * networking in tempArray) {
                if (networking.tag == 1000) {
                    if (!networking.error) {
                        CNLiveCMineInteScoreModel * scoreModel = [CNLiveCMineInteScoreModel mj_objectWithKeyValues:networking.responseObject];
                        [tempArray01 replaceObjectAtIndex:0 withObject:scoreModel];
                        if ([scoreModel.status isEqualToString:@"1"]) [CNLiveCMineInteHeaderCustomView showWithScoreViewForScoreIntergal:scoreModel.score Statue:YES];
                    }
                }
                if (networking.tag == 1001) {
                    if (!networking.error) {
                        CNLiveCMineInteTaskListModel * listModel = [CNLiveCMineInteTaskListModel mj_objectWithKeyValues:networking.responseObject];
                        [tempArray01 replaceObjectAtIndex:1 withObject:listModel];
                        [CNLiveRespCacheManager cachePathWithObject:listModel FileName:NSStringFromClass(CNLiveCMineInteTaskListModel.class)];
                    }else{
                        [QMUITips showWithText:@"接口请求错误" inView:AppKeyWindow hideAfterDelay:1.5];
                        CNLiveCMineInteTaskListModel * listModel = (CNLiveCMineInteTaskListModel *)[CNLiveRespCacheManager objectCacheWithFileName:NSStringFromClass(CNLiveCMineInteTaskListModel.class)];
                        if (listModel) {
                            [tempArray01 replaceObjectAtIndex:1 withObject:listModel];
                        }
                    }
                }
                if (networking.tag == 1002) {
                    if (!networking.error) {
                        CNLiveCMineInteListModel * spListModel = [CNLiveCMineInteListModel mj_objectWithKeyValues:networking.responseObject];
                        [tempArray01 replaceObjectAtIndex:2 withObject:spListModel];
                    }
                }
                if (networking.tag == 1003) {
                    if (!networking.error) {
                        CNLiveCMineInteListModel * spListModel = [CNLiveCMineInteListModel mj_objectWithKeyValues:networking.responseObject];
                        if (spListModel.list.count == 0) {
                            NSMutableArray * tempArray = [[NSMutableArray alloc] init];
                            CNLiveCMineInteListSubModel * subModel = [[CNLiveCMineInteListSubModel alloc] init];
                            subModel.contentId = @"21";
                            subModel.poster = @"c_mine_content_item_task";
                            subModel.title = @"做任务";
                            subModel.subTitle = @"赚取更多积分";
                            subModel.isEmpty = @"1";
                            spListModel.list = tempArray;
                        }
                        [tempArray01 replaceObjectAtIndex:3 withObject:spListModel];
                        [CNLiveRespCacheManager cachePathWithObject:spListModel FileName:NSStringFromClass(CNLiveCMineInteListModel.class)];
                    }else{
                        CNLiveCMineInteListModel * spListModel = (CNLiveCMineInteListModel *)[CNLiveRespCacheManager objectCacheWithFileName:NSStringFromClass(CNLiveCMineInteListModel.class)];
                        if (!spListModel) {
                            spListModel = [[CNLiveCMineInteListModel alloc] init];
                            NSMutableArray * tempArray = [[NSMutableArray alloc] init];
                            CNLiveCMineInteListSubModel * subModel = [[CNLiveCMineInteListSubModel alloc] init];
                            subModel.contentId = @"21";
                            subModel.poster = @"c_mine_content_item_task";
                            subModel.title = @"做任务";
                            subModel.subTitle = @"赚取更多积分";
                            subModel.isEmpty = @"1";
                            [tempArray addObject:subModel];
                            spListModel.list = tempArray;
                        }
                        if (spListModel) {
                            [tempArray01 replaceObjectAtIndex:3 withObject:spListModel];
                        }
                    }
                }
            }
            
            if (completerBlock) completerBlock(tempArray01,nil);
        } @catch (NSException *exception) {
            NSLog(@"异常了");
            if (completerBlock) completerBlock(nil,@"获取失败");
            
        }
    }];
}

/// 获取积分明细(新)
/// @param currentMonth 时间格式 2021-12
/// @param currentType 1-全部 2-获取 3-支出
/// @param pageNo 页码(默认1)
/// @param completerBlock 结果返回
+(void)requestWithIntegraDetailWithCurrentMonth:(NSString *)currentMonth currentType:(NSInteger)currentType pageNo:(NSInteger)pageNo integralBlock:(nonnull void (^)(CNLiveCMineInteTaskListModel * _Nonnull, NSError * _Nonnull))integralBlock CompleterBlock:(void(^)(CNLiveCMineInteDetailModel *, NSArray *,NSError *))completerBlock
{
    NSDictionary * param = @{
        @"sid":[CNUserInfoManager manager].userInfoModel.uid
    };
    NSDictionary * paramDict = @{
        @"sid":[CNUserInfoManager manager].userInfoModel.uid,
        @"currentMonth":currentMonth,
        @"type":[NSString stringWithFormat:@"%ld",currentType],
        @"pageNo":[NSString stringWithFormat:@"%ld",pageNo],
        @"pageSize":@"10"
    };
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:cn_API_Host(@"/Daren/integral/integralRecordNew.action") Param:paramDict CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        CNLiveCMineInteDetailModel * listModel = [CNLiveCMineInteDetailModel mj_objectWithKeyValues:responseObject];
        listModel.date_string = currentMonth;
        NSMutableArray * tempArray = listModel.integralList.mutableCopy;
        if (completerBlock) completerBlock(listModel,tempArray,error);
    }];
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Host(@"/Daren/integral/getTasks.action") Param:param CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if (!error) {
            CNLiveCMineInteTaskListModel * listModel = [CNLiveCMineInteTaskListModel mj_objectWithKeyValues:responseObject];
            if (integralBlock) {
                integralBlock(listModel,error);
            }
        }
        
    }];
}

@end