CNBPerAuthSuccessController.m 11.2 KB
//
//  CNBPerAuthSuccessController.m
//  CNLiveBPersonalVerificationModule
//
//  Created by 殷巧娟 on 2020/4/14.
//

#import "CNBPerAuthSuccessController.h"
#import "CNBPersonalInfoCell.h"
#import "UIColor+CNLiveExtension.h"
#import "CNLiveBaseKit.h"
#import "CNBPerAuthHeaderView.h"
#import "CNBPerAuthViewModel.h"
#import "CNBPerCertificationSuccessModel.h"
#import "CNEmptyView.h"
#import "CNLivePerAuthGetImage.h"
#import "MJExtension.h"
typedef NS_ENUM(NSUInteger,CNBPerAuthErrorType) {
   CNBPerAuthErrorTypeNoNet, //没网
   CNBPerAuthErrorTypeFail,  //请求失败
   CNBPerAuthErrorTypeNoData  //没数据
};
static NSString *infoIdentifier = @"CNBPersonalInfoCell";
static NSString *normalIdentifier = @"CNBInstitutionsInfoCell";
static NSString *groupIdentifier = @"CNBGroupInfoCell";
@interface CNBPerAuthSuccessController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *institutionArray;
@property (nonatomic, strong) CNBUserInfoModel *infoModel;
@property (nonatomic, strong) NSMutableArray *circleArray;
@property (nonatomic, strong) CNEmptyView    *successEmptyView;
@property (nonatomic, strong) NSDictionary   *data; //后台返回的数据
@end

@implementation CNBPerAuthSuccessController

