developer
2023-10-31 c2c0b4f016502c4d1e3a3d63854d800521374f6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
//  RegisteViewController.m
//  BuWanVideo2.0
//
//  Created by apple on 16/10/9.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "RegisteViewController.h"
#import "SettingWebView.h"
@interface RegisteViewController (){
    UITextField * FillInTheAccountNumber;
    UITextField * FillInThePassWord;
    UITextField * FillInTheVerificationCode;
    UIButton * agreeBtn;
    UIButton * registerBtn;
    UIButton * VerificationCode;
    BOOL End_the_countdown;//这是一个在倒计时部分会用到的一个变量,用于判断倒计时是自己结束的,还是验证码请求失败,我们手动结束的倒计时,如果是自动结束的为Yes,手动结束的为NO,默认为NO值,所以需要在请求验证码成功的时候,改变该值。
}
 
@end
 
@implementation RegisteViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"注册";
    self.view.backgroundColor=[UIColor whiteColor];
    if(@available(iOS 13.0,*)){
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    
    //设置账号登录的字体样式
    self.navigationController.navigationBar.titleTextAttributes =@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]};
    
    //设置返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 32, 32);
    UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = iconBarItem;
    
    //设置使用模态跳转的返回按钮
    if(_ispresent){
        //隐藏状态栏
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
        //添加返回按钮
        CGFloat height = 15;
        if (KIsiPhoneX) {
            height = 35;
        }
        UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(15, height, 32, 32)];
        [backButton setImage:[UIImage imageNamed:@"取消登录"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(presentBack) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backButton];
    }
    //设置背景色
    self.view.backgroundColor=KGlobalLightGreyColor_245;
    
    //填写账号
    FillInTheAccountNumber=[[UITextField alloc] init];
    //填写密码
    FillInThePassWord=[[UITextField alloc] init];
    //验证码框
    FillInTheVerificationCode=[[UITextField alloc] init];
    //获取验证码的按钮
    VerificationCode=[[UIButton alloc] init];
    //是否同意协议
    agreeBtn=[[UIButton alloc] init];
    //协议
    UIButton *agreeItem=[[UIButton alloc] init];
    
    if ([UIScreen mainScreen].bounds.size.height>480.0f) {//iphone4s以上
        //表头图片
        UIImageView *titleImage=[[UIImageView alloc] initWithFrame:CGRectMake(60, 85, KScreenW-120,(KScreenW-120)/394*171)];
        titleImage.image=[UIImage imageNamed:@"登录表头"];
        [self.view addSubview:titleImage];
        
        //填写账号
        FillInTheAccountNumber.frame=CGRectMake(50, 120+(KScreenW-120)/394*171, KScreenW-100, 35);
        //填写密码
        FillInThePassWord.frame=CGRectMake(50, 178+(KScreenW-120)/394*171, KScreenW-100, 35);
        //验证码框
        FillInTheVerificationCode.frame=CGRectMake(50, 236+(KScreenW-120)/394*171, 100, 35);
        //获取验证码的按钮
        VerificationCode.frame=CGRectMake(KScreenW-150, 236+(KScreenW-120)/394*171, 100, 35);
        //是否同意协议
        agreeBtn.frame=CGRectMake(60, 285+(KScreenW-120)/394*171, 14, 14);
        //协议
        agreeItem.frame=CGRectMake(75, 282+(KScreenW-120)/394*171, 160, 20);
    }else{
        //填写账号
        FillInTheAccountNumber.frame=CGRectMake(50, 120, KScreenW-100, 35);
        //填写密码
        FillInThePassWord.frame=CGRectMake(50, 178, KScreenW-100, 35);
        //验证码框
        FillInTheVerificationCode.frame=CGRectMake(50, 236, 100, 35);
        //获取验证码的按钮
        VerificationCode.frame=CGRectMake(KScreenW-150, 236, 100, 35);
        //是否同意协议
        agreeBtn.frame=CGRectMake(60, 285, 14, 14);
        //协议
        agreeItem.frame=CGRectMake(75, 282, 160, 20);
    }
    
    
    
    //填写账号
    [FillInTheAccountNumber.layer setMasksToBounds:YES];
    [FillInTheAccountNumber.layer setCornerRadius:5.0];
    [FillInTheAccountNumber setBackgroundColor:KGlobalLightGreyColor_255];
    [FillInTheAccountNumber.layer setBorderWidth:1.0];
    [FillInTheAccountNumber.layer setBorderColor:(kGlobalLightGreyColor_218.CGColor)];
    FillInTheAccountNumber.keyboardType=UIKeyboardTypeEmailAddress;
    FillInTheAccountNumber.autocorrectionType=UITextAutocorrectionTypeNo;
    FillInTheAccountNumber.placeholder=[NSString stringWithFormat:@"请输入注册邮箱"];
    //前面空一个间距
    [self setTextFieldLeftPadding:FillInTheAccountNumber forWidth:10.0];
    [self.view addSubview:FillInTheAccountNumber];
    
    //填写密码
    [FillInThePassWord.layer setMasksToBounds:YES];
    [FillInThePassWord.layer setCornerRadius:5.0];
    [FillInThePassWord setBackgroundColor:KGlobalLightGreyColor_255];
    [FillInThePassWord.layer setBorderWidth:1.0];
    [FillInThePassWord.layer setBorderColor:(kGlobalLightGreyColor_218.CGColor)];
    FillInThePassWord.placeholder=[NSString stringWithFormat:@"请输入密码"];
    FillInThePassWord.autocorrectionType=UITextAutocorrectionTypeNo;
    FillInThePassWord.autocapitalizationType=UITextAutocapitalizationTypeNone;
    //前面空一个间距
    [self setTextFieldLeftPadding:FillInThePassWord forWidth:10.0];
    [self.view addSubview:FillInThePassWord];
    
    //验证码框
    [FillInTheVerificationCode.layer setMasksToBounds:YES];
    [FillInTheVerificationCode.layer setCornerRadius:5.0];
    [FillInTheVerificationCode setBackgroundColor:KGlobalLightGreyColor_255];
    FillInTheVerificationCode.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    [FillInTheVerificationCode.layer setBorderWidth:1.0];
    [FillInTheVerificationCode.layer setBorderColor:(kGlobalLightGreyColor_218.CGColor)];
    FillInTheVerificationCode.placeholder=[NSString stringWithFormat:@"验证码"];
    //前面空一个间距
    [self setTextFieldLeftPadding:FillInTheVerificationCode forWidth:10.0];
    [self.view addSubview:FillInTheVerificationCode];
    
    //获取验证码的按钮
    [VerificationCode.layer setMasksToBounds:YES];
    [VerificationCode.layer setCornerRadius:5.0];
    [VerificationCode.layer setBorderWidth:1.0];
    [VerificationCode.layer setBorderColor:(kGlobalLightGreyColor_218.CGColor)];
    [VerificationCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    VerificationCode.titleLabel.font=[UIFont systemFontOfSize:15.0];
    [VerificationCode addTarget:self action:@selector(startTime:) forControlEvents:UIControlEventTouchUpInside];
    VerificationCode.backgroundColor=kGlobalYellowColor;
    [VerificationCode setTitleColor:kGlobalMainColor forState:UIControlStateNormal];
    [self.view addSubview:VerificationCode];
    
    //用户隐私协议
    //是否同意协议
    [agreeBtn setImage:[UIImage imageNamed:@"同意协议"] forState:UIControlStateNormal];
    [agreeBtn setImage:[UIImage imageNamed:@"同意协议1"] forState:UIControlStateSelected];
    [agreeBtn addTarget:self action:@selector(changeAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [agreeBtn setSelected:YES];
    [self.view addSubview:agreeBtn];
    
    //协议
    NSString *contentStr=[NSString stringWithFormat:@"用户使用协议&隐私条款"];
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    [attriStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(0, contentStr.length)];
    [attriStr addAttribute:NSUnderlineColorAttributeName value:kGlobalLightBlueColor range:NSMakeRange(0, contentStr.length)];
    [attriStr addAttribute:NSForegroundColorAttributeName value:kGlobalLightBlueColor range:NSMakeRange(0, contentStr.length)];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, contentStr.length)];
    
    [agreeItem setAttributedTitle:attriStr forState:UIControlStateNormal];
    [agreeItem addTarget:self action:@selector(LookItem) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:agreeItem];
    
    //注册按钮
    registerBtn=[[UIButton alloc] initWithFrame:CGRectMake(70, KScreenH-110-kNavigationBarH, KScreenW-140, 32)];
    [registerBtn.layer setMasksToBounds:YES];
    [registerBtn.layer setCornerRadius:5.0];
    registerBtn.backgroundColor=kGlobalYellowColor;
    [registerBtn setTitle:@"确 认" forState:UIControlStateNormal];
    [registerBtn setTitleColor:kGlobalMainColor forState:UIControlStateNormal];
    registerBtn.titleLabel.font=[UIFont systemFontOfSize:15.0];
    [registerBtn addTarget:self action:@selector(registerClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [FillInTheAccountNumber resignFirstResponder];
    [FillInThePassWord resignFirstResponder];
    [FillInTheVerificationCode resignFirstResponder];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
/**
 *  退出
 */
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
 
/**
 *  使用模态跳转的退出
 */
-(void)presentBack{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
 
 
/**
 获取验证码,倒计时
 
 @param sender 获取验证码的按钮
 */
-(void)startTime:(UIButton *)sender{
   
    if ([Share isEmailAddress:FillInTheAccountNumber.text]){
        if (![FillInTheAccountNumber.text containsString:@"@qq.com"] ) {
            [Share ShakeView:FillInTheAccountNumber WithInfomation:@"请使用QQ邮箱注册"];
        }else
            {
                __block int timeout=60; //倒计时时间
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
                dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
                
                dispatch_source_set_event_handler(_timer, ^{
                    if(timeout<=0){ //倒计时结束,关闭
                        dispatch_source_cancel(_timer);
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置(倒计时结束后调用)
                            if (End_the_countdown) {
                                [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
                            }
                            //设置可点击
                            sender.userInteractionEnabled = YES;
                            [sender setBackgroundColor:kGlobalYellowColor];
                        });
                    }else{
                        int seconds = timeout % 99;
                        NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [sender setTitle:[NSString stringWithFormat:@"重新发送(%@)",strTime] forState:UIControlStateNormal];
                            //设置不可点击
                            sender.userInteractionEnabled = NO;
                            [sender setBackgroundColor:[UIColor lightGrayColor]];
                        });
                        timeout--;
                    }
                });
                dispatch_resume(_timer);
                [[YTHNetInterface startInterface] getsendVerifyCodeWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" WithEmail:FillInTheAccountNumber.text withBlock:^(BOOL isSuccessful, id result, NSString *error) {
                    if (isSuccessful) {
                        //请求成功
                        End_the_countdown=isSuccessful;
                    }else{
                        timeout=3;
                        [Share animateButtonWithButton:sender WithTitle:error WithColor:[UIColor colorWithRed:49/255.0 green:39/255.0 blue:37/255.0 alpha:1]];
                        [Share delay:3 withBlack:^{
                            [sender setBackgroundColor:kGlobalYellowColor];
                            [Share animateButtonWithButton:sender WithTitle:@"获取验证码" WithColor:kGlobalMainColor];
                        }];
                    }
                }];
            }
    }
  else{
        //邮箱格式不正确
        [Share ShakeView:FillInTheAccountNumber WithInfomation:@"请使用QQ邮箱注册"];
    }
}
 
 
 
