Files
yinmeng-ios/xplan-ios/Main/Login/View/LoginViewController.m
2021-09-06 18:51:44 +08:00

579 lines
21 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// LoginViewController.m
// xplan-ios
//
// Created by zu on 2021/8/31.
//
#import "LoginViewController.h"
#import "LoginPresenter.h"
#import "LoginProtocol.h"
#import "UIImage+Utils.h"
#import "GCDHelper.h"
#import "XCHUDTool.h"
#import <Masonry/Masonry.h>
#import <ReactiveObjC.h>
#import <YYText.h>
#import <NTESQuickPass/NTESQuickPass.h>
// 易盾 本机一键登录
#ifdef DEBUG
NSString *businessID = @"09c1214706c34f4798d3f05d86148608";
#else
NSString *businessID = @"09c1214706c34f4798d3f05d86148608";
#endif
typedef NS_ENUM(NSUInteger, XYLoginType) {
XYLoginTypeUnknow = 0, //未知
XYLoginTypeTelecom = 1, //电信
XYLoginTypeChinaMobile = 2, //移动
XYLoginTypeUnicom = 3 //联通
};
typedef NS_ENUM(NSInteger,XYThirdLoginType) {
XYThirdLoginWechat = 1, // 微信登录
XYThirdLoginQQ = 2, // QQ登录
XYThirdLoginPhoneNum = 3, // 手机号登录
XYThirdLoginApple = 5, // 苹果id登录
};
@interface LLButtonView : UIView
//icon
@property (nonatomic,strong) UIImageView *logoImageView;
//title
@property (nonatomic,strong) UILabel *titleLabel;
@end
@implementation LLButtonView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initView];
[self initContrations];
}
return self;
}
- (void)initView {
[self addSubview:self.logoImageView];
[self addSubview:self.titleLabel];
}
- (void)initContrations {
[self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(36, 36));
make.centerX.left.mas_equalTo(self);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self);
make.top.mas_equalTo(self.logoImageView.mas_bottom).offset(15);
}];
}
- (UIImageView *)logoImageView {
if (!_logoImageView) {
_logoImageView = [[UIImageView alloc] init];
_logoImageView.userInteractionEnabled = YES;
}
return _logoImageView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:11];
_titleLabel.textColor = ThemeTextColor;
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
@end
@interface LoginViewController () <LoginProtocol>
@property (nonatomic, strong) UIImageView *appIcon;
/** 标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 副标题 */
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UIView *contentView;
/** 登录按钮*/
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic,strong) UIStackView *stackView;
///手机
@property (nonatomic,strong) LLButtonView *qqButtonView;
///wx
@property (nonatomic,strong) LLButtonView *wxButtonView;
///qq
@property (nonatomic,strong) LLButtonView *phoneButtonView;
/** 同意勾选按钮*/
@property (nonatomic, strong) UIButton *agreeButton;
/** 同意即可登录 */
@property (nonatomic, strong) YYLabel *agreeLabel;
/** 勾选隐私政策提示泡泡 */
@property (nonatomic, strong) UIImageView *authBubbleView;
/** 泡泡提示内容 */
@property (nonatomic, strong) UILabel *authBubbleLabel;
@end
@implementation LoginViewController
- (LoginPresenter *)createPresenter {
return [[LoginPresenter alloc] init];
}
- (void)phoneQuickLoginSuccess {
[XCHUDTool showSuccessWithMessage:@"一键登录成功。"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
[self setUpConstraints];
[self setEvents];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideNavigationBar];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self showNavigationBar];
}
- (void)initView {
[self.view addSubview:self.appIcon];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.subTitleLabel];
[self.view addSubview:self.contentView];
[self.contentView addSubview:self.loginButton];
[self.contentView addSubview:self.stackView];
[self.view addSubview:self.agreeButton];
[self.view addSubview:self.agreeLabel];
[self.view addSubview:self.authBubbleView];
[self.authBubbleView addSubview:self.authBubbleLabel];
[self.stackView addArrangedSubview:self.qqButtonView];
[self.stackView addArrangedSubview:self.phoneButtonView];
[self.stackView addArrangedSubview:self.wxButtonView];
}
- (void)setUpConstraints {
[self.appIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(110 + kSafeAreaTopHeight);
make.left.mas_equalTo(52);
make.width.mas_equalTo(100);
make.height.mas_equalTo(100);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.appIcon.mas_top).offset(28.5);
make.left.mas_equalTo(self.appIcon.mas_right).offset(7.5);
make.height.mas_equalTo(20);
make.right.mas_equalTo(-20);
}];
[self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(13);
make.left.mas_equalTo(self.titleLabel.mas_left);
make.height.mas_equalTo(13);
make.right.mas_equalTo(-20);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset(iPhoneXSeries ? 300 : 228);
make.left.right.bottom.mas_equalTo(0);
}];
[self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(52);
make.top.mas_equalTo(70);
make.height.mas_equalTo(45);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view).offset(-kSafeAreaBottomHeight - 60);
make.centerX.mas_equalTo(self.view);
make.height.mas_equalTo(65);
}];
[self.agreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.loginButton.mas_bottom).offset(20);
make.height.mas_equalTo(40);
make.width.mas_equalTo(270);
make.centerX.mas_equalTo(self.view).mas_offset(15 - 6);
}];
[self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.agreeLabel.mas_left).mas_offset(6);
make.centerY.mas_equalTo(self.agreeLabel.mas_centerY);
make.width.height.mas_equalTo(30);
}];
[self.authBubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.agreeButton).mas_offset((-16));
make.top.mas_equalTo(self.agreeButton.mas_bottom);
make.width.mas_equalTo(235);
make.height.mas_equalTo(29);
}];
[self.authBubbleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(8);
make.bottom.mas_equalTo(self.authBubbleView).mas_offset(-6);
}];
}
- (void)setEvents {
// 手机登陆按钮点击
@weakify(self)
UITapGestureRecognizer *phoneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickRecognizer:)];
[self.phoneButtonView addGestureRecognizer:phoneTap];
UITapGestureRecognizer * qqTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickRecognizer:)];
[self.qqButtonView addGestureRecognizer:qqTap];
UITapGestureRecognizer * wxTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickRecognizer:)];
[self.wxButtonView addGestureRecognizer:wxTap];
[[self.agreeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
self.agreeButton.selected = !self.agreeButton.selected;
if (self.agreeButton.isSelected) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"hadAgree" forKey:@"kHadAgreePrivacy"];
[defaults synchronize];
[UIView animateWithDuration:0.5 animations:^{
self.authBubbleView.alpha = 0.0;
}];
}
}];
// 一键登录
[[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
@strongify(self)
if (self.agreeButton.isSelected) {
[self phoneQuickLogin];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.authBubbleView.alpha = 1.0;
}];
}
}];
}
- (void)didClickRecognizer:(UITapGestureRecognizer *)tap {
UIView * view = tap.view;
if (self.agreeButton.isSelected) {
if ([view isEqual:self.phoneButtonView]) {
// InputPhoneLoginViewController *inputPhoneVC = [[InputPhoneLoginViewController alloc] init];
// [self.navigationController pushViewController:inputPhoneVC animated:YES];
} else if([view isEqual:self.qqButtonView]) {
[XCHUDTool showLoading];
// [GetCore(AuthCore) thirdLoginPlatform:SSDKPlatformTypeQQ];
} else if([view isEqual:self.wxButtonView]) {
[XCHUDTool showLoading];
// [GetCore(AuthCore) thirdLoginPlatform:SSDKPlatformTypeWechat];
}
} else {
[UIView animateWithDuration:0.5 animations:^{
self.authBubbleView.alpha = 1.0;
}];
}
}
- (void)phoneQuickLogin {
[XCHUDTool showLoading];
// 在使用一键登录之前请先调用shouldQuickLogin方法判断当前上网卡的网络环境和运营商是否可以一键登录
@weakify(self)
NTESQuickLoginManager *qlManager = [NTESQuickLoginManager sharedInstance];
BOOL shouldQL = [qlManager shouldQuickLogin];
if (!shouldQL) {
[self phoneQuickLoginFail];
return;
}
[qlManager registerWithBusinessID:businessID timeout:3*1000 configURL:nil extData:nil completion:^(NSDictionary * _Nullable params, BOOL regSuccess) {
@strongify(self)
if (!regSuccess) {
dispatch_main_sync_safe(^{
[self phoneQuickLoginFail];
});
return;
}
NSString *token = [params objectForKey:@"token"];
[qlManager getPhoneNumberCompletion:^(NSDictionary * _Nonnull resultDic) {
@strongify(self)
NSNumber *boolNum = [resultDic objectForKey:@"success"];
BOOL getPhoneNumberSuccess = [boolNum boolValue];
if (!getPhoneNumberSuccess) {
dispatch_main_sync_safe(^{
[self phoneQuickLoginFail];
});
return;
}
[self configQuickLogin];
[qlManager CUCMCTAuthorizeLoginCompletion:^(NSDictionary * _Nonnull resultDic) {
@strongify(self)
NSNumber *boolNum = [resultDic objectForKey:@"success"];
BOOL authSuccess = [boolNum boolValue];
if (!authSuccess) {
dispatch_main_sync_safe(^{
NSString *resultCode = [resultDic objectForKey:@"resultCode"];
//取消一键登录
if ([resultCode isEqualToString:@"200020"] ||
[resultCode isEqualToString:@"10104"]) {
[XCHUDTool hideHUD];
} else {
[self phoneQuickLoginFail];
}
});
return;
}
dispatch_main_sync_safe(^{
@strongify(self)
// [TTStatisticsService trackEvent:@"one_click_login_succeed" eventDescribe:@"一键登录成功"];
// // 取号成功获取acessToken
[XCHUDTool hideHUD];
[self dismissViewControllerAnimated:YES completion:nil];
[self.presenter phoneQuickLogin:resultDic[@"accessToken"] token:token];
});
}];
}];
}];
}
- (void)phoneQuickLoginFail {
[XCHUDTool showErrorWithMessage:@"一键登录失败,请检查手机网络状态。"];
}
- (void)configQuickLogin {
// 获取当前上网卡的运营商0:未知 1:电信 2.移动 3.联通
NSInteger currentCarrier = [[NTESQuickLoginManager sharedInstance] getCarrier];
NTESQuickLoginModel *CMModel = [[NTESQuickLoginModel alloc] init];
CMModel.currentVC = self;
CMModel.presentDirectionType = NTESPresentDirectionPresent;
if (currentCarrier == XYLoginTypeUnicom) { // 联通
CMModel.logoImg = [UIImage imageNamed:@"login_unicom_mobile"];
}else if (currentCarrier == XYLoginTypeChinaMobile) { //移动
CMModel.logoImg = [UIImage imageNamed:@"login_china_mobile"];
}else { //电信
CMModel.logoImg = [UIImage imageNamed:@"login_ct_mobile"];
}
CMModel.backgroundColor = ThemeBackgroundColor;
CMModel.logoWidth = 95;
CMModel.logoHeight = 95;
CMModel.logoOffsetTopY = 30;
CMModel.navText = @"一键登录";
CMModel.navTextColor = UIColor.whiteColor;
CMModel.navTextFont = [UIFont boldSystemFontOfSize:18];
CMModel.navReturnImg = [UIImage imageNamed:@"common_nav_back"];
CMModel.navBgColor = ThemeBackgroundColor;
CMModel.logBtnHeight = 45;
CMModel.logBtnRadius = 45.f / 2;
CMModel.logBtnOffsetTopY= 260;
CMModel.logBtnOriginLeft = 52;
CMModel.logBtnOriginRight = 52;
CMModel.logBtnUsableBGColor = ThemeDefaultColor;
CMModel.logBtnText = @"本机号码一键登录";
CMModel.logBtnTextColor = UIColor.whiteColor;
CMModel.logBtnTextFont = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
CMModel.numberOffsetTopY = 170;
CMModel.numberColor = UIColor.whiteColor;
CMModel.numberFont = [UIFont boldSystemFontOfSize:20];
CMModel.brandColor = UIColor.whiteColor;
CMModel.uncheckedImg = [UIImage imageNamed:@"common_checkbox_uncheck"];
CMModel.checkedImg = [UIImage imageNamed:@"common_checkbox_checked"];
CMModel.checkboxWH = 20;
CMModel.privacyColor = ThemeTextColor;
CMModel.appPrivacyText = @"登录即代表同意《默认》,并授权音游获取本机号码。";
CMModel.privacyState = YES;
CMModel.privacyFont = [UIFont systemFontOfSize:12];
CMModel.protocolColor = ThemeDefaultColor;
CMModel.backActionBlock = ^{ //点击了返回按钮
[XCHUDTool hideHUD];
};
dispatch_main_sync_safe(^{
[[NTESQuickLoginManager sharedInstance] setupModel:CMModel];
});
}
- (UIImageView *)appIcon {
if (!_appIcon) {
_appIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_appIcon"]];
}
return _appIcon;
}
- (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
_titleLabel.text = AppName;
_titleLabel.textColor = ThemeTextColor;
}
return _titleLabel;
}
- (UILabel *)subTitleLabel {
if (!_subTitleLabel) {
_subTitleLabel = [[UILabel alloc] init];
_subTitleLabel.font = [UIFont systemFontOfSize:13];
_subTitleLabel.text = @"来音游打游戏,争夺更多赏金!";
_subTitleLabel.textColor = ThemeTextColor;
_subTitleLabel.textAlignment = NSTextAlignmentLeft;
}
return _subTitleLabel;
}
- (UIButton *)loginButton{
if (!_loginButton) {
_loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
_loginButton.layer.masksToBounds = YES;
_loginButton.layer.cornerRadius = 45/2.f;
[_loginButton setTitle:@"本机号码一键登录" forState:UIControlStateNormal];
[_loginButton setTitle:@"本机号码一键登录" forState:UIControlStateSelected];
_loginButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
[_loginButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
UIImage *image = [UIImage gradientColorImageFromColors:@[ThemeButtonGradientStartColor,ThemeButtonGradientEndColor] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth - 52 * 2, 45)];
[_loginButton setBackgroundImage:image forState:UIControlStateNormal];
}
return _loginButton;
}
- (UIButton *)agreeButton {
if (!_agreeButton) {
_agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_agreeButton setImage:[UIImage imageNamed:@"common_checkbox_uncheck"] forState:UIControlStateNormal];
[_agreeButton setImage:[UIImage imageNamed:@"common_checkbox_checked"] forState:UIControlStateSelected];
}
return _agreeButton;
}
- (YYLabel *)agreeLabel {
if (!_agreeLabel) {
_agreeLabel = [[YYLabel alloc] init];
_agreeLabel.font = [UIFont systemFontOfSize:12];
_agreeLabel.numberOfLines = 0;
_agreeLabel.attributedText = [self privacyAttributedStr];
_agreeLabel.textAlignment = NSTextAlignmentCenter;
[_agreeLabel sizeToFit];
}
return _agreeLabel;
}
- (NSMutableAttributedString *)privacyAttributedStr{
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"登录即代表同意"];
attString.yy_color = ThemeTextColor;
NSString *userString = @"《用户服务协议》";
NSMutableAttributedString *userAttString = [[NSMutableAttributedString alloc] initWithString:userString attributes:@{NSForegroundColorAttributeName:ThemeDefaultColor}];
@weakify(self)
[userAttString yy_setTextHighlightRange:NSMakeRange(0, userAttString.length) color:nil backgroundColor:nil userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@strongify(self);
// 跳转用户协议
// [self goToWebview:HtmlUrlKey(kUserProtocalURL)];
} longPressAction:nil];
NSMutableAttributedString *andString = [[NSMutableAttributedString alloc] initWithString:@""];
andString.yy_color = ThemeTextColor;
NSString *protocolString = @"《隐私政策》";
NSMutableAttributedString *privateString = [[NSMutableAttributedString alloc] initWithString:protocolString attributes:@{NSForegroundColorAttributeName:ThemeDefaultColor}];
[privateString yy_setTextHighlightRange:NSMakeRange(0, privateString.length) color:nil backgroundColor:nil userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@strongify(self);
// 跳转隐私政策
// [self goToWebview:HtmlUrlKey(kPrivacyURL)];
} longPressAction:nil];
[attString appendAttributedString:userAttString];
[attString appendAttributedString:andString];
[attString appendAttributedString:privateString];
return attString;
}
- (UIImageView *)authBubbleView {
if (!_authBubbleView) {
_authBubbleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_auth_bubble"]];
_authBubbleView.contentMode = UIViewContentModeScaleAspectFit;
_authBubbleView.alpha = 0;
}
return _authBubbleView;
}
- (UILabel *)authBubbleLabel {
if (!_authBubbleLabel) {
_authBubbleLabel = [[UILabel alloc] init];
_authBubbleLabel.text = @"同意隐私政策和用户协议后,才可以注册登录哦~";
_authBubbleLabel.font = [UIFont systemFontOfSize:10];
_authBubbleLabel.textColor = UIColor.whiteColor;
}
return _authBubbleLabel;
}
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
}
return _contentView;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.distribution = UIStackViewDistributionEqualSpacing;
_stackView.spacing = 43;
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.alignment = UIStackViewAlignmentFill;
}
return _stackView;
}
- (LLButtonView *)phoneButtonView {
if (!_phoneButtonView) {
_phoneButtonView = [[LLButtonView alloc] init];
_phoneButtonView.logoImageView.image = [UIImage imageNamed:@"login_phone"];
_phoneButtonView.titleLabel.text = @"手机";
}
return _phoneButtonView;
}
- (LLButtonView *)qqButtonView {
if (!_qqButtonView) {
_qqButtonView = [[LLButtonView alloc] init];
_qqButtonView.logoImageView.image = [UIImage imageNamed:@"login_qq"];
_qqButtonView.titleLabel.text = @"QQ";
}
return _qqButtonView;
}
- (LLButtonView *)wxButtonView {
if (!_wxButtonView) {
_wxButtonView = [[LLButtonView alloc] init];
_wxButtonView.logoImageView.image = [UIImage imageNamed:@"login_wechat"];
_wxButtonView.titleLabel.text = @"微信";
}
return _wxButtonView;
}
@end