IVTBindingTelViewController.m
11.8 KB
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
//
// IVTBindingTelViewController.m
// IVTMeLiveTwo
//
// Created by XRG on 16/8/17.
// Copyright © 2016年 QLQ. All rights reserved.
//
#import "IVTBindingTelViewController.h"
#import "NSString+IVTMeString.h"
#import "AppDelegate.h"
#import "XMPPManager.h"
@interface IVTBindingTelViewController ()<UITextFieldDelegate>
{
UITextField *mobileField;
UITextField *captchaField;
dispatch_source_t timer;
NSInteger countdown;
UIButton *captchaButton;
UIButton *loginButton;
}
@end
@implementation IVTBindingTelViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
[self timerCount];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTap)];
[self.view addGestureRecognizer:tap];
}
//注册登录页面
- (void)initUI{
__unsafe_unretained typeof (self) wself = self;
//整体背景nav view
UIView* navBgView = [[UIView alloc] init];
[self.view addSubview:navBgView];
navBgView.backgroundColor = [UIColor IVTLightRedColor];
[navBgView mas_makeConstraints:^(MASConstraintMaker* make) {
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.top.equalTo(@0);
make.height.equalTo(@58);
}];
UILabel *message = [[UILabel alloc]init];
[navBgView addSubview:message];
message.text = @"绑定手机";
message.textAlignment = 1;
message.textColor = [UIColor IVTTitleColor];
message.font = [UIFont YXFontOfSize:19];
[message mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(navBgView.mas_centerX);
make.top.equalTo(wself.view.mas_top).with.offset(29);
make.size.mas_equalTo(CGSizeMake(130.0f, 19.0f));
}];
UIImageView *inputPhoneView = [[UIImageView alloc]init];
inputPhoneView.image =[UIImage imageNamed:@"请输入手机号框"];
inputPhoneView.userInteractionEnabled = YES;
[self.view addSubview:inputPhoneView];
[inputPhoneView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(navBgView.mas_bottom).with.offset(34);
make.centerX.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(259.0f, 39.0f));
}];
mobileField = [[UITextField alloc]init];
mobileField.backgroundColor = [UIColor clearColor];
mobileField.placeholder = @"请输入手机号";
[mobileField setValue:[UIColor YXColorWithHexCode:@"#A0A0A0"] forKeyPath:@"_placeholderLabel.textColor"];
mobileField.font = [UIFont YXFontOfSize:13];
mobileField.keyboardType = UIKeyboardTypeNumberPad;
mobileField.delegate = self;
[inputPhoneView addSubview:mobileField];
[mobileField mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(inputPhoneView.mas_leading).with.offset(43);
make.top.equalTo(inputPhoneView.mas_top).with.offset(0);
make.size.mas_equalTo(CGSizeMake(200, 39));
}];
UIImageView *captchaView = [[UIImageView alloc]init];
captchaView.image =[UIImage imageNamed:@"输入验证码框"];
captchaView.userInteractionEnabled = YES;
[self.view addSubview:captchaView];
[captchaView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(inputPhoneView.mas_bottom).with.offset(10);
make.leading.equalTo(inputPhoneView.mas_leading);
make.size.mas_equalTo(CGSizeMake(151.5f, 39.5f));
}];
captchaField = [[UITextField alloc]init];
captchaField.backgroundColor = [UIColor clearColor];
captchaField.placeholder = @"请输入验证码";
[captchaField setValue:[UIColor YXColorWithHexCode:@"#A0A0A0"] forKeyPath:@"_placeholderLabel.textColor"];
captchaField.font = [UIFont YXFontOfSize:13];
captchaField.keyboardType = UIKeyboardTypeNumberPad;
captchaField.delegate = self;
[captchaView addSubview:captchaField];
[captchaField mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(captchaView.mas_leading).with.offset(43.0f);
make.size.mas_equalTo(CGSizeMake(110, 39));
make.top.equalTo(inputPhoneView.mas_bottom).with.offset(10);
}];
captchaButton = [UIButton buttonWithType:UIButtonTypeCustom];
[captchaButton setBackgroundColor:[UIColor YXColorWithHexCode:@"#ffa800"]];
[captchaButton setTitle:@"获取验证码" forState:UIControlStateNormal];
captchaButton.titleLabel.font = [UIFont YXFontOfSize:14];
[captchaButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view addSubview:captchaButton];
[captchaButton addTarget:self action:@selector(captchaClick:) forControlEvents:UIControlEventTouchUpInside];
[captchaButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(97.0f, 39.0f));
make.top.equalTo(inputPhoneView.mas_bottom).with.offset(10);
make.leading.equalTo(captchaView.mas_trailing).with.offset(11);
}];
//登录
loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[loginButton setBackgroundImage:[UIImage imageNamed:@"MeOK"] forState:UIControlStateNormal];
[loginButton setBackgroundImage:[UIImage imageNamed:@"MeOkHL"] forState:UIControlStateHighlighted];
[self.view addSubview:loginButton];
[loginButton addTarget:self action:@selector(bindingTelClick:) forControlEvents:UIControlEventTouchUpInside];
[loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(259.0f, 38.0f));
make.top.equalTo(captchaButton.mas_bottom).with.offset(10);
make.centerX.equalTo(self.view);
}];
UILabel *BBlabel = [[UILabel alloc] init];
BBlabel.text = @"为了账号安全,请绑定手机号";
BBlabel.textAlignment = 1;
BBlabel.textColor = [UIColor IVTGrayColor];
BBlabel.font = [UIFont IVTH3Font];
BBlabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:BBlabel];
[BBlabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(259.0f, 38.0f));
make.top.equalTo(loginButton.mas_bottom).with.offset(15);
make.centerX.equalTo(wself.view);
}];
}
-(void)doTap{
[self.view endEditing:YES];
}
- (void)timerCount {
countdown = 60;
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
[captchaButton setEnabled:NO];
[captchaButton setTitle:[NSString stringWithFormat:@"%ld秒后重发",(long)countdown] forState:UIControlStateDisabled];
if (countdown == 0) {
dispatch_suspend(timer);
[captchaButton setTitle:@"重新发送" forState:UIControlStateDisabled];
[captchaButton setTitle:@"重新发送" forState:UIControlStateNormal];
[captchaButton setEnabled:YES];
} else {
countdown--;
}
});
}
#pragma mark - textField delegate
//textField的字数控制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *behoverString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([textField isEqual:mobileField]) {
if (behoverString.length > 11) {
return NO;
}
if ([behoverString isMobileNumber]) {
captchaButton.enabled = YES;
if (captchaField.text.length==4) {
loginButton.enabled = YES;
}
}
}
if ([textField isEqual:captchaField]) {
if (range.location >= 4) {
return NO;
}
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[mobileField resignFirstResponder];
[captchaField resignFirstResponder];
return NO;
}
#pragma mark - 点击获取验证码
-(void)captchaClick:(id)sender{
if (mobileField.text.length > 0 && [mobileField.text isMobileNumber]) {
captchaButton.enabled = NO;
[self performSelector:@selector(timeEnough:) withObject:nil afterDelay:3.0];
//你要实现的button点击事件
[HUDUtil showLoading];
[self.view endEditing:YES];
[[IVTNetworkAPIClient sharedClient] getCaptchaWithPhone:mobileField.text success:^(NSDictionary *successData) {
countdown = 60;
dispatch_resume(timer);
[HUDUtil showSuccess:@"验证码已发送"];
[captchaField becomeFirstResponder];
} error:^(NSDictionary *successData) {
NSString *msg = successData[@"description"];
[HUDUtil showError:msg];
captchaButton.enabled = YES;
countdown = 0;
} failure:^(NSError *failureError) {
[HUDUtil showError:@"网络异常"];
captchaButton.enabled = YES;
countdown = 0;
}];
}else{
[HUDUtil showError:@"请输入手机号,并再次请求验证码"];
}
}
-(void)timeEnough:(UIButton *)btn
{
captchaButton.enabled = YES;
}
// 点击确认绑定
- (void)bindingTelClick:(id)sender{
if (![mobileField.text isMobileNumber] || captchaField.text.length <= 0) {
[HUDUtil showError:@"请输入手机号或验证码"];
return;
}
NSString *accessToken = nil;
if (self.platform == kSinaType) {
accessToken = [AppDelegate appDelegate].wbCurrentUserID;
}
else if (self.platform == kWeChatType){
accessToken = [AppDelegate appDelegate].wxOpenId;
}
else if (self.platform == kQQType){
accessToken = [AppDelegate appDelegate].tencentOpenId;
}
loginButton.enabled = NO;
[HUDUtil showLoading];
[self.view endEditing:YES];
[[IVTNetworkAPIClient sharedClient] thirdPartLoginWithPhoneNumber:mobileField.text captcha:captchaField.text platformName:self.platform value:accessToken success:^(NSDictionary *successData) {
if (_platform == kSinaType) {
[IVTAccountTool setSinaBinding:YES];
}
else if (_platform == kWeChatType){
[IVTAccountTool setWeChatBinding:YES];
}
else if (_platform == kQQType){
[IVTAccountTool setQQBinding:YES];
}
[self bindThirdPartSuccess:mobileField.text captchaField:captchaField.text];
} error:^(NSDictionary *successData) {
NSString *msg = successData[@"description"];
[HUDUtil showError:msg];
loginButton.enabled = YES;
} failure:^(NSError *failureError) {
[HUDUtil showError:@"网络异常"];
loginButton.enabled = YES;
}];
}
-(void)bindThirdPartSuccess:(NSString *)mobilText captchaField:(NSString *)captchaText{
[[IVTNetworkAPIClient sharedClient] loginWithPhone:mobileField.text captcha:captchaField.text success:^(NSDictionary *successData) {
[HUDUtil hide];
[[XMPPManager sharedInstance] loginWithUserId:[IVTAccountTool getUserId] pwd:captchaField.text];
[IVTAccountTool getFollowListRequest];
[self dismissViewControllerAnimated:YES completion:^{
}];
} error:^(NSDictionary *successData) {
NSString *msg = successData[@"description"];
[HUDUtil showError:msg];
loginButton.enabled = YES;
} failure:^(NSError *failureError) {
[HUDUtil showError:@"网络异常"];
loginButton.enabled = YES;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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