单军华
2018-04-28 1d39caf7235522ae121db7a188ba41a706217407
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
//  RegistPage.m
//  GoldRich
//
//  Created by WindShan on 2017/2/9.
//  Copyright © 2017年 WindShan. All rights reserved.
//
 
#import "RegistPage.h"
#import "GloriaLabel.h"
#import "CommonReqModel.h"
#import "NetworkSingleton.h"
 
@interface RegistPage ()<UITextFieldDelegate>
{
    UITextField * userPhoneTextField;
    UITextField * checkCodeTextField;
    UITextField * userNewPwdTextField;
    
    GloriaLabel * getCheckCodeLabel;
    UIButton * resetUserPwdBtn;
    UIButton *sendBtn;
    dispatch_source_t _timer;
}
 
@end
 
@implementation RegistPage
 
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.navigationController.navigationBarHidden = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    if (_timer) {
        dispatch_source_cancel(_timer);
    }
    
    sendBtn.userInteractionEnabled = YES;
    sendBtn.alpha = 1;
    getCheckCodeLabel.text = @"获取验证码";
}
 
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.title = @"用户注册";
    //[self setNavigationLeft:@"返回" sel:@selector(backAction)];
    
    
    UIImageView * loginBk = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    loginBk.image = [UIImage imageNamed:@"login_bk"];
    [self.view addSubview:loginBk];
    
    // 初始化 pagecontrol
    CGFloat logoHeight = 75;
    CGFloat logoWidth = 105;
    CGFloat tipsLabelHeight = NavBarHeight7+75+30;
    CGFloat textFieldW = 292;
    CGFloat textFieldH = 41;
    
    if( IsiPhone4 || IsiPhone5)
    {
        logoWidth = 100;
        logoHeight = 70;
        tipsLabelHeight = NavBarHeight7+70+10;
        textFieldH = 40;
    }
    
    
    UIImageView * logoPic = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-105)/2, NavBarHeight7, logoWidth, logoHeight)];
    logoPic.image = [UIImage imageNamed:@"login_logo"];
    [self.view addSubview:logoPic];
    
    
    UIImageView * edit_bkimg = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2, SCREEN_HEIGHT/2-60, textFieldW, textFieldH)];
    edit_bkimg.image = [UIImage imageNamed:@"edit_bk"];
    [self.view addSubview:edit_bkimg];
    
    UIImageView * edit_name_tagimg = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 20, 20)];
    edit_name_tagimg.image = [UIImage imageNamed:@"edit_tel_no_tag"];
    [edit_bkimg addSubview:edit_name_tagimg];
    
    userPhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2+40, SCREEN_HEIGHT/2-60, textFieldW-40-100, textFieldH)];
    userPhoneTextField.secureTextEntry = NO;
    userPhoneTextField.placeholder = @"请输入手机号";
    userPhoneTextField.font = [UIFont fontWithName:@"Arial" size:16];
    userPhoneTextField.delegate = self;
    [userPhoneTextField setValue:RgbColor(149, 149, 150) forKeyPath:@"_placeholderLabel.textColor"];
    //userPhoneTextField.layer.borderColor = [RgbColor(213, 213, 213) CGColor];
    userPhoneTextField.textColor = RgbColor(149, 149, 150);
    
    userPhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    userPhoneTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    // 设置一个空白blankView,15.0就是这块View的宽度width,也是光标的位置,根据你自己的需要设定大小
    userPhoneTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(userPhoneTextField.frame.origin.x,userPhoneTextField.frame.origin.y,10.0, userPhoneTextField.frame.size.height)];
    userPhoneTextField.leftViewMode =UITextFieldViewModeAlways;
    [self.view addSubview:userPhoneTextField];
    
    UIImageView * edit_bkimg2 = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2, SCREEN_HEIGHT/2, textFieldW, textFieldH)];
    edit_bkimg2.image = [UIImage imageNamed:@"edit_bk"];
    [self.view addSubview:edit_bkimg2];
    
    UIImageView * edit_name_tagimg2 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 18, 18)];
    edit_name_tagimg2.image = [UIImage imageNamed:@"edit_code_tag"];
    [edit_bkimg2 addSubview:edit_name_tagimg2];
    
    
    checkCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2+40, SCREEN_HEIGHT/2, textFieldW-40, textFieldH)];
    checkCodeTextField.secureTextEntry = NO;
    checkCodeTextField.placeholder = @"请输入验证码";
    checkCodeTextField.font = [UIFont fontWithName:@"Arial" size:16];
    checkCodeTextField.delegate = self;
    [checkCodeTextField setValue:RgbColor(149, 149, 150) forKeyPath:@"_placeholderLabel.textColor"];
    //userPhoneTextField.layer.borderColor = [RgbColor(213, 213, 213) CGColor];
    checkCodeTextField.textColor = RgbColor(149, 149, 150);
    checkCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    checkCodeTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    // 设置一个空白blankView,15.0就是这块View的宽度width,也是光标的位置,根据你自己的需要设定大小
    checkCodeTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(checkCodeTextField.frame.origin.x,checkCodeTextField.frame.origin.y,10.0, checkCodeTextField.frame.size.height)];
    checkCodeTextField.leftViewMode =UITextFieldViewModeAlways;
    [self.view addSubview:checkCodeTextField];
    
    UIImageView * edit_bkimg3 = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2, SCREEN_HEIGHT/2+60, textFieldW, textFieldH)];
    edit_bkimg3.image = [UIImage imageNamed:@"edit_bk"];
    [self.view addSubview:edit_bkimg3];
    
    UIImageView * edit_name_tagimg3 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 18, 18)];
    edit_name_tagimg3.image = [UIImage imageNamed:@"edit_pwd_tag"];
    [edit_bkimg3 addSubview:edit_name_tagimg3];
    
    
    userNewPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2+40, SCREEN_HEIGHT/2+60, textFieldW, textFieldH)];
    userNewPwdTextField.secureTextEntry = YES;
    userNewPwdTextField.placeholder = @"请输入密码";
    userNewPwdTextField.font = [UIFont fontWithName:@"Arial" size:16];
    userNewPwdTextField.delegate = self;
    [userNewPwdTextField setValue:RgbColor(149, 149, 150) forKeyPath:@"_placeholderLabel.textColor"];
    //userPhoneTextField.layer.borderColor = [RgbColor(213, 213, 213) CGColor];
    userNewPwdTextField.textColor = RgbColor(149, 149, 150);
    userNewPwdTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    // 设置一个空白blankView,15.0就是这块View的宽度width,也是光标的位置,根据你自己的需要设定大小
    userNewPwdTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(userNewPwdTextField.frame.origin.x,userNewPwdTextField.frame.origin.y,10.0, userNewPwdTextField.frame.size.height)];
    userNewPwdTextField.leftViewMode =UITextFieldViewModeAlways;
    [self.view addSubview:userNewPwdTextField];
    
    sendBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    sendBtn.frame = CGRectMake((SCREEN_WIDTH-textFieldW)/2+40+textFieldW-40-100+2,SCREEN_HEIGHT/2-50,80, textFieldH/2);
    [sendBtn setTitleColor:BLACKTEXTCOLOR_SUB forState:UIControlStateNormal];
    sendBtn.userInteractionEnabled = NO;
    sendBtn.layer.masksToBounds = YES;
    sendBtn.layer.cornerRadius = 4.0f;
    sendBtn.layer.borderWidth= 1.0f;
    sendBtn.backgroundColor = RgbColor(251, 50, 81);
    [sendBtn addTarget:self action:@selector(getCheckCodeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendBtn];
    
    getCheckCodeLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2+40+textFieldW-40-100+2, SCREEN_HEIGHT/2-50, 80, textFieldH/2)];
    getCheckCodeLabel.font = FONT12;
    getCheckCodeLabel.backgroundColor = kUIColorFromRGB(0x00a2ff);
    getCheckCodeLabel.textColor = [UIColor whiteColor];
    getCheckCodeLabel.textAlignment = NSTextAlignmentCenter;
    getCheckCodeLabel.layer.borderColor = [RgbColor(251, 50, 81) CGColor];
    getCheckCodeLabel.layer.masksToBounds=YES;
    getCheckCodeLabel.layer.cornerRadius=4.0f;
    getCheckCodeLabel.text = @"获取验证码";
    [self.view addSubview:getCheckCodeLabel];
    
    resetUserPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    resetUserPwdBtn.frame = CGRectMake((SCREEN_WIDTH-textFieldW)/2, SCREEN_HEIGHT/2+120, textFieldW, textFieldH);
    [resetUserPwdBtn setTitle:@"注 册" forState:UIControlStateNormal];
    [resetUserPwdBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_bk" ] forState:UIControlStateNormal];
    //esetUserPwdBtn.backgroundColor = RgbColor(85, 81, 168);
    resetUserPwdBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
    [resetUserPwdBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    resetUserPwdBtn.layer.masksToBounds = YES;
    resetUserPwdBtn.layer.cornerRadius = 5.0f;
    //resetUserPwdBtn.userInteractionEnabled = NO;
    [resetUserPwdBtn addTarget:self action:@selector(registAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:resetUserPwdBtn];
    
    UIButton * backLoginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    backLoginBtn.frame = CGRectMake((SCREEN_WIDTH-200)/2, SCREEN_HEIGHT/2+225, 200, 40);
    [backLoginBtn setTitle:@"已有账号,返回登录" forState:UIControlStateNormal];
    backLoginBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [backLoginBtn setTitleColor:kUIColorFromRGB(0x00a2ff)  forState:UIControlStateNormal];
    [backLoginBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backLoginBtn];
    
}
 
// 85 81 168
 
#pragma mark custom function begin
////倒计时提醒
-(void)startCountDown
{
    __block int timeout = 59; //倒计时时间
    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);
    _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(), ^{
                //设置界面的按钮显示 根据自己需求设置
                sendBtn.userInteractionEnabled=YES;
                sendBtn.alpha=1;
                
                //[sendBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
                getCheckCodeLabel.text = @"重新获取";
                
            });
        }
        else
        {
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                LOG_INFO(@"____%@",strTime);
                sendBtn.userInteractionEnabled=NO;
                sendBtn.alpha=0.4;
                //[sendBtn setTitle:[NSString stringWithFormat:@"%@s",strTime] forState:UIControlStateNormal];
                getCheckCodeLabel.text = [NSString stringWithFormat:@"%@秒",strTime];
            });
            
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    
}
 
 
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
 
