| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- //
- // LoginViewController.m
- // kneet
- //
- // Created by Jason Lee on 2/4/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- @import QuartzCore;
- #import "RequestHandler.h"
- #import "CustomTextField.h"
- #import "CustomButton.h"
- #import "JDJSONModel.h"
- #import "LoginModel.h"
- #import "LoginViewController.h"
- #import "CustomLabel.h"
- #import "CustomCheckBox.h"
- #import "KeychainItemWrapper.h"
- #import "NewMobileViewController.h"
- #import "ValidateUtil.h"
- #import "CustomAlertView.h"
- #import "CustomImageView.h"
- #import "PwdPopupView.h"
- //#import "MobilesOverViewController.h"
- //#import "InvitationListViewController.h"
- @interface LoginViewController () <CustomTextFieldDelegate> {
- }
- @end
- #pragma mark - Class Definition
- @implementation LoginViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
- CGRect screen = [UIScreen mainScreen].bounds;
- if (screen.size.height == 480) {//iphone4s
- [_imgvBg setImage:[UIImage imageNamed:@"img_login_bg_4inch"]];
-
- } if (screen.size.height == 667.0f) {//iphone6
- _constraintLeftPadding.constant *= 1.25;
- _constraintRightPadding.constant *= 1.25;
-
- } else if (screen.size.height == 736.0f) {//iphone6 plus
-
- _constraintLeftPadding.constant *= 1.5;
- _constraintRightPadding.constant *= 1.5;
- }
-
- // [self.view layoutIfNeeded];
-
- _txtEmail.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
- _txtPasswd.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
- _txtPasswd.secureTextEntry = YES;
- _txtEmail.keyboardType = UIKeyboardTypeEmailAddress;
- _txtPasswd.keyboardType = UIKeyboardTypeDefault;
- _txtEmail.returnKeyType = UIReturnKeyDone;
- _txtPasswd.returnKeyType = UIReturnKeyDone;
- _txtPasswd.delegate = self;
-
- //Localization
- [_chkAutoLogin setTitle:NSLocalizedString(@"자동로그인", @"자동로그인") forState:UIControlStateNormal];
- _txtEmail.placeholder = NSLocalizedString(@"이메일", @"이메일");
- _txtPasswd.placeholder = NSLocalizedString(@"비밀번호", @"비밀번호");
-
- _lblSignUp.text = NSLocalizedString(@"회원가입", @"회원가입");
- _lblFindId.text = NSLocalizedString(@"아이디/비번찾기", @"아이디/비번찾기");
-
-
- [_lblSignUp setUnderLine:_lblSignUp.text];
- [_lblSignUp addTouchEventHandler:^(id label) {
- [self lblSignUpTouched];
- }];
-
- [_lblFindId setUnderLine:_lblFindId.text];
- [_lblFindId addTouchEventHandler:^(id label) {
- [self lblFindIdTouched];
- }];
- }
- - (void)prepareViewDidLoad {
- [RequestHandler handler].authorization = nil;
- _chkAutoLogin.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_AUTO_LOGIN] boolValue];
- if (_chkAutoLogin.checked) {
- [self requestAutoLogin];
- }
- }
- #pragma mark - Main Logic
- - (void)requestLogin {
- NSDictionary *param = @{@"email_id": _txtEmail.text,
- @"password": _txtPasswd.text,
- @"service_id": MOBILE_SERVICE_ID,
- @"os_type": MOBILE_DEVICE_TYPE,
- @"device_sn": [JDFacade facade].deviceUUID,
- @"device_name": [CommonUtil deviceName],
- @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
- @"device_hostname": [JDFacade facade].deviceHostName ? [JDFacade facade].deviceHostName : ksEmptyString};
-
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:API_POST_SIGN_IN parameters:param modelClass:[LoginModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
- LoginModel *loginInfo = (LoginModel *)responseObject;
- NSLog(@"%@", loginInfo);
- if (loginInfo) {//API 성공 ,함
- [self actionForLoginSucceed:loginInfo];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_EMAIL]) {//이메일 아이디 인증이 안 된 계정
- [self actionForWaiting];
- } else if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_DEVICE]) {//새로운 단말 인증 필요
- [self actionForNeedsRegisteringNewDevice:error.nickname];
- } else if ([error.errorCode isEqualToString:API_RESPONSE_MOBILES_OVER]) {//단말 초과
- [self actionForMobilesOver:error.deviceList];
- } else {
- [[JDFacade facade] alert:error.errorMessage];
- }
- }];
- }
- //자동로그인 요청
- - (void)requestAutoLogin {;
- //parameters
- NSString *authToken = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_AUTOTOKEN];
-
- [RequestHandler handler].authorization = authToken;
- NSLog(@"%s\n %@", __PRETTY_FUNCTION__, authToken);
- NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID,
- @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
- @"os_type": MOBILE_DEVICE_TYPE,
- @"device_hostname": [JDFacade facade].deviceHostName ? [JDFacade facade].deviceHostName : ksEmptyString};
- NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
- LoginModel *loginInfo = (LoginModel *)responseObject;
- if (loginInfo) {//API 성공 ,함
- [self actionForLoginSucceed:loginInfo];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_DEVICE]) {//새로운 단말 인증 필요
- [self actionForNeedsRegisteringNewDevice:error.nickname];
- } else {
- [[JDFacade facade] alert:error.errorMessage];
- }
- }];
- }
- //인증 메일 재요청
- - (void)requestAuthMail {
- //parameters
- NSDictionary *parameter = @{@"email_id": [JDFacade facade].tmpEmailId};
-
- NSString *path = [NSString stringWithFormat:API_POST_AUTH_MAIL];
-
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- JDJSONModel *result = (JDJSONModel *) responseObject;
-
- if (result) {//API 성공 ,
- [[JDFacade facade] toast:NSLocalizedString(@"인증 메일이 발송되었습니다", @"인증 메일이 발송되었습니다")];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- //로그인 이후 처리.
- - (void)actionForLoginSucceed:(LoginModel *)loginInfo {
- if (loginInfo) {//API 성공함
- [JDFacade facade].loginUser = loginInfo;
-
- if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].tmpEmailId]) {
- [JDFacade facade].tmpEmailId = ksEmptyString;
- }
- if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].loginUser.newEmailId]) {//이메일 변경 요청 중
- [JDFacade facade].loginUser.newEmailId = ksEmptyString;
- }
- [RequestHandler handler].authorization = loginInfo.authToken;
- if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장
- [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USDEF_APP_AUTO_LOGIN];
- [[JDFacade facade] storeObjectToKeychain:[JDFacade facade].loginUser.authToken forKey:USDEF_SESSION_AUTOTOKEN];
- } else {
- [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
- }
- [self actionAfterLogin];
- }
- }
- //홈그룹 체크 및 대시보드 이동
- - (void)actionAfterLogin {
- if ([[JDFacade facade].loginUser.invitationsCount integerValue] > 0) {
- [self actionForInvitation];
- return;
- }
- [self actionAfterInvitaion];
- }
- //초대알림 화면으로 이동
- - (void)actionForInvitation {
- [[JDFacade facade] loadInvitationView];
- }
- //초대화면 액션이 종료된 후,
- - (void)actionAfterInvitaion {
-
- //메인으로 이동
- [self gotoMainView];
- }
- //새로운 단말 인증 필요 - 화면 이동
- - (void)actionForNeedsRegisteringNewDevice:(NSString *)nickname {
- NewMobileViewController *vc = (NewMobileViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"NewMobileViewController" storyboardName:@"SignUp"];
- vc.checkedAutoLogin = _chkAutoLogin.checked;
- vc.nickname = nickname;
- [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
- }
- //단말대수 초과
- - (void)actionForMobilesOver:(NSArray<MobileDeviceModel> *)deviceList {
- //SHA-2
- }
- //인증대기중 - 화면 이동
- - (void)actionForWaiting {
- NSString *message1 = [NSString stringWithFormat:@"%@에서\n인증 메일을 확인하세요", [JDFacade facade].tmpEmailId];
- NSString *message2 = @"\n\n메일을 확인할 수 없는 경우에는\n스팸 메일함을 확인해보세요";
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"메일을 확인하세요" message:[NSString stringWithFormat:@"%@%@", message1, message2] delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
- [alert.lblMessage1 setColor:kUITextColor02 text:message1];
-
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 1) {//재발송
- [self requestAuthMail];
- }
- }];
- }
- - (void)gotoMainView {
- // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SignUpCompleteViewController" storyboardName:@"SignUp"];
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MainViewController" storyboardName:@"Main"];
-
- [JDFacade facade].mainViewController = (MainViewController *)vc;
- [[JDFacade facade].appDelegate.window setRootViewController:vc];
- }
- - (void)requestLogout {
- //parameters
- NSString *path = [NSString stringWithFormat:API_POST_SIGN_OUT];
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
- [self actionAfterLogout];
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)actionAfterLogout {
- //자동 로그인 설정 - 취소
- [[JDFacade facade] storeObjectToUserDefaults:@(NO) forKey:USDEF_APP_AUTO_LOGIN];
- [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
-
- [JDFacade facade].loginUser = nil;
-
- //root view 변경
- [[JDFacade facade].appDelegate.window setRootViewController:self];
- }
- #pragma mark - UI Events
- - (IBAction)btnLogInTouched:(id)sender {
- // CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:@"테스트 메세지" delegate:nil OKButtonTitle:@"예" cancelButtonTitle:@"아니오"];
- // [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- // if (buttonIndex == 0) {//OK - 이전화면으로
- //
- // return;
- // }
- // }];
- //
- // return;
- #ifdef DEBUG_MODE
- if ([_txtEmail.text isEmptyString]) {
- _txtEmail.text = @"asura77@ncomz.com";
- }
- if ([_txtPasswd.text isEmptyString]) {
- _txtPasswd.text = @"";
- }
- #endif
- if (![ValidateUtil validateTextfiled:_txtEmail type:ValidateTypeEmail title:@"이메일" ]) {
- return;
- }
- if (![ValidateUtil validateTextfiled:_txtEmail type:ValidateTypeNull title:@"비밀번호" ]) {
- return;
- }
- [JDFacade facade].tmpEmailId = _txtEmail.text;
- [JDFacade facade].tmpPassword = _txtPasswd.text;
- [self requestLogin];
- }
- //회원가입 호출
- - (void)lblSignUpTouched {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AgreementViewController" storyboardName:@"SignUp"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (void)lblFindIdTouched {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ResetPwdViewController" storyboardName:@"SignUp"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- #pragma mark - CustomTextField
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- if ([textField isEqual:_txtPasswd]) {
- [self btnLogInTouched:nil];
- }
- return YES;
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|