LoginViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. /**
  26. ID / PW Input Box Text Color
  27. - 기본 : kUITextColor01
  28. - Hint : kUITextColor03
  29. - Disable : kUITextColor06
  30. 로그인 유지 / 아이디 저장 Text Color : kUITextColor01
  31. 아이디 찾기 / 비밀번호 찾기 / 회원가입하기 Text Color
  32. - 아이디 찾기,비밀번호 찾기 : kUITextColor01
  33. - 회원가입 하기 : kUITextColor04
  34. **/
  35. @interface LoginViewController () <CustomTextFieldDelegate> {
  36. }
  37. @end
  38. #pragma mark - Class Definition
  39. @implementation LoginViewController
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Do any additional setup after loading the view.
  43. [self initUI];
  44. [self prepareViewDidLoad];
  45. }
  46. - (void)initUI {
  47. [self.btnLogin setBackgroundImage:[UIImage imageNamed:@"img_btn_common_active"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  48. [self.btnLogin setBackgroundImage:[UIImage imageNamed:@"img_btn_common_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  49. [self.btnLogin setBackgroundImage:[UIImage imageNamed:@"img_btn_common_disable"] forState:UIControlStateDisabled capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  50. [self.lblFindId setHidden:YES];
  51. CGRect screen = [UIScreen mainScreen].bounds;
  52. if (screen.size.height == 480) {//iphone4s
  53. [_imgvBg setImage:[UIImage imageNamed:@"img_login_bg_4inch"]];
  54. } if (screen.size.height == 667.0f) {//iphone6
  55. _constraintLeftPadding.constant *= 1.25;
  56. _constraintRightPadding.constant *= 1.25;
  57. } else if (screen.size.height == 736.0f) {//iphone6 plus
  58. _constraintLeftPadding.constant *= 1.5;
  59. _constraintRightPadding.constant *= 1.5;
  60. }
  61. // [self.view layoutIfNeeded];
  62. _txtEmail.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  63. _txtPasswd.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  64. _txtPasswd.secureTextEntry = YES;
  65. _txtEmail.keyboardType = UIKeyboardTypeDefault;
  66. _txtPasswd.keyboardType = UIKeyboardTypeDefault;
  67. _txtEmail.returnKeyType = UIReturnKeyNext;
  68. _txtPasswd.returnKeyType = UIReturnKeyDone;
  69. _txtPasswd.delegate = self;
  70. //Localization
  71. [_chkAutoLogin setTitle:NSLocalizedString(@"로그인 유지", @"로그인 유지") forState:UIControlStateNormal];
  72. [_chkIdSave setTitle:NSLocalizedString(@"아이디 저장", @"아이디 저장") forState:UIControlStateNormal];
  73. _txtEmail.placeholder = NSLocalizedString(@"아이디", @"아이디");
  74. _txtPasswd.placeholder = NSLocalizedString(@"비밀번호", @"비밀번호");
  75. _lblSignUp.text = NSLocalizedString(@"회원가입하기", @"회원가입하기");
  76. _lblFindId.text = NSLocalizedString(@"아이디/비번찾기", @"아이디/비번찾기");
  77. // [_lblSignUp setUnderLine:_lblSignUp.text];
  78. [_lblSignUp addTouchEventHandler:^(id label) {
  79. [self lblSignUpTouched];
  80. }];
  81. [_lblFindId setUnderLine:_lblFindId.text];
  82. [_lblFindId addTouchEventHandler:^(id label) {
  83. [self lblFindIdTouched];
  84. }];
  85. }
  86. - (void)prepareViewDidLoad {
  87. [RequestHandler handler].authorization = nil;
  88. _chkAutoLogin.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_AUTO_LOGIN] boolValue];
  89. _chkIdSave.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_SAVE_ID] boolValue];
  90. if (_chkAutoLogin.checked) {
  91. [self requestAutoLogin];
  92. // _txtEmail.text = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID];
  93. // _txtPasswd.text = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_PWD];
  94. //
  95. // [self btnLogInTouched:_btnLogin];
  96. }
  97. if (_chkIdSave.checked) {
  98. NSLog(@"Save ID : %@", [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID]);
  99. _txtEmail.text = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID];
  100. }
  101. }
  102. #pragma mark - Main Logic
  103. - (void)requestLogin {
  104. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_LOGIN];
  105. NSDictionary *param = @{@"member_id": [_txtEmail.text trim],
  106. @"password": [_txtPasswd.text trim],
  107. @"os_type": MOBILE_DEVICE_TYPE,
  108. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString};
  109. [request setRequestMsg:param];
  110. [self sendDataToSocket:request modelClass:[LoginModel class] isLoading:YES];
  111. }
  112. //자동로그인 요청
  113. - (void)requestAutoLogin {;
  114. LoginModel *tmpInfo = [[LoginModel alloc] init];
  115. tmpInfo.memberId = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID];
  116. tmpInfo.authToken = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_AUTOTOKEN];
  117. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_AUTO_LOGIN];
  118. NSDictionary *param = @{@"Authorization": tmpInfo.authorization,
  119. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  120. @"os_type": MOBILE_DEVICE_TYPE};
  121. [request setRequestMsg:param];
  122. [self sendDataToSocket:request modelClass:[LoginModel class] isLoading:YES];
  123. // NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
  124. //
  125. // [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  126. // if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  127. // return;
  128. // }
  129. //
  130. // LoginModel *loginInfo = (LoginModel *)responseObject;
  131. //
  132. // if (loginInfo) {//API 성공 ,함
  133. // [self actionForLoginSucceed:loginInfo];
  134. //
  135. // }
  136. // } failure:^(id errorObject) {
  137. // JDErrorModel *error = (JDErrorModel *)errorObject;
  138. //
  139. // if ([error.errorCode isEqualToString:API_RESPONSE_UNAUTHORIZED_DEVICE]) {//새로운 단말 인증 필요
  140. // [self actionForNeedsRegisteringNewDevice:error.nickname];
  141. // } else {
  142. // [[JDFacade facade] alert:error.errorMessage];
  143. // }
  144. // }];
  145. }
  146. //인증 메일 재요청
  147. - (void)requestAuthMail {
  148. //parameters
  149. NSDictionary *parameter = @{@"email_id": [JDFacade facade].tmpEmailId};
  150. NSString *path = [NSString stringWithFormat:API_POST_AUTH_MAIL];
  151. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  152. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  153. return;
  154. }
  155. JDJSONModel *result = (JDJSONModel *) responseObject;
  156. if (result) {//API 성공 ,
  157. [[JDFacade facade] toast:NSLocalizedString(@"인증 메일이 발송되었습니다", @"인증 메일이 발송되었습니다")];
  158. }
  159. } failure:^(id errorObject) {
  160. JDErrorModel *error = (JDErrorModel *)errorObject;
  161. [[JDFacade facade] alert:error.errorMessage];
  162. }];
  163. }
  164. //로그인 이후 처리.
  165. - (void)actionForLoginSucceed:(LoginModel *)loginInfo {
  166. if (loginInfo) {//API 성공함
  167. [JDFacade facade].loginUser = loginInfo;
  168. // [JDFacade facade].loginUser.gradeCode = KNEET_MEMBER_MASTER;
  169. // [JDFacade facade].loginUser.level = 90;
  170. NSLog(@"%@", [JDFacade facade].loginUser);
  171. if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].tmpEmailId]) {
  172. [JDFacade facade].tmpEmailId = ksEmptyString;
  173. }
  174. if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].loginUser.newEmailId]) {//이메일 변경 요청 중
  175. [JDFacade facade].loginUser.newEmailId = ksEmptyString;
  176. }
  177. [RequestHandler handler].authorization = loginInfo.authorization;
  178. [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USDEF_APP_AUTO_LOGIN];
  179. [[JDFacade facade] storeObjectToUserDefaults:@(_chkIdSave.checked) forKey:USDEF_APP_SAVE_ID];
  180. if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장
  181. [[JDFacade facade] storeObjectToKeychain:loginInfo.memberId forKey:USDEF_SESSION_LOGIN_ID];
  182. [[JDFacade facade] storeObjectToKeychain:loginInfo.authToken forKey:USDEF_SESSION_AUTOTOKEN];
  183. } else {
  184. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  185. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_LOGIN_PWD];
  186. if (!_chkIdSave.checked) {
  187. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_LOGIN_ID];
  188. }
  189. }
  190. if (_chkIdSave.checked) {
  191. [[JDFacade facade] storeObjectToKeychain:[_txtEmail.text trim] forKey:USDEF_SESSION_LOGIN_ID];
  192. } else {
  193. if (!_chkAutoLogin.checked) {
  194. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_LOGIN_ID];
  195. }
  196. }
  197. [self actionAfterLogin];
  198. }
  199. }
  200. //홈그룹 체크 및 대시보드 이동
  201. - (void)actionAfterLogin {
  202. if ([[JDFacade facade].loginUser.invitationsCount integerValue] > 0) {
  203. [self actionForInvitation];
  204. return;
  205. }
  206. [self actionAfterInvitaion];
  207. }
  208. //초대알림 화면으로 이동
  209. - (void)actionForInvitation {
  210. [[JDFacade facade] loadInvitationView];
  211. }
  212. //초대화면 액션이 종료된 후,
  213. - (void)actionAfterInvitaion {
  214. //메인으로 이동
  215. [self gotoMainView];
  216. }
  217. //새로운 단말 인증 필요 - 화면 이동
  218. - (void)actionForNeedsRegisteringNewDevice:(NSString *)nickname {
  219. NewMobileViewController *vc = (NewMobileViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"NewMobileViewController" storyboardName:@"SignUp"];
  220. vc.checkedAutoLogin = _chkAutoLogin.checked;
  221. vc.nickname = nickname;
  222. [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
  223. }
  224. //단말대수 초과
  225. - (void)actionForMobilesOver:(NSArray<MobileDeviceModel> *)deviceList {
  226. //SHA-2
  227. }
  228. //인증대기중 - 화면 이동
  229. - (void)actionForWaiting {
  230. NSString *message1 = [NSString stringWithFormat:@"%@에서\n인증 메일을 확인하세요", [JDFacade facade].tmpEmailId];
  231. NSString *message2 = @"\n\n메일을 확인할 수 없는 경우에는\n스팸 메일함을 확인해보세요";
  232. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"메일을 확인하세요" message:[NSString stringWithFormat:@"%@%@", message1, message2] delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
  233. [alert.lblMessage1 setColor:kUITextColor02 text:message1];
  234. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  235. if (buttonIndex == 1) {//재발송
  236. [self requestAuthMail];
  237. }
  238. }];
  239. }
  240. - (void)gotoMainView {
  241. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SignUpCompleteViewController" storyboardName:@"SignUp"];
  242. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MainViewController" storyboardName:@"Main"];
  243. [JDFacade facade].mainViewController = (MainViewController *)vc;
  244. [[JDFacade facade].appDelegate.window setRootViewController:vc];
  245. }
  246. - (void)requestLogout {
  247. [JDFacade facade].loginUser = nil;
  248. [self actionAfterLogout];
  249. // NSDictionary *param = @{API_AUTHORIZATION_KEY: [[JDFacade facade].loginUser authorization],
  250. // @"message_type": @"auth.out"};
  251. //
  252. // [self sendDataToSocket:param];
  253. //parameters
  254. // [self actionAfterLogout];
  255. // NSString *path = [NSString stringWithFormat:API_POST_SIGN_OUT];
  256. //
  257. // [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
  258. // [self actionAfterLogout];
  259. //
  260. // } failure:^(id errorObject) {
  261. // JDErrorModel *error = (JDErrorModel *)errorObject;
  262. // [[JDFacade facade] alert:error.errorMessage];
  263. // }];
  264. }
  265. - (void)actionAfterLogout {
  266. //자동 로그인 설정 - 취소
  267. [[JDFacade facade] storeObjectToUserDefaults:@(NO) forKey:USDEF_APP_AUTO_LOGIN];
  268. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  269. if (![[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_SAVE_ID] boolValue]) {
  270. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_LOGIN_ID];
  271. }
  272. [JDFacade facade].loginUser = nil;
  273. [self closeSocket];
  274. //root view 변경
  275. [[JDFacade facade].appDelegate.window setRootViewController:self];
  276. }
  277. #pragma mark - UI Events
  278. - (IBAction)btnLogInTouched:(id)sender {
  279. #ifdef DEBUG_MODE
  280. if ([_txtEmail.text isEmptyString]) {
  281. _txtEmail.text = @"asura77";
  282. }
  283. if ([_txtPasswd.text isEmptyString]) {
  284. _txtPasswd.text = @"09767955";
  285. }
  286. #endif
  287. // TODO : 아이디 형식 검사할것
  288. // if (![ValidateUtil validateTextfiled:_txtEmail type:ValidateTypeEmail title:@"이메일" ]) {
  289. // return;
  290. // }
  291. if (![ValidateUtil validateTextfiled:_txtEmail type:ValidateTypeNull title:@"비밀번호" ]) {
  292. return;
  293. }
  294. [JDFacade facade].tmpEmailId = [_txtEmail.text trim];
  295. [JDFacade facade].tmpPassword = [_txtPasswd.text trim];
  296. [self requestLogin];
  297. }
  298. //회원가입 호출
  299. - (void)lblSignUpTouched {
  300. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AgreementViewController" storyboardName:@"SignUp"];
  301. UINavigationController *m_navi =[[UINavigationController alloc]initWithRootViewController:vc];
  302. [self presentViewController:m_navi animated:YES completion:nil];
  303. }
  304. - (void)lblFindIdTouched {
  305. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ResetPwdViewController" storyboardName:@"SignUp"];
  306. [self presentViewController:vc animated:YES completion:nil];
  307. }
  308. // 아이디 찾기 호출
  309. - (IBAction)btnIdFindTouched:(id)sender {
  310. NSLog(@"아이디 찾기");
  311. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"IdFindViewController" storyboardName:@"SignUp"];
  312. UINavigationController *m_navi =[[UINavigationController alloc]initWithRootViewController:vc];
  313. [self presentViewController:m_navi animated:YES completion:nil];
  314. }
  315. // 비밀번호 찾기 호출
  316. - (IBAction)btnPwFindTouched:(id)sender {
  317. NSLog(@"비밀번호 찾기");
  318. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"PwFindViewController" storyboardName:@"SignUp"];
  319. UINavigationController *m_navi =[[UINavigationController alloc]initWithRootViewController:vc];
  320. [self presentViewController:m_navi animated:YES completion:nil];
  321. }
  322. #pragma mark - CustomTextField
  323. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  324. if ([textField isEqual:_txtPasswd])
  325. {
  326. [self btnLogInTouched:nil];
  327. }
  328. if ([textField isEqual:_txtEmail])
  329. {
  330. [_txtPasswd becomeFirstResponder];
  331. }
  332. return YES;
  333. }
  334. #pragma mark - SocketService
  335. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  336. {
  337. NSLog(@"Result Info : %@", result);
  338. NSLog(@"Result Info : %@", message);
  339. SWITCH(result.messageType)
  340. {
  341. CASE (MSG_TYPE_LOGIN)
  342. {
  343. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  344. if (result.isSuccess) {
  345. LoginModel *loginInfo = (LoginModel *)message;
  346. [self actionForLoginSucceed:loginInfo];
  347. }
  348. else
  349. {
  350. [[JDFacade facade] toast:result.resultMsg];
  351. }
  352. break;
  353. }
  354. CASE (MSG_TYPE_AUTO_LOGIN)
  355. {
  356. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  357. if (result.isSuccess) {
  358. LoginModel *loginInfo = (LoginModel *)message;
  359. loginInfo.memberId = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID];
  360. loginInfo.authToken = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_AUTOTOKEN];
  361. [self actionForLoginSucceed:loginInfo];
  362. }
  363. else
  364. {
  365. [[JDFacade facade] toast:result.resultMsg];
  366. }
  367. break;
  368. }
  369. CASE(@"auth.out")
  370. {
  371. if (result.isSuccess) {
  372. [self actionAfterLogout];
  373. }
  374. else {
  375. NSLog(@"Result Info : %@", result);
  376. }
  377. }
  378. DEFAULT
  379. {
  380. break;
  381. }
  382. }
  383. }
  384. - (void) socketDidFailWithError:(NSError *)error {
  385. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  386. }
  387. #pragma mark - MemoryWarning
  388. - (void)didReceiveMemoryWarning
  389. {
  390. [super didReceiveMemoryWarning];
  391. // Dispose of any resources that can be recreated.
  392. }
  393. @end