import { Authorization } from '@business/entity/token';
|
import { HttpClient } from '@angular/common/http';
|
import { environment } from 'environments/environment';
|
import { Observable } from 'rxjs/Observable';
|
import { Injectable, Inject } from '@angular/core';
|
import { UserContext } from '@business/entity/data';
|
import { error } from 'selenium-webdriver';
|
import { DA_SERVICE_TOKEN, ITokenService, LocalStorageStore } from '@delon/auth';
|
import { Subject } from 'rxjs/Subject';
|
|
@Injectable()
|
export class LoginService {
|
|
public authorization: Authorization = {token: null};
|
public userContext: UserContext;
|
private urls = {
|
login: environment.SERVER_BASH_URL + 'auth/login',
|
refreshToken: environment.SERVER_BASH_URL + 'auth/token',
|
userContext: environment.SERVER_BASH_URL + 'user-context',
|
};
|
constructor(private http: HttpClient,
|
@Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService) {
|
// 每2秒检查一次刷新token的时间点
|
setInterval(
|
() => {
|
if (this.needFreshFromLocal && this.isReachRefreshTime()) {
|
// 记录刷新时间
|
this.setRefreshTime();
|
// 置空needfreshtoken
|
localStorage.setItem('needRefreshToken', 'false');
|
const _refreshToken = this._refreshToken;
|
if (!!_refreshToken) {
|
this.http.get(this.urls.refreshToken, {headers: {'X-Refrsh-Token': 'Bearer ' + _refreshToken}} )
|
.subscribe(
|
res => {
|
if (res['token'] != null) {
|
this.tokenService.set({'token': res ['token']});
|
console.log(new Date() + localStorage._token);
|
}
|
}
|
);
|
}
|
}
|
}, 2000
|
);
|
}
|
public validate(username: string, password: string): Observable<Authorization> {
|
return this.http.post(this.urls.login, {username: username, password: password, mode: 'Web'}).map(
|
(res: any) => {
|
this.authorization = res;
|
const now = new Date();
|
// expiredTime,refreshToken,refreshTime 与平台的 token 分开储存
|
const expiredTime = !!this.authorization.expiredTime ? this.authorization.expiredTime.toString() : null;
|
this.setRefreshTime();
|
localStorage.setItem('expiredTime', expiredTime);
|
localStorage.setItem('refreshToken', this.authorization.refreshToken);
|
return res;
|
}
|
);
|
}
|
public loadUserContext(): void {
|
this.http.get(this.urls.userContext).subscribe(
|
(res: UserContext) => {
|
this.userContext = res;
|
return res;
|
}
|
);
|
}
|
get _refreshToken(){
|
return !!this.authorization.refreshToken ? this.authorization.refreshToken : this.refreshTokenFromLocal;
|
}
|
get refreshTokenFromLocal(): string {
|
return localStorage.refreshToken;
|
}
|
get needFreshFromLocal(): boolean {
|
return localStorage.needRefreshToken === 'true';
|
}
|
private setRefreshTime() {
|
localStorage.setItem('refreshTime', new Date().getTime().toString());
|
}
|
private setNeedRefreshToken() {
|
localStorage.setItem('needRefreshToken', 'true');
|
}
|
private isReachRefreshTime(): boolean {
|
const expiredTime = Number(localStorage.expiredTime);
|
return !!this.refreshTime && !!expiredTime && this.refreshTime + (expiredTime / 2) * 60000 < new Date().getTime();
|
}
|
get refreshTime(): number {
|
return Number(localStorage.refreshTime);
|
}
|
public refreshToken () {
|
// 设置需要刷新token
|
this.setNeedRefreshToken();
|
}
|
}
|