GiGaMaskTime/GIGA/Modules/LogIn/Controller/GiGaRegistViewController.m

268 lines
9.9 KiB
Objective-C

//
// GiGaRegistViewController.m
// GIGA
//
// Created by lianxiang on 2018/8/21.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaRegistViewController.h"
#import "TTTAttributedLabel.h"
#import "GiGaWebViewController.h"
#import "NSTimer+Convenience.h"
#import "GiGaBaseAPiRequest.h"
#import "GiGaNetManager.h"
#import "MBProgressHUD.h"
#import "GiGaAttributedLabel.h"
#import "UINavigationBar+Custom.h"
#import "GiGaUserDefault.h"
@interface GiGaRegistViewController ()<TTTAttributedLabelDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *sendCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *passTexFied;
@property (weak, nonatomic) IBOutlet UIButton *checkBtn;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;
@property (weak, nonatomic) IBOutlet GiGaAttributedLabel *useragreementLabel;
@property(nonatomic,strong) NSTimer*timer;
@property(nonatomic) NSInteger time;
@end
@implementation GiGaRegistViewController
- (void)viewDidLoad {
[super viewDidLoad];
//self.title = @"注册";
//[self addNavTitile:@"注册"];
_time = 60;
// Do any additional setup after loading the view from its nib.
[self confirmUI];
[self.sendCodeBtn addTarget:self action:@selector(sendCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.registBtn addTarget:self action:@selector(registBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.checkBtn addTarget:self action:@selector(checkBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self resetBackimg];
}
-(void)resetBackimg{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 40, 40);
[backBtn setImage:[UIImage imageNamed:@"nav_redback"] forState:UIControlStateNormal];
[backBtn setImageEdgeInsets:UIEdgeInsetsMake(0,-14, 0, 14)];
//backBtn.backgroundColor = [UIColor redColor];
[backBtn addTarget:self action:@selector(backItemAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = backItem;
}
-(void)backItemAction{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setClearNav];
}
-(void)dealloc{
if (_timer) {
[_timer unfireTimer];
[_timer invalidate];
_timer = nil;
}
}
-(void)sendCodeBtnAction:(UIButton *)btn{
if (self.phoneNumberTextField.text == nil) {
GIGA_ShowToast(@"请输入手机号");
return;
}
if (![GiGaHelper isPhoneNumber:self.phoneNumberTextField.text]) {
GIGA_ShowToast(@"请输入正确手机号");
return;
}
btn.userInteractionEnabled = NO;
[self startTimer:btn];
[self sendVerifiCode:btn];
}
-(void)startTimer:(UIButton *)btn{
if (_time>0) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1 count:60 callback:^{
NSString *timeStr=[NSString stringWithFormat:@"%lds",(long)self->_time];
[btn setTitle:timeStr forState:UIControlStateNormal];
self->_time--;
if (self->_time==0) {
[self.sendCodeBtn setTitle:@"发送短信" forState:UIControlStateNormal];
self->_time =60;
btn.userInteractionEnabled = YES;
}
}];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
}
#pragma mark 发送短信验证码
-(void)sendVerifiCode:(UIButton *)btn{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *phoneNumber = self.phoneNumberTextField.text;
NSDictionary *param = @{
@"mobile":phoneNumber
};
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:@"msg/sms/v1/registersendcode" method:RequestPostMethod parms:param];
[api requstDataWithResult:^(GiGaAPIResult *result) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
GILog(@"短信获取%@",result.message);
btn.userInteractionEnabled = YES;
}];
}
-(void)confirmUI{
self.useragreementLabel.highlightedTextColor = GIGARGB(219, 23, 37, 1);
self.useragreementLabel.textColor = GIGARGB(145,145,145, 1);
self.useragreementLabel.delegate = self;
self.useragreementLabel.enabledTextCheckingTypes= NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress|NSTextCheckingTypeLink;
self.useragreementLabel.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
//链接正常状态文本属性NSTextCheckingTypeLink;
NSString *text = @"已阅读并同意《用户服务协议》";
[self.useragreementLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange fontRange = [[mutableAttributedString string] rangeOfString:@"用户服务协议" options:NSCaseInsensitiveSearch];
UIFont* sysFont = [UIFont fontWithName:@"PingFangSC-Semibold" size:10.f];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)sysFont.fontName, sysFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:fontRange];
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(__bridge id)[GIGARGB(219, 23, 37, 1) CGColor] range:fontRange];
}
return mutableAttributedString;
}];
NSRange fontRange = [text rangeOfString:@"用户服务协议" options:NSCaseInsensitiveSearch];
[self.useragreementLabel addLinkToTransitInformation:@{
@"SourceName":@"useragreement"
} withRange:fontRange];
self.registBtn.layer.masksToBounds = YES;
self.registBtn.layer.cornerRadius = self.registBtn.frame.size.height / 2;
}
#pragma mark - TTTAttributedLabelDelegate
-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
}
-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components
{
NSLog(@"components%@",components);
// GiGaWebViewController *webView = [[GiGaWebViewController alloc] init];
// webView.souceType = WebSourceTypeLoacalHtml;
// webView.fileName = components[@"SourceName"];
// [self.navigationController pushViewController:webView animated:YES];
GiGaWebViewController *webView = [[GiGaWebViewController alloc] init];
webView.souceType = WebSourceTypeUrlLink;
webView.url = @"http://www.youtansu.com";
[self.navigationController pushViewController:webView animated:YES];
}
#pragma mark 注册
-(void)registBtnAction:(UIButton *)btn{
if (self.phoneNumberTextField.text.length ==0) {
GIGA_ShowToast(@"请输入手机号");
return;
}
if (![GiGaHelper isPhoneNumber:self.phoneNumberTextField.text]) {
GIGA_ShowToast(@"请输入正确手机号");
return;
}
if (self.codeTextField.text.length == 0) {
GIGA_ShowToast(@"请输入验证码");
return;
}
BOOL isPass = [GiGaHelper checkPassWord:self.passTexFied.text];
if (isPass == NO) {
GIGA_ShowToast(@"请输入6-20位数字和字母组成的密码");
return;
}
[self regeist:btn];
}
//注册
-(void)regeist:(UIButton *)btn{
btn.userInteractionEnabled = NO;
NSDictionary *weixinInfoDic = [GiGaUserDefault getWeiXinAccessToenDic];
NSDictionary *param = nil;
if (weixinInfoDic) {
param = @{@"username":self.phoneNumberTextField.text,@"verifiedCode":self.codeTextField.text,@"password":self.passTexFied.text,@"openid":weixinInfoDic[@"openid"]};
}else{
param = @{@"username":self.phoneNumberTextField.text,@"verifiedCode":self.codeTextField.text,@"password":self.passTexFied.text};
}
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:@"sys/v1/phoneregister" method:RequestPostMethod parms:param];
[api requstDataWithResult:^(GiGaAPIResult *result) {
btn.userInteractionEnabled = YES;
GILog(@"注册->%@",result.message);
if (result.code == 0) {
NC_POST_NAME_OBJECT(kUserRegistSucccess,self.phoneNumberTextField.text);
GIGA_ShowToast(result.message);
[self.navigationController popToRootViewControllerAnimated:YES];
}else if (result.code == GiGAErrorCode_USER_EXIST){
GIGA_ShowToast(@"用户已存在");
NC_POST_NAME_OBJECT(kUserRegistSucccess,self.phoneNumberTextField.text);
}else{
GIGA_ShowToast(result.message);
}
}];
}
-(void)checkBtnAction:(UIButton *)btn{
btn.selected = !btn.selected;
if (btn.selected) {
[btn setImage:[UIImage imageNamed:@"btn_turndown"] forState:UIControlStateSelected];
[self.registBtn setBackgroundColor:[UIColor lightGrayColor]];
self.registBtn.userInteractionEnabled = NO;
}else{
[btn setImage:[UIImage imageNamed:@"btn_right"] forState:UIControlStateNormal];
[self.registBtn setBackgroundColor:GIGARGB(0, 222, 189, 1)];
self.registBtn.userInteractionEnabled = YES;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end