ViewController.m 7.49 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"

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

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户云";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.loginBtn];

    _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];
    }
}

#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.backgroundColor = [UIColor orangeColor];
        [loginCell.contentView addSubview:iconImg];
        UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];

        [iconImg sd_setImageWithURL:[NSURL URLWithString:model.faceUrl] placeholderImage:[UIImage imageNamed:@""]];
        
        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"]]];
        }];
    }
}

- (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, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(20, 53*5+63+12+80, KScreenWidth-40, 40);
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn setBackgroundColor:[UIColor orangeColor]];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_loginBtn addTarget:self action:@selector(logoutBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.layer.masksToBounds = YES;
    }
    return _loginBtn;
}

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

@end