Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. import { CommunicationService } from './../../providers/communication-service';
  2. import { GlobalService } from './../../providers/global-service';
  3. import { Component } from '@angular/core';
  4. import { NavController, NavParams } from 'ionic-angular';
  5. import 'rxjs/add/operator/map';
  6. import { Home } from '../home/home';
  7. import { Regist } from '../regist/regist';
  8. import { Session } from '../../providers/session';
  9. import { HttpService } from '../../providers/http-service';
  10. import { Facebook, FacebookLoginResponse } from '@ionic-native/facebook';
  11.  
  12.  
  13. @Component({
  14. selector: 'page-login',
  15. templateUrl: 'login.html'
  16. })
  17. export class Login {
  18. public login = {
  19. 'email': '',
  20. 'password': ''
  21. };
  22.  
  23. public currentMail: string = '';
  24.  
  25. register: Regist;
  26. public showPassword: boolean = false;
  27. public mode_login;
  28. public prompt_register_hp;
  29. public fb_userId;
  30. public fb_result;
  31. public loading;
  32. public loginText: boolean = true;
  33. public processingText: boolean = false;
  34. public translate;
  35. public data;
  36.  
  37. constructor(
  38. public navParam: NavParams,
  39. public navCtrl: NavController,
  40. public session: Session,
  41. public http: HttpService,
  42. public GlobalService: GlobalService,
  43. public communicationService: CommunicationService,
  44. private fb: Facebook
  45. ) { }
  46.  
  47. ionViewDidLoad() {
  48. try {
  49. let loginParam: Object = this.navParam.get('loginParam');
  50.  
  51. if (loginParam != null) {
  52. this.currentMail = loginParam['email'];
  53. this.login.email = this.currentMail;
  54. if (loginParam.hasOwnProperty('error') && loginParam['error'] === true) {
  55. // Error
  56. let alert = this.GlobalService.showMyAlert({
  57. title: loginParam['title'],
  58. subtitle: loginParam['message'],
  59. buttons: ['OK']
  60. });
  61. alert.present();
  62. } else {
  63. // not error
  64. let alert = this.GlobalService.showMyAlert({
  65. title: loginParam['title'],
  66. subtitle: loginParam['message'],
  67. buttons: ['OK']
  68. });
  69. alert.present();
  70. }
  71. }
  72. } catch (e) {
  73. this.GlobalService.consoleLog("Message Error", e);
  74. }
  75. }
  76.  
  77. loginForm() {
  78. let params = {
  79. "link": "user/login",
  80. "data": this.login
  81. }
  82. let loader = this.GlobalService.showLoading({
  83. "spinner": "hide",
  84. "content": "Loading..",
  85. "dismissOnPageChange": true
  86. });
  87. loader.present()
  88.  
  89. this.http.post(params).then((response) => {
  90. let data = response['data'][0];
  91.  
  92. let params = {
  93. link: "user/profile",
  94. data: {
  95. id_user: data['user_id']
  96. }
  97. }
  98.  
  99. this.http
  100. .get(params)
  101. .then((response) => {
  102. let session = {
  103. [Session.USER_ID]: data['user_id'],
  104. [Session.USER_NAME]: data['user_name'],
  105. [Session.USER_EMAIL]: data['user_email'],
  106. [Session.USER_FULL_NAME]: data['nama_lengkap'],
  107. [Session.IS_LOGIN]: true,
  108. [Session.LANGUAGE]: 'en',
  109. [Session.USER_PHONE]: response['data']['hp'],
  110. }
  111.  
  112. console.log(session);
  113.  
  114.  
  115. this.session.setSession(session).then(result => {
  116. this.communicationService.announceMain('Login')
  117. if (this.navParam.get('pop') == true) {
  118. this.navCtrl.pop()
  119. } else {
  120. this.navCtrl.setRoot(Home, null);
  121. }
  122. });
  123.  
  124. loader.dismiss();
  125. }).catch(error => {
  126. // if error
  127. console.log('error', error);
  128. loader.dismiss();
  129. });
  130.  
  131. }, error => {
  132. loader.dismiss();
  133. this.GlobalService.consoleLog("Login Error", error);
  134. });
  135. }
  136.  
  137. toRegister() {
  138. this.navCtrl.push(Regist, null);
  139. }
  140.  
  141. resendMail(form) {
  142. let email = form.value.email;
  143. if (this.currentMail == '' && email == '') {
  144. let alert = this.GlobalService.showMyAlert({
  145. subtitle: "Email is null, try reinput your email",
  146. buttons: ['OK']
  147. });
  148. alert.present();
  149. } else {
  150. let loader = this.GlobalService.showLoading();
  151. loader.present()
  152. let send = {
  153. link: "verification/email_reg",
  154. data: {
  155. email: this.currentMail
  156. }
  157. }
  158. if (this.currentMail == '' && email != '') {
  159. send.data.email = email;
  160. }
  161.  
  162. this.http.get(send).then((response) => {
  163. loader.dismiss();
  164. let alert = this.GlobalService.showMyAlert({
  165. title: "Success",
  166. subtitle: response['data']['msg'],
  167. buttons: ['OK']
  168. });
  169. alert.present();
  170. }, error => {
  171. loader.dismiss();
  172. this.GlobalService.consoleLog("Resend Email Error:", error.message);
  173. }).catch((cat) => {
  174. this.GlobalService.consoleLog("GET ERROR PROMISE", cat);
  175. });
  176. }
  177. }
  178.  
  179. checkEnter(event:any) {
  180. let ASCII_ENTER = 13;
  181. if ( event.keyCode == ASCII_ENTER) {
  182. // if ( this.login_data.code_area.length > 1 && this.login_data.hp.length > 5 && this.login.value.password.length > 4 ) {
  183. this.loginForm();
  184. }
  185. }
  186.  
  187. //START FACEBOOK FUNCTION
  188.  
  189. loginWithFacebook(){
  190. this.fb.login(['public_profile', 'user_friends', 'email'])
  191. .then((res: FacebookLoginResponse) => {
  192.  
  193. let loader = this.GlobalService.showLoading({
  194. "spinner": "hide",
  195. "content": "Loading..",
  196. "dismissOnPageChange": true
  197. });
  198. loader.present()
  199.  
  200. console.log(Login, " Logged into Facebook!", res);
  201. this.fb_userId = res.authResponse.userID;
  202. let params = new Array();
  203.  
  204. this.fb.api("/me?locale=en_US&fields=name,email", params)
  205. .then((user) => {
  206. this.fb_result = user;
  207. let response = {
  208. "UserID": this.fb_userId,
  209. "UserName": this.fb_result.name,
  210. "UserEmail": this.fb_result.email,
  211. "hp": ""
  212. };
  213. console.log(Login, " response: ", response);
  214.  
  215. let link = MyApp.GOX_SERVER_NON_API + 'user/json_login_fb';
  216. let THRESHOLD = 5000;
  217. let postParams = {
  218. "link": link,
  219. "data": response,
  220. "show_error": 0
  221. };
  222.  
  223. MyApp.consoleLog(Login.TAG + " Isi Post Param: ", postParams);
  224.  
  225. this.httpServ.postServiceRTO(postParams, THRESHOLD).then((data) => {
  226. this.processLogin(data, response);
  227. }, error => {
  228. // dismiss loading when error login
  229. this.loading.dismiss();
  230. this.loginText = true;
  231. this.processingText = false;
  232. MyApp.consoleError(Login.TAG + "Couldn't Connect to Server", error);
  233. });
  234. })
  235. })
  236. .catch((error) => {
  237. MyApp.consoleLog(Login.TAG, " Error login into Facebook", error);
  238. this.globalService.showMyAlert({
  239. title: this.translate.get('gox.alert_title_msg').value,
  240. message: error,
  241. buttons: [{
  242. text: this.translate.get('gox.label_ok').value
  243. }]
  244. });
  245. });
  246. }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement