CNLiveServicePresent.m 18.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
//
//  CNLiveServicePresent.m
//  CNLiveCServiceModule
//
//  Created by CNLive on 2021/11/24.
//

#import "CNLiveServicePresent.h"

static const CGFloat margin = 15;

@implementation CNLiveServicePresent
+ (void)searchServiceList:(NSString *)searchStr page:(NSString *)page result:(nonnull void (^)(NSArray<CNLiveSearchModel *> * _Nonnull, BOOL))succeed error:(nonnull void (^)(void))failure
{
    
    NSDictionary * params = @{
        @"sid":CNUserShareModelUid?CNUserShareModelUid:@"",
        @"title":searchStr?searchStr:@"",
        @"type":@"1",
        @"page":page?page:@"",
        @"pageSize":@"10"
    };
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/servingApi/search") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if (responseObject&&[responseObject isKindOfClass:[NSDictionary class]]) {
            NSString *lastPageStr = responseObject[@"data"][@"hasNextPage"];
            BOOL isLastPage = !lastPageStr.boolValue;
            NSArray *listArr = responseObject[@"data"][@"list"];
            if (listArr&&[listArr isKindOfClass:[NSArray class]]) {
                if (listArr.count == 0) {
                    !succeed?:succeed(listArr,isLastPage);
                }else{
                    NSMutableArray *dataArray = [NSMutableArray array];
                    for (NSDictionary *dic in listArr) {
                        CNLiveSearchModel *model = [CNLiveSearchModel mj_objectWithKeyValues:dic];
                        model.searchStr = searchStr;
                        [dataArray addObject:model];
                    }
                    !succeed?:succeed(dataArray.copy,isLastPage);
                }
            }
        }else{
            [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:2];
            !failure?:failure();
        }
    }];
}

+ (void)dealHots:(NSArray *)hots searchHistories:(NSArray *)histories results:(void (^)(NSArray * _Nonnull))succeed
{
    CNLiveSearchHotsModel *model1 = nil;
    if (hots && hots.count > 0) {
        model1 = [CNLiveServicePresent dealDataSource:hots title:@"热度推荐" categoryId:@"" subtitle:@"" cellHeight:0 itemSize:CGSizeMake((SCREEN_WIDTH-2*margin)/4.0, 80)showNum:0];
    }
   
    NSMutableArray *array2 = [NSMutableArray array];
    CNLiveSearchHotsModel *model2 = nil;
    if (histories && histories.count > 0) {
        for (NSDictionary *dic in histories) {
            CNLiveHistoryModel *model = [CNLiveHistoryModel mj_objectWithKeyValues:dic];
            [array2 addObject:model];
        }
      
        NSDictionary *dic2 = @{
            @"title":@"历史搜索",
            @"lists":array2,
            @"type" :@(8),
            @"layout": @{
                @"height":@(29),
                @"width":@((SCREEN_WIDTH-4*margin)/3.0),
            },
        };
       model2 = [CNLiveSearchHotsModel mj_objectWithKeyValues:dic2];
    }
    
    NSMutableArray *array3 = [NSMutableArray array];
    if (model1) {
        [array3 addObject:model1];
    }
    if (model2) {
        [array3 addObject:model2];
    }
    succeed(array3.copy);
}

+ (void)requestServiceListResult:(void (^)(NSArray * _Nonnull, NSArray * _Nonnull))result error:(void (^)(void))failure{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @""};
    
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/servingApi/servingPage") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        CNLiveServiceModel * model = [CNLiveServiceModel mj_objectWithKeyValues:responseObject[@"data"]];
        CNLiveSearchHotsModel *categoryModel = nil;
        NSMutableArray *dataArray = [NSMutableArray array];
        if (model.categories&&[model.categories isKindOfClass:[NSArray class]] && model.categories.count>0) {
            CGFloat cellheight = 160;
            if (model.categories.count <= 4) {
                cellheight = 80;
            }
            categoryModel = [CNLiveServicePresent dealDataSource:model.categories title:@"" categoryId:@"" subtitle:@"" cellHeight:cellheight itemSize:CGSizeMake((SCREEN_WIDTH-2*margin)/4.0, 70)showNum:8];
            if (categoryModel) {
                [dataArray addObject:categoryModel];
            }
        }
        if (model.allList && model.allList.count > 0) {
            for (NSDictionary *dic in model.allList) {
                @autoreleasepool {
                    CNLiveSearchHotsModel *serviceModel = nil;
                    CGFloat cellheight = 0;
                    NSInteger showNum = 0;
                    NSString *title = dic[@"name"];
                    NSString *subTitle = @"";
                    CGSize itemSize = CGSizeMake(0, 0);
                    NSArray *lists = dic[@"list"];
                    NSString *showTitle = dic[@"category"][@"showTitle"];
                    if (![showTitle isKindOfClass:[NSNull class]] && !showTitle.boolValue) {
                        title = @"";
                    }
                    NSString *serviceId = [dic[@"category"] stringForKey:@"id"];
                    if (serviceId.integerValue == 6) {
                            cellheight = 150;
                            if (lists.count <= 2) {
                                cellheight = 80;
                            }
                            showNum = 4;
                            itemSize = CGSizeMake((KScreenWidth - 3*margin)/2, 60);
                    }else{
                        showNum = 4;
                        itemSize = CGSizeMake((SCREEN_WIDTH-2*margin)/4.0, 70);
                        subTitle = dic[@"category"][@"subTitle"];
                        if ([subTitle isKindOfClass:[NSString class]] && ![subTitle isEqualToString:@""]) {
                            cellheight = 125;
                        }else{
                            cellheight = 190;
                            if (lists.count <= 4) {
                                cellheight = 105;
                            }
                            showNum = 8;
                            itemSize = CGSizeMake((SCREEN_WIDTH-2*margin)/4.0, 70);
                            subTitle = @"";
                        }
                    }
                    if (lists &&[lists isKindOfClass:[NSArray class]] && lists.count > 0) {
                        serviceModel = [CNLiveServicePresent dealDataSource:dic[@"list"] title:title categoryId: serviceId subtitle:subTitle cellHeight:cellheight itemSize:itemSize showNum:showNum];
                    }
                    if (serviceModel) {
                        [dataArray addObject:serviceModel];
                    }
                }
                
            }
        }
        
       
            if (model) {
                !result?:result(dataArray.copy,model.hots);
            }else{
                [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:3];
                !failure?:failure();
            }
    }];
}

