YRLoginVC.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // YRLoginVC.m
  3. // UU_Ent
  4. //
  5. // Created by huangyp-pc on 2019/4/29.
  6. // Copyright © 2019 UAS. All rights reserved.
  7. //
  8. #import "YRLoginVC.h"
  9. #import "YRUserInfo.h"
  10. #import <GRPCClient/GRPCCall+Tests.h>
  11. #import <UU_Ent/Account.pbrpc.h>
  12. #import <MBProgressHUD/MBProgressHUD.h>
  13. @interface YRLoginVC ()<UITextFieldDelegate>
  14. @property (nonatomic,strong) UIScrollView *scrollView;
  15. @property (nonatomic,strong) UITextField *passwordTF;
  16. @property (nonatomic,strong) UITextField *phoneTF;
  17. @property (nonatomic,strong) YRButton *loginBtn;
  18. @property (nonatomic,strong) UIImageView *headImageV;
  19. @end
  20. @implementation YRLoginVC
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. }
  25. #pragma mark --- 登录操作
  26. -(void)loginAction
  27. {
  28. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  29. [GRPCCall useInsecureConnectionsForHost:kHostAddress];
  30. AccountService *client = [[AccountService alloc]initWithHost:kHostAddress];
  31. AccountSigninRequest *request = [AccountSigninRequest message];
  32. DeviceInfo *model = [DeviceInfo new];
  33. model.os = DeviceInfo_OS_Ios;
  34. request.deviceInfo = model;
  35. request.mobile = _phoneTF.text;
  36. request.password = _passwordTF.text;
  37. [client signinWithRequest:request handler:^(AccountSigninResponse * _Nullable response, NSError * _Nullable error) {
  38. [MBProgressHUD hideHUDForView:self.view animated:YES];
  39. if (!error) {
  40. NSDictionary *resDic = response.mj_keyValues;
  41. [YRUserInfo shareManager].token = response.authedToken.token;
  42. [YRUserInfo shareManager].userInfo = resDic;
  43. self.completed(YES);
  44. }else{
  45. }
  46. }];
  47. }
  48. #pragma mark --- UITextField Delegate
  49. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  50. {
  51. [self.view endEditing:YES];
  52. return YES;
  53. }
  54. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  55. {
  56. if ([string isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return
  57. //在这里做你响应return键的代码
  58. [textField resignFirstResponder];
  59. return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
  60. }
  61. NSString *allText = [textField.text stringByReplacingCharactersInRange:range withString:string];
  62. [self changeRegisterBtn:allText and:textField];
  63. return YES;
  64. }
  65. -(BOOL)textFieldShouldClear:(UITextField *)textField
  66. {
  67. _loginBtn.enabled = NO;
  68. return YES;
  69. }
  70. - (void)changeRegisterBtn:(NSString *)text and:(UITextField *)textField
  71. {
  72. if (textField == _phoneTF) {
  73. if (_passwordTF.text.length!=0 && text.length!=0) {
  74. _loginBtn.enabled = YES;
  75. }else
  76. _loginBtn.enabled = NO;
  77. }
  78. if (textField == _passwordTF) {
  79. if (_phoneTF.text.length!=0 && text.length!=0) {
  80. _loginBtn.enabled = YES;
  81. }else
  82. _loginBtn.enabled = NO;
  83. }
  84. }
  85. #pragma mark --- 注意:事件类型是:`UIControlEventEditingChanged`
  86. -(void)passConTextChange:(id)sender{
  87. // UITextField* target = (UITextField*)sender;
  88. // if (target.text.length == 11) {
  89. // if ([target.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:kMY_USER_LoginName]]) {
  90. // [g_server getHeadImageSmall:g_server.myself.userId imageView:_headImage];
  91. // }else{
  92. // _headImage.image = [UIImage imageNamed:@"UU_newPublicPic"];
  93. // }
  94. // }else{
  95. // _headImage.image = [UIImage imageNamed:@"UU_newPublicPic"];
  96. // }
  97. }
  98. -(void)pwdTextChanged:(id)sender {
  99. UITextField* target = (UITextField*)sender;
  100. [self changeRegisterBtn:target.text and:target];
  101. }
  102. #pragma mark --- 设置UI
  103. -(void)setUpUI
  104. {
  105. [self.view addSubview:self.scrollView];
  106. [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.top.left.right.equalTo(self.view);
  108. make.width.mas_equalTo(self_width);
  109. make.height.mas_equalTo(self_height);
  110. }];
  111. UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
  112. bgView.image = [UIImage imageNamed:@"UU_newpg5-log in"];
  113. [self.scrollView addSubview:bgView];
  114. [self.scrollView addSubview:self.headImageV];
  115. UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headImageV.frame)+50, self_width, 50)];
  116. firstView.backgroundColor = [UIColor clearColor];
  117. [self.scrollView addSubview:firstView];
  118. UIView *secondView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(firstView.frame)+2, self_width, 50)];
  119. secondView.backgroundColor = [UIColor clearColor];
  120. [self.scrollView addSubview:secondView];
  121. UIImageView *account = [[UIImageView alloc]initWithFrame:CGRectMake(31, 15, 20, 20)];
  122. account.image = [UIImage imageNamed:@"UU_newLoginAccount"];
  123. [firstView addSubview:account];
  124. UIImageView *password = [[UIImageView alloc]initWithFrame:CGRectMake(31, 15, 20, 20)];
  125. password.image = [UIImage imageNamed:@"UU_newLoginPassword"];
  126. [secondView addSubview:password];
  127. UIImageView *line1 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 48, self_width-40, 2)];
  128. line1.backgroundColor = HexColor(@"#939393");
  129. [firstView addSubview:line1];
  130. UIImageView *line2 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 48, self_width-40, 2)];
  131. line2.backgroundColor = HexColor(@"#939393");
  132. [secondView addSubview:line2];
  133. _phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(account.frame)+14, 0, self_width-90, 50)];
  134. _phoneTF.delegate = self;
  135. _phoneTF.autocorrectionType = UITextAutocorrectionTypeNo;
  136. _phoneTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
  137. _phoneTF.enablesReturnKeyAutomatically = YES;
  138. _phoneTF.returnKeyType = UIReturnKeyDone;
  139. UIButton *button1 = [_phoneTF valueForKey:@"_clearButton"];
  140. [button1 setImage:[UIImage imageNamed:@"UU_newDELETE"] forState:UIControlStateNormal];
  141. _phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  142. _phoneTF.placeholder = @"登录";
  143. _phoneTF.userInteractionEnabled = YES;
  144. _phoneTF.backgroundColor = [UIColor clearColor];
  145. _phoneTF.textColor = HexColor(@"#FFFFFF");
  146. _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
  147. _phoneTF.font = [UIFont systemFontOfSize:18];
  148. [_phoneTF setValue:HexColor(@"#FFFFFF") forKeyPath:@"_placeholderLabel.textColor"];
  149. [firstView addSubview:_phoneTF];
  150. [_phoneTF addTarget:self action:@selector(passConTextChange:) forControlEvents:UIControlEventEditingChanged];
  151. _phoneTF.text = @"15018524229";
  152. _passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(password.frame)+14, 0, self_width-90, 50)];
  153. _passwordTF.delegate = self;
  154. _passwordTF.autocorrectionType = UITextAutocorrectionTypeNo;
  155. _passwordTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
  156. _passwordTF.enablesReturnKeyAutomatically = YES;
  157. _passwordTF.returnKeyType = UIReturnKeyDone;
  158. UIButton *button2 = [_passwordTF valueForKey:@"_clearButton"];
  159. [button2 setImage:[UIImage imageNamed:@"UU_newDELETE"] forState:UIControlStateNormal];
  160. _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  161. _passwordTF.placeholder = @"密码";
  162. _passwordTF.secureTextEntry = YES;
  163. _passwordTF.userInteractionEnabled = YES;
  164. _passwordTF.backgroundColor = [UIColor clearColor];
  165. _passwordTF.textColor = HexColor(@"#FFFFFF");
  166. _passwordTF.font = [UIFont systemFontOfSize:18];
  167. [_passwordTF setValue:HexColor(@"#FFFFFF") forKeyPath:@"_placeholderLabel.textColor"];
  168. [secondView addSubview:_passwordTF];
  169. [_passwordTF addTarget:self action:@selector(pwdTextChanged:) forControlEvents:UIControlEventAllEditingEvents|UIControlEventValueChanged];
  170. _passwordTF.text = @"111111";
  171. self.loginBtn = [[YRButton alloc]initWithTitle:@"登录" titleColor:nil titleFont:18 buttonNormalColor:nil buttonSelectColor:nil buttonDiabledColor:nil];
  172. self.loginBtn.frame = CGRectMake(20, CGRectGetMaxY(secondView.frame)+25, self_width-40, 40);
  173. [self.scrollView addSubview:self.loginBtn];
  174. [self.loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
  175. }
  176. #pragma mark --- lazy
  177. -(UIScrollView *)scrollView{
  178. if (!_scrollView) {
  179. _scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
  180. _scrollView.backgroundColor = [UIColor redColor];
  181. }
  182. return _scrollView;
  183. }
  184. -(UIImageView *)headImageV
  185. {
  186. if (!_headImageV) {
  187. _headImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
  188. _headImageV.layer.masksToBounds = YES;
  189. _headImageV.layer.cornerRadius = 35;
  190. _headImageV.layer.borderColor = [UIColor whiteColor].CGColor;
  191. _headImageV.layer.borderWidth = 3;
  192. _headImageV.center = CGPointMake(self_width/2, 81);
  193. }
  194. return _headImageV;
  195. }
  196. @end