Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { NavController } from 'ionic-angular';
  3. import { AlertController, Platform } from 'ionic-angular';
  4. import {Network} from "ionic-native";
  5. import {Observable} from 'rxjs/Rx';
  6. import { Http, Response, Headers, RequestOptions, Request, RequestMethod } from '@angular/http';
  7. import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, AbstractControl} from '@angular/common';
  8.  
  9. // declare var navigator: any;
  10. // declare var Connection: any;
  11. /*
  12. Generated class for the LoginPage page.
  13.  
  14. See http://ionicframework.com/docs/v2/components/#navigation for more info on
  15. Ionic pages and navigation.
  16. */
  17. @Component({
  18. templateUrl: 'build/pages/login/login.html',
  19. })
  20. export class LoginPage {
  21. username: string;
  22. password: string;
  23. private restfulURL = 'http://jsonplaceholder.typicode.com/posts/1';
  24. private soapURL = 'http://smes.silverlakegroup.com.my:49167/mbdes/api/rest/getSpeakerList';
  25.  
  26. constructor(private navCtrl: NavController, private alertController: AlertController, private http: Http, private platform: Platform) {
  27.  
  28. }
  29.  
  30. showAlert(title, string) {
  31. let alert = this.alertController.create({
  32. title: title,
  33. subTitle: string,
  34. buttons: ['OK']
  35. });
  36. alert.present();
  37. }
  38.  
  39. login() {
  40. if (!this.username || !this.password) {
  41. this.showAlert('Error', 'User credentials cannot be empty');
  42. }
  43. else {
  44. let isSuccess;
  45. let msg;
  46.  
  47. if (this.checkNetwork()) {
  48. this.callBDESService({ 'lastModifiedDt': '1362868820000' }).subscribe(res => {
  49. // this.showAlert('Success', JSON.stringify(res));
  50. isSuccess = true;
  51. msg = JSON.stringify(res);
  52. console.log("RESPONSE" + msg);
  53. },
  54. (err) => {
  55. isSuccess = false;
  56. console.log(err)
  57. msg = err;
  58. // this.showAlert('Error', err);
  59. },
  60. () => {
  61. if (isSuccess) {
  62. this.showAlert('Complete success', msg);
  63. } else {
  64. this.showAlert('Complete but failed', msg);
  65. }
  66. }
  67. );
  68. }
  69. }
  70. }
  71.  
  72. callRestFul(): Observable<any[]> {
  73.  
  74. return this.http.get(this.restfulURL)
  75. .map((res: Response) => res.json())
  76. .catch((error: any) => Observable.throw(error.json().error))
  77. }
  78.  
  79. callBDESService(body: Object): Observable<any[]> {
  80. let bodyString = JSON.stringify(body);
  81. console.log("BODY " + bodyString);
  82. console.log("URL " + this.soapURL);
  83.  
  84. let headers = new Headers({ 'Content-Type': 'application/json' });
  85. let options = new RequestOptions({
  86. headers: headers,
  87. method: RequestMethod.Post
  88. });
  89.  
  90. return this.http.post(this.soapURL, bodyString, options)
  91. .map((res: Response) => res.json())
  92. .catch((error: any) => Observable.throw(error.json().error));
  93. }
  94.  
  95. checkNetwork(){
  96. return this.platform.ready().then(() => {
  97.  
  98. console.log(Network.connection);
  99. if (Network.connection === 'none') {
  100. // if (Network.connection == Connection.None) {
  101. this.showAlert('Attention', 'No internet connection.');
  102. return false;
  103. } else {
  104. console.log('Network connection true');
  105. return true;
  106. }
  107.  
  108. });
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement