WFCDestroyAccountViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // WFCDestroyAccountViewController.m
  3. // Wildfire Chat
  4. //
  5. // Created by WF Chat on 2017/7/9.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCDestroyAccountViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import <WFChatUIKit/WFChatUIKit.h>
  11. #import "AppDelegate.h"
  12. #import "WFCBaseTabBarController.h"
  13. #import "MBProgressHUD.h"
  14. #import "UILabel+YBAttributeTextTapAction.h"
  15. #import "WFCPrivacyViewController.h"
  16. #import "AppService.h"
  17. #import "OrgService.h"
  18. #import "UIColor+YH.h"
  19. #import "UIFont+YH.h"
  20. #import "SSKeychain.h"
  21. @interface WFCDestroyAccountViewController () <UITextFieldDelegate>
  22. @property (strong, nonatomic) UILabel *hintLabel;
  23. @property (strong, nonatomic) UITextField *passwordField;
  24. @property (strong, nonatomic) UIButton *loginBtn;
  25. @property (strong, nonatomic) UIView *passwordLine;
  26. @property (strong, nonatomic) UIButton *sendCodeBtn;
  27. @property (nonatomic, strong) NSTimer *countdownTimer;
  28. @property (nonatomic, assign) NSTimeInterval sendCodeTime;
  29. @end
  30. @implementation WFCDestroyAccountViewController
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  34. CGRect bgRect = self.view.bounds;
  35. CGFloat paddingEdge = 16;
  36. CGFloat inputHeight = 40;
  37. CGFloat hintHeight = 20;
  38. CGFloat topPos = [WFCUUtilities wf_navigationFullHeight] + 45;
  39. self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(paddingEdge, topPos, bgRect.size.width - paddingEdge - paddingEdge, hintHeight)];
  40. [self.hintLabel setText:@"亲,真的要狠心离开我们吗😭😭😭!"];
  41. self.hintLabel.textAlignment = NSTextAlignmentLeft;
  42. self.hintLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:hintHeight];
  43. topPos += hintHeight + 100;
  44. UIView *passwordContainer = [[UIView alloc] initWithFrame:CGRectMake(paddingEdge, topPos, bgRect.size.width - paddingEdge * 2, inputHeight)];
  45. UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 52, inputHeight - 1)];
  46. passwordLabel.text = @"验证码";
  47. passwordLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:17];
  48. self.passwordLine = [[UIView alloc] initWithFrame:CGRectMake(0, inputHeight - 1, passwordContainer.frame.size.width, 1.f)];
  49. self.passwordLine.backgroundColor = [UIColor colorWithHexString:@"0xd4d4d4"];
  50. self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(87, 0, passwordContainer.frame.size.width - 87 - 72, inputHeight - 1)];
  51. self.passwordField.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
  52. self.passwordField.placeholder = @"请输入验证码";
  53. self.passwordField.returnKeyType = UIReturnKeyDone;
  54. self.passwordField.keyboardType = UIKeyboardTypeNumberPad;
  55. self.passwordField.delegate = self;
  56. self.passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
  57. [self.passwordField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
  58. self.sendCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(passwordContainer.frame.size.width - 72, (inputHeight - 1 - 23) / 2.0, 72, 23)];
  59. [self.sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  60. self.sendCodeBtn.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  61. self.sendCodeBtn.layer.borderWidth = 1;
  62. self.sendCodeBtn.layer.cornerRadius = 4;
  63. self.sendCodeBtn.layer.borderColor = [UIColor colorWithHexString:@"0x191919"].CGColor;
  64. [self.sendCodeBtn setTitleColor:[UIColor colorWithHexString:@"0x171717"] forState:UIControlStateNormal];
  65. [self.sendCodeBtn setTitleColor:[UIColor colorWithHexString:@"0x171717"] forState:UIControlStateSelected];
  66. [self.sendCodeBtn addTarget:self action:@selector(onSendCode:) forControlEvents:UIControlEventTouchDown];
  67. topPos += 71;
  68. self.loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(paddingEdge, topPos, bgRect.size.width - paddingEdge * 2, 43)];
  69. [self.loginBtn addTarget:self action:@selector(onLoginButton:) forControlEvents:UIControlEventTouchDown];
  70. self.loginBtn.layer.masksToBounds = YES;
  71. self.loginBtn.layer.cornerRadius = 4.f;
  72. [self.loginBtn setTitle:@"销毁账户" forState:UIControlStateNormal];
  73. self.loginBtn.backgroundColor = [UIColor colorWithHexString:@"0xe1e1e1"];
  74. [self.loginBtn setTitleColor:[UIColor colorWithHexString:@"0xb1b1b1"] forState:UIControlStateNormal];
  75. self.loginBtn.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleMedium size:16];
  76. self.loginBtn.enabled = NO;
  77. [self.view addSubview:self.hintLabel];
  78. [self.view addSubview:passwordContainer];
  79. [passwordContainer addSubview:passwordLabel];
  80. [passwordContainer addSubview:self.passwordField];
  81. [passwordContainer addSubview:self.passwordLine];
  82. [passwordContainer addSubview:self.sendCodeBtn];
  83. [self.view addSubview:self.loginBtn];
  84. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetKeyboard:)]];
  85. }
  86. - (void)didReceiveMemoryWarning {
  87. [super didReceiveMemoryWarning];
  88. // Dispose of any resources that can be recreated.
  89. }
  90. - (void)onSendCode:(id)sender {
  91. self.sendCodeBtn.enabled = NO;
  92. [self.sendCodeBtn setTitle:@"短信发送中" forState:UIControlStateNormal];
  93. __weak typeof(self)ws = self;
  94. [[AppService sharedAppService] sendDestroyAccountCode:^{
  95. [ws sendCodeDone:YES];
  96. } error:^(int errorCode, NSString * _Nonnull message) {
  97. [ws sendCodeDone:NO];
  98. }];
  99. }
  100. - (void)updateCountdown:(id)sender {
  101. int second = (int)([NSDate date].timeIntervalSince1970 - self.sendCodeTime);
  102. [self.sendCodeBtn setTitle:[NSString stringWithFormat:@"%ds", 60-second] forState:UIControlStateNormal];
  103. if (second >= 60) {
  104. [self.countdownTimer invalidate];
  105. self.countdownTimer = nil;
  106. [self.sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  107. self.sendCodeBtn.enabled = YES;
  108. }
  109. }
  110. - (void)sendCodeDone:(BOOL)success {
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. if (success) {
  113. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  114. hud.mode = MBProgressHUDModeText;
  115. hud.label.text = @"发送成功";
  116. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  117. self.sendCodeTime = [NSDate date].timeIntervalSince1970;
  118. self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
  119. target:self
  120. selector:@selector(updateCountdown:)
  121. userInfo:nil
  122. repeats:YES];
  123. [self.countdownTimer fire];
  124. [hud hideAnimated:YES afterDelay:1.f];
  125. } else {
  126. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  127. hud.mode = MBProgressHUDModeText;
  128. hud.label.text = @"发送失败";
  129. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  130. [hud hideAnimated:YES afterDelay:1.f];
  131. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  132. [self.sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  133. self.sendCodeBtn.enabled = YES;
  134. });
  135. }
  136. });
  137. }
  138. - (void)resetKeyboard:(id)sender {
  139. [self.passwordField resignFirstResponder];
  140. self.passwordLine.backgroundColor = [UIColor grayColor];
  141. }
  142. - (void)onLoginButton:(id)sender {
  143. NSString *password = self.passwordField.text;
  144. if (!password.length) {
  145. return;
  146. }
  147. [self resetKeyboard:nil];
  148. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  149. hud.label.text = @"登录中...";
  150. [hud showAnimated:YES];
  151. [[AppService sharedAppService] destroyAccount:password success:^{
  152. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedName"];
  153. [SSKeychain deletePasswordForWFService:@"savedToken"];
  154. [SSKeychain deletePasswordForWFService:@"savedUserId"];
  155. [[AppService sharedAppService] clearAppServiceAuthInfos];
  156. [[OrgService sharedOrgService] clearOrgServiceAuthInfos];
  157. [[NSUserDefaults standardUserDefaults] synchronize];
  158. //服务器已经删除所有信息了,这里都传NO。不能传YES,如果传YES协议栈会需要跟IM服务进行交互。
  159. [[WFCCNetworkService sharedInstance] disconnect:NO clearSession:NO];
  160. } error:^(int errCode, NSString * _Nonnull message) {
  161. NSLog(@"login error with code %d, message %@", errCode, message);
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. [hud hideAnimated:YES];
  164. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  165. hud.mode = MBProgressHUDModeText;
  166. hud.label.text = @"登录失败";
  167. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  168. [hud hideAnimated:YES afterDelay:1.f];
  169. });
  170. }];
  171. }
  172. #pragma mark - UITextFieldDelegate
  173. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  174. if(textField == self.passwordField) {
  175. [self onLoginButton:nil];
  176. }
  177. return NO;
  178. }
  179. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  180. if (textField == self.passwordField) {
  181. self.passwordLine.backgroundColor = [UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9];
  182. }
  183. return YES;
  184. }
  185. #pragma mark - UITextInputDelegate
  186. - (void)textDidChange:(id<UITextInput>)textInput {
  187. if (textInput == self.passwordField) {
  188. [self updateBtn];
  189. }
  190. }
  191. - (void)updateBtn {
  192. if (!self.countdownTimer) {
  193. self.sendCodeBtn.enabled = YES;
  194. [self.sendCodeBtn setTitleColor:[UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9] forState:UIControlStateNormal];
  195. self.sendCodeBtn.layer.borderColor = [UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9].CGColor;
  196. } else {
  197. self.sendCodeBtn.enabled = NO;
  198. self.sendCodeBtn.layer.borderColor = [UIColor colorWithHexString:@"0x191919"].CGColor;
  199. [self.sendCodeBtn setTitleColor:[UIColor colorWithHexString:@"0x171717"] forState:UIControlStateNormal];
  200. [self.sendCodeBtn setTitleColor:[UIColor colorWithHexString:@"0x171717"] forState:UIControlStateSelected];
  201. }
  202. if ([self isValidCode]) {
  203. [self.loginBtn setBackgroundColor:[UIColor colorWithRed:0.1 green:0.27 blue:0.9 alpha:0.9]];
  204. self.loginBtn.enabled = YES;
  205. } else {
  206. [self.loginBtn setBackgroundColor:[UIColor grayColor]];
  207. self.loginBtn.enabled = NO;
  208. }
  209. }
  210. - (BOOL)isValidCode {
  211. if (self.passwordField.text.length >= 4) {
  212. return YES;
  213. } else {
  214. return NO;
  215. }
  216. }
  217. @end