+ (CNLiveSearchHotsModel *) dealDataSource:(NSArray *)array title:(NSString *)title categoryId:(NSString *)categoryId subtitle:(NSString *)subTitle cellHeight:(CGFloat)cellHeight itemSize:(CGSize) size showNum:(NSInteger)num{
    NSMutableArray *categories = [NSMutableArray array];
    NSMutableArray *squreArray = [NSMutableArray array];
    for (int i = 0; i< array.count ;i++ ) {
        NSDictionary *dic = array[i];
        NSObject *model = nil;
        if ([title isEqualToString:@""] && [subTitle isEqualToString:@""]) {
            model = [CNCategoryModel mj_objectWithKeyValues:dic];
        }else{
        model = [CNLiveSearchModel mj_objectWithKeyValues:dic];
        }
        [categories addObject:model];
        if (num != 0 && (i != 0 ||  array.count == 1) &&((i + 1)%num == 0 || i == array.count - 1)) {
            NSDictionary *category = @{
                @"title":title,
                @"categoryId":categoryId,
                @"subTitle":subTitle,
                @"lists":categories.copy,
                @"cellHeight":@(cellHeight),
                @"layout":@{
                    @"width":@(size.width),
                    @"height":@(size.height)
                }
            };
            CNLiveSearchHotsModel *model = [CNLiveSearchHotsModel mj_objectWithKeyValues:category];
            [squreArray addObject:model];
            [categories removeAllObjects];
        }
    }
    if (categories && num !=0 && categories.count == 0) {
        NSDictionary *category = @{
            @"title":title,
            @"categoryId":categoryId,
            @"subTitle":subTitle,
            @"lists":squreArray.copy,
            @"cellHeight":@(cellHeight),
            @"layout":@{
                @"width":@(size.width),
                @"height":@(size.height)
            }
        };
        CNLiveSearchHotsModel *model = [CNLiveSearchHotsModel mj_objectWithKeyValues:category];
        [categories addObject:model];
    }
    NSDictionary *category = @{
        @"title":title,
        @"categoryId":categoryId,
        @"subTitle":subTitle,
        @"lists":categories.copy,
        @"cellHeight":@(cellHeight),
        @"layout":@{
            @"width":@(size.width),
            @"height":@(size.height)
        }
    };
    CNLiveSearchHotsModel *model = [CNLiveSearchHotsModel mj_objectWithKeyValues:category];
    return model;
}

