PhoneLoginViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // PhoneLoginViewController.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/1/16.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "PhoneLoginViewController.h"
  9. #import "LPhoneRegistVC.h"
  10. @interface PhoneLoginViewController ()<UITextFieldDelegate,MBProgressHUDDelegate>{
  11. NSTimer *_timer; //定时器
  12. int _timeCount; //定时器时间
  13. NSString *_verify_url; //图形验证码
  14. }
  15. @property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
  16. @property (weak, nonatomic) IBOutlet UITextField *codeTextField;
  17. @property (weak, nonatomic) IBOutlet UIButton *codeBtn;
  18. @property (weak, nonatomic) IBOutlet UIButton *loginBtn;
  19. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  20. @end
  21. @implementation PhoneLoginViewController
  22. - (void)awakeFromNib{
  23. [super awakeFromNib];
  24. }
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28. self.view.backgroundColor = kBackGroundColor;
  29. }
  30. - (void)viewWillAppear:(BOOL)animated
  31. {
  32. [super viewWillAppear:animated];
  33. [IQKeyboardManager sharedManager].enable = YES;
  34. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  35. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  36. [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 5.0f;
  37. [self.navigationController setNavigationBarHidden:YES animated:animated];
  38. // [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
  39. }
  40. - (void)viewWillDisappear:(BOOL)animated
  41. {
  42. [super viewWillDisappear:animated];
  43. [IQKeyboardManager sharedManager].enable = NO;
  44. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  45. if (_timer) {
  46. [_timer invalidate];
  47. _timer = nil;
  48. }
  49. }
  50. - (void)initFWUI
  51. {
  52. [super initFWUI];
  53. _timeCount = 60;
  54. if (self.LNSecBPhone||self.LSecBPhone)
  55. {
  56. self.titleLabel.text = ASLocalizedString(@"手机绑定");
  57. [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"lg_btn_bind_nor"] forState:UIControlStateNormal];
  58. [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"lg_btn_bind_sel"] forState:UIControlStateSelected];
  59. }else
  60. {
  61. self.titleLabel.text = ASLocalizedString(@"手机登录");
  62. }
  63. self.phoneTextField.delegate = self;
  64. self.codeTextField.delegate = self;
  65. [self.phoneTextField becomeFirstResponder];
  66. [self.phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  67. [self.codeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  68. }
  69. - (void)initFWData
  70. {
  71. [super initFWData];
  72. }
  73. - (IBAction)backBtnAction:(id)sender {
  74. [self myFiledResignFirstResP];
  75. [self.navigationController popViewControllerAnimated:YES];
  76. }
  77. #pragma mark 键盘回退
  78. - (void)myFiledResignFirstResP
  79. {
  80. [_phoneTextField resignFirstResponder];
  81. [_codeTextField resignFirstResponder];
  82. }
  83. #pragma mark 发验证码的倒计时
  84. - (void)timego
  85. {
  86. [self timerDec:_timeCount];
  87. }
  88. - (void)timerDec:(NSInteger)time
  89. {
  90. if(time > 0)
  91. {
  92. [self.codeBtn setTitle:[NSString stringWithFormat:ASLocalizedString(@"%ld秒"),(long)time] forState:UIControlStateDisabled];
  93. _timeCount --;
  94. }else if(time == 0)
  95. {
  96. self.codeBtn.enabled = YES;
  97. [self.codeBtn setTitle:[NSString stringWithFormat:ASLocalizedString(@"发验证码")] forState:UIControlStateNormal];
  98. [_timer invalidate];
  99. _timeCount = 60;
  100. }
  101. }
  102. #pragma mark ============UITextFieldDelegate===================
  103. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  104. {
  105. [self myFiledResignFirstResP];
  106. }
  107. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  108. {
  109. if (textField == self.phoneTextField)
  110. {
  111. if (![string isNumber])
  112. {
  113. return NO;
  114. }
  115. if (string.length == 0) return YES;
  116. NSInteger existedLength = textField.text.length;
  117. NSInteger selectedLength = range.length;
  118. NSInteger replaceLength = string.length;
  119. if (existedLength - selectedLength + replaceLength > 11)
  120. {
  121. return NO;
  122. }
  123. }
  124. if (textField == self.codeTextField)
  125. {
  126. if (![string isNumber])
  127. {
  128. return NO;
  129. }
  130. if (string.length == 0) return YES;
  131. NSInteger existedLength = textField.text.length;
  132. NSInteger selectedLength = range.length;
  133. NSInteger replaceLength = string.length;
  134. if (existedLength - selectedLength + replaceLength > 4)
  135. {
  136. return NO;
  137. }
  138. }
  139. return YES;
  140. }
  141. - (void)textFieldDidChange:(UITextField *)textField{
  142. if (textField == self.phoneTextField) {
  143. self.codeBtn.enabled = (textField.text.length == 11);
  144. }
  145. if (textField == self.codeTextField) {
  146. if (self.phoneTextField.text.length == 11) {
  147. self.loginBtn.enabled = (textField.text.length == 4);
  148. }
  149. }
  150. }
  151. - (IBAction)codeBtnAction:(id)sender {
  152. if (self.phoneTextField.text.length < 1)
  153. {
  154. [FanweMessage alert:ASLocalizedString(@"手机号限制11位数!")];
  155. return;
  156. }
  157. self.codeBtn.enabled = NO;
  158. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timego) userInfo:nil repeats:YES];
  159. [self timego];
  160. if (self.LSecBPhone || self.LNSecBPhone)
  161. {
  162. FWWeakify(self)
  163. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  164. [parmDict setObject:@"login" forKey:@"ctl"];
  165. [parmDict setObject:@"send_mobile_verify_international" forKey:@"act"];
  166. [parmDict setObject:@"1" forKey:@"wx_binding"];
  167. [parmDict setObject:self.phoneTextField.text forKey:@"mobile"];
  168. [self showMyHud];
  169. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  170. {
  171. FWStrongify(self)
  172. [self hideMyHud];
  173. [GlobalVariables sharedInstance].otp_message_id = [NSString stringWithFormat:@"%@",[responseJson toString:@"otp_message_id"]];
  174. if ([responseJson toInt:@"status"] != 1)
  175. {
  176. self.codeBtn.enabled = YES;
  177. [self.codeBtn setTitle:ASLocalizedString(@"发验证码")forState:UIControlStateNormal];
  178. [self->_timer invalidate];
  179. self->_timer = nil;
  180. }
  181. } FailureBlock:^(NSError *error) {
  182. FWStrongify(self)
  183. [self hideMyHud];
  184. [self->_timer invalidate];
  185. self->_timer = nil;
  186. [self.codeBtn setTitle:ASLocalizedString(@"发验证码")forState:UIControlStateNormal];
  187. [FanweMessage alert:ASLocalizedString(@"发送失败")];
  188. self.codeBtn.enabled = YES;
  189. }];
  190. }
  191. else
  192. {
  193. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  194. [parmDict setObject:@"login" forKey:@"ctl"];
  195. [parmDict setObject:@"send_mobile_verify_international" forKey:@"act"];
  196. [parmDict setObject:self.phoneTextField.text forKey:@"mobile"];
  197. if (_verify_url.length > 1 && self.codeTextField.text.length)
  198. {
  199. [parmDict setObject:self.codeTextField.text forKey:@"image_code"];
  200. }
  201. FWWeakify(self)
  202. [self showMyHud];
  203. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  204. {
  205. FWStrongify(self)
  206. [self hideMyHud];
  207. [GlobalVariables sharedInstance].otp_message_id = [NSString stringWithFormat:@"%@",[responseJson toString:@"otp_message_id"]];
  208. if ([responseJson toInt:@"status"] != 1)
  209. {
  210. self.codeBtn.enabled = YES;
  211. [self.codeBtn setTitle:ASLocalizedString(@"发验证码")forState:UIControlStateNormal];
  212. [self->_timer invalidate];
  213. self->_timer = nil;
  214. [self.phoneTextField resignFirstResponder];
  215. }
  216. } FailureBlock:^(NSError *error)
  217. {
  218. [self hideMyHud];
  219. self.codeBtn.enabled = YES;
  220. [self->_timer invalidate];
  221. self->_timer = nil;
  222. [self.codeBtn setTitle:ASLocalizedString(@"发验证码")forState:UIControlStateNormal];
  223. }];
  224. }
  225. }
  226. - (IBAction)loginBtnAction:(id)sender {
  227. if (self.phoneTextField.text.length < 1) {
  228. [FanweMessage alert:ASLocalizedString(@"手机号限制11位数!")];
  229. return;
  230. }
  231. if (self.codeTextField.text.length < 1)
  232. {
  233. [FanweMessage alert:ASLocalizedString(@"请输入验证码")];
  234. return;
  235. }
  236. if (self.LNSecBPhone || self.LSecBPhone)
  237. {
  238. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  239. [parmDict setObject:@"settings" forKey:@"ctl"];
  240. if (self.LNSecBPhone)
  241. {
  242. [parmDict setObject:@"mobile_login" forKey:@"act"];
  243. }
  244. if (self.LSecBPhone)
  245. {
  246. [parmDict setObject:@"mobile_binding" forKey:@"act"];
  247. }
  248. [parmDict setObject:self.phoneTextField.text forKey:@"mobile"];
  249. [parmDict setObject:self.codeTextField.text forKey:@"verify_code"];
  250. if (self.loginId.length)
  251. {
  252. [parmDict setObject:self.loginId forKey:@"openid"];
  253. }
  254. if (self.loginType.length)
  255. {
  256. [parmDict setObject:self.loginType forKey:@"login_type"];
  257. }
  258. if (self.accessToken.length)
  259. {
  260. [parmDict setObject:self.accessToken forKey:@"access_token"];
  261. }
  262. FWWeakify(self)
  263. [self showMyHud];
  264. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  265. {
  266. FWStrongify(self)
  267. if ([responseJson toInt:@"status"] == 1)
  268. {
  269. if (self.LSecBPhone)
  270. {
  271. [[AppDelegate sharedAppDelegate]popViewController];
  272. }else
  273. {
  274. [BGIMLoginManager sharedInstance].loginParam.identifier = [responseJson toString:@"data"];
  275. [BGIMLoginManager sharedInstance].loginParam.isAgree = [responseJson toInt:@"is_agree"];
  276. self.BuguLive.appModel.first_login = [responseJson toString:@"first_login"];
  277. self.BuguLive.appModel.new_level = [responseJson toInt:@"new_level"];
  278. self.BuguLive.appModel.login_send_score = [responseJson toString:@"login_send_score"];
  279. [[BGIMLoginManager sharedInstance] getUserSig:^{
  280. [[AppDelegate sharedAppDelegate] enterMainUI];
  281. [self hideMyHud];
  282. } failed:^(int errId, NSString *errMsg) {
  283. [self hideMyHud];
  284. }];
  285. }
  286. }else
  287. {
  288. [self hideMyHud];
  289. }
  290. } FailureBlock:^(NSError *error) {
  291. FWStrongify(self)
  292. [self hideMyHud];
  293. [FanweMessage alert:ASLocalizedString(@"验证码过程出错,请重新尝试")];
  294. }];
  295. }
  296. else
  297. {
  298. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  299. [parmDict setObject:@"login" forKey:@"ctl"];
  300. [parmDict setObject:@"do_login" forKey:@"act"];
  301. [parmDict setObject:self.phoneTextField.text forKey:@"mobile"];
  302. [parmDict setObject:self.codeTextField.text forKey:@"verify_coder"];
  303. FWWeakify(self)
  304. [self showMyHud];
  305. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  306. {
  307. FWStrongify(self)
  308. [self hideMyHud];
  309. if ([responseJson toInt:@"status"] == 1)
  310. {
  311. self.BuguLive.appModel.new_level = [responseJson toInt:@"new_level"];
  312. self.BuguLive.appModel.first_login = [responseJson toString:@"first_login"];
  313. self.BuguLive.appModel.login_send_score = [responseJson toString:@"login_send_score"];
  314. if ([responseJson toInt:@"is_lack"] == 1)
  315. {
  316. LPhoneRegistVC *phoneRegist = [[LPhoneRegistVC alloc]init];
  317. phoneRegist.used_id = [responseJson toString:@"user_id"];
  318. phoneRegist.userInfoDic = responseJson[@"user_info"];
  319. phoneRegist.userName = [responseJson[@"user_info"] toString:@"nick_name"];
  320. [[AppDelegate sharedAppDelegate]pushViewController:phoneRegist animated:YES];
  321. }
  322. else
  323. {
  324. [self showMyHud];
  325. [BGIMLoginManager sharedInstance].loginParam.identifier = [responseJson toString:@"user_id"];
  326. [BGIMLoginManager sharedInstance].loginParam.isAgree = [responseJson toInt:@"is_agree"];
  327. [[BGIMLoginManager sharedInstance] getUserSig:^{
  328. [[AppDelegate sharedAppDelegate] enterMainUI];
  329. [self hideMyHud];
  330. } failed:^(int errId, NSString *errMsg) {
  331. [self hideMyHud];
  332. }];
  333. }
  334. }
  335. } FailureBlock:^(NSError *error)
  336. {
  337. FWStrongify(self)
  338. [self hideMyHud];
  339. }];
  340. }
  341. }
  342. /*
  343. #pragma mark - Navigation
  344. // In a storyboard-based application, you will often want to do a little preparation before navigation
  345. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  346. // Get the new view controller using [segue destinationViewController].
  347. // Pass the selected object to the new view controller.
  348. }
  349. */
  350. @end