|
@@ -13,9 +13,12 @@ import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
|
|
|
import { ALAIN_I18N_TOKEN, _HttpClient } from '@delon/theme';
|
|
|
import { environment } from '@env/environment';
|
|
|
import { NzNotificationService } from 'ng-zorro-antd/notification';
|
|
|
+import { CookieService } from 'ngx-cookie-service';
|
|
|
import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
|
|
|
import { catchError, filter, mergeMap, switchMap, take } from 'rxjs/operators';
|
|
|
|
|
|
+import { CONSTS } from '../../shared/consts';
|
|
|
+
|
|
|
const CODEMESSAGE: { [key: number]: string } = {
|
|
|
200: '服务器成功返回请求的数据。',
|
|
|
201: '新建或修改数据成功。',
|
|
@@ -42,6 +45,7 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
private refreshTokenEnabled = environment.api.refreshTokenEnabled;
|
|
|
private refreshTokenType: 're-request' | 'auth-refresh' = environment.api.refreshTokenType;
|
|
|
private refreshToking = false;
|
|
|
+ private notified = false;
|
|
|
private refreshToken$: BehaviorSubject<any> = new BehaviorSubject<any>(null);
|
|
|
|
|
|
constructor(private injector: Injector) {
|
|
@@ -54,6 +58,10 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
return this.injector.get(NzNotificationService);
|
|
|
}
|
|
|
|
|
|
+ private get cookieService(): CookieService {
|
|
|
+ return this.injector.get(CookieService);
|
|
|
+ }
|
|
|
+
|
|
|
private get tokenSrv(): ITokenService {
|
|
|
return this.injector.get(DA_SERVICE_TOKEN);
|
|
|
}
|
|
@@ -63,7 +71,10 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
}
|
|
|
|
|
|
private goTo(url: string): void {
|
|
|
- setTimeout(() => this.injector.get(Router).navigateByUrl(url));
|
|
|
+ setTimeout(() => {
|
|
|
+ this.injector.get(Router).navigateByUrl(url);
|
|
|
+ this.notified = false;
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
private checkStatus(ev: HttpResponseBase): void {
|
|
@@ -91,6 +102,7 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
this.toLogin();
|
|
|
return throwError(ev);
|
|
|
}
|
|
|
+
|
|
|
|
|
|
if (this.refreshToking) {
|
|
|
return this.refreshToken$.pipe(
|
|
@@ -99,17 +111,20 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
switchMap(() => next.handle(this.reAttachToken(req)))
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
|
|
|
this.refreshToking = true;
|
|
|
this.refreshToken$.next(null);
|
|
|
|
|
|
return this.refreshTokenRequest().pipe(
|
|
|
switchMap(res => {
|
|
|
+ console.log(res.data);
|
|
|
|
|
|
this.refreshToking = false;
|
|
|
- this.refreshToken$.next(res);
|
|
|
+ this.refreshToken$.next(res.data.refresh_token);
|
|
|
+ this.cookieService.set(CONSTS.CONGRESS, res.data.token);
|
|
|
|
|
|
- this.tokenSrv.set(res);
|
|
|
+ this.tokenSrv.set(res.data);
|
|
|
|
|
|
return next.handle(this.reAttachToken(req));
|
|
|
}),
|
|
@@ -127,11 +142,14 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
* > 由于已经发起的请求,不会再走一遍 `@delon/auth` 因此需要结合业务情况重新附加新的 Token
|
|
|
*/
|
|
|
private reAttachToken(req: HttpRequest<any>): HttpRequest<any> {
|
|
|
+
|
|
|
|
|
|
const token = this.tokenSrv.get()?.token;
|
|
|
return req.clone({
|
|
|
setHeaders: {
|
|
|
- Authorization: `Bearer ${token}`
|
|
|
+ Authorization: `Bearer ${token}`,
|
|
|
+ hostname: window.location.hostname,
|
|
|
+ AuthServer: 'MaxKey'
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -167,8 +185,11 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
|
|
|
|
|
|
private toLogin(): void {
|
|
|
- this.notification.error(`未登录或登录已过期,请重新登录。`, ``);
|
|
|
- this.goTo(this.tokenSrv.login_url!);
|
|
|
+ if (!this.notified) {
|
|
|
+ this.notified = true;
|
|
|
+ this.notification.error(`未登录或登录已过期,请重新登录。`, ``);
|
|
|
+ this.goTo(this.tokenSrv.login_url!);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private handleData(ev: HttpResponseBase, req: HttpRequest<any>, next: HttpHandler): Observable<any> {
|
|
@@ -213,10 +234,7 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
break;
|
|
|
default:
|
|
|
if (ev instanceof HttpErrorResponse) {
|
|
|
- console.warn(
|
|
|
- '未可知错误,大部分是由于后端不支持跨域CORS或无效配置引起,请参考 https://ng-alain.com/docs/server 解决跨域问题',
|
|
|
- ev
|
|
|
- );
|
|
|
+ console.warn('未可知错误,大部分是由于后端不支持跨域CORS或无效配置引起.', ev);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
@@ -237,6 +255,8 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
if (jwtAuthn !== null) {
|
|
|
res['Authorization'] = `Bearer ${jwtAuthn.token}`;
|
|
|
}
|
|
|
+ res['hostname'] = window.location.hostname;
|
|
|
+ res['AuthServer'] = 'MaxKey';
|
|
|
return res;
|
|
|
}
|
|
|
|