admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
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
//
//  forgetPasswordViewController.m
//  BuWanVideo2.0
//
//  Created by apple on 16/10/11.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "forgetPasswordViewController.h"
 
@interface forgetPasswordViewController (){
    UITextField * FillInTheAccountNumber;
    UITextField * FillInThePassWord;
    UITextField * FillInTheVerificationCode;
    UIButton * registerBtn;
    UIButton * VerificationCode;
    BOOL End_the_countdown;//这是一个在倒计时部分会用到的一个变量,用于判断倒计时是自己结束的,还是验证码请求失败,我们手动结束的倒计时,如果是自动结束的为Yes,手动结束的为NO,默认为NO值,所以需要在请求验证码成功的时候,改变该值。
}
 
@end
 
@implementation forgetPasswordViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"忘记密码";
    self.view.backgroundColor=[UIColor whiteColor];
    
    //设置账号登录的字体样式
    self.navigationController.navigationBar.titleTextAttributes =@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    //设置返回按钮
    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];
        //添加返回按钮
        UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(15, 15, 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] initWithFrame:CGRectMake(50, 85, KScreenW-100, 35)];
    [FillInTheAccountNumber.layer setMasksToBounds:YES];
    [FillInTheAccountNumber.layer setCornerRadius:5.0];
    [FillInTheAccountNumber setBackgroundColor:KGlobalLightGreyColor_255];
    [FillInTheAccountNumber.layer setBorderWidth:1.0];
    [FillInTheAccountNumber.layer setBorderColor:(kGlobalLightGreyColor_210.CGColor)];
    FillInTheAccountNumber.keyboardType=UIKeyboardTypeEmailAddress;
    FillInTheAccountNumber.autocorrectionType=UITextAutocorrectionTypeNo;
    FillInTheAccountNumber.placeholder=[NSString stringWithFormat:@"请输入注册邮箱"];
    if (self.accountNum.length>0) {
        FillInTheAccountNumber.text=self.accountNum;
    }
    
    //前面空一个间距
    [self setTextFieldLeftPadding:FillInTheAccountNumber forWidth:10.0];
    [self.view addSubview:FillInTheAccountNumber];
    //验证码输入框
    FillInTheVerificationCode=[[UITextField alloc] initWithFrame:CGRectMake(50, 140, KScreenW-218, 35)];
    [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_210.CGColor)];
    FillInTheVerificationCode.placeholder=[NSString stringWithFormat:@"验证码"];
    
    //前面空一个间距
    [self setTextFieldLeftPadding:FillInTheVerificationCode forWidth:10.0];
    [self.view addSubview:FillInTheVerificationCode];
    //获取验证码的按钮
    VerificationCode=[[UIButton alloc] initWithFrame:CGRectMake(KScreenW-150, 140, 100, 35)];
    [VerificationCode.layer setMasksToBounds:YES];
    [VerificationCode.layer setCornerRadius:5.0];
    [VerificationCode.layer setBorderWidth:1.0];
    [VerificationCode.layer setBackgroundColor:kGlobalYellowColor.CGColor];
    [VerificationCode.layer setBorderColor:(kGlobalLightGreyColor_210.CGColor)];
    [VerificationCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    VerificationCode.titleLabel.font=[UIFont systemFontOfSize:15.0];
    [VerificationCode addTarget:self action:@selector(startTime:) forControlEvents:UIControlEventTouchUpInside];
 
    [VerificationCode setTitleColor:kGlobalMainColor forState:UIControlStateNormal];
    [self.view addSubview:VerificationCode];
    //输入新密码的输入框
    FillInThePassWord=[[UITextField alloc] initWithFrame:CGRectMake(50, 195, KScreenW-100, 35)];
    [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:@"输入新密码"];
    //前面空一个间距
    [self setTextFieldLeftPadding:FillInThePassWord forWidth:10.0];
    [self.view addSubview:FillInThePassWord];
    //确认修改的按钮
    registerBtn=[[UIButton alloc] initWithFrame:CGRectMake(70, KScreenH-110, 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(resetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    
}
 
/**
 确认重置密码
 */
-(void)resetPassword:(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] getsetPwdWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" WithEmail:FillInTheAccountNumber.text WithPwd:FillInThePassWord.text WithVerifyCode:FillInTheVerificationCode.text 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:@"请求失败" 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位"];
        }
        
    }
}
 
/**
 获取验证码,倒计时
 
 @param sender 获取验证码的按钮
 */
-(void)startTime:(UIButton *)sender{
    if ([Share isEmailAddress:FillInTheAccountNumber.text]){
        __block int timeout=98; //倒计时时间
        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];
                    //设置不可点击
                    sender.userInteractionEnabled = YES;
                    [sender setTitleColor:kGlobalMainColor forState:UIControlStateNormal];
                });
            }else{
                int seconds = timeout % 99;
                NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [sender setTitle:[NSString stringWithFormat:@"重新发送(%@)",strTime] forState:UIControlStateNormal];
                });
                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:kGlobalMainColor];
                [Share delay:3 withBlack:^{
                    [sender setBackgroundColor:kGlobalYellowColor];
                    [Share animateButtonWithButton:sender WithTitle:@"获取验证码" WithColor:kGlobalMainColor];
                }];
            }
        }];
    }else{
        //邮箱格式不正确
        [Share ShakeView:FillInTheAccountNumber WithInfomation:@"邮箱格式错误!"];
    }
}
 
-(void)touchesBegan:(NSSet<UITouch *> *)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:^{
        
    }];
}
 
-(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