ModifyPhoneChangeController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // ModifyPhoneChangeController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/22.
  6. //
  7. #import "ModifyPhoneChangeController.h"
  8. #import "PhoneChangeBodyView.h"
  9. #import "VeriCheckView.h"
  10. #import "LoginViewController.h"
  11. #import "AppDelegate.h"
  12. #import "CustomNavViewController.h"
  13. #import "JPUSHService.h"
  14. #import "RCConnectionManager.h"
  15. #import "NSString+phone.h"
  16. @interface ModifyPhoneChangeController ()
  17. {
  18. NSTimer *_time;
  19. int _count;
  20. }
  21. @property (nonatomic, strong) PhoneChangeBodyView *bodyView;
  22. @end
  23. @implementation ModifyPhoneChangeController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. [self allocTitle:@"新手机号绑定"];
  28. [self configUI];
  29. }
  30. - (void)configUI {
  31. self.bodyView = [PhoneChangeBodyView shareInstance];
  32. [self.scrollView addSubview:_bodyView];
  33. [_bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.mas_equalTo(self.scrollView.mas_top);
  35. make.right.left.mas_equalTo(self.view);
  36. make.height.mas_equalTo(kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
  37. }];
  38. MJWeakSelf;
  39. [self.bodyView changePhoneCallback:^(CHANGEACTION action, NSDictionary * _Nonnull parm) {
  40. [weakSelf operationWithAction:action parm:parm];
  41. }];
  42. }
  43. - (void)operationWithAction:(CHANGEACTION)action parm:(NSDictionary *)parm {
  44. if (![NSString isMobilePhoneNumber:[parm stringValueForKey:@"phone"]]) {
  45. [self MBPShow:@"手机号码输入有误"];
  46. return;
  47. }
  48. if (action == CHANGEACTION_CODE) {
  49. // 图形化验证
  50. [self veriCheckWithParm:parm];
  51. }
  52. else if (action == CHANGEACTION_SURE) {
  53. [self modifyPhoneWithParm:parm];
  54. }
  55. }
  56. #pragma mark --- 修改手机号
  57. - (void)modifyPhoneWithParm:(NSDictionary *)parm {
  58. [self showhud];
  59. [KSNetworkingManager updatePhoneRequest:KS_POST oldPhone:self.prePhone authCode:[parm stringValueForKey:@"code"] newPhone:[parm stringValueForKey:@"phone"] success:^(NSDictionary * _Nonnull dic) {
  60. [self removehub];
  61. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  62. MJWeakSelf;
  63. [self KSShowMsg:@"修改成功" promptCompletion:^{
  64. UserDefaultSet([parm stringValueForKey:@"phone"], PHONEKEY);
  65. [[NSUserDefaults standardUserDefaults] synchronize];
  66. [weakSelf successChangeBack];
  67. }];
  68. }
  69. else {
  70. [self MBPShow:MESSAGEKEY];
  71. }
  72. } faliure:^(NSError * _Nonnull error) {
  73. [self removehub];
  74. }];
  75. }
  76. - (void)successChangeBack {
  77. [RCConnectionManager shareManager].isNeedJoin = NO;
  78. [RCConnectionManager shareManager].isNeedShowMessage = NO;
  79. [[RCIM sharedRCIM] logout];
  80. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  81. // 取消推送别名
  82. [JPUSHService deleteAlias:nil seq:0];
  83. [[NSUserDefaults standardUserDefaults] removeObjectForKey:TokenKey];
  84. [[NSUserDefaults standardUserDefaults] removeObjectForKey:Token_type];
  85. [[NSUserDefaults standardUserDefaults] removeObjectForKey:RefreshToken];
  86. [[NSUserDefaults standardUserDefaults] removeObjectForKey:RongTokenKey];
  87. [[NSUserDefaults standardUserDefaults] synchronize];
  88. [KSNetworkingManager clearRequestHeader];
  89. LoginViewController *loginVC = [[LoginViewController alloc] init];
  90. CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:loginVC];
  91. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  92. appDelegate.window.rootViewController = navCtrl;
  93. }
  94. #pragma mark --- 验证码
  95. - (void)veriCheckWithParm:(NSDictionary *)parm {
  96. NSString *phoneNo = [parm stringValueForKey:@"phone"];
  97. // 图形化验证
  98. VeriCheckView *view = [VeriCheckView shareInstanceShowInView:self.view];
  99. __weak typeof(view) weakView = view;
  100. [view showViewCallback:^(NSDictionary * _Nonnull parm) {
  101. [self veriImageCodeWithParm:parm checkView:weakView];
  102. }];
  103. [view disPlayImageWithPhone:phoneNo];
  104. }
  105. - (void)veriImageCodeWithParm:(NSDictionary *)parm checkView:(VeriCheckView *)checkView {
  106. [self showhud];
  107. [KSNetworkingManager verifyLoginImageRequest:KS_POST phone:[parm stringValueForKey:@"phone"] code:[parm stringValueForKey:@"code"] success:^(NSDictionary * _Nonnull dic) {
  108. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  109. [checkView hiddView];
  110. [self sendSMSRequestWithParm:parm];
  111. }
  112. else {
  113. [self removehub];
  114. [self MBShowInWindow:MESSAGEKEY];
  115. }
  116. } faliure:^(NSError * _Nonnull error) {
  117. [self removehub];
  118. }];
  119. }
  120. // 发送短信
  121. - (void)sendSMSRequestWithParm:(NSDictionary *)parm {
  122. [KSNetworkingManager sendSmsRequest:KS_POST mobile:[parm stringValueForKey:@"phone"] type:@"PHONE" success:^(NSDictionary * _Nonnull dic) {
  123. [self removehub];
  124. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  125. [self MBPShow:@"验证码已发送"];
  126. [self codeButtonCountDown];
  127. }
  128. else {
  129. [self MBPShow:MESSAGEKEY];
  130. }
  131. } faliure:^(NSError * _Nonnull error) {
  132. [self removehub];
  133. }];
  134. }
  135. - (void)codeButtonCountDown {
  136. _bodyView.codeButton.userInteractionEnabled = NO;
  137. [_bodyView.codeButton setTitleColor:HexRGB(0xe5e5e5) forState:UIControlStateNormal];
  138. __weak typeof(self) weakSelf = self;
  139. _time = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(buttonChangeAction) userInfo:nil repeats:YES];
  140. [[NSRunLoop mainRunLoop] addTimer:_time forMode:NSRunLoopCommonModes];
  141. _count = 89;
  142. }
  143. - (void)clearButtonState {
  144. [_time invalidate];
  145. _time = nil;
  146. _count = 0;
  147. [_bodyView.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
  148. [_bodyView.codeButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  149. _bodyView.codeButton.userInteractionEnabled = YES;
  150. }
  151. #pragma mark ---- 验证码按钮状态
  152. - (void)buttonChangeAction {
  153. _bodyView.codeButton.userInteractionEnabled = NO;
  154. NSString *title = [NSString stringWithFormat:@"%ds",_count];
  155. [_bodyView.codeButton setTitle:title forState:UIControlStateNormal];
  156. _count--;
  157. if(_count == -1){
  158. [_time invalidate];
  159. _time = nil;
  160. [_bodyView.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
  161. [_bodyView.codeButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  162. _bodyView.codeButton.userInteractionEnabled = YES; }
  163. }
  164. - (void)dealloc {
  165. [_time invalidate];
  166. _time = nil;
  167. }
  168. /*
  169. #pragma mark - Navigation
  170. // In a storyboard-based application, you will often want to do a little preparation before navigation
  171. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  172. // Get the new view controller using [segue destinationViewController].
  173. // Pass the selected object to the new view controller.
  174. }
  175. */
  176. @end