File

src/app/auth/auth.service.ts

Index

Properties
Methods

Constructor

constructor(http: HttpClient, cookie: CookieService, md5: Md5Service)
Parameters :
Name Type Optional
http HttpClient No
cookie CookieService No
md5 Md5Service No

Methods

Public login
login(user: User)
Parameters :
Name Type Optional
user User No
Returns : Observable<object>
Public logout
logout()
Returns : void

Properties

Public isLoggedIn
Default value : false
Public redirectUrl
Type : string
Public user
Type : User
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Md5Service } from '../share/md5.service';
import { User } from './auth.user.entity';

@Injectable({
  providedIn: 'root',
})
export class AuthService {

  // 是否已登陆
  public isLoggedIn = false;
  // 登陆前的url
  public redirectUrl: string;
  // 用户信息
  public user: User;

  constructor(
    private http: HttpClient,
    private cookie: CookieService,
    private md5: Md5Service,
  ) { }

  public login(user: User): Observable<object> {
    user.password = this.md5.fromString(user.password);
    return this.http.post('https://int.mycht.cn/port/4201/user/login?_allow_anonymous=true', user).pipe(
      tap((data: any) => {
        if (data.msg === 'ok') {
          this.isLoggedIn = true;
          this.user = data.user;
          this.cookie.set('user', JSON.stringify(data.user), new Date('2088-12-12'), undefined, undefined, undefined, 'Lax');
        }
      }),
    );
  }

  public logout(): void {
    this.isLoggedIn = false;
    this.cookie.delete('user');
  }
}

result-matching ""

    No results matching ""