// // LoginViewController.m // xplan-ios // // Created by zu on 2021/8/31. // #import "LoginViewController.h" ///Third #import #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" #import "XPHtmlUrl.h" ///Present #import "LoginPresenter.h" #import "LoginProtocol.h" ///VC #import "LoginPasswordViewController.h" #import "UserPrivacyView.h" #import "XPWebViewController.h" UIKIT_EXTERN NSString * const kYinyouPrivateKey; NSString * const kPhoneQuickPrivacyKey = @"PhoneQuickPrivacyKey"; NSString * const kHadAgreePrivacy = @"HadAgreePrivacy"; typedef NS_ENUM(NSUInteger, XYLoginType) { XYLoginTypeUnknow = 0, //未知 XYLoginTypeTelecom = 1, //电信 XYLoginTypeChinaMobile = 2, //移动 XYLoginTypeUnicom = 3 //联通 }; @interface LoginViewController () @property (nonatomic,strong) SVGAPlayer *logoImageView; @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]; [self loginAnimation]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setConfigPrivacyAlertView]; } - (BOOL)isHiddenNavBar { return YES; } - (void)initView { self.view.backgroundColor = UIColor.whiteColor; [self.view addSubview:self.logoImageView]; [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 { CGFloat kscale = 363.0 / 375.0; [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(self.view); make.height.mas_equalTo(KScreenWidth * kscale); }]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.view).offset(329); 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(220); 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.width.mas_equalTo(50); // }]; //#endif } - (void)loginAnimation { SVGAParser *parser = [[SVGAParser alloc] init]; @weakify(self); [parser parseWithNamed:@"login_logo" inBundle:nil completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) { @strongify(self); if (videoItem != nil) { self.logoImageView.videoItem = videoItem; [self.logoImageView startAnimation]; } } failureBlock:nil]; } - (void)setEvents { // 手机登陆按钮点击 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]; } - (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]) { LoginPasswordViewController *inputPhoneVC = [[LoginPasswordViewController 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) configURL:nil extData:nil]; [qlManager getPhoneNumberCompletion:^(NSDictionary * _Nonnull resultDic) { @strongify(self) NSNumber *boolNum = [resultDic objectForKey:@"success"]; NSString * token = [resultDic objectForKey:@"token"]; BOOL getPhoneNumberSuccess = [boolNum boolValue]; if (!getPhoneNumberSuccess || token.length <= 0) { 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:@"一键登录失败,请检查手机网络状态。"]; LoginPasswordViewController *inputPhoneVC = [[LoginPasswordViewController 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 = [ThemeColor mainTextColor]; 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 = [ThemeColor mainTextColor]; CMModel.logBtnTextFont = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]; CMModel.numberOffsetTopY = 170; CMModel.numberColor = [ThemeColor mainTextColor]; CMModel.numberFont = [UIFont boldSystemFontOfSize:20]; CMModel.brandColor = [ThemeColor mainTextColor]; CMModel.uncheckedImg = [UIImage imageNamed:@"common_checkbox_uncheck"]; CMModel.checkedImg = [UIImage imageNamed:@"common_checkbox_checked"]; CMModel.checkboxWH = 20; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString * hadAgree = [defaults stringForKey:kPhoneQuickPrivacyKey]; if (hadAgree.length > 0) { CMModel.privacyState = YES; } CMModel.privacyColor = [ThemeColor secondTextColor]; CMModel.appPrivacyText = [NSString stringWithFormat:@"同意《默认》,并授权%@获取本机号码。",AppName]; CMModel.privacyFont = [UIFont systemFontOfSize:12]; CMModel.protocolColor = [ThemeColor mainTextColor]; CMModel.appPrivacyOriginBottomMargin = 30; CMModel.backActionBlock = ^{ //点击了返回按钮 [self hideHUD]; }; CMModel.checkActionBlock = ^(BOOL isChecked) { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (isChecked) { [defaults setObject:@"phonequickhadAgree" forKey:kPhoneQuickPrivacyKey]; [defaults synchronize]; } else { NSString * hadAgree = [defaults stringForKey:kPhoneQuickPrivacyKey]; if (hadAgree.length > 0) { [defaults removeObjectForKey:kPhoneQuickPrivacyKey]; } } }; dispatch_main_sync_safe(^{ [[NTESQuickLoginManager sharedInstance] setupModel:CMModel]; }); } #pragma mark - LoginProtocol - (void)loginSuccess { [self dismissViewControllerAnimated:YES completion:nil]; [self showSuccessToast:@"登录成功"]; } #pragma mark - Event Response - (void)agreeButtonAction:(UIButton *)sender { 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; }]; } else { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString * hadAgree = [defaults stringForKey:kHadAgreePrivacy]; if (hadAgree.length > 0) { [defaults removeObjectForKey:kHadAgreePrivacy]; } } } - (void)loginButtonAction:(UIButton *)sender { if (self.agreeButton.isSelected) { [self phoneQuickLogin]; } else { [UIView animateWithDuration:0.5 animations:^{ self.authBubbleView.alpha = 1.0; }]; } } #pragma mark - Getters And Setters - (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:ThemeColor.mainTextColor forState:UIControlStateNormal]; UIImage *image = [UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor],[ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth - 52 * 2, 45)]; [_loginButton setBackgroundImage:image forState:UIControlStateNormal]; [_loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } 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]; [_agreeButton addTarget:self action:@selector(agreeButtonAction:) forControlEvents:UIControlEventTouchUpInside]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *hadAgree = [defaults objectForKey:kHadAgreePrivacy]; if (hadAgree.length > 0) { _agreeButton.selected = YES; } } 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 secondTextColor]; NSString *userString = @"《用户服务协议》"; NSMutableAttributedString *userAttString = [[NSMutableAttributedString alloc] initWithString:userString attributes:@{NSForegroundColorAttributeName:[ThemeColor mainTextColor]}]; @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); // 跳转用户协议 XPWebViewController * webVC = [[XPWebViewController alloc] init]; webVC.url = URLWithType(kUserProtocalURL); [self.navigationController pushViewController:webVC animated:YES]; } longPressAction:nil]; NSMutableAttributedString *andString = [[NSMutableAttributedString alloc] initWithString:@"和"]; andString.yy_color = [ThemeColor secondTextColor]; NSString *protocolString = @"《隐私政策》"; NSMutableAttributedString *privateString = [[NSMutableAttributedString alloc] initWithString:protocolString attributes:@{NSForegroundColorAttributeName:[ThemeColor mainTextColor]}]; [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); // 跳转隐私政策 XPWebViewController * webVC = [[XPWebViewController alloc] init]; webVC.url = URLWithType(kPrivacyURL); [self.navigationController pushViewController:webVC animated:YES]; } 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; _qqButtonView.hidden = YES; } return _qqButtonView; } - (UIButton *)wxButtonView { if (!_wxButtonView) { _wxButtonView = [UIButton buttonWithType:UIButtonTypeCustom]; [_wxButtonView setImage: [UIImage imageNamed:@"login_wechat"] forState:UIControlStateNormal]; _wxButtonView.tag = 1000 + ThirdLoginType_WeChat; _wxButtonView.hidden = YES; } 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:ASAuthorizationAppleIDButtonStyleWhite]; } else { // Fallback on earlier versions } _appleButton.cornerRadius = 25; _appleButton.tag = 1000 + ThirdLoginType_Apple; _appleButton.hidden = YES; } return _appleButton; } #endif - (SVGAPlayer *)logoImageView { if (!_logoImageView) { _logoImageView = [[SVGAPlayer alloc] init]; _logoImageView.loops = 1; _logoImageView.clearsAfterStop = NO; } return _logoImageView; } @end