CNUserAgreementManager.m 10.2 KB
//
//  CNUserAgreementManager.m
//  Test
//
//  Created by CNLive-zxw on 2019/4/13.
//  Copyright © 2019 CNLive. All rights reserved.
//

#import "CNUserAgreementManager.h"
#import "CNUserAgreementWebController.h"
#import "CNLiveNetworking.h"
#import "CNLiveEnvironment.h"

@BeeHiveService(CNLiveUserAgreementServiceProtocol,CNUserAgreementManager)
@interface CNUserAgreementManager ()<CNLiveUserAgreementServiceProtocol>

@end

@implementation CNUserAgreementManager

static CNUserAgreementManager *_instance = nil;

/**
 * @abstract 单例初始化
 */
+ (instancetype)manager{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _instance = [[CNUserAgreementManager alloc]init];
    });
    return _instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    return _instance;
}

- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
    return _instance;
}

+ (void)jumpToAgreementH5WithType:(CNGetAgreementH5Type)type {
    [CNUserAgreementManager getAgreementH5WithType:type success:^(NSString *h5, NSString *title) {
        UIViewController *root = [UIApplication sharedApplication].delegate.window.rootViewController;
        
        //跟控制器是Navigation
        if ([root isKindOfClass:[UINavigationController class]]){
            UINavigationController *nav = (UINavigationController *)root;
            CNUserAgreementWebController *vc = [[CNUserAgreementWebController alloc] initWithUrl:h5 name:title];
            [nav pushViewController:vc animated:YES];

        }
        //跟控制器是TabBar
        else if ([root isKindOfClass:[UITabBarController class]]){
            UITabBarController *tab = (UITabBarController *)root;
            UINavigationController *nav = tab.selectedViewController;
            CNUserAgreementWebController *vc = [[CNUserAgreementWebController alloc] initWithUrl:h5 name:title];
            [nav pushViewController:vc animated:YES];

        }else{
            CNUserAgreementWebController *vc = [[CNUserAgreementWebController alloc] initWithUrl:h5 name:title];
            [root presentViewController:vc animated:YES completion:nil];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

+ (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure {
    [[CNUserAgreementManager manager] getAgreementH5WithType:type success:^(NSString * _Nonnull h5, NSString * _Nonnull title) {
        success(h5, title);
        
    } failure:^(NSError * _Nonnull error) {
        failure(error);

    }];
    
}

- (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure {
    [CNLiveNetworking setAllowRequestDefaultArgument:NO];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:CNGetAgreementH5Url Param:@{} CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if (error) {
            if (failure) {
                failure(error);
            }
        }
        else{
            if ([responseObject isKindOfClass:[NSArray class]]) {

                for (NSDictionary *dic in responseObject) {

                    switch (type) {
                        case CNAgreementUser:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"yhxy"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementPrivacy:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"yszc"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementHelp:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"bzfk"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementUnderstand:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"ljwjj"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementIntroduction:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"yyjj"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementCopyright:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"bqxx"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementPermission:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"rjxkxy"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementIntegralRules:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"jfgz"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementSigninRules:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"jfjlgz"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementReviewApply:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"shsqxy"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementCancellation:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"zxxz"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementEmojiService:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"fwsm"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementEmojiComplaints:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"tssm"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        case CNAgreementEnterpriseQualification:
                        {
                            if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:@"qyzz"]) {
                                if (success) {
                                    success(dic[@"detailUrl"],dic[@"title"]);
                                    return ;
                                }
                            }
                        }
                            break;
                        default:
                            break;
                    }

                }

            }
            if (failure) {
                failure(error);
            }
        }
    }];
    
}

@end