/**
 用户同意或取消用户使用协议&隐私条款
 
 @param sender 传入按钮
 */
-(void)changeAgreeBtn:(UIButton *)sender{
    if([sender isSelected]){
        [sender setSelected:NO];
        [registerBtn setEnabled:NO];
        registerBtn.backgroundColor=[UIColor lightGrayColor];
    }else{
        [sender setSelected:YES];
        [registerBtn setEnabled:YES];
        registerBtn.backgroundColor=kGlobalYellowColor;
    }
}
 
/**
 用户查看用户使用协议&隐私条款
 */
-(void)LookItem{
    SettingWebView *settingwebVC=[[SettingWebView alloc] init];
    settingwebVC.webTitle=@"用户协议&隐私条款";
    settingwebVC.requestURL=[NSString stringWithFormat:@"%@/api_control_ios.jsp",iconImageUrl];
    if(_ispresent){//模态跳转
        [self presentViewController:settingwebVC animated:YES completion:^{
            
        }];
    }else{
        [self.navigationController pushViewController:settingwebVC animated:YES];
    }
}
 
/**
 注册按钮被点击
 
 @param sender 被点击的注册按钮
 */
-(void)registerClicked:(UIButton *)sender{
    //首先,需要验证电子邮箱格式是否正确
    if ([Share isEmailAddress:FillInTheAccountNumber.text]) {
        //其次,验证密码是否为6~18位
        if (FillInThePassWord.text.length>5&&FillInThePassWord.text.length<18) {
            //最后,验证 验证码是否为6位数 ,否者可以判断验证码错误
            if (FillInTheVerificationCode.text.length==6) {
                [Share animateButtonWithButton:sender WithTitle:@"注册中..." WithColor:[UIColor colorWithRed:165/255.0 green:42/255.0 blue:42/255.0 alpha:1]];
                
                //联网注册
                [[YTHNetInterface startInterface] getRegisterWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" WithEmail:FillInTheAccountNumber.text WithPwd:FillInThePassWord.text WithNickName:nil WithVerifyCode:FillInTheVerificationCode.text WithPortrait:nil withBlock:^(BOOL isSuccessful, id result, NSString *error) {
                    if (isSuccessful) {
                        NSString *err=[[NSString alloc] initWithFormat:@"%@",[result objectForKey:@"Error"]];
                        if (err.length==0) {
                            [Share delay:1.5 withBlack:^{
                                [Share animateButtonWithButton:sender WithTitle:@"注册成功!" WithColor:[UIColor colorWithRed:238/255.0 green:99/255.0 blue:99/255.0 alpha:1]];
                            }];
                            [Share delay:2.5 withBlack:^{
                                if(_ispresent){//模态
                                    [self presentBack];
                                }else{
                                    [self back];
                                }
                            }];
                        }else{
                            [Share delay:1.5 withBlack:^{
                                [Share animateButtonWithButton:sender WithTitle:@"验证码错误!" WithColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:128/255.0 alpha:1]];
                            }];
                            [Share delay:3.0 withBlack:^{
                                [Share animateButtonWithButton:sender WithTitle:@"确 认" WithColor:kGlobalMainColor];
                            }];
                        }
                    }else{
                        [Share delay:1.5 withBlack:^{
                            [Share animateButtonWithButton:sender WithTitle:error WithColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:128/255.0 alpha:1]];
                        }];
                        [Share delay:3.0 withBlack:^{
                            [Share animateButtonWithButton:sender WithTitle:@"确 认" WithColor:kGlobalMainColor];
                        }];
                    }
                }];
            }else{
                if (FillInTheVerificationCode.text.length==0) {
                    [Share ShakeView:FillInTheVerificationCode WithInfomation:@"验证码不能为空!"];
                    [VerificationCode setTitle:@"点击这里获取" forState:UIControlStateNormal];
                    [VerificationCode setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                }else{
                    [Share ShakeView:FillInTheVerificationCode WithInfomation:@"验证码错误!"];
                }
            }
        }else{
            [Share ShakeView:FillInThePassWord WithInfomation:@"密码长度设为6~18位"];
        }
    }else{
        //邮箱格式不正确
        [Share ShakeView:FillInTheAccountNumber WithInfomation:@"邮箱格式错误!"];
        //同时检查密码是否正确
        if (!(FillInThePassWord.text.length>6&&FillInThePassWord.text.length<18)) {
            [Share ShakeView:FillInThePassWord WithInfomation:@"密码长度设为6~18位"];
        }
    }
}
 
/**
 textField前面空一段距离
 
 @param textField 传入textField
 @param leftWidth 前面空的宽度
 */
-(void)setTextFieldLeftPadding:(UITextField *)textField forWidth:(CGFloat)leftWidth
{
    CGRect frame = textField.frame;
    frame.size.width = leftWidth;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftview;
}
 
@end