UserBindPhoneViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // UserBindPhoneViewController.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/30.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XenonSDK.h"
  9. #import "RootViewController.h"
  10. #import "UserBindPhoneViewController.h"
  11. @interface UserBindPhoneViewController ()
  12. @property (nonatomic, strong) NSMutableDictionary* users;
  13. @end
  14. @implementation UserBindPhoneViewController
  15. - (NSInteger)smsType {
  16. return 1;
  17. }
  18. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
  19. // The callback is automatically invoked when the controller is destroyed.
  20. if (self.callback == nil) {
  21. self.callback();
  22. self.callback = nil;
  23. }
  24. [super dismissViewControllerAnimated:flag completion:completion];
  25. }
  26. - (void)securityCodeForPrepare:(id)sender {
  27. // Resign editing.
  28. [self.view endEditing:YES];
  29. // Must enter phone.
  30. NSString* phone = self.phoneTextField.text;
  31. if (phone.length == 0) {
  32. [XSNetwork showHudFailure:@"请输入手机号"];
  33. return;
  34. }
  35. if (!sdk_verifyPhone(phone)) {
  36. [XSNetwork showHudFailure:@"请输入正确的手机号格式"];
  37. return;
  38. }
  39. // If the account has never been registered, an error message appears.
  40. void (^handler1)(void) = ^{
  41. NSString* str = @"该手机号己经被其他帐号绑定, 请使用新的手机号或者联系客服。";
  42. UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:str preferredStyle:UIAlertControllerStyleAlert];
  43. [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:nil]];
  44. [self presentViewController:alert animated:YES completion:nil];
  45. };
  46. // Send SMS security code.
  47. void (^handler2)(void) = ^{
  48. [super securityCodeForPrepare:sender];
  49. };
  50. // Result map.
  51. NSDictionary<NSNumber*, void(^)(void)>* mapper = @{@1: handler1, @0: handler2};
  52. // Read cache results first.
  53. id status = self.users[phone];
  54. if (status != nil) {
  55. mapper[status]();
  56. return;
  57. }
  58. // Check user with phone.
  59. [XSNetwork checkWithPhone:phone complete:^(id object, NSError *error) {
  60. // Got it wrong.
  61. if (error != nil) {
  62. [XSNetwork showHudFailure:error];
  63. return;
  64. }
  65. // Cache.
  66. NSInteger dstatus = !![object[@"status"] boolValue];
  67. self.users[phone] = @(dstatus);
  68. mapper[@(dstatus)]();
  69. }];
  70. }
  71. - (void)securityCodeForVerify:(id)sender {
  72. [super securityCodeForVerify:sender];
  73. XSUser* user = XenonSDK.sharedSDK.user;
  74. if (user == nil) {
  75. return;
  76. }
  77. // Must enter phone.
  78. NSString* phone = self.phoneTextField.text;
  79. if (phone.length == 0) {
  80. [XSNetwork showHudFailure:@"请输入手机号"];
  81. return;
  82. }
  83. if (!sdk_verifyPhone(phone)) {
  84. [XSNetwork showHudFailure:@"请输入正确的手机号格式"];
  85. return;
  86. }
  87. NSString* code = self.securityCodeTextField.text;
  88. if (code.length == 0) {
  89. [XSNetwork showHudFailure:@"请输入验证码"];
  90. return;
  91. }
  92. [XSNetwork showHudLoading];
  93. [XSNetwork bindWithPhone:phone code:code username:user.account complete:^(id object, NSError* error) {
  94. if (error != nil) {
  95. [XSNetwork showHudFailure:error];
  96. return;
  97. }
  98. [XSNetwork showHudSuccess:@"帐号绑定成功"];
  99. // Update account information.
  100. user.phone = phone;
  101. XenonSDK.sharedSDK.user = user;
  102. // Cancel the display.
  103. [self sdk_dismissViewController:nil];
  104. }];
  105. }
  106. - (BOOL)prefersAutomaticBackView {
  107. return NO;
  108. }
  109. @end