UserBindViewController.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // UserBindViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by wujiangbo on 2018/11/2.
  6. // Copyright © 2018年 wujiangbo. All rights reserved.
  7. //
  8. #import "UserBindViewController.h"
  9. #import "UserBindView.h"
  10. #import "UserAccountModel.h"
  11. #import "NSString+Extention.h"
  12. #import "Header.h"
  13. @interface UserBindViewController ()<UserAccountModelDelegate>
  14. {
  15. UserBindView *userBindView; // 用户手机号/邮箱绑定视图
  16. UserAccountModel *accountModel; //账号相关功能接口管理器
  17. NSInteger sendTime; //倒计时时间
  18. NSTimer *countDownTimer; //倒计时计时器
  19. BOOL isReceivingCode; //是否正在接收验证码
  20. }
  21. @end
  22. @implementation UserBindViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. //账号相关功能接口管理器
  26. accountModel = [[UserAccountModel alloc] init];
  27. accountModel.delegate = self;
  28. // 用户手机号/邮箱绑定视图初始化
  29. userBindView = [[UserBindView alloc] init];
  30. __weak typeof(self) weakSelf = self;
  31. // 获取验证码事件处理(绑定手机或邮箱)
  32. userBindView.getCodeBtnClicked = ^(NSString * _Nonnull phoneEmail) {
  33. [weakSelf getCodeBtnClickedWithPhoneEmail:phoneEmail];
  34. };
  35. //绑定手机或者邮箱
  36. userBindView.bindBtnClicked = ^(NSString * _Nonnull phoneEmail, NSString * _Nonnull code) {
  37. [weakSelf bindBtnClickedWithPhoneEmail:phoneEmail code:code];
  38. };
  39. self.view = userBindView;
  40. self.view.backgroundColor = [UIColor whiteColor];
  41. [self configNav];
  42. }
  43. -(void)viewWillAppear:(BOOL)animated
  44. {
  45. // 判断当前需要绑定的是否为手机号,是的话更新界面
  46. if ([self.navigationItem.title isEqualToString:TS("bind_phoneNumber")]) {
  47. [userBindView.bindBtn setTitle:TS("bind_phoneNumber") forState:UIControlStateNormal];
  48. userBindView.phoneEmailTF.placeholder = TS("enter_phoneNum");
  49. }
  50. }
  51. #pragma mark - 设置导航栏
  52. -(void)configNav
  53. {
  54. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backItemClicked)];
  55. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  56. self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
  57. }
  58. #pragma mark - EventAction
  59. -(void)backItemClicked
  60. {
  61. if([SVProgressHUD isVisible]){
  62. [SVProgressHUD dismiss];
  63. }
  64. [self.navigationController popViewControllerAnimated:YES];
  65. }
  66. #pragma mark 获取验证码
  67. -(void)getCodeBtnClickedWithPhoneEmail:(NSString *)phoneEmail
  68. {
  69. if (isReceivingCode == NO) {
  70. //手机号码或者邮箱不得为空
  71. if (phoneEmail.length == 0) {
  72. [SVProgressHUD showErrorWithStatus:TS("moblie_error")];
  73. return;
  74. }
  75. //判断用户输入的邮箱还是手机号
  76. if([phoneEmail containsString:@"@"]) {
  77. //邮箱
  78. if (![NSString isValidateEmail:phoneEmail])//邮箱格式判断是否正确
  79. {
  80. [SVProgressHUD showErrorWithStatus:TS("PhoneOrEmailError")];
  81. return;
  82. }
  83. }
  84. else{
  85. //手机号
  86. if (phoneEmail.length != 11) {
  87. [SVProgressHUD showErrorWithStatus:TS("PhoneOrEmailError")];;
  88. return;
  89. }
  90. }
  91. [SVProgressHUD show];
  92. //开始倒计时
  93. sendTime = 120;
  94. isReceivingCode = YES;
  95. countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownFunction) userInfo:nil repeats:YES];
  96. NSString *useName = [[LoginShowControl getInstance] getLoginUserName];
  97. NSString *passWord = [[LoginShowControl getInstance] getLoginPassword];
  98. //发送获取验证码命令
  99. [accountModel getBindingPhoneEmailCode:useName password:passWord PhoneOrEmail:phoneEmail];
  100. }
  101. }
  102. #pragma mark 发送绑定手机号或邮箱命令
  103. -(void)bindBtnClickedWithPhoneEmail:(NSString *)phoneEmail code:(NSString *)code
  104. {
  105. if (phoneEmail.length == 0) {
  106. [SVProgressHUD showErrorWithStatus:TS("moblie_error")];
  107. return;
  108. }
  109. //判断用户输入的邮箱还是手机号
  110. if([phoneEmail containsString:@"@"]) {
  111. //邮箱
  112. if (![NSString isValidateEmail:phoneEmail])//邮箱格式判断是否正确
  113. {
  114. [SVProgressHUD showErrorWithStatus:TS("PhoneOrEmailError")];
  115. return;
  116. }
  117. }
  118. else{
  119. //手机号
  120. if (phoneEmail.length != 11) {
  121. [SVProgressHUD showErrorWithStatus:TS("PhoneOrEmailError")];;
  122. return;
  123. }
  124. }
  125. if (code.length == 0) {
  126. [SVProgressHUD showErrorWithStatus:TS("input_code")];
  127. return;
  128. }
  129. [SVProgressHUD show];
  130. NSString *useName = [[LoginShowControl getInstance] getLoginUserName];
  131. NSString *passWord = [[LoginShowControl getInstance] getLoginPassword];
  132. //发送绑定手机号或邮箱命令
  133. [accountModel bindPhoneEmail:useName password:passWord PhoneOrEmail:phoneEmail code:code];
  134. }
  135. //获取验证码倒计时处理
  136. -(void)countDownFunction
  137. {
  138. if(sendTime > 0) {//刷新倒计时时间
  139. NSString *getCodeBtnTitle = [NSString stringWithFormat:@"%d%@%@",(int)sendTime,TS("general_second"),TS("ReGetRegCode")];
  140. [userBindView.getCodeBtn setTitle:getCodeBtnTitle forState:UIControlStateNormal];
  141. sendTime--;
  142. }else{
  143. [self getCodeBtnResign];
  144. }
  145. }
  146. //重置计时器
  147. -(void)getCodeBtnResign
  148. {
  149. if (countDownTimer) {
  150. [countDownTimer invalidate];
  151. countDownTimer = nil;
  152. }
  153. [userBindView.getCodeBtn setTitle:TS("get_code") forState:UIControlStateNormal];
  154. isReceivingCode = NO;
  155. }
  156. #pragma mark - funsdk回调处理
  157. #pragma mark 获取验证码回调(绑定邮箱/手机)
  158. -(void)getCodeForBindPhoneEmailResult:(long)result
  159. {
  160. [SVProgressHUD dismiss];
  161. if (result >= 0) {
  162. //倒计时开始
  163. }
  164. else{
  165. [MessageUI ShowErrorInt:(int)result];
  166. [self getCodeBtnResign];
  167. }
  168. }
  169. #pragma mark 绑定邮箱/手机回调
  170. -(void)bindPhoneEmailResult:(long)result
  171. {
  172. [SVProgressHUD dismiss];
  173. if (result >= 0) {
  174. [SVProgressHUD showErrorWithStatus:TS("Success")];
  175. if (self.bindPhoneEmailSuccess) {
  176. self.bindPhoneEmailSuccess();
  177. }
  178. [self.navigationController popViewControllerAnimated:YES];
  179. }
  180. else{
  181. [MessageUI ShowErrorInt:(int)result];
  182. }
  183. }
  184. @end