From 08331b1ac9025e935cd6fdf155ca27dc12cfdbcf Mon Sep 17 00:00:00 2001 From: AlexBob <5199840@qq.com> Date: Tue, 2 Jul 2024 15:34:44 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(core):=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=92=8C=E9=87=8D=E6=9E=84=E4=BA=86=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E6=9C=8D=E5=8A=A1=E5=92=8C=E7=BB=84=E4=BB=B6=E4=BB=A5?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=BB=A3=E7=A0=81=E5=86=97=E4=BD=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除了`http.Interceptor.ts`中的默认和authToken拦截器。 - 移除了`loading.service.ts`中的LoadingService服务。 - 移除了`not-found.component.ts`中的NotFoundComponent组件。 - 重构了`title-strategy.service.ts`以优化标题更新逻辑。 --- ui/projects/web/src/core/http.Interceptor.ts | 86 +++++++++++++++++++ ui/projects/web/src/core/loading.service.ts | 17 ++++ .../web/src/core/not-found.component.ts | 26 ++++++ .../web/src/core/title-strategy.service.ts | 17 ++++ 4 files changed, 146 insertions(+) create mode 100644 ui/projects/web/src/core/http.Interceptor.ts create mode 100644 ui/projects/web/src/core/loading.service.ts create mode 100644 ui/projects/web/src/core/not-found.component.ts create mode 100644 ui/projects/web/src/core/title-strategy.service.ts diff --git a/ui/projects/web/src/core/http.Interceptor.ts b/ui/projects/web/src/core/http.Interceptor.ts new file mode 100644 index 00000000..20828293 --- /dev/null +++ b/ui/projects/web/src/core/http.Interceptor.ts @@ -0,0 +1,86 @@ +import {inject} from '@angular/core'; +import {HttpEvent, HttpHandlerFn, HttpRequest} from '@angular/common/http'; +import {catchError, finalize, Observable, throwError, timeout} from 'rxjs'; +import {AuthService} from './auth.service'; +import {LoadingService} from './loading.service'; +import {MessageService} from '../shared/message.service'; +import {Router} from '@angular/router'; +import {environment} from '../../environments/environment'; + +export function defaultInterceptor( + req: HttpRequest, + next: HttpHandlerFn +): Observable> { + const _loading = inject(LoadingService); + const _message = inject(MessageService); + + _loading.show(); + if (req.url.indexOf('assets/') > -1) { + return next(req); + } + const originalUrl = + req.url.indexOf('http') > -1 ? req.url : environment.host + req.url; + const xRequestedReq = req.clone({ + headers: req.headers.append('X-Requested-With', 'XMLHttpRequest'), + url: originalUrl, + }); + return next(xRequestedReq).pipe( + timeout({first: 5_000, each: 10_000}), + catchError(errorResponse => { + if (errorResponse.error.message) { + _message.error(errorResponse.error.message); + return throwError(() => errorResponse.error.message); + } + console.error($localize`:@@errorMessage:Backend returned code ${errorResponse.status}, + body was: ${errorResponse.message}`); + return throwError(() => errorResponse); + }), + finalize(() => _loading.hide()) + ); +} + +export function authTokenInterceptor( + req: HttpRequest, + next: HttpHandlerFn +): Observable> { + const _auth = inject(AuthService); + const _route = inject(Router); + + if (!_auth.isLoggedIn) { + return next(req); + } + + const authReq = req.clone({ + headers: req.headers.set('Authorization', `Bearer ${_auth.authToken()}`), + }); + + return next(authReq).pipe( + catchError(errorResponse => { + if (errorResponse.status === 401) { + _auth.logout(); + _route.navigate([_auth.loginUrl]).then(); + return throwError( + () => $localize`:@@errorMessage401:身份验证无效,请重新登录。` + ); + } else if (errorResponse.status === 407) { + _auth.logout(); + _route.navigate([_auth.loginUrl]).then(); + return throwError( + () => $localize`:@@errorMessage407:认证不正确,请重新登录。` + ); + } else if (errorResponse.status === 403) { + _auth.logout(); + _route.navigate([_auth.loginUrl]).then(); + return throwError( + () => $localize`:@@errorMessage403:验证码令牌错误,请重新登录。` + ); + } else { + console.error( + `Backend returned authToken code ${errorResponse.status}, body was: `, + errorResponse.error + ); + return throwError(() => errorResponse); + } + }) + ); +} diff --git a/ui/projects/web/src/core/loading.service.ts b/ui/projects/web/src/core/loading.service.ts new file mode 100644 index 00000000..5bf68233 --- /dev/null +++ b/ui/projects/web/src/core/loading.service.ts @@ -0,0 +1,17 @@ +import {Injectable} from '@angular/core'; +import {Observable, Subject} from 'rxjs'; + +@Injectable({providedIn: 'root'}) +export class LoadingService { + // Observable string sources + private progressSource: Subject = new Subject(); + progress$: Observable = this.progressSource.asObservable(); + + show(): void { + this.progressSource.next(true); + } + + hide(): void { + this.progressSource.next(false); + } +} diff --git a/ui/projects/web/src/core/not-found.component.ts b/ui/projects/web/src/core/not-found.component.ts new file mode 100644 index 00000000..165d987b --- /dev/null +++ b/ui/projects/web/src/core/not-found.component.ts @@ -0,0 +1,26 @@ +import {Component} from '@angular/core'; +import {NzResultModule} from 'ng-zorro-antd/result'; + +@Component({ + selector: 'app-not-found', + standalone: true, + imports: [NzResultModule], + template: ` + +
+ Back Home +
+
+ `, + styles: ` + :host { + min-height: 100%; + min-width: 100%; + } + `, +}) +export class NotFoundComponent { +} diff --git a/ui/projects/web/src/core/title-strategy.service.ts b/ui/projects/web/src/core/title-strategy.service.ts new file mode 100644 index 00000000..5c2ef238 --- /dev/null +++ b/ui/projects/web/src/core/title-strategy.service.ts @@ -0,0 +1,17 @@ +import {RouterStateSnapshot, TitleStrategy} from '@angular/router'; +import {Title} from '@angular/platform-browser'; +import {Injectable} from '@angular/core'; + +@Injectable({providedIn: 'root'}) +export class PageTitleStrategy extends TitleStrategy { + constructor(private readonly title: Title) { + super(); + } + + override updateTitle(routerState: RouterStateSnapshot) { + const title = this.buildTitle(routerState); + if (title !== undefined) { + this.title.setTitle(`盘子管理平台 | ${title}`); + } + } +}