// // LoginViewController.m // xplan-ios // // Created by zu on 2021/8/31. // #import "LoginViewController.h" ///Third #import #import #import #import #import #import ///Tool #import "UIImage+Utils.h" #import "GCDHelper.h" #import "XPMacro.h" #import "ThemeColor.h" #import "XPConstant.h" #import "StatisticsServiceHelper.h" #import "TTPopup.h" ///Present #import "LoginPresenter.h" #import "LoginProtocol.h" ///VC #import "LoginPhoneViewController.h" #import "UserPrivacyView.h" UIKIT_EXTERN NSString * const kYinyouPrivateKey; typedef NS_ENUM(NSUInteger, XYLoginType) { XYLoginTypeUnknow = 0, //未知 XYLoginTypeTelecom = 1, //电信 XYLoginTypeChinaMobile = 2, //移动 XYLoginTypeUnicom = 3 //联通 }; @interface LoginViewController () @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) UIButton *qqButtonView; ///wx @property (nonatomic, strong) UIButton *wxButtonView; ///qq @property (nonatomic, strong) UIButton *phoneButtonView; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 @property (nonatomic, strong) ASAuthorizationAppleIDButton *appleButton; #endif /** 同意勾选按钮*/ @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)viewDidLoad { [super viewDidLoad]; [self initView]; [self setUpConstraints]; [self setEvents]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setConfigPrivacyAlertView]; } - (BOOL)isHiddenNavBar { return YES; } - (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]; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 [self.stackView addArrangedSubview:self.appleButton]; #endif if (![ShareSDK isClientInstalled:SSDKPlatformTypeQQ]) { self.qqButtonView.hidden = YES; } if (![ShareSDK isClientInstalled:SSDKPlatformTypeWechat]) { self.wxButtonView.hidden = YES; } } - (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(50); }]; [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); }]; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 [self.appleButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(50, 50)); }]; #endif } - (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]; UITapGestureRecognizer * appleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickRecognizer:)]; [self.appleButton addGestureRecognizer:appleTap]; [[self.agreeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) { @strongify(self) 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)setConfigPrivacyAlertView { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *isShow = [defaults objectForKey:kYinyouPrivateKey]; // 隐私协议弹出 if (!isShow && isShow.length <= 0) { UserPrivacyView * alertView = [[UserPrivacyView alloc] init]; alertView.controller = self; TTPopupService * config = [[TTPopupService alloc] init]; config.shouldDismissOnBackgroundTouch = NO; config.contentView = alertView; [TTPopup popupWithConfig:config]; }; } - (void)didClickRecognizer:(UITapGestureRecognizer *)tap { UIView * view = tap.view; if (self.agreeButton.isSelected) { if ([view isEqual:self.phoneButtonView]) { LoginPhoneViewController *inputPhoneVC = [[LoginPhoneViewController alloc] init]; [self.navigationController pushViewController:inputPhoneVC animated:YES]; [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventLoginPhoneClick]; } else{ ThirdLoginType type = view.tag - 1000; [self.presenter thirdLoginWithType:type]; ///埋点 switch (type) { case ThirdLoginType_QQ: [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventLoginQQClick]; break; case ThirdLoginType_WeChat: [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventLoginWXClick]; break; case ThirdLoginType_Apple: break; default: break; } } } else { [UIView animateWithDuration:0.5 animations:^{ self.authBubbleView.alpha = 1.0; }]; } } - (void)phoneQuickLogin { [self showLoading]; // 在使用一键登录之前,请先调用shouldQuickLogin方法,判断当前上网卡的网络环境和运营商是否可以一键登录 @weakify(self) NTESQuickLoginManager *qlManager = [NTESQuickLoginManager sharedInstance]; BOOL shouldQL = [qlManager shouldQuickLogin]; if (!shouldQL) { [self phoneQuickLoginFail]; return; } [qlManager registerWithBusinessID:KeyWithType(KeyType_NTESQuickLoginBusinessId) 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"]) { [self hideHUD]; } else { [self phoneQuickLoginFail]; } }); return; } dispatch_main_sync_safe(^{ @strongify(self) [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventOneClickLoginSuccess]; // // 取号成功,获取acessToken [self hideHUD]; [self dismissViewControllerAnimated:YES completion:nil]; [self.presenter phoneQuickLogin:resultDic[@"accessToken"] token:token]; }); }]; }]; }]; } - (void)phoneQuickLoginFail { [self showErrorToast:@"一键登录失败,请检查手机网络状态。"]; LoginPhoneViewController *inputPhoneVC = [[LoginPhoneViewController alloc] init]; [self.navigationController pushViewController:inputPhoneVC animated:YES]; } - (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 = [ThemeColor appBackgroundColor]; 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 = [ThemeColor appBackgroundColor]; CMModel.logBtnHeight = 45; CMModel.logBtnRadius = 45.f / 2; CMModel.logBtnOffsetTopY= 260; CMModel.logBtnOriginLeft = 52; CMModel.logBtnOriginRight = 52; CMModel.logBtnUsableBGColor = [ThemeColor appMainColor]; 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 = [ThemeColor mainTextColor]; CMModel.appPrivacyText = @"登录即代表同意《默认》,并授权音游获取本机号码。"; CMModel.privacyState = YES; CMModel.privacyFont = [UIFont systemFontOfSize:12]; CMModel.protocolColor = [ThemeColor appMainColor]; CMModel.backActionBlock = ^{ //点击了返回按钮 [self hideHUD]; }; dispatch_main_sync_safe(^{ [[NTESQuickLoginManager sharedInstance] setupModel:CMModel]; }); } #pragma mark - LoginProtocol - (void)loginSuccess { [self dismissViewControllerAnimated:YES completion:nil]; [self showSuccessToast:@"登录成功"]; } #pragma mark - Getters And Setters - (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 = [ThemeColor mainTextColor]; } return _titleLabel; } - (UILabel *)subTitleLabel { if (!_subTitleLabel) { _subTitleLabel = [[UILabel alloc] init]; _subTitleLabel.font = [UIFont systemFontOfSize:13]; _subTitleLabel.text = @"来音游打游戏,争夺更多赏金!"; _subTitleLabel.textColor = [ThemeColor secondTextColor]; _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.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18]; [_loginButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal]; UIImage *image = [UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor],[ThemeColor confirmButtonGradientEndColor]] 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 = [ThemeColor mainTextColor]; NSString *userString = @"《用户服务协议》"; NSMutableAttributedString *userAttString = [[NSMutableAttributedString alloc] initWithString:userString attributes:@{NSForegroundColorAttributeName:[ThemeColor appMainColor]}]; // @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 = [ThemeColor mainTextColor]; NSString *protocolString = @"《隐私政策》"; NSMutableAttributedString *privateString = [[NSMutableAttributedString alloc] initWithString:protocolString attributes:@{NSForegroundColorAttributeName:[ThemeColor appMainColor]}]; [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; } - (UIButton *)phoneButtonView { if (!_phoneButtonView) { _phoneButtonView = [UIButton buttonWithType:UIButtonTypeCustom]; [_phoneButtonView setImage: [UIImage imageNamed:@"login_phone"] forState:UIControlStateNormal]; } return _phoneButtonView; } - (UIButton *)qqButtonView { if (!_qqButtonView) { _qqButtonView = [UIButton buttonWithType:UIButtonTypeCustom]; [_qqButtonView setImage: [UIImage imageNamed:@"login_qq"] forState:UIControlStateNormal]; _qqButtonView.tag = 1000 + ThirdLoginType_QQ; } return _qqButtonView; } - (UIButton *)wxButtonView { if (!_wxButtonView) { _wxButtonView = [UIButton buttonWithType:UIButtonTypeCustom]; [_wxButtonView setImage: [UIImage imageNamed:@"login_wechat"] forState:UIControlStateNormal]; _wxButtonView.tag = 1000 + ThirdLoginType_WeChat; } return _wxButtonView; } #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 - (ASAuthorizationAppleIDButton *)appleButton API_AVAILABLE(ios(13.0)){ if (!_appleButton) { if (@available(iOS 13.0, *)) { _appleButton = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeSignIn authorizationButtonStyle:ASAuthorizationAppleIDButtonStyleBlack]; } else { // Fallback on earlier versions } _appleButton.cornerRadius = 25; _appleButton.tag = 1000 + ThirdLoginType_Apple; } return _appleButton; } #endif @end