ICRLoginViewController.m 8.76 KB
Newer Older
mei's avatar
mei committed
1 2
//
//  ICRLoginViewController.m
mei's avatar
mei committed
3
//  XFFruit
mei's avatar
mei committed
4 5 6 7 8 9 10 11
//
//  Created by Xummer on 3/22/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "ICRLoginViewController.h"
#import "ICRLoginContentView.h"

12 13
#import "GXFFunctionDB.h"
#import "GXFFunctionDBHelper.h"
freecui's avatar
freecui committed
14
#import "APService.h"
zhu's avatar
zhu committed
15
#import "ICRAppDelegate.h"
16

mei's avatar
mei committed
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
#define LOGIN_CONTAINER_LEFT_MARGIN         (20)
#define LOGIN_CONTAINER_HEIGHT              (450)

@interface ICRLoginViewController ()
{
    CGFloat m_fDefaultContainerY;
}
@property (strong, nonatomic) ICRLoginContentView *m_containerView;
@end

@implementation ICRLoginViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupSubviews];
}

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

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    if (m_fDefaultContainerY == 0) {
        m_fDefaultContainerY = _m_containerView.y;
    }
}

mei's avatar
mei committed
49 50
- (void)viewWillAppear:(BOOL)animated {}

mei's avatar
mei committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self
                   selector:@selector(inputTextEditChanged:)
                       name:UITextFieldTextDidChangeNotification
                     object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter removeObserver:self];
}

#pragma mark - Private Method
- (void)setupSubviews {
    UIImageView *bg = [[UIImageView alloc] initWithFrame:self.view.bounds];
freecui's avatar
freecui committed
71
    bg.image = [UIImage imageWithColor:GXF_SAVE_COLOR];//
mei's avatar
mei committed
72
    [bg autoresizingWithStrechFullSize];
freecui's avatar
freecui committed
73
   
mei's avatar
mei committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(onBGTapped:)];
    bg.userInteractionEnabled = YES;
    [bg addGestureRecognizer:tapGesture];
    
    [self.view addSubview:bg];
    
    self.m_containerView =
    [[ICRLoginContentView alloc] initWithFrame:(CGRect){
        .origin.x = LOGIN_CONTAINER_LEFT_MARGIN,
        .origin.y = (self.view.height - LOGIN_CONTAINER_HEIGHT) * .5f,
        .size.width = self.view.width - 2 * LOGIN_CONTAINER_LEFT_MARGIN,
        .size.height = LOGIN_CONTAINER_HEIGHT
    }
                                     showCCode:YES];
    [_m_containerView autoresizingWithVerticalCenter];
mei's avatar
mei committed
91
    
mei's avatar
mei committed
92 93 94 95 96 97
    [_m_containerView.m_loginBtn addTarget:self
                                    action:@selector(onLoginTapped:)
                          forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_m_containerView];
    
管鹏飞's avatar
管鹏飞 committed
98
//#if DEBUG
mei's avatar
mei committed
99 100 101
//    self.m_containerView.m_cCodeTextF.text = @"A93jie957lK";
//    self.m_containerView.m_userNameTextF.text = @"kevin";
//    self.m_containerView.m_passwordTextF.text = @"aaa";
freecui's avatar
freecui committed
102 103

    //@"http://218.244.151.129:7580/cruiser-server/rest"
管鹏飞's avatar
管鹏飞 committed
104 105 106
//    self.m_containerView.m_cCodeTextF.text = @"211534962";
//    self.m_containerView.m_userNameTextF.text = @"admin";
//    self.m_containerView.m_passwordTextF.text = @"thoradmin";
freecui's avatar
freecui committed
107 108
    
    
陈俊俊's avatar
陈俊俊 committed
109
//@"http://122.224.171.126:7280/cruiser-server/rest"
陈俊俊's avatar
陈俊俊 committed
110 111 112
//        self.m_containerView.m_cCodeTextF.text = @"923";
//        self.m_containerView.m_userNameTextF.text = @"admin";
//        self.m_containerView.m_passwordTextF.text = @"thoradmin";
陈俊俊's avatar
陈俊俊 committed
113 114

    
陈俊俊's avatar
陈俊俊 committed
115 116 117 118
//现场正式环境登陆
//    self.m_containerView.m_cCodeTextF.text = @"211534968";
//    self.m_containerView.m_userNameTextF.text = @"admin";
//    self.m_containerView.m_passwordTextF.text = @"thoradmin";
陈俊俊's avatar
陈俊俊 committed
119
    
管鹏飞's avatar
管鹏飞 committed
120
    ICRUserUtil *userU = [ICRUserUtil sharedInstance];
mei's avatar
mei committed
121
    
管鹏飞's avatar
管鹏飞 committed
122 123 124 125 126 127 128
    self.m_containerView.m_cCodeTextF.text = userU.registerCode; //*registerCode;  //企业认证码
    self.m_containerView.m_userNameTextF.text =userU.userName;
    self.m_containerView.m_passwordTextF.text = userU.password;
    [self.m_containerView checkLoginEnable];
