LoginViewController.m 14 KB

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