LoginViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // LoginViewController.m
  3. // AICity
  4. //
  5. // Created by 刘伟伟 on 2023/7/13.
  6. // Copyright © 2023 wei.z. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "CommonBarView.h"
  10. #import "AppDelegate.h"
  11. #import "CTTabbarController.h"
  12. #import "ComonWKWebViewController.h"
  13. @interface LoginViewController ()<UITextViewDelegate>
  14. @property(nonatomic,strong)UIImageView *loadNetView;
  15. @property(nonatomic,strong)inputView *phoneInput;
  16. @property(nonatomic,strong)inputView *codeInput;
  17. @property(nonatomic,strong)UIButton *loginButton;
  18. @property(nonatomic,strong)UIButton *agreeButton;
  19. @end
  20. @implementation LoginViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. // [self.view addSubview:self.customerbar];
  25. [self setupUI];
  26. self.view.backgroundColor = rgba(18, 23, 41, 1);
  27. //https://www.apple.com/v/home/bc/images/logos/iphone-tradein/logo_tradein__d1fpktgipvki_large.png
  28. self.loadNetView = [UIImageView new];
  29. [self.loadNetView sd_setImageWithURL:[NSURL URLWithString:@"https://www.apple.com/v/home/bc/images/logos/iphone-tradein/logo_tradein__d1fpktgipvki_large.png"]];
  30. }
  31. -(UIStatusBarStyle)preferredStatusBarStyle{
  32. return UIStatusBarStyleLightContent;
  33. }
  34. -(void)loginAct{
  35. [self.view endEditing:YES];
  36. // 临时注释协议检查,方便测试
  37. // if(!self.agreeButton.isSelected){
  38. // [MBProgressHUD showMessage:@"请仔细阅读并同意《用户协议》和《隐私政策》"];
  39. // return;
  40. // }
  41. NSString *account = self.phoneInput.textField.text ?: @"";
  42. NSString *password = self.codeInput.textField.text ?: @"";
  43. if(account.length == 0 || password.length == 0){
  44. [MBProgressHUD showMessage:@"请输入手机号和验证码"];
  45. return;
  46. }
  47. NSLog(@"[LoginViewController] 准备发送登录请求: account=%@", account);
  48. [RequestTool post:@"user/userLogin" params:@{@"account":account,@"password":password} success:^(id responseObject) {
  49. NSLog(@"user/userLogin-%@",responseObject);
  50. [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"] forKey:@"existUserData"];
  51. AppDelegate *myDelegate =(AppDelegate *) [[UIApplication sharedApplication] delegate];
  52. CTTabbarController *vtab=[[CTTabbarController alloc] init];
  53. myDelegate.window.rootViewController=vtab;
  54. } failure:^(NSError *error) {
  55. NSLog(@"%@",error);
  56. }];
  57. }
  58. -(void)setupUI{
  59. UILabel *n = [UILabel new];
  60. n.text = @"手机号登录";
  61. n.font = [UIFont systemFontOfSize:24];
  62. n.textColor = rgba(255, 255, 255, 1);
  63. [self.view addSubview:n];
  64. [n mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.left.equalTo(self.view).offset(40);
  66. make.top.equalTo(self.view).offset(152);
  67. }];
  68. self.phoneInput = [inputView new];
  69. self.phoneInput.type = 0;
  70. [self.view addSubview:self.phoneInput];
  71. [self.phoneInput mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.left.right.equalTo(self.view);
  73. make.top.equalTo(n.mas_bottom).offset(96);
  74. make.height.mas_equalTo(@35);
  75. }];
  76. self.codeInput = [inputView new];
  77. self.codeInput.type = 1;
  78. self.codeInput.tapBlock = ^{
  79. };
  80. [self.view addSubview:self.codeInput];
  81. [self.codeInput mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.left.right.equalTo(self.view);
  83. make.top.equalTo(self.phoneInput.mas_bottom).offset(37);
  84. make.height.mas_equalTo(@35);
  85. }];
  86. UILabel *lab = [UILabel new];
  87. lab.text = @"忘记密码?";
  88. lab.textColor = rgba(255, 255, 255, 0.4);
  89. lab.font = [UIFont systemFontOfSize:12];
  90. [self.view addSubview:lab];
  91. lab.hidden = true;
  92. [lab mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.right.equalTo(self.view).offset(-40);
  94. make.top.equalTo(self.codeInput.mas_bottom).offset(8);
  95. }];
  96. UIButton *btn = [UIButton new];
  97. [btn setBackgroundImage:[UIImage imageNamed:@"Rectangle 19465"] forState:UIControlStateNormal];
  98. [btn setTitle:@"登录" forState:UIControlStateNormal];
  99. [btn setTitleColor:rgba(18, 23, 41, 0.5) forState:UIControlStateNormal];
  100. [btn setTitleColor:rgba(18, 23, 41, 0.3) forState:UIControlStateDisabled];
  101. btn.titleLabel.font = [UIFont systemFontOfSize:16];
  102. [btn addTarget:self action:@selector(loginAct) forControlEvents:UIControlEventTouchUpInside];
  103. [self.view addSubview:btn];
  104. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.left.equalTo(self.view).offset(40);
  106. make.right.equalTo(self.view).offset(-40);
  107. make.height.mas_equalTo(@48);
  108. make.top.equalTo(self.codeInput.mas_bottom).offset(56);
  109. }];
  110. btn.enabled = NO;
  111. btn.alpha = 0.5;
  112. self.loginButton = btn;
  113. [self.phoneInput.textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
  114. [self.codeInput.textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
  115. self.phoneInput.textField.keyboardType = UIKeyboardTypeNumberPad;
  116. self.codeInput.textField.keyboardType = UIKeyboardTypeNumberPad;
  117. NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"已仔细阅读并同意"];
  118. [attributedStr addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.4) range:NSMakeRange(0, attributedStr.length)];
  119. [attributedStr addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr.length)];
  120. NSMutableAttributedString *attributedStr2 = [[NSMutableAttributedString alloc] initWithString:@"《用户协议》"];
  121. [attributedStr2 addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 1) range:NSMakeRange(0, attributedStr2.length)];
  122. [attributedStr2 addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr2.length)];
  123. NSURL *baiduURL = [NSURL URLWithString:@"privateProtocal://www.com"];
  124. [attributedStr2 addAttribute:NSLinkAttributeName value:baiduURL range:NSMakeRange(0, attributedStr2.length)];
  125. NSMutableAttributedString *attributedStr3 = [[NSMutableAttributedString alloc] initWithString:@"和"];
  126. [attributedStr3 addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.4) range:NSMakeRange(0, attributedStr3.length)];
  127. [attributedStr3 addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr3.length)];
  128. NSMutableAttributedString *attributedStr4 = [[NSMutableAttributedString alloc] initWithString:@"《隐私政策》"];
  129. [attributedStr4 addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 1) range:NSMakeRange(0, attributedStr4.length)];
  130. [attributedStr4 addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr4.length)];
  131. NSURL *baiduURL2 = [NSURL URLWithString:@"privacyPolicy://www.com"];
  132. [attributedStr4 addAttribute:NSLinkAttributeName value:baiduURL2 range:NSMakeRange(0, attributedStr4.length)];
  133. [attributedStr appendAttributedString:attributedStr2];
  134. [attributedStr appendAttributedString:attributedStr3];
  135. [attributedStr appendAttributedString:attributedStr4];
  136. UITextView *lab2 = [UITextView new];
  137. lab2.backgroundColor = [UIColor clearColor];
  138. lab2.attributedText = attributedStr;
  139. lab2.textAlignment = NSTextAlignmentCenter;
  140. lab2.linkTextAttributes = @{NSForegroundColorAttributeName:rgba(255, 255, 255, 1)};
  141. lab2.delegate = self;
  142. lab2.editable = NO;
  143. lab2.scrollEnabled = NO;
  144. // lab2.selectable = NO;
  145. // lab.textColor = rgba(255, 255, 255, 0.4);
  146. // lab.font = [UIFont systemFontOfSize:12];
  147. [self.view addSubview:lab2];
  148. [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.centerX.equalTo(self.view).offset(7);
  150. make.top.equalTo(btn.mas_bottom).offset(12);
  151. make.width.mas_equalTo(@270);
  152. // make.height.mas_equalTo(@15);
  153. }];
  154. UIButton *selbtn = [UIButton new];
  155. selbtn.tag = 130;
  156. [selbtn setBackgroundImage:[UIImage imageNamed:@"login_cirle"] forState:UIControlStateNormal];
  157. [selbtn setBackgroundImage:[UIImage imageNamed:@"login_chick"] forState:UIControlStateSelected];
  158. [self.view addSubview:selbtn];
  159. [selbtn addTarget:self action:@selector(changeSelState) forControlEvents:UIControlEventTouchUpInside];
  160. [selbtn mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.right.equalTo(lab2.mas_left).offset(-2);
  162. make.size.mas_equalTo(CGSizeMake(12, 12));
  163. make.centerY.equalTo(lab2);
  164. }];
  165. self.agreeButton = selbtn;
  166. [self updateLoginButtonState];
  167. }
  168. -(void)changeSelState{
  169. self.agreeButton.selected = !self.agreeButton.isSelected;
  170. [self updateLoginButtonState];
  171. }
  172. -(void)textFieldEditingChanged:(UITextField *)textField{
  173. [self updateLoginButtonState];
  174. }
  175. -(void)updateLoginButtonState{
  176. BOOL hasPhone = self.phoneInput.textField.text.length > 0;
  177. BOOL hasCode = self.codeInput.textField.text.length > 0;
  178. // 临时移除协议检查,方便测试
  179. BOOL canLogin = hasPhone && hasCode; // && self.agreeButton.isSelected;
  180. self.loginButton.enabled = canLogin;
  181. self.loginButton.alpha = canLogin ? 1.0 : 0.5;
  182. UIColor *titleColor = canLogin ? rgba(18, 23, 41, 1.0) : rgba(18, 23, 41, 0.5);
  183. [self.loginButton setTitleColor:titleColor forState:UIControlStateNormal];
  184. }
  185. -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction
  186. {
  187. if ([[URL scheme] isEqualToString:@"privateProtocal"]) {
  188. NSLog(@"看天用户协议");
  189. //用户协议
  190. ComonWKWebViewController *vc =[[ComonWKWebViewController alloc] init];
  191. vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
  192. vc.title = @"用户协议";
  193. vc.url = @"https://backend.jsnoopyay.com/user_agreement.html";
  194. [self presentViewController:vc animated:YES completion:nil];
  195. return NO;
  196. } else if ([[URL scheme] isEqualToString:@"privacyPolicy"]) {
  197. NSLog(@"隐私协议");
  198. //隐私协议
  199. ComonWKWebViewController *vc =[[ComonWKWebViewController alloc] init];
  200. vc.title = @"隐私协议";
  201. vc.url = @"https://backend.jsnoopyay.com/unregister.html";
  202. [self presentViewController:vc animated:YES completion:nil];
  203. return NO;
  204. }
  205. return YES;
  206. }
  207. //-(CommonBarView *)customerbar{
  208. // if(!_customerbar){
  209. // CGFloat h=0;
  210. // if(@available(iOS 13.0, *)){
  211. // UIStatusBarManager *sc= [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
  212. // CGRect f=sc.statusBarFrame;
  213. // h=f.size.height;
  214. // }else{
  215. // h=[UIApplication sharedApplication].statusBarFrame.size.height;
  216. // }
  217. // if(h>=40){
  218. // h=30;
  219. // }
  220. // _customerbar =[[CommonBarView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 72-20+h)];
  221. // _customerbar.bgView.backgroundColor = [UIColor clearColor];
  222. // _customerbar.label.text=@"手机号登录";
  223. // _customerbar.bgView.alpha = 1;
  224. // _customerbar.label.textColor = rgba(255, 255, 255, 1);
  225. // }
  226. // return _customerbar;
  227. //}
  228. @end
  229. @interface inputView()
  230. @property(nonatomic,strong)NSTimer *mytime;
  231. @property(nonatomic,assign)int second;
  232. @property(nonatomic,strong)UITextField *textField;
  233. @end
  234. @implementation inputView
  235. - (instancetype)init
  236. {
  237. self = [super init];
  238. if (self) {
  239. self.second = 60;
  240. }
  241. return self;
  242. }
  243. //0手机号 1密码
  244. -(void)setType:(int)type{
  245. _type = type;
  246. [self initView:type];
  247. }
  248. - (void)initView:(int)type{
  249. UIImageView *iv = [UIImageView new];
  250. if(type == 0){
  251. iv.image = [UIImage imageNamed:@"Casino"];
  252. }else{
  253. iv.image = [UIImage imageNamed:@"Casino2"];
  254. }
  255. [self addSubview:iv];
  256. [iv mas_makeConstraints:^(MASConstraintMaker *make) {
  257. make.left.equalTo(self).offset(40);
  258. make.top.equalTo(self);
  259. make.size.mas_equalTo(CGSizeMake(20, 20));
  260. }];
  261. UITextField *tf =[UITextField new];
  262. NSString *placeholder = type == 0 ? @"请输入手机号" : @"请输入验证码";
  263. NSMutableAttributedString *attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:placeholder];
  264. [attributedPlaceholder addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.2) range:NSMakeRange(0, attributedPlaceholder.length)];
  265. [attributedPlaceholder addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:14.0] range:NSMakeRange(0, attributedPlaceholder.length)];
  266. tf.attributedPlaceholder = attributedPlaceholder;
  267. tf.textColor = [UIColor whiteColor];
  268. tf.font = [UIFont italicSystemFontOfSize:14.0];
  269. [self addSubview:tf];
  270. self.textField = tf;
  271. if(type == 0){
  272. UILabel *lab = [UILabel new];
  273. lab.text = @"+86";
  274. lab.textColor = rgba(255, 255, 255, 0.4);
  275. lab.font = [UIFont systemFontOfSize:14];
  276. [self addSubview:lab];
  277. [lab mas_makeConstraints:^(MASConstraintMaker *make) {
  278. make.left.equalTo(iv.mas_right).offset(12);
  279. make.centerY.equalTo(iv);
  280. }];
  281. [tf mas_makeConstraints:^(MASConstraintMaker *make) {
  282. make.left.equalTo(lab.mas_right).offset(8);
  283. make.centerY.equalTo(iv);
  284. make.size.mas_equalTo(CGSizeMake(200, 20));
  285. }];
  286. }else{
  287. [tf mas_makeConstraints:^(MASConstraintMaker *make) {
  288. make.left.equalTo(iv.mas_right).offset(12);
  289. make.centerY.equalTo(iv);
  290. make.size.mas_equalTo(CGSizeMake(200, 20));
  291. }];
  292. UIButton *showP =[UIButton new];
  293. [showP setTitle:@"获取验证码" forState:UIControlStateNormal];
  294. [showP setTitleColor:rgba(255, 212, 0, 1) forState:UIControlStateNormal];
  295. showP.titleLabel.font = [UIFont systemFontOfSize:14];
  296. [showP addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
  297. [self addSubview:showP];
  298. [showP mas_makeConstraints:^(MASConstraintMaker *make) {
  299. make.right.equalTo(self).offset(-40);
  300. make.centerY.equalTo(iv);
  301. make.size.mas_equalTo(CGSizeMake(80, 20));
  302. }];
  303. }
  304. UIView *line = [UIView new];
  305. line.backgroundColor = rgba(255, 255, 255, 0.2);
  306. [self addSubview:line];
  307. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  308. make.left.equalTo(iv);
  309. make.right.equalTo(self).offset(-40);
  310. make.bottom.equalTo(self);
  311. make.height.mas_equalTo(@1);
  312. }];
  313. }
  314. -(void)getCode:(UIButton *)showP{
  315. if(self.mytime){
  316. return;
  317. }
  318. self.mytime = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
  319. [showP setTitle:[NSString stringWithFormat:@"%ds",self.second] forState:UIControlStateNormal];
  320. self.second -=1;
  321. if(self.second <= -1){
  322. [self.mytime invalidate];
  323. self.mytime = nil;
  324. self.second = 60;
  325. [showP setTitle:@"获取验证码" forState:UIControlStateNormal];
  326. }
  327. }];
  328. if(self.tapBlock){
  329. self.tapBlock();
  330. }
  331. }
  332. @end