LoginViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // LoginViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 2/4/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. @import QuartzCore;
  9. #import "RequestHandler.h"
  10. #import "CustomTextField.h"
  11. #import "CustomButton.h"
  12. #import "JDJSONModel.h"
  13. #import "LoginModel.h"
  14. #import "LoginViewController.h"
  15. #import "CustomLabel.h"
  16. #import "CustomCheckBox.h"
  17. #import "KeychainItemWrapper.h"
  18. #import "NewMobileViewController.h"
  19. #import "ValidateUtil.h"
  20. #import "CustomAlertView.h"
  21. #import "CustomImageView.h"
  22. #import "PwdPopupView.h"
  23. //#import "MobilesOverViewController.h"
  24. //#import "InvitationListViewController.h"
  25. @interface LoginViewController () <CustomTextFieldDelegate> {
  26. }
  27. @end
  28. #pragma mark - Class Definition
  29. @implementation LoginViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. [self initUI];
  34. [self prepareViewDidLoad];
  35. }
  36. - (void)initUI {
  37. CGRect screen = [UIScreen mainScreen].bounds;
  38. if (screen.size.height == 480) {//iphone4s
  39. [_imgvBg setImage:[UIImage imageNamed:@"img_login_bg_4inch"]];
  40. } if (screen.size.height == 667.0f) {//iphone6
  41. _constraintLeftPadding.constant *= 1.25;
  42. _constraintRightPadding.constant *= 1.25;
  43. } else if (screen.size.height == 736.0f) {//iphone6 plus
  44. _constraintLeftPadding.constant *= 1.5;
  45. _constraintRightPadding.constant *= 1.5;
  46. }
  47. // [self.view layoutIfNeeded];
  48. _txtEmail.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  49. _txtPasswd.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  50. _txtPasswd.secureTextEntry = YES;
  51. _txtEmail.keyboardType = UIKeyboardTypeEmailAddress;
  52. _txtPasswd.keyboardType = UIKeyboardTypeDefault;
  53. _txtEmail.returnKeyType = UIReturnKeyDone;
  54. _txtPasswd.returnKeyType = UIReturnKeyDone;
  55. _txtPasswd.delegate = self;
  56. //Localization
  57. [_chkAutoLogin setTitle:NSLocalizedString(@"자동로그인", @"자동로그인") forState:UIControlStateNormal];
  58. _txtEmail.placeholder = NSLocalizedString(@"이메일", @"이메일");
  59. _txtPasswd.placeholder = NSLocalizedString(@"비밀번호", @"비밀번호");
  60. _lblSignUp.text = NSLocalizedString(@"회원가입", @"회원가입");
  61. _lblFindId.text = NSLocalizedString(@"아이디/비번찾기", @"아이디/비번찾기");
  62. [_lblSignUp setUnderLine:_lblSignUp.text];
  63. [_lblSignUp addTouchEventHandler:^(id label) {
  64. [self lblSignUpTouched];
  65. }];
  66. [_lblFindId setUnderLine:_lblFindId.text];
  67. [_lblFindId addTouchEventHandler:^(id label) {
  68. [self lblFindIdTouched];
  69. }];
  70. }
  71. - (void)prepareViewDidLoad {
  72. [RequestHandler handler].authorization = nil;
  73. _chkAutoLogin.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_AUTO_LOGIN] boolValue];
  74. if (_chkAutoLogin.checked) {
  75. [self requestAutoLogin];
  76. }
  77. }
  78. #pragma mark - Main Logic
  79. - (void)requestLogin {
  80. NSDictionary *param = @{@"email_id": _txtEmail.text,
  81. @"password": _txtPasswd.text,
  82. @"service_id": MOBILE_SERVICE_ID,
  83. @"os_type": MOBILE_DEVICE_TYPE,
  84. @"device_sn": [JDFacade facade].deviceUUID,
  85. @"device_name": [CommonUtil deviceName],
  86. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  87. @"device_hostname": [JDFacade facade].deviceHostName ? [JDFacade facade].deviceHostName : ksEmptyString};
  88. [[RequestHandler handler] sendAsyncPostRequestAPIPath:API_POST_SIGN_IN parameters:param modelClass:[LoginModel class] completion:^(id responseObject) {
  89. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  90. return;
  91. }
  92. LoginModel *loginInfo = (LoginModel *)responseObject;
  93. NSLog(@"%@", loginInfo);
  94. if (loginInfo) {//API 성공 ,함
  95. [self actionForLoginSucceed:loginInfo];
  96. }
  97. } failure:^(id errorObject) {
  98. JDErrorModel *error = (JDErrorModel *)errorObject;
  99. if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_EMAIL]) {//이메일 아이디 인증이 안 된 계정
  100. [self actionForWaiting];
  101. } else if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_DEVICE]) {//새로운 단말 인증 필요
  102. [self actionForNeedsRegisteringNewDevice:error.nickname];
  103. } else if ([error.errorCode isEqualToString:API_RESPONSE_MOBILES_OVER]) {//단말 초과
  104. [self actionForMobilesOver:error.deviceList];
  105. } else {
  106. [[JDFacade facade] alert:error.errorMessage];
  107. }
  108. }];
  109. }
  110. //자동로그인 요청
  111. - (void)requestAutoLogin {;
  112. //parameters
  113. NSString *authToken = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_AUTOTOKEN];
  114. [RequestHandler handler].authorization = authToken;
  115. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, authToken);
  116. NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID,
  117. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  118. @"os_type": MOBILE_DEVICE_TYPE,
  119. @"device_hostname": [JDFacade facade].deviceHostName ? [JDFacade facade].deviceHostName : ksEmptyString};
  120. NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
  121. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  122. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  123. return;
  124. }
  125. LoginModel *loginInfo = (LoginModel *)responseObject;
  126. if (loginInfo) {//API 성공 ,함
  127. [self actionForLoginSucceed:loginInfo];
  128. }
  129. } failure:^(id errorObject) {
  130. JDErrorModel *error = (JDErrorModel *)errorObject;
  131. if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_DEVICE]) {//새로운 단말 인증 필요
  132. [self actionForNeedsRegisteringNewDevice:error.nickname];
  133. } else {
  134. [[JDFacade facade] alert:error.errorMessage];
  135. }
  136. }];
  137. }
  138. //인증 메일 재요청
  139. - (void)requestAuthMail {
  140. //parameters
  141. NSDictionary *parameter = @{@"email_id": [JDFacade facade].tmpEmailId};
  142. NSString *path = [NSString stringWithFormat:API_POST_AUTH_MAIL];
  143. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  144. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  145. return;
  146. }
  147. JDJSONModel *result = (JDJSONModel *) responseObject;
  148. if (result) {//API 성공 ,
  149. [[JDFacade facade] toast:NSLocalizedString(@"인증 메일이 발송되었습니다", @"인증 메일이 발송되었습니다")];
  150. }
  151. } failure:^(id errorObject) {
  152. JDErrorModel *error = (JDErrorModel *)errorObject;
  153. [[JDFacade facade] alert:error.errorMessage];
  154. }];
  155. }
  156. //로그인 이후 처리.
  157. - (void)actionForLoginSucceed:(LoginModel *)loginInfo {
  158. if (loginInfo) {//API 성공함
  159. [JDFacade facade].loginUser = loginInfo;
  160. if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].tmpEmailId]) {
  161. [JDFacade facade].tmpEmailId = ksEmptyString;
  162. }
  163. if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].loginUser.newEmailId]) {//이메일 변경 요청 중
  164. [JDFacade facade].loginUser.newEmailId = ksEmptyString;
  165. }
  166. [RequestHandler handler].authorization = loginInfo.authToken;
  167. if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장
  168. [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USDEF_APP_AUTO_LOGIN];
  169. [[JDFacade facade] storeObjectToKeychain:[JDFacade facade].loginUser.authToken forKey:USDEF_SESSION_AUTOTOKEN];
  170. } else {
  171. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  172. }
  173. [self actionAfterLogin];
  174. }
  175. }
  176. //홈그룹 체크 및 대시보드 이동
  177. - (void)actionAfterLogin {
  178. if ([[JDFacade facade].loginUser.invitationsCount integerValue] > 0) {
  179. [self actionForInvitation];
  180. return;
  181. }
  182. [self actionAfterInvitaion];
  183. }
  184. //초대알림 화면으로 이동
  185. - (void)actionForInvitation {
  186. [[JDFacade facade] loadInvitationView];
  187. }
  188. //초대화면 액션이 종료된 후,
  189. - (void)actionAfterInvitaion {
  190. //메인으로 이동
  191. [self gotoMainView];
  192. }
  193. //새로운 단말 인증 필요 - 화면 이동
  194. - (void)actionForNeedsRegisteringNewDevice:(NSString *)nickname {
  195. NewMobileViewController *vc = (NewMobileViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"NewMobileViewController" storyboardName:@"SignUp"];
  196. vc.checkedAutoLogin = _chkAutoLogin.checked;
  197. vc.nickname = nickname;
  198. [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
  199. }
  200. //단말대수 초과
  201. - (void)actionForMobilesOver:(NSArray<MobileDeviceModel> *)deviceList {
  202. //SHA-2
  203. }
  204. //인증대기중 - 화면 이동
  205. - (void)actionForWaiting {
  206. NSString *message1 = [NSString stringWithFormat:@"%@에서\n인증 메일을 확인하세요", [JDFacade facade].tmpEmailId];
  207. NSString *message2 = @"\n\n메일을 확인할 수 없는 경우에는\n스팸 메일함을 확인해보세요";
  208. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"메일을 확인하세요" message:[NSString stringWithFormat:@"%@%@", message1, message2] delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
  209. [alert.lblMessage1 setColor:kUITextColor02 text:message1];
  210. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  211. if (buttonIndex == 1) {//재발송
  212. [self requestAuthMail];
  213. }
  214. }];
  215. }
  216. - (void)gotoMainView {
  217. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SignUpCompleteViewController" storyboardName:@"SignUp"];
  218. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MainViewController" storyboardName:@"Main"];
  219. [JDFacade facade].mainViewController = (MainViewController *)vc;
  220. [[JDFacade facade].appDelegate.window setRootViewController:vc];
  221. }
  222. - (void)requestLogout {
  223. //parameters
  224. NSString *path = [NSString stringWithFormat:API_POST_SIGN_OUT];
  225. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
  226. [self actionAfterLogout];
  227. } failure:^(id errorObject) {
  228. JDErrorModel *error = (JDErrorModel *)errorObject;
  229. [[JDFacade facade] alert:error.errorMessage];
  230. }];
  231. }
  232. - (void)actionAfterLogout {
  233. //자동 로그인 설정 - 취소
  234. [[JDFacade facade] storeObjectToUserDefaults:@(NO) forKey:USDEF_APP_AUTO_LOGIN];
  235. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  236. [JDFacade facade].loginUser = nil;
  237. //root view 변경
  238. [[JDFacade facade].appDelegate.window setRootViewController:self];
  239. }
  240. #pragma mark - UI Events
  241. - (IBAction)btnLogInTouched:(id)sender {
  242. // CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:@"테스트 메세지" delegate:nil OKButtonTitle:@"예" cancelButtonTitle:@"아니오"];
  243. // [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  244. // if (buttonIndex == 0) {//OK - 이전화면으로
  245. //
  246. // return;
  247. // }
  248. // }];
  249. //
  250. // return;
  251. #ifdef DEBUG_MODE
  252. if ([_txtEmail.text isEmptyString]) {
  253. _txtEmail.text = @"asura77@ncomz.com";
  254. }
  255. if ([_txtPasswd.text isEmptyString]) {
  256. _txtPasswd.text = @"";
  257. }
  258. #endif
  259. if (![ValidateUtil validateTextfiled:_txtEmail type:ValidateTypeEmail title:@"이메일" ]) {
  260. return;
  261. }
  262. if (![ValidateUtil validateTextfiled:_txtEmail type:ValidateTypeNull title:@"비밀번호" ]) {
  263. return;
  264. }
  265. [JDFacade facade].tmpEmailId = _txtEmail.text;
  266. [JDFacade facade].tmpPassword = _txtPasswd.text;
  267. [self requestLogin];
  268. }
  269. //회원가입 호출
  270. - (void)lblSignUpTouched {
  271. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AgreementViewController" storyboardName:@"SignUp"];
  272. [self presentViewController:vc animated:YES completion:nil];
  273. }
  274. - (void)lblFindIdTouched {
  275. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ResetPwdViewController" storyboardName:@"SignUp"];
  276. [self presentViewController:vc animated:YES completion:nil];
  277. }
  278. #pragma mark - CustomTextField
  279. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  280. if ([textField isEqual:_txtPasswd]) {
  281. [self btnLogInTouched:nil];
  282. }
  283. return YES;
  284. }
  285. #pragma mark - MemoryWarning
  286. - (void)didReceiveMemoryWarning
  287. {
  288. [super didReceiveMemoryWarning];
  289. // Dispose of any resources that can be recreated.
  290. }
  291. @end