// // AccountBindingViewController.m // YuMi // // Created by P on 2025/3/14. // #import "AccountBindingViewController.h" #import "LoginProtocol.h" #import "LoginPresenter.h" #import "LoginInputItemView.h" #import "XPLoginAraeViewController.h" #import "XPLoginVerifBindPhonePresenter.h" #import "UserInfoModel.h" #import "XPMineSettingViewController.h" #import "XPWebViewController.h" @interface AccountBindingViewController () @property(nonatomic, assign) BindingDisplayType type; @property(nonatomic, copy) NSString *currentAccount; @property(nonatomic, copy) NSString *currentAreaCode; @property(nonatomic, strong) UILabel *currentAccountLabel; @property(nonatomic, strong) UIStackView *stackView; @property(nonatomic, strong) LoginInputItemView *firstLineInputView; @property(nonatomic, strong) LoginInputItemView *secondLineInputView; @property(nonatomic, strong) UIButton *bottomActionButton; @property(nonatomic, strong) UserInfoModel *userInfo; @property(nonatomic, strong) XPWebViewController *webVC; @end @implementation AccountBindingViewController - (LoginPresenter *)createPresenter { return [[LoginPresenter alloc] init]; } - (instancetype)initWithType:(BindingDisplayType)type currentBindingAccount:(NSString *)account areaCode:(NSString *)areaCode userInfo:(UserInfoModel *)userInfo { if (self = [super init]) { self.type = type; self.currentAccount = account; self.currentAreaCode = areaCode; self.userInfo = userInfo; } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } - (void)setupUI { [self setupDefaultUI]; if ([NSString isEmpty:self.currentAccount]) { [self setupNewBindingUI]; } else { [self setupUpdateBindingUI]; } } - (void)setupDefaultUI { self.view.backgroundColor = [UIColor whiteColor]; self.title = [self displayTitle]; } - (void)setupNewBindingUI { [self displayInputArea:25 topView:self.view isBottom:NO]; } - (void)setupUpdateBindingUI { UIImageView *topImageView = [self topImageView]; [self.view addSubview:topImageView]; [topImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.view).offset(16); make.centerX.mas_equalTo(self.view); make.size.mas_equalTo(CGSizeMake(170, 170)); }]; UILabel *descLabel = [self descriptionLabel]; [self.view addSubview:descLabel]; [descLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(topImageView.mas_bottom).offset(14); make.leading.trailing.mas_equalTo(self.view).inset(40); }]; [self.view addSubview:self.currentAccountLabel]; [self.currentAccountLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(descLabel.mas_bottom).offset(9); make.centerX.mas_equalTo(self.view); }]; self.currentAccountLabel.text = self.currentAccount; [self displayInputArea:25 topView:self.currentAccountLabel isBottom:YES]; } #pragma mark - - (NSString *)displayTitle { if ([NSString isEmpty:self.currentAccount]) { if (self.type == BindingDisplayType_email) { return YMLocalizedString(@"20.20.51_text_13"); } else { return YMLocalizedString(@"20.20.51_text_14"); } } else { return YMLocalizedString(@"20.20.51_text_15"); } } - (void)displayInputArea:(NSInteger)topSpace topView:(UIView *)topView isBottom:(BOOL)isBottom { [self.view addSubview:self.stackView]; [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) { if (isBottom) { make.top.mas_equalTo(topView.mas_bottom).offset(topSpace); } else { make.top.mas_equalTo(topView).offset(topSpace); } make.leading.trailing.mas_equalTo(self.view).inset(40); }]; @kWeakify(self); if ([NSString isEmpty:self.currentAccount]) { if (self.type == BindingDisplayType_email) { self.firstLineInputView = [[LoginInputItemView alloc] initWithType:LoginInputType_email]; } else { self.firstLineInputView = [[LoginInputItemView alloc] initWithType:LoginInputType_phoneNum]; } [self.firstLineInputView updateBGColor:UIColorFromRGB(0xf2f3f7)]; [self.firstLineInputView setHandleItemAction:^(LoginInputType inputType) { @kStrongify(self); if (inputType == LoginInputType_phoneNum) { [self handleTapAreaCode]; } }]; [self.firstLineInputView setHandleFirstInputContentUpdate:^(NSString *inputContent) { @kStrongify(self); [self checkActionButtonStatus]; }]; [self.stackView addArrangedSubview:self.firstLineInputView]; } self.secondLineInputView = [[LoginInputItemView alloc] initWithType:LoginInputType_verificationCode]; [self.secondLineInputView updateBGColor:UIColorFromRGB(0xf2f3f7)]; [self.secondLineInputView setHandleItemAction:^(LoginInputType inputType) { @kStrongify(self); if (inputType == LoginInputType_verificationCode) { if (self.type == BindingDisplayType_phoneNum) { [self handleTapGetPhoneVerificationCode]; } else if (self.type == BindingDisplayType_email) { [self handleTapGetMailVerificationCode]; } } }]; [self.secondLineInputView setHandleSecondInputContentUpdate:^(NSString *inputContent) { @kStrongify(self); [self checkActionButtonStatus]; }]; [self.stackView addArrangedSubview:self.secondLineInputView]; [self.stackView addArrangedSubview:self.bottomActionButton]; [self.firstLineInputView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(46); }]; [self.secondLineInputView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(46); }]; [self.bottomActionButton mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(46); }]; } #pragma mark - - (void)handleTapGetPhoneVerificationCode { NSString *phone = @""; NSString *areaCode = @""; if ([NSString isEmpty:self.currentAccount]) { phone = [self.firstLineInputView inputContent]; areaCode = [self.firstLineInputView loadAreaCode]; } else { phone = self.currentAccount; areaCode = self.currentAreaCode; } if (phone.length == 0 ) { [self showErrorToast:YMLocalizedString(@"XPLoginPhoneViewController0")]; [self.secondLineInputView endVerificationCountDown]; return; } @kWeakify(self); [self loadCaptchaWebView:^{ @kStrongify(self); [self.presenter phoneSmsCode:[NSString stringWithFormat:@"%@%@", areaCode,phone] type:[NSString isEmpty:self.currentAccount] ? GetSmsType_Regist : GetSmsType_Unbind_Phone phoneAreaCode:areaCode]; }]; } - (void)handleTapGetMailVerificationCode { NSString *email = @""; if (![NSString isEmpty:self.currentAccount]) { email = self.currentAccount; } else { email = [self.firstLineInputView inputContent]; } if (email.length == 0 ) { // [self showErrorToast:YMLocalizedString(@"20.20.51_text_12")]; [self.secondLineInputView endVerificationCountDown]; return; } [self.presenter sendMailVerificationCode:email type:[NSString isEmpty:self.currentAccount] ? GetSmsType_Regist : GetSmsType_Unbind_Phone]; } #pragma mark - - (void)didTapActionButton { [self.view endEditing:true]; if (self.type == BindingDisplayType_email) { if ([NSString isEmpty:self.currentAccount]) { [self.presenter bindingNewEmail:[self.firstLineInputView inputContent] code:[self.secondLineInputView inputContent]]; } else { [self.presenter checkEmailCode:self.currentAccount code:[self.secondLineInputView inputContent]]; } } else { if ([NSString isEmpty:self.currentAccount]) { [self.presenter bindingNewPhone:[self.firstLineInputView inputContent] code:[self.secondLineInputView inputContent] areaCode:[self.firstLineInputView loadAreaCode]]; } else { [self.presenter checkPhoneCode:self.currentAccount code:[self.secondLineInputView inputContent] areaCode:self.userInfo.phoneAreaCode]; } } } - (void)handleTapAreaCode { XPLoginAraeViewController *codeVC = [XPLoginAraeViewController new]; codeVC.delegate = self; [self.navigationController pushViewController:codeVC animated:YES]; } - (void)checkActionButtonStatus { NSString *accountString = [self.firstLineInputView inputContent]; NSString *codeString = [self.secondLineInputView inputContent]; if ([NSString isEmpty:self.currentAccount]) { if (![NSString isEmpty:accountString] && ![NSString isEmpty:codeString]) { self.bottomActionButton.enabled = YES; } else { self.bottomActionButton.enabled = NO; } } else { if (![NSString isEmpty:codeString]) { self.bottomActionButton.enabled = YES; } else { self.bottomActionButton.enabled = NO; } } } - (void)loadCaptchaWebView:(void(^)(void))finishBlock { if ([ClientConfig.shareConfig shouldDisplayCaptcha]) { [self.view endEditing:YES]; XPWebViewController * webVC =[[XPWebViewController alloc] initWithRoomUID:nil]; webVC.view.frame = CGRectMake(0, 0, KScreenWidth*0.8, KScreenWidth*1.2); webVC.view.backgroundColor = [UIColor clearColor]; [webVC.view setCornerRadius:12]; webVC.isLoginStatus = NO; webVC.isPush = NO; [webVC hideNavigationBar]; webVC.url = URLWithType(kCaptchaSwitchPath); [webVC setVerifyCaptcha:^(BOOL result) { if (result == NO) { return; } if (result && finishBlock) { [TTPopup dismiss]; finishBlock(); } }]; [TTPopup popupView:webVC.view style:TTPopupStyleAlert]; self.webVC = webVC; } } #pragma mark - API presenter Delegate - (void)phoneSmsCodeSuccess:(NSString *)message type:(GetSmsType)type{ if (type == GetSmsType_Unbind_Phone) { XPLoginVerifBindPhonePresenter *presenter = [[XPLoginVerifBindPhonePresenter alloc] init]; [presenter checkMoblieCodeWithMoblie:self.currentAccount code:[self.secondLineInputView inputContent] phoneAreaCode:self.userInfo.phoneAreaCode]; } else { [self showSuccessToast:YMLocalizedString(@"XPLoginPhoneViewController2")]; [self.secondLineInputView startVerificationCountDown]; [self.secondLineInputView displayKeyboard]; } } - (void)phoneSmsCodeFailure { [self.secondLineInputView endVerificationCountDown]; } - (void)emailCodeSucess:(NSString *)message type:(GetSmsType)type{ [self showSuccessToast:YMLocalizedString(@"XPLoginPhoneViewController2")]; [self.secondLineInputView startVerificationCountDown]; [self.secondLineInputView displayKeyboard]; } - (void)emailCodeFailure { [self.secondLineInputView endVerificationCountDown]; [self.navigationController popViewControllerAnimated:YES]; } - (void)bindingNewEmailSuccess:(NSString *)message { [self showSuccessToast:YMLocalizedString(@"XPLoginBindSuccessView0")]; for (UIViewController *VC in self.navigationController.viewControllers) { if ([VC isKindOfClass:[XPMineSettingViewController class]]) { [self.navigationController popToViewController:VC animated:YES]; return; } } } - (void)bindingNewEmailFailure:(NSString *)message { [self showSuccessToast:message]; } - (void)chooseAreaCodeSuccess:(NSString *)code { [self.firstLineInputView updateAreaCode:code]; } - (void)checkEmailSuccess { AccountBindingViewController *vc = [[AccountBindingViewController alloc] initWithType:self.type currentBindingAccount:@"" areaCode:@"" userInfo:self.userInfo]; [self.navigationController pushViewController:vc animated:YES]; } - (void)checkPhoneSuccess { AccountBindingViewController *vc = [[AccountBindingViewController alloc] initWithType:self.type currentBindingAccount:@"" areaCode:@"" userInfo:self.userInfo]; [self.navigationController pushViewController:vc animated:YES]; } - (void)bindingNewPhoneSuccess:(NSString *)message { [self showSuccessToast:YMLocalizedString(@"XPLoginBindSuccessView0")]; for (UIViewController *VC in self.navigationController.viewControllers) { if ([VC isKindOfClass:[XPMineSettingViewController class]]) { [self.navigationController popToViewController:VC animated:YES]; return; } } } - (void)bindingNewPhoneFailure:(NSString *)message { [self showSuccessToast:message]; } #pragma mark - - (UIStackView *)stackView { if (!_stackView) { _stackView = [[UIStackView alloc] init]; _stackView.axis = UILayoutConstraintAxisVertical; _stackView.spacing = 24; _stackView.distribution = UIStackViewDistributionFillEqually; } return _stackView; } - (UIButton *)bottomActionButton { if (!_bottomActionButton) { _bottomActionButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_bottomActionButton setCornerRadius:23]; [_bottomActionButton setTitle:YMLocalizedString(@"XPAnchorAudienceUpMicView2") forState:UIControlStateNormal]; [_bottomActionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; UIImage *normalBG = [UIImage gradientColorImageFromColors:@[ UIColorFromRGB(0xe29030), UIColorFromRGB(0xfcc074) ] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth-84, 46)] ; UIImage *disableBG = [UIImage gradientColorImageFromColors:@[ [UIColor colorWithRed:245/255.0 green:199/255.0 blue:129/255.0 alpha:1], [UIColor colorWithRed:253/255.0 green:217/255.0 blue:154/255.0 alpha:1], ] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth-84, 46)] ; [_bottomActionButton setBackgroundImage:normalBG forState:UIControlStateNormal]; [_bottomActionButton setBackgroundImage:disableBG forState:UIControlStateDisabled]; [_bottomActionButton addTarget:self action:@selector(didTapActionButton) forControlEvents:UIControlEventTouchUpInside]; _bottomActionButton.enabled = NO; } return _bottomActionButton; } - (UIImageView *)topImageView { UIImageView *imageView = [[UIImageView alloc] initWithImage:kImage(@"binding_page_top")]; imageView.contentMode = UIViewContentModeScaleAspectFit; return imageView; } - (UILabel *)descriptionLabel { UILabel *label = [UILabel labelInitWithText:YMLocalizedString(@"20.20.51_text_16") font:kFontRegular(14) textColor:UIColorFromRGB(0xafb1b3)]; label.textAlignment = NSTextAlignmentCenter; label.numberOfLines = 0; return label; } - (UILabel *)currentAccountLabel { if (!_currentAccountLabel) { _currentAccountLabel = [UILabel labelInitWithText:@"" font:kFontMedium(16) textColor:UIColorFromRGB(0x313131)]; _currentAccountLabel.textAlignment = NSTextAlignmentCenter; } return _currentAccountLabel; } @end