//#else
//    
//#endif
mei's avatar
mei committed
129 130 131 132 133 134 135 136 137 138 139
}

#pragma mark - Logic


#pragma mark - Actions
- (void)onBGTapped:(__unused id)sender {
    [self.view endEditing:YES];
}

- (void)onLoginTapped:(__unused id)sender {
zhu's avatar
zhu committed
140 141 142
//
    ICRAppDelegate *appDelegate = (ICRAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate comperVersion];
陈俊俊's avatar
陈俊俊 committed
143 144 145 146 147 148
    NSString *nsRegisterCode = self.m_containerView.m_cCodeTextF.text;
    NSString *nsUserName = self.m_containerView.m_userNameTextF.text;
    NSString *nsPassword = [self.m_containerView.m_passwordTextF.text uppercaseMD5String];
//        NSString *nsRegisterCode = @"211534962";
//        NSString *nsUserName = @"admin";
//        NSString *nsPassword = @"thoradmin";
陈俊俊's avatar
陈俊俊 committed
149
   
mei's avatar
mei committed
150
    
陈俊俊's avatar
陈俊俊 committed
151 152 153
    void(^succ)(id) = ^(id data) {
        ICRUserUtil *userUtil = [ICRUserUtil sharedInstance];
        userUtil.bAutoLogin = @( [_m_containerView isAutoLogin] );
管鹏飞's avatar
管鹏飞 committed
154
      
陈俊俊's avatar
陈俊俊 committed
155 156 157 158 159
        [userUtil saveArchive];
        
        ICRAppViewControllerManager *mgr =
        [ICRAppViewControllerManager getAppViewControllerManager];
        [mgr openMainFrame];
160 161 162 163 164 165 166
        
        
#pragma 可以开新线程 ,待优化
        //当登陆成功时就建立功能数据表
        [[GXFFunctionDB sharedInstance]createTables];
        
        //完成对初始化数据书库的操作
freecui's avatar
freecui committed
167 168 169 170 171 172 173
        if (![[GXFFunctionDBHelper sharedInstance] successInsertOriginFunctions]) {
            [[GXFFunctionDBHelper sharedInstance] insertAllOriginFunctions];
        }
        if (![[GXFFunctionDBHelper sharedInstance] sucessInsertOriginDisplayFunctions]) {
            [[GXFFunctionDBHelper sharedInstance] insertAllOriginDisplayFunctions];
        }
        
freecui's avatar
freecui committed
174

freecui's avatar
freecui committed
175 176 177 178 179 180 181 182 183 184
        
        //更改功能权限
        for (int count = 0; count < userUtil.permissions.count; count ++) {
            GXFFunction *function = [[GXFFunctionDB sharedInstance]functionSelectWithId:[userUtil.permissions[count] integerValue]];
            if (function.Id) {
                function.hasPermission = 1;
                [[GXFFunctionDB sharedInstance] updateFunctionHasPermissionWithId:function];
            }
        }
        
freecui's avatar
freecui committed
185 186 187 188
        //配置功能 不是默认配置
        NSUserDefaults *f_default = [NSUserDefaults standardUserDefaults];
        BOOL noDefault = [[f_default objectForKey:@"noDefault"] integerValue] > 0 ? YES : NO;
        if (!noDefault) {
freecui's avatar
freecui committed
189 190
            [[GXFFunctionDB sharedInstance]defaultdisplayFunctions];
        }
freecui's avatar
freecui committed
191
        
192
        
freecui's avatar
freecui committed
193 194 195
        //极光别名
        NSSet *setTags = [NSSet setWithArray:@[@""]];
        [APService setTags:setTags
freecui's avatar
freecui committed
196
                     alias:userUtil.userId
freecui's avatar
freecui committed
197 198
          callbackSelector:nil object:nil];
        
陈俊俊's avatar
陈俊俊 committed
199
    };
mei's avatar
mei committed
200
    
陈俊俊's avatar
陈俊俊 committed
201
    void(^fail)(id) = ^(id data) {
freecui's avatar
freecui committed
202 203 204
        CLog(@"error =%@",data);
       // [IBTLoadingView showTips:data];//@"输入的信息不正确,请重新输入"
        [IBTLoadingView showTextOnly: data inView:self.view];
陈俊俊's avatar
陈俊俊 committed
205 206 207 208 209 210 211
    };
    ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
    [httpCtrl doLoginWithUserName:nsUserName
                         password:nsPassword
                     registerCode:nsRegisterCode
                          success:succ
                          failure:fail];
mei's avatar
mei committed
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
}

#pragma mark - TextObserver
- (void)inputTextEditChanged:(NSNotification *)obj {
    [self.m_containerView checkLoginEnable];
}

#pragma mark - Keyboard
- (void)keyboardWillShow:(NSNotification *)note {
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
    
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    // set views with new info
    self.m_containerView.y = - 50.0f;
    
    // commit animations
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)note {
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    // set views with new info
    self.m_containerView.y = m_fDefaultContainerY;
    
    // commit animations
    [UIView commitAnimations];
}

@end