CNWitnessSearchPresent.m 7.61 KB
//
//  CNWitnessSearchPresent.m
//  CNLiveVideoClassifyKit
//
//  Created by CNLive on 2022/6/16.
//

#import "CNWitnessSearchPresent.h"
#import "CNWitnessSearchModel.h"

@implementation CNWitnessSearchPresent
+ (void)requestHotListResponse:(void (^)(NSArray * _Nonnull, NSString * _Nonnull))response{
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/ugcApi/getHotList") Param:@{} CacheType:CNLiveNetworkCacheTypeCacheNetwork CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        NSMutableArray *listArr = [NSMutableArray array];
        if (responseObject && [responseObject isKindOfClass:[NSArray class]]) {
            NSArray *modelArray = [CNWitnessHotModel mj_objectArrayWithKeyValuesArray:responseObject];
            for (CNWitnessHotModel *model in modelArray) {
                [listArr addObject:model.value];
            }
        }
        if (response) {
            response(listArr.copy, error.localizedDescription);
        }
    }];
}
+ (void)saveSearchHistories:(NSMutableArray *)data
{
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [Path stringByAppendingPathComponent:@"WitnessSearchHistories.data"];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSMutableArray *)readSearchHistories{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"WitnessSearchHistories.data"];
    //解档
    NSMutableArray *data = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    return data;
}
+ (NSArray *)dealHots:(NSArray *)hots isShowAll:(BOOL)isShowAll allHistories:(NSArray *)histories{
    if (!isShowAll&&histories.count >2) {
        histories = [histories subarrayWithRange:NSMakeRange(0, 2)];
    }
    NSArray *dataArray = @[
        @{
            @"title":@"",
            @"list":histories?histories:@[]
        },
        @{
            @"title":@"热门搜索",
            @"list":hots?hots:@[]
        }
    ];
    NSArray *modelArray = [CNWitnessSearchModel mj_objectArrayWithKeyValuesArray:dataArray];
    return modelArray;
}

+ (void)requestSearchVideoListWithTS:(NSString *)ts type:(NSString *)type searchType:(NSInteger)searchType tagId:(NSString *)tagId keyword:(NSString *)keyword page:(NSInteger)page responseBlock:(void (^)(NSArray *, BOOL, NSString *))responseBlock{
    NSDictionary *params = @{
        @"ts":ts?ts:@"",
        @"userId":CNUserShareModelUid,
        @"keyword":keyword?keyword:@"",
        @"searchType":@(searchType),
        @"type":type,
        @"tagId":tagId?tagId:@"",
        @"page":@(page),
        @"pageSize":@(10)
    };
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:cn_API_Classify(@"/ugcApi/ugcVideoList") Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask,  id responseObject, NSError *error) {
        BOOL lastPage = NO;
        NSArray *dataArr = nil;
        if (responseObject&& [responseObject isKindOfClass:[NSDictionary class]]) {
            NSArray *array = responseObject[@"list"];
            if (array &&[array isKindOfClass:[NSArray class]]) {
                if (array.count == 0) {
                    lastPage = YES;
                }else{
                    if (searchType == 1) {
                        dataArr = [CNWitnessSearchDetailModel mj_objectArrayWithKeyValuesArray:array];
                    }else{
                        dataArr = [CNPersonMessageModel mj_objectArrayWithKeyValuesArray:array];
                    }
                   
                }
            }
        }
        if (responseBlock) {
            responseBlock(dataArr,lastPage ,error.localizedDescription);
        }
    }];

}
+ (void)requestWitnessLikeMomentWithPid:(NSString *)pid ContentSid:(NSString *)contentSid support:(NSString *)support contentTag:(NSString *)contentTag responseBlock:(void (^)(BOOL ))responseBlock{
    NSDictionary *params = @{@"contentId":pid?pid:@"",
                             @"appId":AppId,
                             @"sid":CNLiveStringIsEmpty(CNUserShareModel.uid)? @"":CNUserShareModel.uid,
                             @"contentTag":contentTag?contentTag:@"",
                             @"contentSid":contentSid?contentSid:@"",
                             @"support":support,
                             @"isRank":@"false"
                             };
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];

    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:CNGetWitnessFavoriteMomentUrl Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if (!error) {
            if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
                NSString *errorCode = [NSString stringWithFormat:@"%@",responseObject[@"errorCode"]];
                NSString *errorMessage = responseObject[@"errorMessage"];
                if ([errorCode isEqualToString:@"0"]) {
                    !responseBlock?:responseBlock(YES);
                }else {
                    [QMUITips showWithText:errorMessage inView:AppKeyWindow hideAfterDelay:1.5f];
                    !responseBlock?:responseBlock(NO);;
                }
            }else{
                [QMUITips showWithText:error.localizedDescription inView:AppKeyWindow hideAfterDelay:1.5f];
                !responseBlock?:responseBlock(NO);;
            }
            
        }else{
            !responseBlock?:responseBlock(NO);;
        }
        
    }];
    
}
/// 对显示的用户进行关注
/// @param isFocus 是否关注
/// @param toSid 显示sid
/// @param responseBlock 完成回调
+(void)requestContentGetContentHeaderFocusType:(BOOL)isFocus ToSid:(NSString *)toSid responseBlock:(void (^)(BOOL ))responseBlock
{
    NSDictionary * dict = @{
                            @"toSid":toSid,
                            @"type":isFocus?@"focus":@"cancelFollow"
                            };
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [CNLiveNetworking setupShowResult:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:CNGetWitnessFollowsContent Param:dict CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if (!error) {
            if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
                NSString *errorCode = [NSString stringWithFormat:@"%@",responseObject[@"errorCode"]];
                NSString *errorMessage = responseObject[@"errorMessage"];
                if ([errorCode isEqualToString:@"0"]) {
                    !responseBlock?:responseBlock(YES);
                }else {
                    [QMUITips showWithText:errorMessage inView:AppKeyWindow hideAfterDelay:1.5f];
                    !responseBlock?:responseBlock(NO);;
                }
            }else{
                [QMUITips showWithText:error.localizedDescription inView:AppKeyWindow hideAfterDelay:1.5f];
                !responseBlock?:responseBlock(NO);;
            }
            
        }else{
            !responseBlock?:responseBlock(NO);;
        }
    }];
}

@end