LoginViewController.m 10.9 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9 10 11 12
//
//  LoginViewController.m
//  redstar
//
//  Created by admin on 15/10/22.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "CustomDropMenuView.h"

13
#import "RootTabBarController.h"
admin's avatar
admin committed
14

15 16
#import <AFNetworking.h>
#import "CommonFunc.h"
17 18 19
#import "HttpClient.h"

#import <MBProgressHUD.h>
20

admin's avatar
admin committed
21 22
#define kUsernameTableViewCell @"usernameTableViewCell"

admin's avatar
admin committed
23
@interface LoginViewController () <CustomDropMenuDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UITabBarControllerDelegate, UIAlertViewDelegate> {
admin's avatar
admin committed
24 25 26 27 28 29 30
    BOOL isOpened; // 判断存取本地的tableView是否打开
}
@property (nonatomic, strong) LoginView *loginView;
@property (nonatomic, strong) NSMutableDictionary *allUserDict;

@property (nonatomic, strong) UITableView *dropTableView;
@property (nonatomic, strong) CustomDropMenuView *menu;
31 32

@property (nonatomic, strong) RootTabBarController *rootTBC;
admin's avatar
admin committed
33
@property (nonatomic, strong) NSString *jumpURL;
admin's avatar
admin committed
34 35 36 37 38 39 40 41
@end

@implementation LoginViewController

#pragma mark - ViewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
admin's avatar
admin committed
42 43 44
    
    [self getVersion];
    
admin's avatar
admin committed
45 46 47 48 49 50
    isOpened = NO;
    
    [self addAllClick];
   
    
    self.loginView.usernameTextFiled.delegate = self;
admin's avatar
admin committed
51
    self.loginView.passwordTextFiled.delegate = self;
admin's avatar
admin committed
52
    // 初始化数组
53
    self.allUserDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"thoradmin", @"admin", nil];
54 55
    
    [self requestLogin];
admin's avatar
admin committed
56 57 58 59 60 61 62
}

- (void)loadView
{
    self.view = self.loginView;
}

63 64
- (void)didReceiveMemoryWarning
{
admin's avatar
admin committed
65 66 67 68 69
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Methods
admin's avatar
admin committed
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
// 获取版本
- (void)getVersion
{
    HttpClient *httpCilent = [[HttpClient alloc] initWithUrl:[NSString stringWithFormat:@"%@%@", kRedStarURL, kCheckUpdateURL]];
    [httpCilent checkAndUpdateCurrentVersionWithCompletion:^(id response, NSError *error) {
    
        if (response[@"data"] == nil && response[@"data"] == NULL && response[@"data"] == [NSNull null]) {
            NSDictionary *dict = response[@"data"];
            NSString *newVersion = [NSString stringWithFormat:@"%@", dict[@"version"]];
            self.jumpURL = [NSString stringWithFormat:@"%@", dict[@"url"]];
            [self checkAppUpdate:newVersion];
        } else {
            return;
        }
    }];
}

- (void)checkAppUpdate:(NSString *)newVersion
{
    // 获取当前版本
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [NSString stringWithFormat:@"%@",infoDic[@"CFBundleShortVersionString"]];
    NSLog(@"当前版本是:%@", currentVersion);
    
    if (![currentVersion isEqualToString:newVersion]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示!" message:[NSString stringWithFormat:@"发现新版本:%@", newVersion] delegate:self cancelButtonTitle:@"稍后再说" otherButtonTitles:@"前往更新", nil];
        [alert show];
        alert.tag = 3058284;
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 && alertView.tag == 3058284) {
        NSString *url = self.jumpURL;
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }
}



111 112
- (void)requestLogin
{
113
   
114 115 116 117 118 119 120
    // 获取上次登陆信息
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"loginMessage"] isEqualToString:@"ok"]) {
        NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        
        
        HttpClient *httpClient = [[HttpClient alloc] initWithLogin];
121 122
        UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
        [MBProgressHUD showHUDAddedTo:window animated:YES];
123
        [httpClient loginWithUsername:username password:password completion:^(id response, NSError *error) {
124
            NSLog(@"登陆 respo = %@", response);
125
            NSLog(@"error = %@", error);
126 127 128 129 130 131 132
            if ([response[@"message"] isEqualToString:@"ok"]) {
                // 保存登陆信息
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                [userDefault setObject:username forKey:@"username"];
                [userDefault setObject:password forKey:@"password"];
                [userDefault setObject:response[@"message"] forKey:@"loginMessage"];
                
133 134 135 136 137 138 139 140
                NSDictionary *dict = response[@"data"];
                NSString *user_code = dict[@"user_code"];
                NSString *user_name = dict[@"user_name"];
                NSString *user_uuid = dict[@"user_uuid"];
                [userDefault setObject:user_code forKey:@"user_code"];
                [userDefault setObject:user_name forKey:@"user_name"];
                [userDefault setObject:user_uuid forKey:@"user_uuid"];

141
                self.rootTBC = [[RootTabBarController alloc] init];
142
                [MBProgressHUD hideHUDForView:window animated:YES];
143 144 145
                [self presentViewController:_rootTBC animated:YES completion:nil];
            } else {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", response[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
146
                [MBProgressHUD hideHUDForView:window animated:YES];
147 148 149 150 151 152
                [alertView show];
            }
        }];
        
    }
}
admin's avatar
admin committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

