-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor(core): 删除和重构了多个服务和组件以减少代码冗余
- 删除了`http.Interceptor.ts`中的默认和authToken拦截器。 - 移除了`loading.service.ts`中的LoadingService服务。 - 移除了`not-found.component.ts`中的NotFoundComponent组件。 - 重构了`title-strategy.service.ts`以优化标题更新逻辑。
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} | ||
} |