// // UserBindPhoneViewController.m // XenonSDK // // Created by SAGESSE on 2019/5/30. // Copyright © 2019 SAGESSE. All rights reserved. // #import "XenonSDK.h" #import "RootViewController.h" #import "UserBindPhoneViewController.h" @interface UserBindPhoneViewController () @property (nonatomic, strong) NSMutableDictionary* users; @end @implementation UserBindPhoneViewController - (NSInteger)smsType { return 1; } - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { // The callback is automatically invoked when the controller is destroyed. if (self.callback == nil) { self.callback(); self.callback = nil; } [super dismissViewControllerAnimated:flag completion:completion]; } - (void)securityCodeForPrepare:(id)sender { // Resign editing. [self.view endEditing:YES]; // Must enter phone. NSString* phone = self.phoneTextField.text; if (phone.length == 0) { [XSNetwork showHudFailure:@"请输入手机号"]; return; } if (!sdk_verifyPhone(phone)) { [XSNetwork showHudFailure:@"请输入正确的手机号格式"]; return; } // If the account has never been registered, an error message appears. void (^handler1)(void) = ^{ NSString* str = @"该手机号己经被其他帐号绑定, 请使用新的手机号或者联系客服。"; UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:str preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; }; // Send SMS security code. void (^handler2)(void) = ^{ [super securityCodeForPrepare:sender]; }; // Result map. NSDictionary* mapper = @{@1: handler1, @0: handler2}; // Read cache results first. id status = self.users[phone]; if (status != nil) { mapper[status](); return; } // Check user with phone. [XSNetwork checkWithPhone:phone complete:^(id object, NSError *error) { // Got it wrong. if (error != nil) { [XSNetwork showHudFailure:error]; return; } // Cache. NSInteger dstatus = !![object[@"status"] boolValue]; self.users[phone] = @(dstatus); mapper[@(dstatus)](); }]; } - (void)securityCodeForVerify:(id)sender { [super securityCodeForVerify:sender]; XSUser* user = XenonSDK.sharedSDK.user; if (user == nil) { return; } // Must enter phone. NSString* phone = self.phoneTextField.text; if (phone.length == 0) { [XSNetwork showHudFailure:@"请输入手机号"]; return; } if (!sdk_verifyPhone(phone)) { [XSNetwork showHudFailure:@"请输入正确的手机号格式"]; return; } NSString* code = self.securityCodeTextField.text; if (code.length == 0) { [XSNetwork showHudFailure:@"请输入验证码"]; return; } [XSNetwork showHudLoading]; [XSNetwork bindWithPhone:phone code:code username:user.account complete:^(id object, NSError* error) { if (error != nil) { [XSNetwork showHudFailure:error]; return; } [XSNetwork showHudSuccess:@"帐号绑定成功"]; // Update account information. user.phone = phone; XenonSDK.sharedSDK.user = user; // Cancel the display. [self sdk_dismissViewController:nil]; }]; } - (BOOL)prefersAutomaticBackView { return NO; } @end