iam.service.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { Injectable, OnInit } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import { MatSnackBar } from '@angular/material/snack-bar';
  4. import { Router } from '@angular/router';
  5. import { ResponseLogin } from 'src/app/interfaces/iam/response-login';
  6. import { IAMInterface } from 'src/app/interfaces/iam/iam-interface';
  7. import { ENCService } from '../enc/enc.service';
  8. import { WebSocketService } from '../socket/web-socket.service';
  9. import { ResponseData } from 'src/app/interfaces/response-data';
  10. import { LocalstorageService } from '../localstorage.service';
  11. import { AuthGuard } from 'src/app/auth/auth.guard';
  12. import { AdminGuard } from 'src/app/auth/admin.guard';
  13. import { UserGuard } from 'src/app/auth/user.guard';
  14. @Injectable({
  15. providedIn: 'root',
  16. })
  17. export class IAMService implements OnInit {
  18. private _url = 'https://qasirh.ittec.mx/v1/public/api';
  19. header: HttpHeaders = new HttpHeaders();
  20. current_user: string = '';
  21. jwt: string = '';
  22. constructor(
  23. private http: HttpClient,
  24. private _snackBar: MatSnackBar,
  25. private router: Router,
  26. private _encService: ENCService,
  27. private webSocketService: WebSocketService,
  28. public setUserStorage: LocalstorageService,
  29. private adminGuard: AdminGuard,
  30. private userGuard: UserGuard
  31. ) {
  32. this.getIPAddress();
  33. }
  34. ngOnInit(): void {}
  35. public usuario_rfc: string = '';
  36. public redirectUrl: string | null = null;
  37. private ipAddress: string = '';
  38. async login(data: IAMInterface, solicitud_datos: string) {
  39. if (this.ipAddress != '') {
  40. let objeto = {
  41. email: data.email.trim(),
  42. password: data.password.trim(),
  43. ip_address: this.ipAddress,
  44. };
  45. let respuesta = this.http
  46. .post<ResponseLogin>(`${this._url}/login`, objeto)
  47. .toPromise();
  48. await respuesta.then(
  49. async (data) => {
  50. if (!data?.error) {
  51. let status = this._encService.desencriptar(data!.response.ESTATUS);
  52. let perfil = this._encService.desencriptar(data!.response.PERFIL);
  53. let rfc_empresa = this._encService.desencriptar(
  54. data!.response.RFCEMPRESA
  55. );
  56. if (rfc_empresa == localStorage.getItem('RFCEMPRESA')) {
  57. switch (status) {
  58. case 'Activo':
  59. this.current_user = JSON.stringify(data!.response);
  60. this.jwt = data!.response.TOKEN;
  61. localStorage.setItem('jwt', data!.response.TOKEN);
  62. localStorage.setItem('TIMUSERENC',JSON.stringify(data?.response));
  63. this.userType(perfil, solicitud_datos);
  64. break;
  65. case 'Eliminado':
  66. this.snackAlert('El usuario no existe');
  67. break;
  68. case 'Inactivo':
  69. this.snackAlert('El usuario esta bloqueado');
  70. break;
  71. default:
  72. this.snackAlert('Usuario inexistente');
  73. break;
  74. }
  75. } else {
  76. this.snackAlert(
  77. 'El R.F.C. del usuario no coincide con el R.F.C. del sistema al que desea entrar'
  78. );
  79. }
  80. } else {
  81. this.snackAlert(data.msg);
  82. }
  83. },
  84. (error) => {
  85. if (!error.ok) {
  86. this.snackAlert('Ocurrió un error inesperado');
  87. }
  88. if (error.error.msg != undefined) {
  89. this.snackAlert(error.error.msg);
  90. }
  91. if (error.status == 408) {
  92. this.snackAlert('Conexion lenta');
  93. }
  94. }
  95. );
  96. }
  97. }
  98. private snackAlert(mensaje: string) {
  99. this._snackBar.open(mensaje, 'Cerrar', {
  100. duration: 4000,
  101. });
  102. }
  103. private getIPAddress() {
  104. this.http.get('https://api.ipify.org?format=json').subscribe(
  105. (res: any) => {
  106. this.ipAddress = res.ip;
  107. },
  108. (error) => {
  109. if (!error.ok) {
  110. this.snackAlert('Ocurrió un error inesperado');
  111. }
  112. if (error.status == 408) {
  113. this.snackAlert('Conexion lenta');
  114. }
  115. }
  116. );
  117. }
  118. public logout(): void {
  119. clearInterval(this.adminGuard.aux_test);
  120. clearInterval(this.userGuard.aux_test);
  121. let rfc = localStorage.getItem('RFCEMPRESA');
  122. if (rfc === null || rfc === undefined || rfc === '') {
  123. localStorage.setItem('RFCEMPRESA', 'ITTEC');
  124. }
  125. localStorage.removeItem('jwt');
  126. localStorage.removeItem('TIMUSERENC');
  127. this.router.navigate(['/iam']);
  128. this.snackAlert('¡Hasta pronto!');
  129. }
  130. private userType(perfil: string, solicitud_datos: string) {
  131. if (perfil == "1") {
  132. if (solicitud_datos == '') {
  133. this.router.navigate(['/admin/mesmpr']);
  134. } else {
  135. this.router.navigate([
  136. '/admin/mesmav',
  137. {
  138. solicitud_datos: solicitud_datos,
  139. },
  140. ]);
  141. }
  142. } else {
  143. if (solicitud_datos == '') {
  144. this.router.navigate(['/mesmpr']);
  145. } else {
  146. this.router.navigate([
  147. '/mesmav',
  148. {
  149. solicitud_datos: solicitud_datos,
  150. },
  151. ]);
  152. }
  153. }
  154. }
  155. getJWT() {
  156. return this.jwt;
  157. }
  158. getCurrentUser() {
  159. return this.current_user;
  160. }
  161. getRFCEmpresa() {
  162. return this._encService.desencriptar(
  163. JSON.parse(this.current_user).RFCEMPRESA
  164. );
  165. }
  166. }