-(void)registAction
{
    // 重置密码操作
    if(userPhoneTextField.text.length == 0)
    {
        [Global alertMessageEx:@"请输入手机号." title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
    }
    else if(checkCodeTextField.text.length == 0)
    {
        [Global alertMessageEx:@"请输入验证码." title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
    }
    else if(userNewPwdTextField.text.length == 0)
    {
        [Global alertMessageEx:@"请输入新密码." title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
    }
    else
    {
        
        NSString *path = [[NSString alloc] initWithFormat:USER_REGISTER];
        
        CommonReqModel * model = [[CommonReqModel alloc] init];
        
        model.phone = userPhoneTextField.text;
        model.password = userNewPwdTextField.text;
        model.code     = checkCodeTextField.text;
        
        MPWeakSelf(self);
        [NetworkSingleton networkingGetMethod:model.toDic urlName:path success:^(id responseBody)
         {
             MPStrongSelf(self);
             BaseResModel * resModel = [Global toBaseModel:responseBody];
             if(resModel.code == 0)
             {
                 [Global alertMessageEx:@"恭喜你,账号注册成功!" title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
                 [weakself.navigationController popViewControllerAnimated:YES];
             }
             else
             {
                 [Global alertMessageEx:resModel.desc title:@"注册失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
             }
         }
           failure:^(NSString *error)
         {
             [Global alertMessageEx:error title:@"注册失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
         }];
    }
}
 
//获取验证码
-(void)getCheckCodeAction
{
    NSString *path = [[NSString alloc] initWithFormat:SEND_REGISTER_SMS];
    
    CommonReqModel * model = [[CommonReqModel alloc] init];
    model.phone = userPhoneTextField.text;
    model.type = @"register";
    
    //NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    //[param setObject:userPhoneTextField.text forKey:@"phone"];
    
    MPWeakSelf(self);
    [NetworkSingleton networkingGetMethod:model.toDic urlName:path success:^(id responseBody)
     {
         BaseResModel * resModel = [Global toBaseModel:responseBody];
         
         if(resModel.code == 0)
         {
             //发送成功提示
             [Global alertMessageEx:resModel.desc title:@"提示信息"  okTtitle:nil cancelTitle:@"OK" delegate:self];
             MPStrongSelf(self);
             [self startCountDown];
         }
         else
         {
             [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
         }
     }
      failure:^(NSString *error)
     {
         LOG_INFO(@"获取验证码失败:%@",error);
         [Global alertMessageEx:error title:@"验证码获取失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
     }];
 
}
 
# pragma mark custom function end
 
 
#pragma mark disappear keyboard begin
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int textLength = 0;
    
    if ([string isEqualToString:@""]) {
        textLength = textField.text.length-1;
    }else
    {
        textLength = textField.text.length+1;
    }
    
    BOOL flag = NO;
    if( textField == userPhoneTextField )
    {
        // 大于11位数不让输入
        if( textLength > 11 )
            return NO;
        
        // 检测手机号是否合法
        if( textLength == 11 && [StringUtil isMobile:[userPhoneTextField.text stringByAppendingString:string]] == NO)
        {
            [Global alertMessage:@"手机号码不合法,请重新输入!"];
        }
        else if( textLength == 11 && [StringUtil isMobile:[userPhoneTextField.text stringByAppendingString:string]] == YES)
        {
            getCheckCodeLabel.textColor = [UIColor whiteColor];
            getCheckCodeLabel.backgroundColor = RgbColor(251, 50, 81);
            sendBtn.userInteractionEnabled = YES;
        }
        else
        {
            getCheckCodeLabel.backgroundColor = kUIColorFromRGB(0x00a2ff);
            getCheckCodeLabel.textColor = [UIColor whiteColor];
            sendBtn.userInteractionEnabled = NO;
        }
    }
    
    flag = YES;
    
    return true;
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    //    if (verifyTextField.text.length > 0 && phoneTextField.text.length == 11 ) {
    //        //高亮登录
    //        quickBtn.backgroundColor = NAVIGATIONTINTCOLOR;
    //        quickBtn.userInteractionEnabled = YES;
    //    }else
    //    {
    //        quickBtn.backgroundColor = BLACKTEXTCOLOR_SUB;
    //        [quickBtn setBackgroundImage:nil forState:UIControlStateNormal];
    //        quickBtn.userInteractionEnabled = NO;
    //    }
}
 
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return true;
}
 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self textFieldShouldReturn:checkCodeTextField];
    [self textFieldShouldReturn:userPhoneTextField];
    [self textFieldShouldReturn:userNewPwdTextField];
}
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end