单军华
2018-03-29 89d748b77b478905732e60f0b4c5807c274b6565
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
//
//  LoginPage.m
//  GoldRich
//
//  Created by WindShan on 2017/2/9.
//  Copyright © 2017年 WindShan. All rights reserved.
//
 
#import "LoginPage.h"
#import "GloriaLabel.h"
#import "RegistPage.h"
#import "FogetPwdPage.h"
#import "HomePage.h"
#import "CommonReqModel.h"
#import "NetworkSingleton.h"
#import "UserModel.h"
 
@interface LoginPage ()
{
    UIButton * nomalLoginBtn;
    
    UITextField * userPhoneTextField;
    UITextField * userPwdTextFeild;
    
    UIButton * registBtn;
    UIButton * forgetBtn;
 
}
 
@end
 
@implementation LoginPage
 
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.navigationController.navigationBarHidden = YES;
    
    if([UserDefault stringForKey:@"isLogin"] != nil && [[UserDefault stringForKey:@"isLogin"] intValue] == 1 )
    {
        // 跳转主界面
        [GetAppDelegate getUserInfo];
    }
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    //self.navigationController.navigationBarHidden = NO;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    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 = 134;
    CGFloat logoWidth = 113;
    CGFloat tipsLabelHeight = NavBarHeight7+75+30;
    CGFloat textFieldW = 292;
    CGFloat textFieldH = 56;
 
    if( IsiPhone4 || IsiPhone5)
    {
        logoWidth = 80;
        logoHeight = 90;
        tipsLabelHeight = NavBarHeight7+70+10;
        textFieldH = 50;
    }
 
    
    UIImageView * logoPic = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-logoWidth)/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-120, textFieldW, textFieldH)];
    edit_bkimg.image = [UIImage imageNamed:@"edit_bk"];
    [self.view addSubview:edit_bkimg];
    
    UIImageView * edit_name_tagimg = [[UIImageView alloc] initWithFrame:CGRectMake(15, (textFieldH-22)/2, 22, 22)];
    edit_name_tagimg.image = [UIImage imageNamed:@"edit_name_tag"];
    [edit_bkimg addSubview:edit_name_tagimg];
    
    userPhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2+40, SCREEN_HEIGHT/2-120, textFieldW-40, textFieldH)];
    userPhoneTextField.font = [UIFont fontWithName:@"Arial" size:16];
    userPhoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入用户名"attributes:@{ NSForegroundColorAttributeName:kUIColorFromRGB(0x00b744)}];
    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,也是光标的位置,根据你自己的需要设定大小
    UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(userPhoneTextField.frame.origin.x,userPhoneTextField.frame.origin.y,10.0, userPhoneTextField.frame.size.height)];
    
    userPhoneTextField.leftView = blankView;
    userPhoneTextField.leftViewMode =UITextFieldViewModeAlways;
    [self.view addSubview:userPhoneTextField];
    
    UIImageView * edit_bkimg2 = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2, SCREEN_HEIGHT/2-60, textFieldW, textFieldH)];
    edit_bkimg2.image = [UIImage imageNamed:@"edit_bk"];
    [self.view addSubview:edit_bkimg2];
    
    UIImageView * edit_name_tagimg2 = [[UIImageView alloc] initWithFrame:CGRectMake(15, (textFieldH-22)/2, 22, 22)];
    edit_name_tagimg2.image = [UIImage imageNamed:@"edit_pwd_tag"];
    [edit_bkimg2 addSubview:edit_name_tagimg2];
    
    
    userPwdTextFeild = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-textFieldW)/2+40, SCREEN_HEIGHT/2-60, textFieldW-40, textFieldH)];
    userPwdTextFeild.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码"attributes:@{ NSForegroundColorAttributeName:kUIColorFromRGB(0x00b744)}];
    userPwdTextFeild.secureTextEntry = YES;
    userPwdTextFeild.font = [UIFont fontWithName:@"Arial" size:16];
    userPwdTextFeild.delegate = self;
    [userPwdTextFeild setValue:RgbColor(149, 149, 150) forKeyPath:@"_placeholderLabel.textColor"];
    //userPhoneTextField.layer.borderColor = [RgbColor(213, 213, 213) CGColor];
    userPwdTextFeild.textColor = RgbColor(149, 149, 150);
    userPwdTextFeild.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    // 设置一个空白blankView,15.0就是这块View的宽度width,也是光标的位置,根据你自己的需要设定大小
    UIView *blankView2 = [[UIView alloc] initWithFrame:CGRectMake(userPwdTextFeild.frame.origin.x,userPwdTextFeild.frame.origin.y,10.0, userPwdTextFeild.frame.size.height)];
    userPwdTextFeild.leftView = blankView2;
    userPwdTextFeild.leftViewMode =UITextFieldViewModeAlways;
    [self.view addSubview:userPwdTextFeild];
    
    nomalLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nomalLoginBtn.frame = CGRectMake((SCREEN_WIDTH-textFieldW)/2, SCREEN_HEIGHT/2+60, textFieldW, textFieldH);
    [nomalLoginBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_bk" ] forState:UIControlStateNormal];
    [nomalLoginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    nomalLoginBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
    [nomalLoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nomalLoginBtn.layer.masksToBounds=YES;
    nomalLoginBtn.layer.cornerRadius=8.0f;
    [nomalLoginBtn addTarget:self action:@selector(userLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nomalLoginBtn];
    
    forgetBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    forgetBtn.frame = CGRectMake(SCREEN_WIDTH-140, SCREEN_HEIGHT/2+45-30, 120, 40);
    [forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [forgetBtn setTitleColor:kUIColorFromRGB(0x8c928d) forState:UIControlStateNormal];
    [forgetBtn addTarget:self action:@selector(fogotPwdAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    
    registBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    registBtn.frame = CGRectMake((SCREEN_WIDTH-120)/2, SCREEN_HEIGHT/2+225, 120, 40);
    [registBtn setTitle:@"新用户注册" forState:UIControlStateNormal];
    registBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [registBtn setTitleColor:kUIColorFromRGB(0x269af4)  forState:UIControlStateNormal];
    [registBtn addTarget:self action:@selector(registAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registBtn];
    
    
    
    
}
 
#pragma mark - begin
-(void) userLogin
{
    // 重置密码操作
    if(userPhoneTextField.text.length == 0)
    {
        [Global alertMessageEx:@"请输入手机号." title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
    }
    else if(userPwdTextFeild.text.length == 0)
    {
        [Global alertMessageEx:@"请输入登录密码." title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
    }
    else
    {
        // 执行登录操作
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.labelText = @"登录中...";
        // 隐藏时候从父控件中移除
        HUD.removeFromSuperViewOnHide = YES;
        // YES代表需要蒙版效果
        HUD.dimBackground = YES;
        
        NSString *path = [[NSString alloc] initWithFormat:USER_LOGIN];
        
        CommonReqModel * model = [[CommonReqModel alloc] init];
        [model setPhone:userPhoneTextField.text];
        [model setPassword:userPwdTextFeild.text];
        
        MPWeakSelf(self);
        [NetworkSingleton networkingGetMethod:model.toDic urlName:path success:^(id responseBody)
         {
             HUD.hidden = YES;
             MPStrongSelf(self);
             BaseResModel * resModel = [Global toBaseModel:responseBody];
             
             if(resModel.code == 0)
             {
                 if(![Global isEmptyObject:resModel.content])
                 {
                     // 成功处理
                     _loginUser = [resModel.content mutableCopy];
                     
                     UserModel * userModel = [UserModel mj_objectWithKeyValues:resModel.content];
                     
                     // 存储用户信息
                     [UserDefault setObject:@"1" forKey:@"isLogin"];
                     [UserDefault setObject:userModel._id forKey:@"user_id"];
                     [UserDefault setObject:userModel.phone forKey:@"phone"];
                     [UserDefault setObject:userModel.password forKey:@"password"];
                     [UserDefault setObject:userModel.nickname forKey:@"nickname"];
                     [UserDefault setObject:userModel.video forKey:@"video"];
                     [UserDefault setObject:userModel.refresh_frequency forKey:@"refresh_frequency"];
                     [UserDefault setObject:userModel.is_open_upload forKey:@"is_open_upload"];
                     [UserDefault synchronize];//使用synchronize强制立即将数据写入磁盘,防止在写完NSUserDefaults后程序退出导致的数据丢失
                     
                     // 跳转主界面
                     //[GetAppDelegate showHomePage];
                     // 调转重置界面操作
                     HomePage * page = [[HomePage alloc] init];
                     [weakself.navigationController pushViewController:page animated:YES];
                 }
                 else
                 {
                     // 出错处理
                     [UserDefault setObject:@"0" forKey:@"isLogin"];
                     [Global alertMessageEx:resModel.desc title:@"登录失败" okTtitle:nil cancelTitle:@"确定" delegate:self];
                 }
                 
             }
             else
             {
                 [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
             }
         }
                                       failure:^(NSString *error)
         {
             HUD.hidden = YES;
             [Global alertMessageEx:error title:@"登录失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
         }];
    }
    
}
 
-(void) fogotPwdAction
{
    // 调转重置界面操作
    FogetPwdPage * fogetPwdPage = [[FogetPwdPage alloc] initIsFirstPage:NO];
    [self.navigationController pushViewController:fogetPwdPage animated:YES];
}
 
-(void) registAction
{
    // 调转注册界面操作
    RegistPage * registPage = [[RegistPage alloc] initIsFirstPage:NO];
    [self.navigationController pushViewController:registPage animated:YES];
}
 
 
#pragma mark - end
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger textLength = 0;
    
    if ([string isEqualToString:@""]) {
        textLength = textField.text.length-1;
    }
    else
    {
        textLength = textField.text.length+1;
    }
    
    BOOL flag = NO;
    if( textField == userPhoneTextField )
    {
        // 检测手机号是否合法
        if( textLength == 11 && [StringUtil isMobile:[userPhoneTextField.text stringByAppendingString:string]] == NO)
        {
            [Global alertMessage:@"手机号码不合法,请重新输入!"];
        }
        
        // 大于11位数不让输入
        if( textLength > 11 )
            return NO;
    }
    
    flag = YES;
    if (flag)
    {
        //        [loginBtn setBackgroundColor:BLUECOLOR];
        //        loginBtn.userInteractionEnabled = YES;
    }
    else
    {
        //        loginBtn.backgroundColor = [UIColor lightGrayColor];
        //        loginBtn.userInteractionEnabled = NO;
    }
    
    return flag;
}
 
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self textFieldShouldReturn:userPhoneTextField];
    [self textFieldShouldReturn:userPwdTextFeild];
}
 
/*
#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