GiGaMaskTime/GIGA/Modules/Me/Controller/GiGaChangePhoneNumberVC.m

409 lines
14 KiB
Objective-C

//
// GiGaChangePhoneNumberVC.m
// GIGA
//
// Created by lianxiang on 2018/9/19.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaChangePhoneNumberVC.h"
#import "GIGaUserChangeMobleCell.h"
#import "GiGaBaseAPiRequest.h"
#import "NSTimer+Convenience.h"
//typedef NS_ENUM(NSInteger,PohoneCodeType)
//{
// PohoneCodeTypeOld = 0,//原手机号
// PohoneCodeTypeNew//新手机号
//
//
//};
@interface GiGaChangePhoneNumberVC ()
//@property(nonatomic) PohoneCodeType viewCodeType;
@property(nonatomic) BOOL nextSteep;
@property(nonatomic,copy) NSString *oldCode;
@property(nonatomic,copy) NSString *xinCode;
@property(nonatomic,strong) NSTimer*timer;
@property(nonatomic) NSInteger time;
@end
@implementation GiGaChangePhoneNumberVC
- (void)dealloc
{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addNavTitile:@"修改手机号"];
_nextSteep = NO;
_time = 60;
// Do any additional setup after loading the view from its nib.
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.tableView.sectionFooterHeight = 0;
self.tableView.estimatedSectionFooterHeight= 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.bounces = NO;
self.tableView.frame = CGRectMake(0,0, KMainW, self.view.bounds.size.height - SAFE_NAV_HEIGHT);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
GIGaUserChangeMobleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserChangeMobleCell"];
if(!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserChangeMobleCell" owner:self options:nil] lastObject];
}
[cell.codeSendBtn addTarget:self action:@selector(sendCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
if (_nextSteep) {
cell.numberTexFiled.userInteractionEnabled = YES;
cell.numberTexFiled.text = @"";
cell.numberTexFiled.placeholder = @"请输入新手机号";
cell.codeTexFiled.text = @"";
cell.codeTexFiled.placeholder = @"请输入新手机验证码";
cell.oldBtn.backgroundColor = GIGARGB(227, 227, 227, 1);
NSAttributedString *att = [GiGaHelper stringWithText:@"发送验证码" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(10) leterSpace:0];
[cell.codeSendBtn setAttributedTitle:att forState:UIControlStateNormal];
cell.codeSendBtn.backgroundColor = GIGARGB(181, 14, 14, 1);
cell.codeSendBtn.userInteractionEnabled = YES;
NSAttributedString *atti1 = [GiGaHelper stringWithText:@"1.验证原号码" textColor:GIGARGB(165,165, 165, 1) textFont:GIGA_TEXTFONTMEDIUM(13) leterSpace:0];
[cell.oldBtn setAttributedTitle:atti1 forState:UIControlStateNormal];
cell.xinPhoneBtn.backgroundColor = GIGARGB(227, 227, 227, 1);
NSAttributedString *atti = [GiGaHelper stringWithText:@"2.绑定新手机" textColor:GIGARGB(131, 10, 10, 1) textFont:GIGA_TEXTFONTMEDIUM(13) leterSpace:0];
[cell.xinPhoneBtn setAttributedTitle:atti forState:UIControlStateNormal];
[cell.xinPhoneBtn setBackgroundImage:[UIImage imageNamed:@"user_changephonewihte"] forState:UIControlStateNormal];
}else{
GiGaUser *user = [[GiGaUserManager shareUser] getCurrentUser];
if (user.mobileMask) {
cell.numberTexFiled.text = user.mobileMask;
}
cell.numberTexFiled.userInteractionEnabled = NO;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 170.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 115.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] init];
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
nextBtn.backgroundColor = GIGARGB(181, 14, 14, 1);
nextBtn.layer.masksToBounds = YES;
nextBtn.layer.cornerRadius = 41/2;
[nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
nextBtn.frame = CGRectMake((KMainW - 244)/2 , 5, 244, 41);
if (_nextSteep) {
NSAttributedString *attrititle = [GiGaHelper stringWithText:@"完成" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(16) leterSpace:0];
[nextBtn setAttributedTitle:attrititle forState:UIControlStateNormal];
}else{
NSAttributedString *attrititle = [GiGaHelper stringWithText:@"下一步" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(16) leterSpace:0];
[nextBtn setAttributedTitle:attrititle forState:UIControlStateNormal];
}
[view addSubview:nextBtn];
UILabel *tishiLab = [[UILabel alloc] init];
tishiLab.frame = CGRectMake((KMainW - 70)/2,CGRectGetMaxY(nextBtn.frame) + 32, 70, 14);
NSMutableAttributedString *attriTile = [[NSMutableAttributedString alloc] init];
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image = [UIImage imageNamed:@"ic_love"];
attch.bounds = CGRectMake(-2,-3, 12, 12);
NSAttributedString *imastr=[NSAttributedString attributedStringWithAttachment:attch];
[attriTile appendAttributedString:imastr];
NSAttributedString *titleStr = [[NSAttributedString alloc] initWithString:@"温馨提示:" attributes:@{NSFontAttributeName:GIGA_TEXTFONTMEDIUM(10), NSForegroundColorAttributeName:GIGARGB(187, 187, 187, 1)}];
[attriTile appendAttributedString:titleStr];
tishiLab.attributedText = attriTile;
[view addSubview:tishiLab];
UILabel *desc =[[UILabel alloc] init];
desc.frame = CGRectMake((KMainW - 240.f) / 2 , CGRectGetMaxY(tishiLab.frame) + 2, 240, 14);
desc.text =@"手机号码修改成功后需要使用新的手机号进行登陆哟。";
desc.textColor = GIGARGB(187, 187, 187, 1);
desc.font = GIGA_TEXTFONTMEDIUM(10);
[view addSubview:desc];
return view;
}
#pragma mark 下一步 Or 完成
-(void)nextBtnAction:(UIButton *)btn{
if (_nextSteep) {
GILog(@"完成");
GIGaUserChangeMobleCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if (cell.numberTexFiled.text.length == 0) {
GIGA_ShowToast(@"请输入新手机号");
return;
}
if (![GiGaHelper isPhoneNumber:cell.numberTexFiled.text]) {
GIGA_ShowToast(@"请输入正确手机号");
return;
}
if (cell.codeTexFiled.text.length == 0) {
GIGA_ShowToast(@"请输入新手机验证码");
return;
}
[self verifyPhoneCode:cell.numberTexFiled.text code:cell.codeTexFiled.text btn:btn];
}else{
GILog(@"下一步");
GIGaUserChangeMobleCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//
if (cell.codeTexFiled.text.length == 0) {
GIGA_ShowToast(@"请输入验证码");
return;
}
GiGaUser *user = [[GiGaUserManager shareUser] getCurrentUser];
if (!user.mobile || [user.mobile isKindOfClass:[NSNull class]]) {
GILog(@"手机号为空");
return;
}
[self verifyPhoneCode:user.mobile code:cell.codeTexFiled.text btn:btn];
}
}
-(void)finishBtn:(UIButton *)btn wtihNewPhone:(NSString *)xinNumber{
GiGaUser *user = [[GiGaUserManager shareUser] getCurrentUser];
NSString *oldphone = user.mobile;
GIGaUserChangeMobleCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
inSection:0]];
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath: kApiChangePhone method:RequestPostMethod parms:@{@"oldMobile":oldphone,@"newMobile":xinNumber,@"oldVerifiedCode":self.oldCode,@"newVerifiedCode":cell.codeTexFiled.text}];
[api requstDataWithResult:^(GiGaAPIResult *result) {
btn.userInteractionEnabled = YES;
[self.view hideToastActivity];
if (result.success) {
GIGA_WIndowTost(@"手机号修改成功");
[self.navigationController popViewControllerAnimated:YES];
}else{
GIGA_ShowToast(result.message);
}
}];
}
#pragma mark - 发送验证码
-(void)sendCodeBtnAction:(UIButton *)btn{
if (_nextSteep) {
GIGaUserChangeMobleCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if (cell.numberTexFiled.text.length == 0) {
GIGA_ShowToast(@"请输新手机号");
return;
}
if (![GiGaHelper isPhoneNumber:cell.numberTexFiled.text]) {
GIGA_ShowToast(@"请输入正确手机号");
return;
}
self.time = 60;
self.xinCode = cell.codeTexFiled.text;
//发送新手机验证码
[self sendCodeWith:cell.numberTexFiled.text
codebtn:btn];
}else{
GiGaUser *user = [[GiGaUserManager shareUser] getCurrentUser];
if (!user.mobile || [user.mobile isKindOfClass:[NSNull class]]) {
GILog(@"手机号为空");
return;
}
//发送旧手机验证码
[self sendCodeWith:user.mobile codebtn:btn];
}
}
//计时
-(void)startTimer{
[self cancleTimerCount];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimercount) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
-(void)startTimercount{
GIGaUserChangeMobleCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
inSection:0]];
cell.codeSendBtn.backgroundColor = [UIColor lightGrayColor];
cell.codeSendBtn.userInteractionEnabled = NO;
__weak typeof(cell) weakCell = cell;
if (_time>0) {
weakify(self);
weakSelf.time -- ;
NSString *timeStr=[NSString stringWithFormat:@"%lds",(long)weakSelf.time];
NSAttributedString *att = [GiGaHelper stringWithText:timeStr textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(10) leterSpace:0];
[weakCell.codeSendBtn setAttributedTitle:att forState:UIControlStateNormal];
if (weakSelf.time==0) {
[weakSelf codeBtnStateNormal];
weakCell.codeSendBtn.userInteractionEnabled = YES;
weakCell.codeSendBtn.backgroundColor = GIGARGB(181, 14, 14, 1);
[self cancleTimerCount];
}
}
}
-(void)cancleTimerCount{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
}
-(void)sendCodeWith:(NSString *)phone codebtn:(UIButton *)btn{
NSString *utl = nil;
if (_nextSteep) {
utl= kApiChangeMobleNewCode;
}else{
utl= kApiChangeMobleOldCode;
}
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:utl method:RequestPostMethod parms:@{@"mobile":phone}];
[api requstDataWithResult:^(GiGaAPIResult *result) {
if (result.success) {
[self startTimer];
GIGA_ShowToast(@"已发送");
}else{
GIGA_ShowToast(result.message);
}
}];
}
-(void)codeBtnStateNormal{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
self.time =60;
}
#pragma mark - 验证手机验证码
-(void)verifyPhoneCode:(NSString *)phonenumber code:(NSString *)code btn:(UIButton *)btn
{
btn.userInteractionEnabled = NO;
[self.view makeToastActivity:CSToastPositionCenter];
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:kApiVersifyPhoneCode method:RequestPostMethod parms:@{@"mobile":phonenumber,@"verifiedCode":code}];
[api requstDataWithResult:^(GiGaAPIResult *result) {
if (result.success) {
if (self.nextSteep) {
//验证码通过 完成
[self finishBtn:btn wtihNewPhone:phonenumber];
}else{
btn.userInteractionEnabled = YES;
[self.view hideToastActivity];
self.nextSteep = YES;
self.oldCode = code;
[self.tableView reloadData];
[self codeBtnStateNormal];
}
}else{
btn.userInteractionEnabled = YES;
[self.view hideToastActivity];
GIGA_ShowToast(result.message);
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end