CNLiveUpdateTelView.m
18.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// CNLiveUpdateTelView.m
// CNLiveSettingModule
//
// Created by CNLive-zxw on 2019/3/21.
// Copyright © 2019年 cnlive. All rights reserved.
//
#import "CNLiveUpdateTelView.h"
#import "YYKit.h"
#import <Masonry/Masonry.h>
#import "QMUIKit.h"
#import <CNLiveUserSystemSDK/CNLiveUserSystemCenter.h>
#import "NSString+CNLiveExtension.h"
#import "CNUserInfoManager.h"
#import "CNLiveCategory.h"
#define TIMECOUNT 60;
@interface CNLiveUpdateTelView()<UITextFieldDelegate>
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIImageView *countryImg;
@property (nonatomic, strong) UILabel *numberLabel;//手机号
@property (nonatomic, strong) UILabel *codeLabel;//验证码
@property (nonatomic, strong) UIButton *verification;//验证码按钮
@property (nonatomic, strong) UIView *bindingView;//改变按钮
@property (nonatomic, strong) UIButton *binding;//改变按钮
@end
@implementation CNLiveUpdateTelView
static const NSInteger margin = 15;
static const NSInteger timeValue = 1.5;
#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self createSubViews];
}
return self;
}
#pragma mark - Data
- (void)setCodeNum:(NSString *)codeNum{
_codeNum = codeNum;
}
#pragma mark - UI
- (void)createSubViews {
[self addSubview:self.countryName];
[self addSubview:self.lineView];
[self addSubview:self.countryImg];
[self addSubview:self.countryCode];
[self addSubview:self.numberLabel];
[self addSubview:self.telTF];
[self addSubview:self.codeLabel];
[self addSubview:self.codeTF];
[self addSubview:self.verification];
[self addSubview:self.bindingView];
[self addSubview:self.binding];
}
- (void)layoutSubviews{
[super layoutSubviews];
[self.countryName mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(0);
make.left.equalTo(self).offset(margin);
make.height.mas_equalTo(50);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.countryName.mas_bottom).offset(0);
make.left.right.equalTo(self).offset(0);
make.height.mas_equalTo(10);
}];
[self.countryImg mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(0);
make.right.equalTo(self).offset(-margin);
make.height.mas_equalTo(50);
make.width.mas_equalTo(10);
}];
[self.countryCode mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(0);
make.right.equalTo(self.countryImg.mas_left).offset(-5);
make.height.mas_equalTo(50);
}];
[self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.lineView.mas_bottom).offset(0);
make.left.equalTo(self).offset(margin);
make.height.mas_equalTo(50);
make.width.mas_equalTo(45);
}];
[self.verification mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.numberLabel);
make.right.equalTo(self).offset(-margin);
make.width.mas_equalTo(100);
make.height.mas_equalTo(30);
}];
[self.telTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.numberLabel);
make.left.equalTo(self.numberLabel.mas_right).offset(20);
make.right.equalTo(self.verification.mas_left).offset(-10);
}];
[self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.numberLabel.mas_bottom).offset(0);
make.left.equalTo(self.numberLabel);
make.width.mas_equalTo(45);
make.height.mas_equalTo(50);
}];
[self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.codeLabel);
make.left.equalTo(self.codeLabel.mas_right).offset(20);
make.width.equalTo(self.telTF);
}];
[self.bindingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.codeTF.mas_bottom).offset(15);
make.left.equalTo(self).offset(0);
make.right.equalTo(self).offset(-0);
make.bottom.equalTo(self.mas_bottom).offset(-0);
}];
[self.binding mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.codeTF.mas_bottom).offset(30);
make.left.equalTo(self).offset(60);
make.right.equalTo(self).offset(-60);
make.height.mas_equalTo(45);
}];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)textField {
if (textField.tag == 10001) { //手机号
if (!self.gotVerificationCodeIng) {
[self setupTextFieldUI:textField];
}
}
else if (textField.tag == 10002) { //验证码
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([string length] != 0){
NSLog(@"增");
if (textField.tag == 10002) {
if (textField.text.length>=6) {
return NO;
}
}
if (textField.tag == 10001) {
if (textField.text.length>=11) {
return NO;
}
}
}
else {
NSLog(@"删");
return YES;
}
return YES;
}
#pragma mark - 懒加载
- (UILabel *)countryName {
if (!_countryName) {
_countryName = [[UILabel alloc] init];
_countryName.textColor = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:1.0];
_countryName.font = [UIFont systemFontOfSize:14];
_countryName.text = @"中国大陆";
_countryName.textAlignment = NSTextAlignmentLeft;
}
return _countryName;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
}
return _lineView;
}
- (UIImageView *)countryImg{
if (!_countryImg) {
_countryImg = [[UIImageView alloc] init];
_countryImg.userInteractionEnabled = YES;
UIImage *image = [self getImageWithImageName:@"mine_info_front" bundleName:@"CNLiveSettingModule" targetClass:[self class]];
_countryImg.image = image;
_countryImg.contentMode = UIViewContentModeCenter;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(countryTap:)];
[_countryImg addGestureRecognizer:tap];
}
return _countryImg;
}
- (UILabel *)countryCode {
if (!_countryCode) {
_countryCode = [[UILabel alloc] init];
_countryCode.textColor = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:1.0];
_countryCode.font = [UIFont systemFontOfSize:14];
_countryCode.text = @"+86";
_countryCode.textAlignment = NSTextAlignmentLeft;
_countryCode.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(countryTap:)];
[_countryCode addGestureRecognizer:tap];
}
return _countryCode;
}
- (UILabel *)numberLabel {
if (!_numberLabel) {
_numberLabel = [[UILabel alloc] init];
_numberLabel.textColor = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:1.0];
_numberLabel.font = [UIFont systemFontOfSize:14];
_numberLabel.text = @"手机号";
_numberLabel.textAlignment = NSTextAlignmentLeft;
}
return _numberLabel;
}
- (UILabel *)codeLabel {
if (!_codeLabel) {
_codeLabel = [[UILabel alloc] init];
_codeLabel.textColor = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:1.0];
_codeLabel.font = [UIFont systemFontOfSize:14];
_codeLabel.text = @"验证码";
_codeLabel.textAlignment = NSTextAlignmentLeft;
}
return _codeLabel;
}
- (UITextField *)telTF {
if (!_telTF) {
_telTF = [[UITextField alloc] init];
_telTF.textColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1.0];
_telTF.font = [UIFont systemFontOfSize:14];
_telTF.placeholder = @"请输入新手机号";
_telTF.delegate = self;
_telTF.keyboardType = UIKeyboardTypeNumberPad;
[_telTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
_telTF.tag = 10001;
}
return _telTF;
}
- (UITextField *)codeTF {
if (!_codeTF) {
_codeTF = [[UITextField alloc] init];
_codeTF.textColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1.0];
_codeTF.font = [UIFont systemFontOfSize:14];
_codeTF.placeholder = @"请输入验证码";
_codeTF.delegate = self;
_codeTF.keyboardType = UIKeyboardTypeNumberPad;
[_codeTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
_codeTF.tag = 10002;
}
return _codeTF;
}
- (UIButton *)verification{
if (!_verification) {
_verification = [UIButton buttonWithType:UIButtonTypeCustom];
[_verification setTitle:@"获取验证码" forState:UIControlStateNormal];
[_verification setTitleColor:[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0] forState:UIControlStateNormal];
_verification.titleLabel.font = [UIFont systemFontOfSize:14];
_verification.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
_verification.layer.cornerRadius = 15;
_verification.layer.masksToBounds = YES;
[_verification addTarget:self action:@selector(verificationDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _verification;
}
- (UIView *)bindingView{
if (!_bindingView) {
_bindingView = [[UIView alloc]init];
_bindingView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
}
return _bindingView;
}
- (UIButton *)binding{
if (!_binding) {
_binding = [UIButton buttonWithType:UIButtonTypeCustom];
[_binding setTitle:@"验证后绑定新手机号" forState:UIControlStateNormal];
[_binding setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_binding.titleLabel.font = [UIFont systemFontOfSize:15];
_binding.backgroundColor = [UIColor colorWithRed:0/255.0 green:194/255.0 blue:0/255.0 alpha:1.0];
_binding.layer.cornerRadius = 45/2.0;
_binding.layer.masksToBounds = YES;
[_binding addTarget:self action:@selector(bindingDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _binding;
}
#pragma mark - 私有方法
#pragma mark - Action
- (void)countryTap:(UITapGestureRecognizer *)tap{
if (self.delegate && [self.delegate respondsToSelector:@selector(selectCountry:)]) {
[self.delegate selectCountry:self];
}
}
- (void)verificationDidClicked:(UIButton *)sender {
if ([NSString isEmpty:self.telTF.text] || [self.telTF.text isWhitespaceAndNewlines]) {
QMUIAlertAction *sure = [QMUIAlertAction actionWithTitle:@"我知道了" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"请输入正确的手机号码" message:@"" preferredStyle:QMUIAlertControllerStyleAlert];
[alertController addAction:sure];
NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
sure.buttonAttributes = titleAttributs;
[alertController showWithAnimated:YES];
}
else if ([self.telTF.text isEqualToString:CNUserShareModel.mobile]) {
[QMUITips showWithText:@"您更新的手机号与旧手机号一致,请重新输入确认" inView:[UIApplication sharedApplication].delegate.window hideAfterDelay:timeValue];
}
else {
// 发送验证码
[self.telTF resignFirstResponder];
[self.codeTF becomeFirstResponder];
self.verification.userInteractionEnabled = NO;
self.verification.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
[self.verification setTitleColor:[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0] forState:UIControlStateNormal];
self.gotVerificationCodeIng = YES;
[CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeRegister mobile:self.telTF.text countryCode:_codeNum success:^(id result) {
[QMUITips showSucceed:@"短信发送成功" inView:[UIApplication sharedApplication].delegate.window hideAfterDelay:timeValue];
self.gotVerificationCode = YES;
[self countDown];
} failure:^(NSDictionary *errorDic) {
NSString *errMsg;
if ([[NSString stringWithFormat:@"%@",errorDic[@"errorCode"]] isEqualToString:@"408"]) {
errMsg = @"请填写正确的手机号";
} else {
errMsg = errorDic[@"errorMessage"];
}
[QMUITips showError:errMsg inView:[UIApplication sharedApplication].delegate.window hideAfterDelay:timeValue];
self.gotVerificationCode = NO;
self.verification.userInteractionEnabled = YES;
self.verification.backgroundColor = [UIColor colorWithRed:0/255.0 green:194/255.0 blue:0/255.0 alpha:1.0];
[self.verification setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.gotVerificationCodeIng = NO;
}];
}
}
- (void)bindingDidClicked:(UIButton *)sender {
if (self.gotVerificationCode) {
if (self.codeTF.text.length == 6) {
[QMUITips showLoadingInView:self];
[CNLiveUserSystemCenter updateMobileWithUid:CNUserShareModel.uid mobile:self.telTF.text countryCode:_codeNum verificationCode:self.codeTF.text success:^(id result) {
CNUserShareModel.mobile = self.telTF.text;
[CNUserInfoModel setLocalUserInfo:self.countryName.text withKey:@"countryname"];
[CNUserInfoModel setLocalUserInfo:self.countryCode.text withKey:@"countrycode"];
[QMUITips hideAllToastInView:self animated:YES];
if (self.delegate && [self.delegate respondsToSelector:@selector(bindingSuccess:)]) {
[self.delegate bindingSuccess:self];
}
} failure:^(NSDictionary *errorDic) {
[QMUITips hideAllToastInView:self animated:YES];
[QMUITips showError:errorDic[@"errorMessage"] inView:[UIApplication sharedApplication].delegate.window hideAfterDelay:timeValue];
}];
} else {
QMUIAlertAction *sure = [QMUIAlertAction actionWithTitle:@"我知道了" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"请输入正确的验证码" message:@"" preferredStyle:QMUIAlertControllerStyleAlert];
[alertController addAction:sure];
NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
sure.buttonAttributes = titleAttributs;
[alertController showWithAnimated:YES];
}
} else {
NSString *alertText = @"请先获取验证码";
if (self.telTF.text.length != 11) {
alertText = @"请先输入正确的手机号码";
} else {
alertText = @"请先获取验证码";
}
QMUIAlertAction *sure = [QMUIAlertAction actionWithTitle:@"我知道了" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:alertText message:@"" preferredStyle:QMUIAlertControllerStyleAlert];
[alertController addAction:sure];
NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
sure.buttonAttributes = titleAttributs;
[alertController showWithAnimated:YES];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.telTF resignFirstResponder];
}
#pragma mark - 倒计时
- (void)countDown {
__block NSInteger second = TIMECOUNT;
//(1)
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//(2)
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
//(3)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//(4)
dispatch_source_set_event_handler(timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (second == 0) {
[self setupTextFieldUI:self.telTF];
self.gotVerificationCodeIng = NO;
[self.verification setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
second = TIMECOUNT;
dispatch_cancel(timer);
} else {
[self.verification setTitle:[NSString stringWithFormat:@"%lds",second] forState:UIControlStateNormal];
second--;
}
});
});
//(5)
dispatch_resume(timer);
}
- (void)setupTextFieldUI:(UITextField *)textField{
if (textField.text.length > 0) {
_verification.userInteractionEnabled = YES;
_verification.backgroundColor = [UIColor colorWithRed:0/255.0 green:194/255.0 blue:0/255.0 alpha:1.0];
[_verification setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
else {
_verification.userInteractionEnabled = NO;
_verification.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
[_verification setTitleColor:[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0] forState:UIControlStateNormal];
}
}
@end