- (instancetype)initControllerWithParam:(NSDictionary *)param {
    self = [super init];
    if (self) {
        _data = param;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.title = @"身份校验";
    if (self.data && [self.data isKindOfClass:[NSDictionary class]] && self.data.count > 0) {
        if ([[self.data allKeys] containsObject:@"institution"]) {
            NSArray *array = self.data[@"institution"];
            if (array.count > 0 ) {
                CNBPerCertificationSuccessModel *successModel = [CNBPerCertificationSuccessModel mj_objectWithKeyValues:self.data];
                self.infoModel = successModel.bUserIdentity;
                for (CNBInstitutionModel *model in successModel.institution) {
                    [self.institutionArray addObject:model];
                }
                [self.institutionArray insertObject:successModel.bUserIdentity atIndex:0];
                [self.tableView reloadData];
            }else {
                [self requestPerAuthSuccessData];
            }
        }else {
              [self requestPerAuthSuccessData];
        }
    }else {
        [self requestPerAuthSuccessData];
    }
}
#pragma mark - 请求数据
- (void)requestPerAuthSuccessData {
    __weak typeof(self) weakSelf = self;
    [CNBPerAuthViewModel getPerAuthSuccessList:^(NSMutableArray * _Nonnull dataArray) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
               if (!strongSelf) {
                   return ;
               }
        if (dataArray.count > 0) {
            CNBPerCertificationSuccessModel *successModel = dataArray[0];
            if (successModel.bUserIdentity && successModel.institution.count > 0) {
                strongSelf.infoModel = successModel.bUserIdentity;
                           for (CNBInstitutionModel *model in successModel.institution) {
                               [strongSelf.institutionArray addObject:model];
                           }
                           [strongSelf.emptyView removeFromSuperview ];
                           strongSelf.tableView.tableFooterView = [UIView new];
                           [strongSelf.institutionArray insertObject:successModel.bUserIdentity atIndex:0];
                           [strongSelf.tableView reloadData];
            }else {
                [strongSelf showEmptyView:CNBPerAuthErrorTypeNoData];
            }
           
        }else {
            [strongSelf showEmptyView:CNBPerAuthErrorTypeNoData];
        }
        
    } failureBlock:^(NSError * _Nonnull error) {
        if (error.code == -1009 || error.code == -1001) {
            [weakSelf showEmptyView:CNBPerAuthErrorTypeNoNet];
        }else {
            [weakSelf showEmptyView:CNBPerAuthErrorTypeFail];
        }
    }];
}
#pragma mark - 重试
- (void)retryloadData {
    [self requestPerAuthSuccessData];
}
#pragma mark - 展示空视图
- (void)showEmptyView:(CNBPerAuthErrorType)errorType {
    NSString *titleName = @"";
    NSString *imageName = @"";
    NSString *retryName = @"";
    if (errorType == CNBPerAuthErrorTypeNoNet) {
        imageName = @"dd_wlcw";
        titleName = @"网络开小差了,刷新试试";
        retryName = @"刷新";
    }else if (errorType == CNBPerAuthErrorTypeFail) {
        imageName = @"wufalianjie";
        titleName = @"接口开小差了,试试重新请求";
        retryName = @"重试";
    }else if (errorType == CNBPerAuthErrorTypeNoData) {
        imageName = @"zhuangtai_kong";
        titleName = @"暂无您的身份内容";
        retryName = @"";
    }
    [self.successEmptyView showImage:[CNLivePerAuthGetImage cnLivePerAuth_getImageName:imageName bundle:@"CNLiveBPersonalVerificationModule.bundle/CNLiveBPersonalVerificationModule" targetClass:[self class]] title:titleName detailTitle:@"" retryBtnTitle:retryName];
    BOOL isHidden = errorType == CNBPerAuthErrorTypeNoData;
    self.successEmptyView.retryBtn.hidden = isHidden;
    self.successEmptyView.retryBtn.backgroundColor = kWhiteColor;
    self.successEmptyView.retryBtn.layer.masksToBounds = YES;
    self.successEmptyView.retryBtn.layer.cornerRadius = 14;
    self.successEmptyView.retryBtn.layer.borderWidth = 1;
    self.successEmptyView.retryBtn.layer.borderColor = CNLiveColorWithHexString(@"48A7F9").CGColor;
    [self.successEmptyView.retryBtn setTitleColor:CNLiveColorWithHexString(@"48A7F9") forState:UIControlStateNormal];
    [self.successEmptyView.retryBtn setTitleColor:CNLiveColorWithHexString(@"48A7F9") forState:UIControlStateHighlighted];
    errorType == CNBPerAuthErrorTypeNoData ? self.tableView.tableFooterView = self.successEmptyView :[self.view addSubview:self.successEmptyView];
    [self.successEmptyView.retryBtn addTarget:self action:@selector(retryloadData) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - tableViewDelegateAndDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.institutionArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CNBInstitutionModel *model = [[CNBInstitutionModel alloc]init];
    if (self.institutionArray.count > 0) {
        model = self.institutionArray[section];
    }
    if (section == 0) {
        return 1;
    }else {
        return model.quanziItem.count > 0 ? (model.quanziItem.count + 1):2;

    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CNBPersonalInfoCell *infoCell = [tableView dequeueReusableCellWithIdentifier:infoIdentifier];
        if (!infoCell) {
            infoCell = [[CNBPersonalInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:infoIdentifier];
        }
        infoCell.infoModel = self.infoModel;
        return infoCell;
    }else {
        if (indexPath.row == 0) {
            CNBInstitutionsInfoCell *normalCell = [tableView dequeueReusableCellWithIdentifier:normalIdentifier];
            if (!normalCell) {
                normalCell = [[CNBInstitutionsInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalIdentifier];
            }
            normalCell.institutionModel = self.institutionArray[indexPath.section];
            return normalCell;
        }else {
            CNBGroupInfoCell *groupCell = [tableView dequeueReusableCellWithIdentifier:groupIdentifier];
            if (!groupCell) {
                groupCell = [[CNBGroupInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:groupIdentifier];
            }
            CNBInstitutionModel *model = self.institutionArray[indexPath.section];
            NSArray *circleArray = model.quanziItem;
           if (circleArray.count > 0) {
               groupCell.circleModel = circleArray[indexPath.row -1];
           }else {
               CNBCirccleModel *circleModel = [[CNBCirccleModel alloc]init];
               circleModel.quanziName = @"无";
               NSMutableArray *groupArray = [NSMutableArray array];
               NSArray *noneArray = @[@{@"groupId":@"",@"groupName":@"无"},
               ];
               for (NSDictionary *dic in noneArray) {
                   CNBGroupModel *model = [CNBGroupModel mj_objectWithKeyValues:dic];
                   [groupArray addObject:model];
               }
               circleModel.groupItem = groupArray;
               groupCell.circleModel = circleModel;
           }
            return groupCell;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 232;
    }else {
        if (indexPath.row == 0) {
            return 40;
        }else {
            return UITableViewAutomaticDimension;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return [UIView new];
    }else {
        CNBPerAuthHeaderView *headerView = [[CNBPerAuthHeaderView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 41)];
        headerView.backgroundColor = [UIColor whiteColor];
        return headerView;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    }else {
        return 41;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = CNLiveColorWithHexString(@"F2F2F2");
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
#pragma mark - Lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,kNavigationBarHeight , KScreenWidth, KScreenHeight - kNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = CNLiveColorWithHexString(@"F2F2F2");
        _tableView.estimatedRowHeight = 70;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}
- (NSMutableArray *)institutionArray {
    if (!_institutionArray) {
        _institutionArray = [NSMutableArray array];
    }
    return _institutionArray;
}
- (NSMutableArray *)circleArray {
    if (!_circleArray) {
        _circleArray = [NSMutableArray array];
    }
    return _circleArray;
}
- (CNEmptyView *)successEmptyView {
    if (!_successEmptyView) {
        _successEmptyView= [[CNEmptyView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, KScreenWidth, KScreenHeight - kNavigationBarHeight) type:0];
        _successEmptyView.backgroundColor = [UIColor whiteColor];
    }
    return _successEmptyView;
}
@end