YMVerifyCodeLoginView.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. //
  2. // YMVerifyCodeLoginView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/4.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMVerifyCodeLoginView.h"
  9. #import "YMVerifyCodeLoginViewModel.h"
  10. #import "YMCaptchaPopupView.h"
  11. #import "CaptchaCenter.h"
  12. @interface YMVerifyCodeLoginView ()
  13. /// 验证码登录VM
  14. @property (nonatomic, strong) YMVerifyCodeLoginViewModel *viewModel;
  15. /// 手机视图
  16. @property (nonatomic, strong) UIView *mobileView;
  17. /// 手机号标签
  18. @property (nonatomic, strong) UILabel *mobileLb;
  19. /// 手机号输入框
  20. @property (nonatomic, strong) UITextField *mobileInputBox;
  21. /// 验证码视图
  22. @property (nonatomic, strong) UIView *verifyCodeView;
  23. /// 验证码标签
  24. @property (nonatomic, strong) UILabel *verifyCodeLb;
  25. /// 验证码输入框
  26. @property (nonatomic, strong) UITextField *verifyCodeInputBox;
  27. /// 获取验证码
  28. @property (nonatomic, strong) YMCaptchaCountdownButton *getVerifyCodeBtn;
  29. /// 协议视图
  30. @property (nonatomic, strong) UIView *agreementView;
  31. /// 单选按钮
  32. @property (nonatomic, strong) UIButton *radioBtn;
  33. /// 协议标签
  34. @property (nonatomic, strong) YYLabel *agreementLb;
  35. /// 登录按钮
  36. @property (nonatomic, strong) UIButton *loginBtn;
  37. /** 注册按钮*/
  38. @property (nonatomic, strong) UIButton *registerBtn;
  39. @end
  40. @implementation YMVerifyCodeLoginView
  41. - (void)ym_setupViews{
  42. [self addSubview:self.mobileView];
  43. [self.mobileView addSubview:self.mobileLb];
  44. [self.mobileView addSubview:self.mobileInputBox];
  45. [self addSubview:self.verifyCodeView];
  46. [self.verifyCodeView addSubview:self.verifyCodeLb];
  47. [self.verifyCodeView addSubview:self.verifyCodeInputBox];
  48. [self.verifyCodeView addSubview:self.getVerifyCodeBtn];
  49. [self addSubview:self.agreementView];
  50. [self.agreementView addSubview:self.radioBtn];
  51. [self.agreementView addSubview:self.agreementLb];
  52. [self addSubview:self.loginBtn];
  53. [self addSubview:self.registerBtn];
  54. [self setNeedsUpdateConstraints];
  55. [self updateConstraintsIfNeeded];
  56. }
  57. - (void)updateConstraints{
  58. [self.mobileView mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.top.equalTo(self).offset(adapt(24));
  60. make.left.equalTo(self).offset(adapt(28));
  61. make.right.equalTo(self).offset(adapt(-28));
  62. }];
  63. [self.mobileLb mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.top.equalTo(self.mobileView);
  65. make.left.equalTo(self.mobileView);
  66. }];
  67. [self.mobileInputBox mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.top.equalTo(self.mobileLb.mas_bottom).offset(adapt(10));
  69. make.left.equalTo(self.mobileView);
  70. make.right.equalTo(self.mobileView);
  71. make.bottom.equalTo(self.mobileView);
  72. make.height.mas_equalTo(adapt(30));
  73. }];
  74. [self.verifyCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.top.equalTo(self.mobileView.mas_bottom).offset(adapt(24));
  76. make.left.equalTo(self.mobileView.mas_left);
  77. make.right.equalTo(self.mobileView.mas_right);
  78. }];
  79. [self.verifyCodeLb mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(self.verifyCodeView);
  81. make.left.equalTo(self.verifyCodeView);
  82. }];
  83. [self.verifyCodeInputBox mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.top.equalTo(self.verifyCodeLb.mas_bottom).offset(adapt(10));
  85. make.left.equalTo(self.verifyCodeView);
  86. make.bottom.equalTo(self.verifyCodeView);
  87. make.height.mas_equalTo(adapt(30));
  88. }];
  89. [self.getVerifyCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.top.equalTo(self.verifyCodeLb.mas_bottom).offset(adapt(10));
  91. make.left.equalTo(self.verifyCodeInputBox.mas_right).offset(adapt(5));
  92. make.right.equalTo(self.verifyCodeView);
  93. make.bottom.equalTo(self.verifyCodeView);
  94. make.width.mas_equalTo(adapt(100));
  95. }];
  96. [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.top.equalTo(self.verifyCodeView.mas_bottom).offset(adapt(24));
  98. make.left.equalTo(self.mobileView.mas_left);
  99. make.right.equalTo(self.mobileView.mas_right);
  100. make.height.mas_equalTo(adapt(40));
  101. }];
  102. [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.centerX.equalTo(self.loginBtn.mas_centerX);
  104. make.top.equalTo(self.loginBtn.mas_bottom).offset(adapt(16));
  105. make.height.mas_equalTo(adapt(25));
  106. }];
  107. [self.agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.centerX.equalTo(self.mas_centerX);
  109. make.bottom.equalTo(self).offset(Is_iPhoneX ? adapt(-32) : adapt(-12));
  110. }];
  111. [self.radioBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.centerY.equalTo(self.agreementLb.mas_centerY);
  113. make.left.equalTo(self.agreementView).offset(adapt(5));
  114. make.width.height.mas_equalTo(adapt(12));
  115. }];
  116. [self.agreementLb mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.top.equalTo(self.agreementView).offset(adapt(10));
  118. make.left.equalTo(self.radioBtn.mas_right).offset(adapt(5));
  119. make.right.equalTo(self.agreementView).offset(adapt(-5));
  120. make.bottom.equalTo(self.agreementView).offset(adapt(-10));
  121. }];
  122. [super updateConstraints];
  123. }
  124. - (void)ym_bindViewModel:(YMVerifyCodeLoginViewModel *)viewModel{
  125. if (!viewModel) {
  126. return;
  127. }
  128. _viewModel = viewModel;
  129. RAC(self.viewModel , mobile) = [[RACSignal merge:@[RACObserve(self.mobileInputBox, text),self.mobileInputBox.rac_textSignal]] takeUntil:self.rac_willDeallocSignal];
  130. RAC(self.viewModel , verifyCode) = [[RACSignal merge:@[RACObserve(self.verifyCodeInputBox, text),self.verifyCodeInputBox.rac_textSignal]]takeUntil:self.rac_willDeallocSignal];
  131. RAC(self.loginBtn , enabled) = self.viewModel.validLoginSignal;
  132. [self.viewModel.validLoginSignal subscribeNext:^(id _Nullable value) {
  133. self.loginBtn.enabled = [value boolValue];
  134. if ([value boolValue]) {
  135. self.loginBtn.alpha = 1;
  136. } else {
  137. self.loginBtn.alpha = 0.5;
  138. }
  139. }];
  140. }
  141. - (UIView *)mobileView{
  142. if (!_mobileView) {
  143. _mobileView = [[UIView alloc]init];
  144. }
  145. return _mobileView;
  146. }
  147. - (UILabel *)mobileLb{
  148. if (!_mobileLb) {
  149. _mobileLb = [[UILabel alloc]init];
  150. _mobileLb.font = LCBoldFont(17);
  151. _mobileLb.textColor = HexColorFromRGB(0x1B2739);
  152. _mobileLb.textAlignment = NSTextAlignmentLeft;
  153. _mobileLb.text = @"手机号";
  154. }
  155. return _mobileLb;
  156. }
  157. - (UITextField *)mobileInputBox{
  158. if (!_mobileInputBox) {
  159. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
  160. style.minimumLineHeight = 0;
  161. NSMutableAttributedString *placeholderAttributed = [[NSMutableAttributedString alloc]initWithString:@"请输入手机号"];
  162. placeholderAttributed.yy_paragraphStyle = style;
  163. placeholderAttributed.yy_font = LCFont(14);
  164. placeholderAttributed.yy_color = HexColorFromRGB(0xADB0BC);
  165. _mobileInputBox = [[UITextField alloc]init];
  166. _mobileInputBox.attributedPlaceholder = placeholderAttributed;
  167. _mobileInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
  168. _mobileInputBox.autocorrectionType = UITextAutocorrectionTypeDefault;
  169. _mobileInputBox.autocapitalizationType = UITextAutocapitalizationTypeNone;
  170. _mobileInputBox.keyboardType = UIKeyboardTypePhonePad;
  171. _mobileInputBox.ba_maxLength = 11;
  172. }
  173. return _mobileInputBox;
  174. }
  175. - (UIView *)verifyCodeView{
  176. if (!_verifyCodeView) {
  177. _verifyCodeView = [[UIView alloc]init];
  178. }
  179. return _verifyCodeView;
  180. }
  181. - (UILabel *)verifyCodeLb{
  182. if (!_verifyCodeLb) {
  183. _verifyCodeLb = [[UILabel alloc]init];
  184. _verifyCodeLb.font = LCBoldFont(17);
  185. _verifyCodeLb.textColor = HexColorFromRGB(0x1B2739);
  186. _verifyCodeLb.textAlignment = NSTextAlignmentLeft;
  187. _verifyCodeLb.text = @"验证码";
  188. }
  189. return _verifyCodeLb;
  190. }
  191. - (UITextField *)verifyCodeInputBox{
  192. if (!_verifyCodeInputBox) {
  193. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
  194. style.minimumLineHeight = 0;
  195. NSMutableAttributedString *placeholderAttributed = [[NSMutableAttributedString alloc]initWithString:@"请输入验证码"];
  196. placeholderAttributed.yy_paragraphStyle = style;
  197. placeholderAttributed.yy_font = LCFont(14);
  198. placeholderAttributed.yy_color = HexColorFromRGB(0xADB0BC);
  199. _verifyCodeInputBox = [[UITextField alloc]init];
  200. _verifyCodeInputBox.attributedPlaceholder = placeholderAttributed;
  201. _verifyCodeInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
  202. _verifyCodeInputBox.autocorrectionType = UITextAutocorrectionTypeDefault;
  203. _verifyCodeInputBox.autocapitalizationType = UITextAutocapitalizationTypeNone;
  204. _verifyCodeInputBox.keyboardType = UIKeyboardTypeDecimalPad;
  205. _verifyCodeInputBox.ba_maxLength = 4;
  206. }
  207. return _verifyCodeInputBox;
  208. }
  209. - (YMCaptchaCountdownButton *)getVerifyCodeBtn{
  210. if (!_getVerifyCodeBtn) {
  211. _getVerifyCodeBtn = [YMCaptchaCountdownButton buttonWithType:UIButtonTypeCustom];
  212. _getVerifyCodeBtn.titleLabel.font = LCFont(14);
  213. [_getVerifyCodeBtn setTitleColor:HexColorFromRGB(0xB26AFD) forState:UIControlStateNormal];
  214. [_getVerifyCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  215. @weakify(self)
  216. [_getVerifyCodeBtn countDownButtonHandler:^(YMCaptchaCountdownButton *sender, NSInteger tag) {
  217. @strongify(self)
  218. if (OCStringIsEmpty(self.mobileInputBox.text)) {
  219. [ZCHUDHelper showTitle:@"请输入手机号"];
  220. return;
  221. }
  222. [self.mobileInputBox resignFirstResponder];
  223. [self.verifyCodeInputBox resignFirstResponder];
  224. [self.verifyCodeInputBox resignFirstResponder];
  225. [CaptchaCenter.defaultCenter getCaptchaType:^(NSNumber * _Nullable type, NSError * _Nullable error) {
  226. if (error) {
  227. [ZCHUDHelper showTitle:@"获取验证码失败"];
  228. return;
  229. }
  230. if ([type isEqualToNumber:@1]) { // 原生
  231. YMCaptchaPopupView *view = [[YMCaptchaPopupView alloc] init];
  232. view.cancelButtonTappedBlock = ^{};
  233. @weakify(self)
  234. view.confirmButtonTappedBlock = ^(NSString * _Nonnull input) {
  235. @strongify(self)
  236. [self getVerifyCodeWithCapthca:input sender:sender];
  237. };
  238. UIView *currentVCView = [LCTools getCurrentVC].view;
  239. if (currentVCView != nil && ![currentVCView isEqual:NSNull.null]) {
  240. [view showInView:currentVCView];
  241. }
  242. } else { // 阿里
  243. @weakify(self)
  244. [CaptchaCenter.defaultCenter verifyCompletion:^(NSString * _Nonnull status, NSDictionary * _Nonnull result) {
  245. @strongify(self)
  246. [self getVerifyCodeWithCapthca:result sender:sender];
  247. }];
  248. }
  249. }];
  250. }];
  251. }
  252. return _getVerifyCodeBtn;
  253. }
  254. - (void)getVerifyCodeWithCapthca:(id)capthca sender:(YMCaptchaCountdownButton *)sender {
  255. //@weakify(self)
  256. [self.viewModel getVerifyCodeWithCaptcha:capthca Handler:^(NSDictionary * _Nonnull dic, NSError * _Nullable error) {
  257. //@strongify(self)
  258. sender.enabled = NO;
  259. [sender startCountDownWithSecond:59];
  260. [sender countDownChanging:^NSString *(YMCaptchaCountdownButton *countDownButton, NSUInteger second) {
  261. NSString *title = [NSString stringWithFormat:@"重新发送(%zds)",second];
  262. return title;
  263. }];
  264. [sender countDownFinished:^NSString *(YMCaptchaCountdownButton *countDownButton, NSUInteger second) {
  265. countDownButton.enabled = YES;
  266. return @"重新发送";
  267. }];
  268. }];
  269. }
  270. - (UIView *)agreementView{
  271. if (!_agreementView) {
  272. _agreementView = [[UIView alloc]init];
  273. }
  274. return _agreementView;
  275. }
  276. - (UIButton *)radioBtn{
  277. if (!_radioBtn) {
  278. _radioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  279. [_radioBtn setBackgroundImage:ImageByName(@"ym_login_auth_normal_icon") forState:UIControlStateNormal];
  280. [_radioBtn setBackgroundImage:ImageByName(@"ym_login_auth_selected_icon") forState:UIControlStateSelected];
  281. WS(weakSelf)
  282. [[[_radioBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(__kindof UIButton * _Nullable sender) {
  283. sender.selected = !sender.selected;
  284. }];
  285. }
  286. return _radioBtn;
  287. }
  288. - (YYLabel *)agreementLb{
  289. if (!_agreementLb) {
  290. _agreementLb = [[YYLabel alloc] init];
  291. _agreementLb.numberOfLines = 0;
  292. _agreementLb.preferredMaxLayoutWidth = kFrameWidth - adapt(55);
  293. NSString *agreementText = @"同意《用户协议》和《隐私协议》";
  294. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  295. paragraphStyle.alignment = NSTextAlignmentLeft;
  296. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:agreementText];
  297. attributedString.yy_font = LCFont(11);
  298. attributedString.yy_color = HexColorFromRGB(0x7E848D);
  299. attributedString.yy_paragraphStyle = paragraphStyle;
  300. //设置高亮色和点击事件
  301. [attributedString yy_setTextHighlightRange:[agreementText rangeOfString:@"《用户协议》"] color:HexColorFromRGB(0xB26AFD) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
  302. YMWebArticleViewModel *webArticleVM = [[YMWebArticleViewModel alloc]initWithParams:@{
  303. ParamsUrl:[NSString stringWithFormat:@"%@%@",[LCSaveData getBaseURL]?[LCSaveData getBaseURL]:BaseURL,UserProtocolH5]
  304. }];
  305. [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_WEB_ARTICLE) withUserInfo:@{
  306. RouterViewModel:webArticleVM
  307. } completion:nil];
  308. }];
  309. //设置高亮色和点击事件
  310. [attributedString yy_setTextHighlightRange:[[attributedString string] rangeOfString:@"《隐私协议》"] color:HexColorFromRGB(0xB26AFD) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
  311. YMWebArticleViewModel *webArticleVM = [[YMWebArticleViewModel alloc]initWithParams:@{
  312. ParamsUrl:[NSString stringWithFormat:@"%@%@",[LCSaveData getBaseURL]?[LCSaveData getBaseURL]:BaseURL,UserPrivacyH5]
  313. }];
  314. [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_WEB_ARTICLE) withUserInfo:@{
  315. RouterViewModel:webArticleVM
  316. } completion:nil];
  317. }];
  318. _agreementLb.attributedText = attributedString;
  319. }
  320. return _agreementLb;
  321. }
  322. - (UIButton *)loginBtn{
  323. if (!_loginBtn) {
  324. _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  325. _loginBtn.titleLabel.font = LCBoldFont(15);
  326. [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
  327. [_loginBtn setTitleColor:kMainGradTitleC forState:UIControlStateNormal];
  328. [_loginBtn ym_setGradientBackgroundWithColors:kMainGradColors locations:kMainGradLocation startPoint:kMainGradStartP endPoint:kMainGradEndP];
  329. _loginBtn.layer.cornerRadius = adapt(40)/2;
  330. WS(weakSelf)
  331. [[[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(__kindof UIButton * _Nullable sender) {
  332. if (OCStringIsEmpty(weakSelf.mobileInputBox.text)) {
  333. [ZCHUDHelper showTitle:@"请输入手机号"];
  334. return;
  335. }
  336. if (OCStringIsEmpty(weakSelf.verifyCodeInputBox.text)) {
  337. [ZCHUDHelper showTitle:@"请输入验证码"];
  338. return;
  339. }
  340. if (!weakSelf.radioBtn.selected) {
  341. YMLoginRegistrAgreementPopupView *customView = [[YMLoginRegistrAgreementPopupView alloc]init];
  342. YMPopupView *popupView = [YMPopupView initWithCustomView:customView parentView:nil popStyle:YMPopupStyleFade dismissStyle:YMDismissStyleFade];
  343. popupView.priority = 999;
  344. popupView.cornerRadius = adapt(10);
  345. popupView.rectCorners = UIRectCornerAllCorners;
  346. popupView.positionStyle = YMPositionStyleCenter;
  347. popupView.isHideBg = NO;
  348. popupView.bgAlpha = 0.3;
  349. [popupView pop];
  350. @weakify(popupView)
  351. customView.buttonBlock = ^(BOOL isConfirm) {
  352. @strongify(popupView)
  353. if (isConfirm) {
  354. weakSelf.radioBtn.selected = YES;
  355. [weakSelf.viewModel loginRequest];
  356. }
  357. [popupView dismissWithStyle:YMDismissStyleFade duration:2.0];
  358. };
  359. customView.dismissBlock = ^{
  360. @strongify(popupView)
  361. [popupView dismissWithStyle:YMDismissStyleFade duration:2.0];
  362. };
  363. return;
  364. }
  365. [weakSelf.viewModel loginRequest];
  366. }];
  367. }
  368. return _loginBtn;
  369. }
  370. - (UIButton *)registerBtn{
  371. if (!_registerBtn) {
  372. _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  373. _registerBtn.titleLabel.font = LCFont(14);
  374. [_registerBtn setTitle:@"新用户注册" forState:UIControlStateNormal];
  375. [_registerBtn setTitleColor:HexColorFromRGB(0xB26AFD) forState:UIControlStateNormal];
  376. WS(weakSelf)
  377. [[[_registerBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(__kindof UIButton * _Nullable sender) {
  378. [weakSelf.viewModel gotoRegisterVC];
  379. }];
  380. }
  381. return _registerBtn;
  382. }
  383. @end