LoginViewController.m 8.28 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  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"

#import "HomeViewController.h"
#import "AnnounceViewController.h"
#import "ChartViewController.h"
#import "MineViewController.h"

18 19 20
#import <AFNetworking.h>
#import "CommonFunc.h"

admin's avatar
admin committed
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
#define kUsernameTableViewCell @"usernameTableViewCell"

@interface LoginViewController () <CustomDropMenuDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UITabBarControllerDelegate> {
    BOOL isOpened; // 判断存取本地的tableView是否打开
}
@property (nonatomic, strong) LoginView *loginView;
@property (nonatomic, strong) NSMutableDictionary *allUserDict;

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

@implementation LoginViewController

#pragma mark - ViewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
    isOpened = NO;
    
    [self addAllClick];
   
    
    self.loginView.usernameTextFiled.delegate = self;
    
    // 初始化数组
47
    self.allUserDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"thoradmin", @"admin", nil];
admin's avatar
admin committed
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
}

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Methods

- (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
{
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    [session.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", kRedStarURL, kLoginURL, self.loginView.usernameTextFiled.text];
    NSString *password = self.loginView.passwordTextFiled.text;
    NSString *passwordMD5 = [CommonFunc md5:password];
    NSDictionary *parameters = @{@"authenticode":@"211534962",@"password":passwordMD5};
    [session POST:urlStr parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"message"] isEqualToString:@"ok"]) {
            [self setupTabbar];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", responseObject[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error = %@", error);
    }];

}
admin's avatar
admin committed
95

96 97 98 99
//
- (void)setupTabbar
{
    // 跳转到tabbarController
admin's avatar
admin committed
100 101 102 103 104
    UITabBarController *tabBC = [[UITabBarController alloc] init];
    tabBC.delegate = self;
    
    HomeViewController *home=[[HomeViewController alloc]init];
    UINavigationController *homeNav = [self setUpOneChildViewController:home title:@"首页" imageName:@"home_unchecked" selImageName:@"home_checked"];
105
    
admin's avatar
admin committed
106 107 108
    
    AnnounceViewController *announce = [[AnnounceViewController alloc] init];
    UINavigationController *announceNav = [self setUpOneChildViewController:announce title:@"公告" imageName:@"notice_unchecked" selImageName:@"notice_checked"];
109
    
admin's avatar
admin committed
110 111
    ChartViewController *chart = [[ChartViewController alloc] init];
    UINavigationController *chartNav = [self setUpOneChildViewController:chart title:@"报表查询" imageName:@"report_unchecked" selImageName:@"report_checked"];
112
    
admin's avatar
admin committed
113 114 115 116
    MineViewController *mine = [[MineViewController alloc] init];
    mine.tabBarItem.title= @"我的";
    mine.tabBarItem.image = [UIImage imageNamed:@"aboutme_unchecked"];
    mine.tabBarItem.selectedImage = [[UIImage imageNamed:@"aboutme_checke"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
117
    
admin's avatar
admin committed
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
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       kNavigationBarColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];
    tabBC.viewControllers = @[homeNav, announceNav, chartNav, mine];
    
    [self presentViewController:tabBC animated:YES completion:nil];
}

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

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

// 添加一个控制器的属性
- (UINavigationController *)setUpOneChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selImageName:(NSString *)selImageName
{
    // 添加TabBar按钮的图片文字
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *selImage = [UIImage imageNamed:selImageName];
    selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = selImage;
    
    // 添加nav
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [nav.navigationBar setBarTintColor:kNavigationBarColor];
    [nav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    return nav;
}

#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 - UITabBarViewController Delegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[MineViewController class]]) {
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
        tabBarController.tabBar.hidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
195
    }
admin's avatar
admin committed
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
}


#pragma mark - UItextField Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([self.loginView.usernameTextFiled isFirstResponder]) {
        // 创建下拉菜单
        self.menu = [CustomDropMenuView defaultMenuView];
        _menu.delegate = self;
        _menu.count = _allUserDict.count;
        _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;
}




@end