ViewController.m 10 KB
//
//  ViewController.m
//  CNLiveUserSystemDemo
//
//  Created by iOS on 16/9/12.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "ViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "UserInfoViewController.h"
#import "CNLiveLogout.h"
#import "UserInfoSava.h"
#import "ThirdPartyView.h"
#import "CNLiveThirdLoginManager.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,ThirdPartyViewDelegate,UIGestureRecognizerDelegate>
{
    NSArray *_dataArr;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ThirdPartyView *thirPartydView;
@property (nonatomic, strong) UIButton *loginBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户云 SDK Demo";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"back";
    self.navigationItem.backBarButtonItem = backItem;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-40, CGRectGetWidth(self.view.frame), 30)];
    label.text = @"北京中投视讯文化传媒股份有限公司";
    [self.view addSubview:label];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:label];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.thirPartydView];
    
    _dataArr = @[@"通过手机号修改密码",@"更新附加信息",@"修改用户信息",@"查询用户信息",@"反馈",@"绑定第三方账号"];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([Tools isLogin]) {
        [_loginBtn setTitle:@"注销" forState:UIControlStateNormal];
    } else {
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    }
    [_tableView reloadData];
}

#pragma mark - action
- (void)logoutBtnAction
{
    if ([Tools isLogin]) {
        [CNLiveLogout clearLocalUserInfo];
        [Tools setUserLogin:NO];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_tableView reloadData];
    } else {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

- (void)tapGesturedDetected:(UITapGestureRecognizer *)recognizer
{
    [UIView animateWithDuration:0.25 animations:^{
        _thirPartydView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 160);
    }];
}

#pragma mark - ThirdPartyViewDelegate
- (void)thirdPartyBtnAction:(NSString *)title
{
    if ([title isEqualToString:@"微信"]) {
        [[CNLiveThirdLoginManager manager] weChatLoginIsBindingThirdParty:YES];
    } else if ([title isEqualToString:@"微博"]) {
        [[CNLiveThirdLoginManager manager] sinaLoginIsBindingThirdParty:YES];
    } else {
        [[CNLiveThirdLoginManager manager] qqLoginIsBindingThirdParty:YES];
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        _thirPartydView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 160);
    }];
}

- (void)cancelThirdView
{
    [UIView animateWithDuration:0.25 animations:^{
        _thirPartydView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 160);
    }];
}

#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([Tools isLogin]) {
        return _dataArr.count+1;
    } else {
        return 1;
    }
    return _dataArr.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        UITableViewCell *loginCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        
        UIImageView *iconImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
        iconImg.layer.cornerRadius = 40;
        iconImg.layer.masksToBounds = YES;
        [loginCell.contentView addSubview:iconImg];
        UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];

        [iconImg sd_setImageWithURL:[NSURL URLWithString:model.faceUrl] placeholderImage:[UIImage imageNamed:@"touxiang"]];
        
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, KScreenWidth- 160, 120)];
        if ([Tools isLogin]) {
            name.text = @"已登录";
        } else {
            name.text = @"登录";
        }
        
        [loginCell.contentView addSubview:name];
        
        return loginCell;
        
    } else {
        static NSString *cellIndentify = @"cellIndentify";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentify];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%@",_dataArr[indexPath.row-1]];
        return cell;
    
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 1) {
        RegisterViewController *regVC = [[RegisterViewController alloc] init];
        regVC.type = @"forgetPwd";
        [self.navigationController pushViewController:regVC animated:YES];
    }
    
    if (indexPath.row == 2) {
        
        [SVProgressHUD show];
        
        UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];

        [CNLiveUserSystemCenter updateOtherInfoWithUid:model.uid extInfo:@"将要更新的附加信息,NSString类型" success:^(id result) {
            
            NSLog(@"--------%@",result);
            
            [SVProgressHUD showSuccessWithStatus:@"更新附加信息成功"];
            [self.navigationController popViewControllerAnimated:YES];

        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
            [SVProgressHUD dismiss];
        }];
    }
    
    if (indexPath.row == 3) {
        UserInfoViewController *userInfo = [[UserInfoViewController alloc] init];
        [self.navigationController pushViewController:userInfo animated:YES];
    }
    
    if (indexPath.row == 4) {
        UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];
        
        [CNLiveUserSystemCenter queryUserInfoWithUid:model.uid srcUid:model.uid success:^(id result) {
            NSLog(@"%@",result);

            NSDictionary *dic = result[@"data"];
            NSString *message = [NSString stringWithFormat:@" uid:%@\n nickName:%@\n gender:%@\n location:%@\n extInfo:%@\n mobile:%@\n email:%@",dic[@"uid"],dic[@"nickName"],dic[@"gender"],dic[@"location"],dic[@"extInfo"],dic[@"mobile"],dic[@"email"]];
            NSString *icon = dic[@"faceUrl"];

            [UserInfoSava setLocalUserInfo:icon withKey:@"faceUrl"];
            [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            [Tools toast:message];
 
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
        }];
    }
    
    if (indexPath.row == 5) {

        [CNLiveUserSystemCenter feedbackWithContentId:@"11" message:@"fhsjhifkncznshfu" success:^(id result) {
            [Tools toast:[NSString stringWithFormat:@"反馈--%@",result[@"errorMessage"]]];

        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
        }];
    }
    
    if (indexPath.row == 6) {
        
        [UIView animateWithDuration:0.25 animations:^{
            _thirPartydView.frame = CGRectMake(0, KScreenHeight-160, KScreenWidth, 160);
        }];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 120;
    } else {
        return 50;
    }
}

#pragma mark - getter
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight - 120 - 64) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (ThirdPartyView *)thirPartydView
{
    if (!_thirPartydView) {
        _thirPartydView = [[ThirdPartyView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 160) binding:YES];
        _thirPartydView.delegate = self;
    }
    return _thirPartydView;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(20, KScreenHeight-100, KScreenWidth-40, 40);
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_loginBtn addTarget:self action:@selector(logoutBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_loginBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _loginBtn.layer.borderWidth = 1;
    }
    return _loginBtn;
}

- (void)dealloc
{
    _thirPartydView.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end