Broker System for Supercredit
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

107 linhas
3.3KB

  1. import {EventEmitter, Injectable, OnDestroy, OnInit} from '@angular/core';
  2. import { NotificationService } from '@progress/kendo-angular-notification';
  3. import {HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
  4. import {apiV1LoginResponse} from '../models/api-v1-login-response';
  5. import {Observable} from 'rxjs';
  6. import {AppComponent} from '../app.component';
  7. import {Router} from '@angular/router';
  8. @Injectable()
  9. export class AuthService {
  10. public apiUrl = 'https://svr2021.lawipac.com:8080/api/v1/';
  11. public apiWsUrl = 'wss://svr2021.lawipac.com:8080/api/v1/ws';
  12. public loggedIn = new apiV1LoginResponse(false, '', '',0 );
  13. loginSuccess = new EventEmitter <apiV1LoginResponse>();
  14. constructor( private http: HttpClient ,
  15. private router: Router) {
  16. }
  17. // tslint:disable-next-line:typedef
  18. public AutoLogin() {
  19. const sfm: apiV1LoginResponse = JSON.parse(localStorage.getItem('sfm'));
  20. if (!sfm) {
  21. console.log('no auto login');
  22. return;
  23. }
  24. this.loggedIn = new apiV1LoginResponse(
  25. sfm.login,
  26. sfm.machineId,
  27. sfm.session,
  28. sfm.sessionExpire
  29. );
  30. this.loginSuccess.emit(this.loggedIn);
  31. //console.log ( 'auto login emit events', this.loggedIn);
  32. }
  33. // tslint:disable-next-line:typedef
  34. isAuthenticated() {
  35. return this.loggedIn.login;
  36. }
  37. allowEditLoan(): boolean{
  38. return true;
  39. }
  40. // tslint:disable-next-line:typedef
  41. login(email: string, password: string) {
  42. this.http.post<apiV1LoginResponse>(this.apiUrl + 'login', {u: email, p: password}).subscribe(
  43. responseData => {
  44. this.loggedIn.session = responseData['Biukop-Session'];
  45. this.loggedIn.login = responseData.login;
  46. this.loggedIn.machineId = responseData['Biukop-Mid'];
  47. this.loggedIn.sessionExpire = responseData.sessionExpire;
  48. localStorage.setItem('sfm', JSON.stringify(this.loggedIn));
  49. localStorage.setItem('mid', this.loggedIn.machineId);
  50. this.loginSuccess.emit(responseData);
  51. },
  52. error => {
  53. const fail = new apiV1LoginResponse(false,'','',0);
  54. this.loggedIn = fail;
  55. console.log('login error', error);
  56. this.loginSuccess.emit(this.loggedIn);
  57. }
  58. );
  59. }
  60. // tslint:disable-next-line:typedef
  61. logout() {
  62. this.loggedIn = new apiV1LoginResponse(false,'','',0);
  63. localStorage.removeItem('sfm');
  64. this.loginSuccess.emit(this.loggedIn);
  65. this.router.navigate(['/login']).then(r =>{
  66. console.log('prepare to log back in');
  67. } );
  68. this.http.post(`${this.apiUrl}logout`, '').subscribe(
  69. resp => {
  70. console.log('successfully logout from server');
  71. },
  72. event => {
  73. console.log('error logout from server', event);
  74. }
  75. );
  76. }
  77. // tslint:disable-next-line:variable-name
  78. public getUrl(key: string): string{
  79. const s = this.apiUrl + key;
  80. const kvPair: {key: string, value: string}[] = [
  81. {key: 'login', value: this.apiUrl + 'login'},
  82. {key: 'logout', value: this.apiUrl + 'logout'}
  83. ];
  84. kvPair.forEach( item => {
  85. if ( item.key === key) {
  86. return item.value;
  87. }
  88. });
  89. // not found if arrive here
  90. return s;
  91. }}