Guest User

Untitled

a guest
Dec 13th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. login() {
  2. this.Loading("","send");
  3. this.http.postData(this.userData, "post/login").subscribe( result => {
  4. this.responseData = result;
  5. if (this.responseData.success) {
  6. localStorage.setItem('userSesion', JSON.stringify(this.responseData.dataUser));
  7. if (this.responseData.dataUser.tipo === '1') {
  8. this.navCtrl.setRoot(InicioPage);
  9. } else if (this.responseData.dataUser.tipo === '2') {
  10. this.navCtrl.setRoot(IniciodocentePage);
  11. }
  12. } else {
  13. this.Loading(this.responseData.dataUser,"error");
  14. }
  15. }, (err) => {
  16. console.log(err);
  17. });
  18. }
  19.  
  20. export class MyApp {
  21. @ViewChild(Nav) nav: Nav;
  22.  
  23. rootPage: any = HomePage;
  24.  
  25. pages: Array<{title: string, component: any}>;
  26. public sesion:boolean;
  27. public opt:boolean;
  28. userDetails:any = {};
  29.  
  30. constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen) {
  31. this.initializeApp();
  32.  
  33. if (localStorage.getItem('userSesion')) {
  34. const data = JSON.parse(localStorage.getItem('userSesion'));
  35. this.userDetails = data;
  36. this.sesion = true;
  37. if (this.userDetails.tipo == '1') {
  38. this.pages = [
  39. { title: 'Inicio', component: InicioPage },
  40. { title: 'Registro de Inscripción', component: CarrerasestudiantePage },
  41. { title: 'Historial Academico', component: HistoricoPage },
  42. { title: 'Procesar Inscripción', component: ProcesarinscripcionPage }
  43. ];
  44. } else if (this.userDetails.tipo == '2') {
  45. this.pages = [
  46. { title: 'Inicio', component: IniciodocentePage },
  47. { title: 'Mi Carga Académica', component: CargaacademicaprofesorPage }
  48. ];
  49. }
  50. } else {
  51. this.pages = [
  52. { title: 'Entrar', component: HomePage },
  53. { title: 'Olvide mi clave', component: AboutPage },
  54. { title: 'Consultar Estatus', component: ContactPage }
  55. ];
  56. this.sesion = false;
  57. }
  58. }
  59.  
  60. initializeApp() {
  61. this.platform.ready().then(() => {
  62. this.statusBar.styleDefault();
  63. this.splashScreen.hide();
  64. });
  65. }
  66.  
  67. openPage(page) {
  68. this.nav.setRoot(page.component);
  69. }
  70. }
  71.  
  72. export class User {
  73. username: string;
  74. email: string;
  75. password: string;
  76. tokens: Array<Token> = [];
  77.  
  78. constructor(username, password, email = '') {
  79. this.username = username;
  80. this.email = email;
  81. this.password = password;
  82. }
  83. }
  84.  
  85. //user.service.ts
  86.  
  87. import { BehaviorSubject, Observable } from 'rxjs';
  88. export class UserService {
  89.  
  90. user: User;
  91. private userSubject = new BehaviorSubject(this.user);
  92.  
  93. // Lo demas componentes se ponen a la esucha de cambio
  94. // a través de esté método
  95. getUserObservable(): Observable<User> {
  96. return this.userSubject.asObservable();
  97. }
  98.  
  99. // Este método se usa para enviar los cambios a todos los componentes a la escucha
  100. private setUser(user: User) {
  101. this.user = user;
  102. // Refrescar user en los observables
  103. this.userSubject.next(this.user);
  104. }
  105. }
  106.  
  107. import {UserService} from 'user.service'
  108.  
  109. export class Menu {
  110.  
  111. user: User;
  112.  
  113. constructor(userService:UserService){
  114. // REGISTRASE AL OBSERVABLE DE UserService
  115. // para estar a la escucha de cambios de user
  116. this.userService.getUserObservable().subscribe(
  117. (data) => {
  118. this.user = data;
  119. }, (error) => {
  120. console.log(error);
  121. });
  122. }
  123. }
Add Comment
Please, Sign In to add comment