+ (void)requesrServiceGetPageResult:(void (^)(NSArray * _Nonnull))result error:(void (^)(void))failure
{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @"",
                              @"pageId":@"10"
    };
    
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:CNGetClassifyPageUrl Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        NSArray *array = responseObject[@"data"];
        NSMutableArray *dataArray = [NSMutableArray array];
        if (array &&[array isKindOfClass:[NSArray class]]) {
            for (int i = 0;i < array.count;i ++) {
                NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithDictionary:array[i]];
                NSArray *contents = mDic[@"contents"];
                NSString *type = mDic[@"type"];
                if (type.integerValue == 36) {
                    NSString *nextType = array[i + 1][@"type"];
                    if (nextType.integerValue == 37) {
                        [mDic setObject:array[i + 1][@"contents"] forKey:@"rightLists"];
                    }
                    [mDic setObject:@(214*KScreenWidth/375 ) forKey:@"cellHeight"];
                }else if (type.integerValue == 38){
                    [mDic setObject:@(230*KScreenWidth/375 ) forKey:@"cellHeight"];
                }else if (type.integerValue == 1){
                    [mDic setObject:@(87*KScreenWidth/375) forKey:@"cellHeight"];
                }else if (type.integerValue == 9){
                    [mDic setObject:@(90) forKey:@"cellHeight"];
                }else if (type.integerValue == 30){
                    [mDic setObject:@(96+70 *RATIO) forKey:@"cellHeight"];
                    if ([contents isKindOfClass:[NSArray class]] && contents.count>0) {
                        NSString *modelType = contents[0][@"model"];
                        if (modelType &&[modelType isKindOfClass:[NSString class]] && [modelType isEqualToString:@"product"]) {
                            [mDic setObject:@(96 + 50*RATIO) forKey:@"cellHeight"];
                        }
                    }
                    
                }
                
                if ((type.integerValue == 36||type.integerValue == 38||type.integerValue == 1||type.integerValue == 9||type.integerValue == 30) && contents&&[contents isKindOfClass:[NSArray class]] && contents.count>0){
                    CNliveServicePageModel *model = [CNliveServicePageModel mj_objectWithKeyValues:mDic];
                    [dataArray addObject:model];
                }
            }
        }
        if (array) {
            !result?:result(dataArray);
        }else{
            [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:3];
            !failure?:failure();
        }
    }];
}

+ (void)loadMyServicepage:(NSString *)page result:(nonnull void (^)(NSArray * _Nonnull, BOOL))succeed error:(nonnull void (^)(void))failure{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @"",
                              @"pageSize":@"100",
                              @"type":@"2",
                              @"page":page?page:@""
                              
    };
    
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/servingApi/servingList") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        NSDictionary * dic = responseObject[@"data"];
        NSMutableArray *array = [NSMutableArray array];
        if (dic) {
            if (dic[@"list"]&&[dic[@"list"] isKindOfClass:[NSArray class]]) {
                NSArray *list = dic[@"list"];
                for (NSDictionary *dict in list) {
                    CNLiveSearchModel *searchModel = [CNLiveSearchModel mj_objectWithKeyValues:dict];
                    [array addObject:searchModel];
                }
            }
        }
        
            if (dic) {
                NSString *lastPage = dic[@"hasNextPage"];
                !succeed?:succeed(array.copy,!lastPage.boolValue);
            }else{
                [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:3];
                !failure?:failure();
            }
        
    }];
}

+ (void)usedServiceWithServingId:(NSString *)servingId{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @"",
                              @"servingId":servingId?servingId:@""};
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:cn_API_Classify(@"/servingApi/click") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
      
    }];
}

+ (void)removeMyServiceResult:(void (^) (void)) succeed{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @"",
                              @"id":@""
    };
    
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:cn_API_Classify(@"/servingApi/removeMyNewest") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if (!error) {
            succeed();
        }else{
            [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:3];
        }
    }];
}

+ (void)loadAllServiceWithServiceId:(NSString *)serviceId page:(nonnull NSString *)page result:(nonnull void (^)(NSArray * _Nonnull, BOOL))succeed error:(void (^)(void))failure
{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @"",
                              @"categoryId":serviceId?serviceId:@"",
                              @"page":page?page:@""
    };
    
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/servingApi/allCategory") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        NSDictionary * dic = responseObject[@"data"];
        NSMutableArray *array = [NSMutableArray array];
        if (dic&&[dic isKindOfClass:[NSDictionary class]]) {
            if (dic[@"list"]&&[dic[@"list"] isKindOfClass:[NSArray class]]) {
                NSArray *list = dic[@"list"];
                
                for (NSDictionary *dict in list) {
                    NSDictionary *dictionary = @{
                        @"title":dict[@"title"],
                        @"lists":dict[@"list"],
                        @"layout":@{
                            @"width":@((SCREEN_WIDTH-2*margin)/4.0),
                            @"height":@(70)
                        }
                    };
                    CNLiveSearchHotsModel *searchModel = [CNLiveSearchHotsModel mj_objectWithKeyValues:dictionary];
                    [array addObject:searchModel];
                }
            }
        }
        
        
            if (dic&&[dic isKindOfClass:[NSDictionary class]]) {
                NSString *lastPage = dic[@"hasNextPage"];
                !succeed?:succeed(array.copy,!lastPage.boolValue);
            }else{
                [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:3];
                !failure?:failure();
            }
        
    }];
}

+ (void)loadServiceInfoServiceId:(NSString *)serviceId result:(nonnull void (^)(CNLiveSearchModel * _Nonnull))succeed error:(void(^)(void))failure
{
    NSDictionary * params = @{@"sid": CNUserShareModelUid ? CNUserShareModelUid : @"",
                              @"serviceId":serviceId?serviceId:@""
    };
    
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/contentApi/getServiceInfo") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        CNLiveSearchModel *model = [CNLiveSearchModel mj_objectWithKeyValues:responseObject[@"data"]];
        if (model) {
            !succeed?:succeed(model);
        }else{
            [QMUITips showError:error.localizedDescription inView:AppKeyWindow hideAfterDelay:2];
            !failure?:failure();
        }
    }];
}
@end