- (void)addAllClick
{
    [self.loginView.loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.loginView.retrieveButton addTarget:self action:@selector(retrieveButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.loginView.registerButton addTarget:self action:@selector(registerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *  登录按钮
 */
- (void)loginButtonClick:(UIButton *)sender
{
168 169 170
    self.rootTBC = [[RootTabBarController alloc] init];
    [self presentViewController:_rootTBC animated:YES completion:nil];
    
171 172 173 174 175 176 177 178 179 180
    if (self.loginView.usernameTextFiled.text.length == 0 || self.loginView.passwordTextFiled.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"账号、密码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    HttpClient *httpClient = [[HttpClient alloc] initWithLogin];
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [MBProgressHUD showHUDAddedTo:window animated:YES];
    [httpClient loginWithUsername:self.loginView.usernameTextFiled.text password:self.loginView.passwordTextFiled.text completion:^(id response, NSError *error) {
admin's avatar
admin committed
181
        NSLog(@"response = %@, error = %@", response, error);
182
        if ([response[@"message"] isEqualToString:@"ok"]) {
183
           
184 185 186 187
            // 保存登陆信息
            [[NSUserDefaults standardUserDefaults] setObject:self.loginView.usernameTextFiled.text forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] setObject:self.loginView.passwordTextFiled.text forKey:@"password"];
            [[NSUserDefaults standardUserDefaults] setObject:response[@"message"] forKey:@"loginMessage"];
188 189 190 191 192 193 194
            NSDictionary *dict = response[@"data"];
            NSString *user_code = dict[@"user_code"];
            NSString *user_name = dict[@"user_name"];
            NSString *user_uuid = dict[@"user_uuid"];
            [[NSUserDefaults standardUserDefaults] setObject:user_code forKey:@"user_code"];
            [[NSUserDefaults standardUserDefaults] setObject:user_name forKey:@"user_name"];
            [[NSUserDefaults standardUserDefaults] setObject:user_uuid forKey:@"user_uuid"];
195 196 197 198 199 200 201 202 203 204 205
            // 进入RootTabBar
            self.rootTBC = [[RootTabBarController alloc] init];
            [MBProgressHUD hideHUDForView:window animated:YES];
            [self presentViewController:_rootTBC animated:YES completion:nil];
            
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", response[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [MBProgressHUD hideHUDForView:window animated:YES];
            [alertView show];
        }
    }];
206
}
admin's avatar
admin committed
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


// 找回密码
- (void)retrieveButtonClick:(UIButton *)sender
{
    NSLog(@"找回密码");
}

// 找回密码
- (void)registerButtonClick:(UIButton *)sender
{
    NSLog(@"用户注册");
}

#pragma mark - TableView Delegate/DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allUserDict.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kUsernameTableViewCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kUsernameTableViewCell];
    }
    cell.textLabel.text = _allUserDict.allKeys[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:self.dropTableView cellForRowAtIndexPath:indexPath];
    self.loginView.usernameTextFiled.text = cell.textLabel.text;
    NSString *key = _allUserDict.allKeys[indexPath.row];
    self.loginView.passwordTextFiled.text = _allUserDict[key];
    [_menu dismiss];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}


#pragma mark - UItextField Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([self.loginView.usernameTextFiled isFirstResponder]) {
        // 创建下拉菜单
        self.menu = [CustomDropMenuView defaultMenuView];
admin's avatar
admin committed
258
        _menu.showTop = 300;
259 260 261 262
        _menu.showLeft = 20;
        _menu.showRight = -20;
        _menu.showHeight = 40 * _allUserDict.count;
        
admin's avatar
admin committed
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
        _menu.delegate = self;
        _menu.content = self.dropTableView;
        
        // 显示
        [_menu showFrom:self.loginView.usernameView];
    }
}

#pragma mark - layz loading
- (LoginView *)loginView
{
    if (!_loginView) {
        _loginView = [[LoginView alloc] init];
    }
    return _loginView;
}

- (UITableView *)dropTableView
{
    if (!_dropTableView) {
        _dropTableView = [[UITableView alloc] init];
    }
    [_dropTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kUsernameTableViewCell];
    _dropTableView.translatesAutoresizingMaskIntoConstraints = NO;
    _dropTableView.delegate = self;
    _dropTableView.dataSource = self;
    return _dropTableView;
}

admin's avatar
admin committed
292 293 294 295
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

admin's avatar
admin committed
296 297


admin's avatar
admin committed
298 299 300 301 302 303 304
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField resignFirstResponder]) {
        return YES;
    }
    return NO;
}
admin's avatar
admin committed
305 306

@end