You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

459 lines
18 KiB

//
// RegisterViewController.m
// LekangGuard
//
// Created by ecell on 2022/10/9.
//
#import "RegisterViewController.h"
#import "commWebViewController.h"
#import "AppDelegate.h"
@interface RegisterViewController ()<UITextViewDelegate>
/// 密码View
@property (nonatomic ,weak) UIView *passView;
/// 邮箱TextField
@property (nonatomic ,weak) UITextField *emailField;
/// 验证码TextField
@property (nonatomic ,weak) UITextField *codeField;
/// 密码TextField
@property (nonatomic ,weak) UITextField *passField;
/// 确认密码TextField
@property (nonatomic ,weak) UITextField *passIField;
/// 验证码按钮
@property (nonatomic ,weak) UIButton *codeBtn;
@property (nonatomic ,weak) UIButton *registerBtn;
/// 国家/地区
@property (nonatomic ,strong) UIButton *countryBtn;
/// 国家/地区编码
@property (nonatomic ,weak) UILabel *countryCodeLabel;
/// 国家/地区名称
@property (nonatomic ,weak) UILabel *countryTitleLabel;
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.zx_navBarBackgroundColor = KKClearColor;
self.zx_navTintColor = KKTextBlackColor;
self.backImgName = @"asd";
// Do any additional setup after loading the view.
self.zx_navTitle = GJText(self.navTitle);
[self.view addSubview:self.countryBtn];
NSArray *titleArr = @[GJText(@"手机号"),
GJText(@"验证码"),
GJText(@"密码"),
GJText(@"确认密码")];
NSArray *placeholderArr = @[GJText(@"请输入手机号"),
GJText(@"请输入验证码"),
GJText(@"请输入6~16个字符"),
GJText(@"请输入6~16个字符")];
for (int i = 0; i < titleArr.count; i++)
{
UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:RGB(245, 245, 245) cornerRadius:5 borderWidth:0 borderColor:KKClearColor];
if (i == titleArr.count-1) self.passView = bgView;
[self.view addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view).inset(15);
make.top.equalTo(self.countryBtn.mas_bottom).offset(15+Adapted(50)*i+10*i);
make.height.mas_equalTo(Adapted(50));
}];
CGFloat width = [(NSString *)titleArr[i] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FontADA_(14)} context:nil].size.width;
UILabel *leftTitleLabel = [UICommon ui_label:CGRectZero lines:1 align:NSTextAlignmentLeft font:FontADA_(14) textColor:KKTextBlackColor text:titleArr[i] Radius:0];
if (i == 0)
{
self.countryCodeLabel = leftTitleLabel;
self.countryCodeLabel.text = @"+86";
}
[self.view addSubview:leftTitleLabel];
[leftTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(bgView).offset(10);
make.centerY.equalTo(bgView);
make.width.mas_equalTo(width+3);
}];
UIButton *rightBtn;
if (i == 1)
{
rightBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(12) normalColor:KKMainColor normalText:GJText(@"获取验证码") click:^(UIButton *sender) {
[self GetEmailCode];
}];
CGRect rect = [UICommon GetTextWidth:rightBtn.currentTitle ViewHeight:Adapted(30) fontSize:FontADA_(12) type:@"w"];
self.codeBtn = rightBtn;
[bgView addSubview:rightBtn];
[rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(bgView);
make.right.equalTo(bgView.mas_right).inset(10);
make.size.mas_equalTo(CGSizeMake(Adapted(rect.size.width), Adapted(30)));
}];
}
UITextField *textfield = [UICommon ui_textField:CGRectZero textColor:KKTextBlackColor backColor:KKClearColor font:FontADA_(14) maxTextNum:20 placeholderColor:KKGrey143 placeholder:placeholderArr[i] toMaxNum:^(UITextField *textField) {
} change:^(UITextField *textField) {
}];
if (i == 0)
{
textfield.keyboardType = UIKeyboardTypePhonePad;
self.emailField = textfield;
}
else if (i == 1)
{
textfield.keyboardType = UIKeyboardTypeNumberPad;
self.codeField = textfield;
}
else if (i == 2)
{
textfield.secureTextEntry = YES;
self.passField = textfield;
}
else
{
textfield.secureTextEntry = YES;
self.passIField = textfield;
}
[bgView addSubview:textfield];
[textfield mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(leftTitleLabel.mas_right).offset(7);
if (i == 1)
make.right.equalTo(rightBtn.mas_left).inset(10);
else
make.right.equalTo(bgView).inset(10);
make.centerY.equalTo(bgView);
make.height.mas_equalTo(Adapted(44));
}];
}
/// 注册
kWeakSelf(self)
UIButton *registerBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(15) normalColor:KKWhiteColorColor normalText:NSLocalizedString(self.isViewType == 0 ? GJText(@"注册") : self.isViewType == 1 ? GJText(@"重置密码") : GJText(@"确定"),nil) click:^(id x) {
[self UserRegister];
}];
registerBtn.backgroundColor = RGB(255, 173, 1);
registerBtn.tintColor = KKMainColor;
registerBtn.layer.cornerRadius = 5;
registerBtn.layer.masksToBounds = YES;
self.registerBtn = registerBtn;
[self.view addSubview:registerBtn];
[registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.passView.mas_bottom).inset(Adapted(15));
make.left.right.equalTo(self.view).inset(Adapted(20));
make.height.mas_equalTo(Adapted(44));
}];
if (self.isViewType == 0)
{
UITextView *agreementView = [[UITextView alloc] init];
agreementView.backgroundColor = [UIColor clearColor];
agreementView.delegate = self;
agreementView.editable = NO;
agreementView.contentSize = CGSizeMake(0, 0);
//去除配置 可自定义链接样式
agreementView.linkTextAttributes = @{};
// lable.text = @"点击“注册”即表示你同意《用户服务协议》";
NSString * userAgreement = GJText(@"用户服务协议");
NSString * privacyPolicy = GJText(@"隐私政策");
NSString * allStr = [NSString stringWithFormat:GJText(@"点击注册即表示您同意%@和%@"),userAgreement,privacyPolicy];
NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:allStr];
[attributedStr addAttribute:NSForegroundColorAttributeName value:KKMainColor range:[allStr rangeOfString:userAgreement]];
[attributedStr addAttribute:NSLinkAttributeName value:AccoutXieYiURLStr range:[allStr rangeOfString:userAgreement]];
[attributedStr addAttribute:NSForegroundColorAttributeName value:KKMainColor range:[allStr rangeOfString:privacyPolicy]];
[attributedStr addAttribute:NSLinkAttributeName value:PrivacyPolicyURLStr range:[allStr rangeOfString:privacyPolicy]];
[attributedStr addAttribute:NSForegroundColorAttributeName value:KKGrey121 range:[allStr rangeOfString:GJText(@"点击注册即表示您同意")]];
[attributedStr addAttribute:NSForegroundColorAttributeName value:KKGrey121 range:[allStr rangeOfString:GJText(@"")]];
agreementView.attributedText = attributedStr;
agreementView.textAlignment = NSTextAlignmentCenter;
agreementView.font = FontADA_(10);
[self.view addSubview:agreementView];
[agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.bottom.equalTo(self.view.mas_bottom).inset(iPhoneX_TabbarSafeBottomMargin > 0 ? iPhoneX_TabbarSafeBottomMargin : 34);
make.height.offset(30);
make.width.offset(SCREEN_WIDTH-28);
}];
}
}
#pragma mark 用户注册/忘记密码
/// 用户注册/忘记密码
- (void)UserRegister
{
WeakSelf
NSLog(@"%ld---%ld",self.passField.text.length,self.passIField.text.length);
[UICommon resignKeyboard];
if (!self.emailField.text || self.emailField.text.length != 11)
{
[UICommon MessageErrorText:@"请输入手机号"];
return;
}
else if (self.passField.text.length < 6 || self.passField.text.length > 16)
{
[UICommon MessageErrorText:@"密码应该为6-16位数"];
return;
}
else if (self.passIField.text.length < 6 || self.passIField.text.length > 16)
{
[UICommon MessageErrorText:@"密码应该为6-16位数"];
return;
}
else if (![self.passField.text isEqualToString:self.passIField.text])
{
[UICommon MessageErrorText:@"输入密码不一致"];
return;
}
else if (self.codeField.text.length <= 1)
{
[UICommon MessageErrorText:@"请输入验证码"];
return;
}
self.registerBtn.enabled = NO;
[UICommon MessageUpload:@"加载中"];
if (self.isViewType != 2)
{
// [SMSSDK commitVerificationCode:weakSelf.codeField.text phoneNumber:weakSelf.emailField.text zone:@"86" result:^(NSError *error) {
// if (!error)
// {
// [self UserRegisterUp];
// } else
// {
// [UICommon HidenLoading];
// [UICommon MessageErrorText:@"验证码验证失败"];
// self.registerBtn.enabled = YES;
// }
// }];
[self UserRegisterUp];
}
else
{
NSMutableDictionary *parameters = self.authInfo;
[parameters setValue:self.emailField.text forKey:@"phone"];
[parameters setValue:self.passIField.text forKey:@"password"];
[parameters setValue:self.codeField.text forKey:@"code"];
[[[APIManager sharedManager] APPOST:WechatLogin_URL parameters:parameters isJson:YES resultClass:LoginModel.class] subscribeNext:^(id _Nullable x) {
[UICommon HidenLoading];
NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
[userDic setValue:self.emailField.text forKey:@"phone"];
[userDic setValue:self.passIField.text forKey:@"password"];
[userDic setValue:@"1" forKey:@"isRememberPWD"];
[userDic setValue:self.countryCodeLabel.text forKey:@"code"];
[userDic setValue:self.countryTitleLabel.text forKey:@"gj"];
YYCache *userCache = [YYCache cacheWithName:UserCacheName];
[userCache setObject:userDic forKey:UserCacheKey];
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[app showRootViewController];
} error:^(NSError * _Nullable error) {
NSDictionary *dic = error.userInfo;
[UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]];
}];
}
}
/// 注册/忘记密码
- (void)UserRegisterUp
{
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:self.emailField.text forKey:@"phone"];
[parameters setValue:self.passIField.text forKey:@"password"];
[parameters setValue:self.codeField.text forKey:@"code"];
[[[APIManager sharedManager] APPOST:self.isViewType == 0 ? Register_URL : UpdatePwd_URL parameters:parameters isJson:YES resultClass:nil] subscribeNext:^(id _Nullable x) {
if (self.isViewType == 0)
{
NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
[userDic setValue:self.emailField.text forKey:@"phone"];
[userDic setValue:@"" forKey:@"password"];
[userDic setValue:@"0" forKey:@"isRememberPWD"];
[userDic setValue:self.countryCodeLabel.text forKey:@"code"];
[userDic setValue:self.countryTitleLabel.text forKey:@"gj"];
YYCache *userCache = [YYCache cacheWithName:UserCacheName];
[userCache setObject:userDic forKey:UserCacheKey];
self.isRegisterSuccess();
}
[UICommon MessageSuccessText:self.isViewType == 0 ? GJText(@"注册成功,请登录账号") : GJText(@"密码重置成功,请登录账号")];
AfterDispatch(1, ^{
[[UICommon currentVC].navigationController popViewControllerAnimated:YES];
self.registerBtn.enabled = YES;
});
} error:^(NSError * _Nullable error) {
self.registerBtn.enabled = YES;
NSDictionary *dic = error.userInfo;
[UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]];
}];
}
#pragma mark 获取验证码
/// 获取验证码
- (void)GetEmailCode
{
if (!self.emailField.text || self.emailField.text.length != 11)
{
[UICommon MessageErrorText:@"请输入正确的手机号"];
return;
}
[UICommon MessageUpload:@"加载中"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:self.emailField.text forKey:@"phoneNum"];
[[[APIManager sharedManager] APGET:EmailCode_URL parameters:parameters resultClass:nil] subscribeNext:^(id _Nullable x) {
[UICommon MessageSuccessText:@"获取验证码成功"];
[self timeOutCount];
} error:^(NSError * _Nullable error) {
NSDictionary *dic = error.userInfo;
[UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]];
}];
// [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.emailField.text zone:@"86" template:@"" result:^(NSError *error) {
// [UICommon HidenLoading];
// if (!error)
// {
// [UICommon MessageSuccessText:@"获取验证码成功"];
// [self timeOutCount];
// }
// else
// {
// NSDictionary *dic = error.userInfo;
// [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]];
// }
// }];
}
/// 倒计时时间
- (void)timeOutCount
{
__block int timeout = 60;
kWeakSelf(self)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 * NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout <= 0)
{ //倒计时结束,关闭
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
weakself.codeBtn.selected = NO;
weakself.codeBtn.userInteractionEnabled = YES;
});
}
else
{
int seconds = timeout;
NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
timeout--;
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
weakself.codeBtn.selected = YES;
[weakself.codeBtn setTitle:[NSString stringWithFormat:@"%@ s",strTime] forState:UIControlStateSelected];
weakself.codeBtn.userInteractionEnabled = NO;
});
});
}
});
dispatch_resume(_timer);
}
#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
if ([URL.absoluteString containsString:AccoutXieYiURLStr])
{
commWebViewController *vc = [commWebViewController new];
vc.url = AccoutXieYiURLStr;
vc.navTitle = GJText(@"用户协议");
[self.navigationController pushViewController:vc animated:YES];
return NO;
}
else if ([URL.absoluteString containsString:PrivacyPolicyURLStr])
{
commWebViewController *vc = [commWebViewController new];
vc.url = AccoutXieYiURLStr;
vc.navTitle = GJText(@"隐私政策");
[self.navigationController pushViewController:vc animated:YES];
return NO;
}
return YES;
}
- (UIButton *)countryBtn
{
if (!_countryBtn)
{
_countryBtn = [UICommon ui_buttonSimple:CGRectMake(15, iPhoneX_NavHeight+Adapted(44), SCREEN_WIDTH-30, Adapted(50)) font:FontADA_(14) normalColor:KKTextBlackColor normalText:@"国家/地区" click:^(id x) {
// SelectCountryViewController *vc = [[SelectCountryViewController alloc] init];
// [self.navigationController pushViewController:vc animated:YES];
}];
[_countryBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[_countryBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
_countryBtn.layer.cornerRadius = 5;
_countryBtn.layer.masksToBounds = YES;
_countryBtn.backgroundColor = RGB(245, 245, 245);
UIImageView *rightImg = [UICommon ui_imageView:CGRectZero fileName:@"icon_enter_gray"];
[_countryBtn addSubview:rightImg];
[rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_countryBtn.mas_right).inset(10);
make.centerY.equalTo(_countryBtn);
make.size.mas_equalTo(rightImg.size);
}];
UILabel *countryTitleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(14) textColor:KKMainColor text:@"中国" Radius:0];
self.countryTitleLabel = countryTitleLabel;
[_countryBtn addSubview:countryTitleLabel];
[countryTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(_countryBtn);
make.right.equalTo(rightImg.mas_left).inset(10);
}];
}
return _countryBtn;
}
/*
#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