Skip to content

Commit

Permalink
♻️ refactor(core): 删除和重构了多个服务和组件以减少代码冗余
Browse files Browse the repository at this point in the history
- 删除了`http.Interceptor.ts`中的默认和authToken拦截器。
- 移除了`loading.service.ts`中的LoadingService服务。
- 移除了`not-found.component.ts`中的NotFoundComponent组件。
- 重构了`title-strategy.service.ts`以优化标题更新逻辑。
  • Loading branch information
vnobo committed Jul 2, 2024
1 parent 798a16e commit 08331b1
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
86 changes: 86 additions & 0 deletions ui/projects/web/src/core/http.Interceptor.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
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<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
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);
}
})
);
}
17 changes: 17 additions & 0 deletions ui/projects/web/src/core/loading.service.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> = new Subject<boolean>();
progress$: Observable<boolean> = this.progressSource.asObservable();

show(): void {
this.progressSource.next(true);
}

hide(): void {
this.progressSource.next(false);
}
}
26 changes: 26 additions & 0 deletions ui/projects/web/src/core/not-found.component.ts
Original file line number Diff line number Diff line change
@@ -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: `
<nz-result
nzStatus="403"
nzSubTitle="Sorry, you are not authorized to access this page."
nzTitle="403">
<div class="" nz-result-extra>
<a href="/home" nz-button nzType="primary">Back Home</a>
</div>
</nz-result>
`,
styles: `
:host {
min-height: 100%;
min-width: 100%;
}
`,
})
export class NotFoundComponent {
}
17 changes: 17 additions & 0 deletions ui/projects/web/src/core/title-strategy.service.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
}

0 comments on commit 08331b1

Please sign in to comment.