Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { NavController } from 'ionic-angular';
  3. import { Validators, FormBuilder, FormGroup } from '@angular/forms';
  4. import { IndexPage } from '../../pages/index/index';
  5. import { dataService } from '../../pages/services/dataService';
  6. import { AlertController } from 'ionic-angular';
  7. import { Storage } from '@ionic/storage';
  8. import { App } from 'ionic-angular';
  9.  
  10.  
  11. @Component({
  12. selector: 'page-home',
  13. templateUrl: 'home.html'
  14. })
  15. export class HomePage {
  16.  
  17. private login : FormGroup;
  18.  
  19. backimg : string;
  20.  
  21. constructor(
  22. public appCtrl: App,
  23. public navCtrl: NavController,
  24. private dataService : dataService,
  25. private formBuilder: FormBuilder,
  26. private alertCtrl: AlertController,
  27. private storage: Storage) {
  28. this.login = this.formBuilder.group({
  29. username: ['', Validators.required],
  30. password: ['', Validators.compose([
  31. Validators.minLength(5),
  32. Validators.required]
  33. )]
  34. });
  35. this.storage.get('access_token').then((val) => {
  36. if(val!=null){
  37. this.navCtrl.push(IndexPage);
  38. }
  39. });
  40. }
  41.  
  42. loginForm(){
  43. const data = {
  44. "username" : this.login.value.username,
  45. "email" : this.login.value.username,
  46. "password" : this.login.value.password,
  47. }
  48. this.dataService.loginUser(data).subscribe(
  49. (data) => {
  50. let token = data.key;
  51. this.dataService.checkAccessUserGroup(token).subscribe(
  52. (data) => {
  53. if(data[0] == 200){
  54. this.storage.set('access_token',token).then((val) => {
  55. localStorage.setItem("access_token",token);
  56. this.navCtrl.setRoot(IndexPage);
  57. this.navCtrl.push(IndexPage);
  58. });
  59.  
  60. }
  61. if(data[0] == 500){
  62. this.generateAlert("Error",'No tienes permisos adecuados para acceder. Ponte en contacto con el administrador de tu Deck.');
  63. }
  64. },
  65. (err) => {
  66. if(err.status == 400){
  67. this.generateAlert("Error",'No hemos podido verificar tus datos. Intentalo de nuevo');
  68. }
  69. }
  70. );
  71. },
  72. (err) => {
  73. if(err.status == 400){
  74. this.generateAlert("Error",'Usuario o constraseña no válido. Intentalo de nuevo');
  75. }
  76. }
  77. );
  78. }
  79.  
  80. private generateAlert(title,body){
  81. let alert = this.alertCtrl.create({
  82. title: title,
  83. subTitle: body,
  84. buttons: ['Aceptar']
  85. });
  86. alert.present();
  87. }
  88. }
  89.  
  90. <ion-content padding id="container-home" style="background-image:
  91.  
  92. url('assets/img/bg-login.png')">
  93.  
  94. <ion-row>
  95. <ion-img class="logo-md" width="120" height="120" src="assets/img/mydecklogocolor.png"></ion-img>
  96. </ion-row>
  97.  
  98. <ion-row id="auth-login">
  99. <ion-col col-12 no-padding>
  100. <ion-row class="header">
  101. <h3>Ingresa</h3>
  102. </ion-row>
  103. <form id="login-container" [formGroup]="login" (ngSubmit)="loginForm()">
  104. <ion-row>
  105. <ion-item>
  106. <ion-input type="text" formControlName="username"
  107. class="input-md"placeholder="Correo electrónico / usuario"></ion-input>
  108. </ion-item>
  109. <ion-item>
  110. <ion-input type="password" formControlName="password"
  111. class="input-md" placeholder="Contraseña"></ion-input>
  112. </ion-item>
  113. </ion-row>
  114. <ion-row>
  115. <button ion-button class="auth-btn" type="submit" [disabled]="!login.valid">Ingresar</button>
  116. </ion-row>
  117. </form>
  118. <ion-row>
  119. <a href="">¿Olvidaste tu contraseña?</a>
  120. </ion-row>
  121. </ion-col>
  122. </ion-row>
  123.  
  124. </ion-content>
  125.  
  126. import { Injectable } from '@angular/core';
  127. import { Http, Headers, Response } from '@angular/http';
  128. import { Storage } from '@ionic/storage';
  129. import 'rxjs/Rx';
  130.  
  131. // Import RxJs required methods
  132. import 'rxjs/add/operator/map';
  133. import 'rxjs/add/operator/catch';
  134.  
  135. @Injectable()
  136. export class dataService {
  137.  
  138. private mainUrl : string = "https://****.***/api";
  139.  
  140. constructor (
  141. private http : Http
  142. ){
  143. }
  144.  
  145. public loginUser(data){
  146. const body = JSON.stringify(data);
  147. const noAuthHeaders = new Headers();
  148. noAuthHeaders.append('Content-Type','application/json');
  149. return this.http.post(this.mainUrl+'/auth/login/',body, {
  150. headers: noAuthHeaders,
  151. }).map((res:Response) => res.json());
  152. }
  153.  
  154. public checkAccessUserGroup(token){
  155. const body = {};
  156. const noAuthHeaders = new Headers();
  157. noAuthHeaders.append('Authorization','Token '+token);
  158. noAuthHeaders.append('Content-Type','application/json');
  159. return this.http.post(this.mainUrl+'/check-login-info/',body,{
  160. headers: noAuthHeaders,
  161. }).map((res:Response) => res.json());
  162. }
  163.  
  164. public markNotificationAsRead(notificationId){
  165.  
  166. const body = {
  167. "notification_id" : notificationId
  168. };
  169. return this.http.post(this.mainUrl+'/read-notification/',body,{
  170. headers: this.getAuthHeaders(),
  171. }).map((res:Response) => res.json());
  172. }
  173.  
  174. public getAllNotifications(){
  175. return this.http.get(this.mainUrl+'/get-all-notifications/',{
  176. headers: this.getAuthHeaders(),
  177. }).map((res:Response) => res.json());
  178. }
  179.  
  180. public getAvailableSchedules(){
  181. return this.http.get(this.mainUrl+'/available-lesson/',{
  182. headers: this.getAuthHeaders(),
  183. }).map((res:Response) => res.json());
  184. }
  185.  
  186. public createReservation(scheduleId){
  187. let body = {
  188. "schedule_id" : scheduleId
  189. }
  190. return this.http.post(this.mainUrl+'/subscribe/',body,{
  191. headers: this.getAuthHeaders(),
  192. }).map((res:Response) => res.json());
  193. }
  194.  
  195. public getProfile(){
  196. return this.http.get(this.mainUrl+'/get-profile/',{
  197. headers: this.getAuthHeaders(),
  198. }).map((res:Response) => res.json());
  199.  
  200.  
  201. }
  202.  
  203. public getMembership(){
  204. return this.http.get(this.mainUrl+'/get-membership/',{
  205. headers: this.getAuthHeaders(),
  206. }).map((res:Response) => res.json());
  207. }
  208.  
  209. public getAllActiveSubscription(){
  210. return this.http.get(this.mainUrl+'/get-all-subscriptions/',{
  211. headers: this.getAuthHeaders(),
  212. }).map((res:Response) => res.json());
  213. }
  214.  
  215. public updateProfile(data){
  216. return this.http.post(this.mainUrl+'/set-profile/',data,{
  217. headers: this.getAuthHeaders(),
  218. }).map((res:Response) => res.json());
  219. }
  220.  
  221. public getPlans(){
  222. return this.http.get(this.mainUrl+'/get-plans/',{
  223. headers: this.getAuthHeaders(),
  224. }).map((res:Response) => res.json());
  225. }
  226.  
  227. public cancelReservation(scheduleId){
  228. let body = {
  229. "schedule_id" : scheduleId
  230. }
  231. return this.http.post(this.mainUrl+'/cancel-subscribe/',body,{
  232. headers: this.getAuthHeaders(),
  233. }).map((res:Response) => res.json());
  234. }
  235.  
  236. private getAuthHeaders(){
  237. const authHeaders = new Headers();
  238. authHeaders.append('Authorization','Token '+localStorage.getItem("access_token"));
  239. authHeaders.append('Content-Type','application/json');
  240. return authHeaders;
  241. }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement