From 392c7f399647e7f1d51dbbffdc492a3a72867c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 12:54:58 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=B4=E6=8A=A4?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/view/maintain/maintain.component.html | 5 +++++ src/app/view/maintain/maintain.component.less | 0 src/app/view/maintain/maintain.component.ts | 16 ++++++++++++++++ src/app/view/maintain/maintain.module.ts | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 src/app/view/maintain/maintain.component.html create mode 100644 src/app/view/maintain/maintain.component.less create mode 100644 src/app/view/maintain/maintain.component.ts create mode 100644 src/app/view/maintain/maintain.module.ts diff --git a/src/app/view/maintain/maintain.component.html b/src/app/view/maintain/maintain.component.html new file mode 100644 index 0000000..3d73c89 --- /dev/null +++ b/src/app/view/maintain/maintain.component.html @@ -0,0 +1,5 @@ + +
+ +
+
diff --git a/src/app/view/maintain/maintain.component.less b/src/app/view/maintain/maintain.component.less new file mode 100644 index 0000000..e69de29 diff --git a/src/app/view/maintain/maintain.component.ts b/src/app/view/maintain/maintain.component.ts new file mode 100644 index 0000000..fbe4099 --- /dev/null +++ b/src/app/view/maintain/maintain.component.ts @@ -0,0 +1,16 @@ +import {Component, OnInit} from '@angular/core'; + +@Component({ + selector: 'app-maintain', + templateUrl: './maintain.component.html', + styleUrls: ['./maintain.component.less'] +}) +export class MaintainComponent implements OnInit { + + constructor() { + } + + ngOnInit(): void { + } + +} diff --git a/src/app/view/maintain/maintain.module.ts b/src/app/view/maintain/maintain.module.ts new file mode 100644 index 0000000..ee5388a --- /dev/null +++ b/src/app/view/maintain/maintain.module.ts @@ -0,0 +1,18 @@ +import {NgModule} from '@angular/core'; +import {CommonModule} from '@angular/common'; +import {MaintainComponent} from './maintain.component'; +import {RouterModule} from '@angular/router'; +import {NzButtonModule, NzResultModule} from 'ng-zorro-antd'; + + +@NgModule({ + declarations: [MaintainComponent], + imports: [ + CommonModule, + RouterModule.forChild([{path: '', component: MaintainComponent}]), + NzResultModule, + NzButtonModule + ] +}) +export class MaintainModule { +} From 34ff380731f16687187184c87c0f3510a2f20273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 12:55:12 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/app-routing.module.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 76d430f..93fb679 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -11,17 +11,15 @@ const routes: Routes = [ {path: 'resetPwd', loadChildren: () => import('./view/reset-pwd/reset-pwd.module').then(mod => mod.ResetPwdModule)}, {path: 'write', loadChildren: () => import('./view/write/write.module').then(mod => mod.WriteModule)}, {path: 'links', loadChildren: () => import('./view/link/link.module').then(mod => mod.LinkModule)}, + {path: 'admin', loadChildren: () => import('./view/admin/admin.module').then(mod => mod.AdminModule)}, + {path: 'maintain', loadChildren: () => import('./view/maintain/maintain.module').then(mod => mod.MaintainModule)}, { path: 'emailVerify', loadChildren: () => import('./view/email-verify/email-verify.module').then(mod => mod.EmailVerifyModule) }, { - path: 'user', loadChildren: () => import('./view/login-registration/login-registration.module') - .then(mod => mod.LoginRegistrationModule) - }, - { - path: 'admin', - loadChildren: () => import('./view/admin/admin.module').then(mod => mod.AdminModule), + path: 'user', + loadChildren: () => import('./view/login-registration/login-registration.module').then(mod => mod.LoginRegistrationModule) }, { path: '**', From 8fb268f4cf2323697f4ef211c77760c1ea3006d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 12:55:58 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=B0=8Fheader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/components/header/header.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/components/header/header.component.ts b/src/app/components/header/header.component.ts index 1da6ca0..34de1e8 100644 --- a/src/app/components/header/header.component.ts +++ b/src/app/components/header/header.component.ts @@ -37,7 +37,8 @@ export class HeaderComponent implements OnInit { }); // 订阅一级路由的变化 componentStateService.watchRouterChange().subscribe(prefix => { - if (prefix === '/user' || prefix === '/write' || prefix === '/update') { + // TODO:: 使用service来获取 size + if (prefix === '/user' || prefix === '/write' || prefix === '/update' || prefix === '/maintain') { this.size = 'default'; } else { this.size = 'large'; From 3ff80e5b54aa416201b1cae91f00db2ca2af94e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 21:31:24 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E8=A7=A3=E9=99=A4=E7=BB=A7=E6=89=BF?= =?UTF-8?q?=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/api.service.ts | 136 ++++++++---------- src/app/view/index/index.component.ts | 2 +- .../registration/registration.component.ts | 2 +- 3 files changed, 65 insertions(+), 75 deletions(-) diff --git a/src/app/api/api.service.ts b/src/app/api/api.service.ts index 227b368..5a0d33b 100644 --- a/src/app/api/api.service.ts +++ b/src/app/api/api.service.ts @@ -1,10 +1,7 @@ -import {Injectable} from '@angular/core'; -import {HttpClient} from '@angular/common/http'; - +import {forwardRef, Inject, Injectable} from '@angular/core'; import {Article} from '../class/Article'; import {HttpService} from './http/http.service'; import {PageList} from '../class/HttpReqAndResp'; -import {ErrDispatch} from '../class/ErrDispatch'; import {ArticleReq} from '../class/Article'; import {Category, Tag} from '../class/Tag'; import {Comment} from '../class/Comment'; @@ -12,28 +9,21 @@ import {CommentReq} from '../class/Comment'; import {ApplyLinkReq, Link} from '../class/Link'; import {User} from '../class/User'; import {LoginReq} from '../class/User'; - -import {LocalStorageService} from '../services/local-storage.service'; import {Visitor} from '../class/Visitor'; import {UpdateInfo} from '../class/UpdateInfo'; @Injectable({ providedIn: 'root' }) -export class ApiService extends HttpService { +export class ApiService { - constructor(httpClient: HttpClient, - localStorageService: LocalStorageService) { - super(httpClient, localStorageService); - } - setErrDispatch(errDispatch: ErrDispatch) { - super.setErrDispatch(errDispatch); + constructor(private httpService: HttpService) { } createArticle(article: ArticleReq) { article.id = null; - return super.Service
({ + return this.httpService.Service
({ path: '/admin/article/create', contentType: 'application/json', method: 'POST', @@ -42,7 +32,7 @@ export class ApiService extends HttpService { } deleteArticle(id: number) { - return super.Service({ + return this.httpService.Service({ path: '/admin/article/del', method: 'DELETE', queryParam: {articleID: id} @@ -50,7 +40,7 @@ export class ApiService extends HttpService { } articles(pageNumber: number = 1, pageSize: number = 5) { - return super.Service>({ + return this.httpService.Service>({ path: '/articles', method: 'GET', queryParam: { @@ -61,7 +51,7 @@ export class ApiService extends HttpService { } adminArticles(pageNumber: number = 1, pageSize: number = 10) { - return super.Service>({ + return this.httpService.Service>({ path: '/admin/articles', method: 'GET', queryParam: { @@ -72,7 +62,7 @@ export class ApiService extends HttpService { } updateArticle(article: ArticleReq) { - return super.Service
({ + return this.httpService.Service
({ path: '/admin/article/update', method: 'PUT', contentType: 'application/json', @@ -81,7 +71,7 @@ export class ApiService extends HttpService { } getArticle(articleId: number, is4Update: boolean = false) { - return super.Service
({ + return this.httpService.Service
({ path: `/article/articleID/${articleId}`, method: 'GET', queryParam: {update: is4Update}, @@ -89,7 +79,7 @@ export class ApiService extends HttpService { } articlesByCategory(category: string, pageNumber: number = 1, pageSize: number = 10) { - return super.Service>({ + return this.httpService.Service>({ path: `/articles/category/${category}`, method: 'GET', queryParam: { @@ -100,7 +90,7 @@ export class ApiService extends HttpService { } articlesByTag(tag: string, pageNumber: number = 1, pageSize: number = 10) { - return super.Service>({ + return this.httpService.Service>({ path: `/articles/tag/${tag}`, method: 'GET', queryParam: { @@ -111,14 +101,14 @@ export class ApiService extends HttpService { } categories() { - return super.Service>({ + return this.httpService.Service>({ path: '/categories', method: 'GET' }); } createCategory(nameStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/admin/category/create', method: 'POST', queryParam: {name: nameStr} @@ -126,7 +116,7 @@ export class ApiService extends HttpService { } deleteCategory(categoryId: number) { - return super.Service({ + return this.httpService.Service({ path: '/admin/category/del', method: 'DELETE', queryParam: {id: categoryId} @@ -134,7 +124,7 @@ export class ApiService extends HttpService { } updateCategory(categoryId: number, nameStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/admin/category/update', method: 'PUT', queryParam: {id: categoryId, name: nameStr} @@ -142,7 +132,7 @@ export class ApiService extends HttpService { } tags(pageNumber: number = 1, pageSize: number = 10) { - return super.Service>({ + return this.httpService.Service>({ path: '/tags', method: 'GET', queryParam: { @@ -153,14 +143,14 @@ export class ApiService extends HttpService { } tagsNac() { - return super.Service<{ name: string, size: number }[]>({ + return this.httpService.Service<{ name: string, size: number }[]>({ path: '/tags/nac', method: 'GET' }); } createTag(nameStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/admin/tag/create', method: 'POST', queryParam: {name: nameStr} @@ -168,7 +158,7 @@ export class ApiService extends HttpService { } deleteTag(TagId: number) { - return super.Service({ + return this.httpService.Service({ path: '/admin/tag/del', method: 'DELETE', queryParam: {id: TagId} @@ -176,7 +166,7 @@ export class ApiService extends HttpService { } updateTag(TagId: number, nameStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/admin/tag/update', method: 'PUT', queryParam: {id: TagId, name: nameStr} @@ -184,7 +174,7 @@ export class ApiService extends HttpService { } getCommentByTypeForAdmin(pagePath: string, pageNumber: number = 1, pageSize: number = 10) { - return super.Service>({ + return this.httpService.Service>({ path: `/admin/comment/pagePath/${pagePath}`, method: 'GET', queryParam: { @@ -195,7 +185,7 @@ export class ApiService extends HttpService { } getCommentByTypeForUser(pagePath: string, pageNumber: number = 1, pageSize: number = 10) { - return super.Service>({ + return this.httpService.Service>({ path: `/user/comment/pagePath/${pagePath}`, method: 'GET', queryParam: { @@ -206,7 +196,7 @@ export class ApiService extends HttpService { } deleteComment(idNumer: number) { - return super.Service({ + return this.httpService.Service({ path: `/user/comment/del`, method: 'DELETE', queryParam: {id: idNumer} @@ -214,7 +204,7 @@ export class ApiService extends HttpService { } updateComment(commentReq: CommentReq) { - return super.Service({ + return this.httpService.Service({ path: `/user/comment/update`, method: 'PUT', data: commentReq, @@ -223,7 +213,7 @@ export class ApiService extends HttpService { } comments(pagePath: string, pageSize: number = 10, pageNumber: number = 1) { - return super.Service>({ + return this.httpService.Service>({ path: `/comment/pagePath/${pagePath}`, method: 'GET', queryParam: { @@ -234,7 +224,7 @@ export class ApiService extends HttpService { } createComment(commentReq: CommentReq) { - return super.Service({ + return this.httpService.Service({ path: '/user/comment/create', method: 'POST', contentType: 'application/json', @@ -244,7 +234,7 @@ export class ApiService extends HttpService { counts() { - return super.Service<{ + return this.httpService.Service<{ articleCount: number, visitorCount: number, categoryCount: number, @@ -257,7 +247,7 @@ export class ApiService extends HttpService { } adminLinks(pageSize: number = 10, pageNumber: number = 1) { - return super.Service>({ + return this.httpService.Service>({ path: '/admin/links', method: 'GET', queryParam: { @@ -268,7 +258,7 @@ export class ApiService extends HttpService { } createLink(linkReq: Link) { - return super.Service({ + return this.httpService.Service({ path: '/admin/links/create', method: 'POST', data: linkReq, @@ -277,14 +267,14 @@ export class ApiService extends HttpService { } deleteLink(idNumber: number) { - return super.Service({ + return this.httpService.Service({ path: `/admin/links/del/${idNumber}`, method: 'DELETE', }); } updateLink(linkReq: Link) { - return super.Service({ + return this.httpService.Service({ path: '/admin/links/update', method: 'PUT', data: linkReq, @@ -293,7 +283,7 @@ export class ApiService extends HttpService { } applyLink(link: ApplyLinkReq) { - return super.Service({ + return this.httpService.Service({ path: '/apply', method: 'POST', data: link, @@ -302,7 +292,7 @@ export class ApiService extends HttpService { } reapplyLink(keyStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/reapply', method: 'POST', queryParam: { @@ -312,14 +302,14 @@ export class ApiService extends HttpService { } links() { - return super.Service({ + return this.httpService.Service({ path: '/links', method: 'GET', }); } verifyImgCode(codeStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/verCode', method: 'POST', queryParam: {code: codeStr} @@ -328,7 +318,7 @@ export class ApiService extends HttpService { login(loginReq: LoginReq) { - return super.Service({ + return this.httpService.Service({ path: '/login', method: 'POST', contentType: 'application/json', @@ -337,14 +327,14 @@ export class ApiService extends HttpService { } logout() { - return super.Service({ + return this.httpService.Service({ path: '/logout', method: 'GET', }); } registration(emailStr: string, pwd: string) { - return super.Service({ + return this.httpService.Service({ path: '/registration', method: 'POST', queryParam: { @@ -355,7 +345,7 @@ export class ApiService extends HttpService { } resetPwd(idStr: string, emailStr: string, pwdStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/resetPwd', method: 'POST', queryParam: { @@ -367,7 +357,7 @@ export class ApiService extends HttpService { } emailVerify(idStr: string, emailStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/emailVerify', method: 'POST', queryParam: { @@ -379,7 +369,7 @@ export class ApiService extends HttpService { sendResetPwdEmail(emailStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/sendResetPwdEmail', method: 'POST', queryParam: {email: emailStr} @@ -387,7 +377,7 @@ export class ApiService extends HttpService { } sendVerifyEmail(emailStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/sendVerifyEmail', method: 'POST', queryParam: {email: emailStr} @@ -395,14 +385,14 @@ export class ApiService extends HttpService { } userInfo() { - return super.Service({ + return this.httpService.Service({ path: '/user/userInfo', method: 'GET', }); } adminUpdateUser(user: User) { - return super.Service({ + return this.httpService.Service({ path: '/admin/user', method: 'PUT', data: user, @@ -411,14 +401,14 @@ export class ApiService extends HttpService { } deleteUser(id: number) { - return super.Service({ + return this.httpService.Service({ path: `/admin/user/delete/${id}`, method: 'DELETE', }); } multipleDeleteUser(idArray: number[]) { - return super.Service<{ id: number; msg: string; status: boolean }[]>({ + return this.httpService.Service<{ id: number; msg: string; status: boolean }[]>({ path: `/admin/user/delete`, method: 'DELETE', data: idArray, @@ -428,14 +418,14 @@ export class ApiService extends HttpService { // 获取邮件是否已注册 emailStatus(email: string) { - return super.Service({ + return this.httpService.Service({ path: `/emailStatus/${email}`, method: 'GET' }) } updateUserInfo(descStr: string, disPlayNameStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/user/userInfo/update', method: 'PUT', queryParam: { @@ -446,7 +436,7 @@ export class ApiService extends HttpService { } adminUsers(pageSize: number = 10, pageNumber: number = 1) { - return super.Service>({ + return this.httpService.Service>({ path: '/admin/users', method: 'GET', queryParam: { @@ -457,14 +447,14 @@ export class ApiService extends HttpService { } visit() { - return super.Service({ + return this.httpService.Service({ path: '/visit', method: 'POST' }); } adminVisitors(location: boolean = false, pageSize: number = 10, pageNumber: number = 1) { - return super.Service>({ + return this.httpService.Service>({ path: '/admin/visitor/page', method: 'GET', queryParam: { @@ -476,42 +466,42 @@ export class ApiService extends HttpService { } dayVisitCount() { - return super.Service({ + return this.httpService.Service({ path: '/dayVisitCount', method: 'GET', }); } getLocalIp() { - return super.Service({ + return this.httpService.Service({ path: '/ip', method: 'GET', }); } getIpLocation(ip: string) { - return super.Service({ + return this.httpService.Service({ path: `/ip/${ip}`, method: 'GET', }); } visitorCount() { - return super.Service({ + return this.httpService.Service({ path: `/visitor/count`, method: 'GET', }); } webUpdate() { - return super.Service<{ id: number, info: string, time: string }[]>({ + return this.httpService.Service<{ id: number, info: string, time: string }[]>({ path: '/webUpdate', method: 'GET' }); } webUpdatePage(pageSize: number = 10, pageNumber: number = 1) { - return super.Service>({ + return this.httpService.Service>({ path: '/webUpdate/pages', method: 'GET', queryParam: { @@ -522,7 +512,7 @@ export class ApiService extends HttpService { } lastestUpdate() { - return super.Service<{ + return this.httpService.Service<{ lastUpdateTime: string; lastUpdateInfo: string; lastCommit: string; @@ -536,7 +526,7 @@ export class ApiService extends HttpService { } createWebUpdateInfo(infoStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/admin/webUpdate/create', method: 'POST', queryParam: {info: infoStr} @@ -544,14 +534,14 @@ export class ApiService extends HttpService { } deleteWebUpdateInfo(idNumber: number) { - return super.Service({ + return this.httpService.Service({ path: `/admin/webUpdate/del/${idNumber}`, method: 'DELETE', }); } updateWebUpdateInfo(idNumber: number, infoStr: string) { - return super.Service({ + return this.httpService.Service({ path: '/admin/webUpdate/update', method: 'PUT', queryParam: {id: idNumber, info: infoStr} @@ -559,14 +549,14 @@ export class ApiService extends HttpService { } bingPic() { - return super.Service({ + return this.httpService.Service({ path: '/bingPic', method: 'GET' }); } setPwd(pwdStr: string, newPwdStr: string, confirmPwdStr: string,) { - return super.Service({ + return this.httpService.Service({ path: '/user/setPwd', method: 'POST', queryParam: { diff --git a/src/app/view/index/index.component.ts b/src/app/view/index/index.component.ts index 0c8b905..e839d74 100644 --- a/src/app/view/index/index.component.ts +++ b/src/app/view/index/index.component.ts @@ -24,7 +24,7 @@ export class IndexComponent implements OnInit, ErrDispatch { private router: Router, private title: Title) { this.iconService.addIconLiteral('blog:location', SvgIconUtil.locationIcon); - apiService.setErrDispatch(this); + // apiService.setErrDispatch(this); title.setTitle('小海博客'); } diff --git a/src/app/view/login-registration/components/registration/registration.component.ts b/src/app/view/login-registration/components/registration/registration.component.ts index c4788a7..85d03f1 100644 --- a/src/app/view/login-registration/components/registration/registration.component.ts +++ b/src/app/view/login-registration/components/registration/registration.component.ts @@ -20,7 +20,7 @@ export class RegistrationComponent implements OnInit, ErrDispatch { private nzMessageService: NzMessageService, private router: Router, private title: Title) { - apiService.setErrDispatch(this); + // apiService.setErrDispatch(this); this.title.setTitle('小海博客 | 注册'); } From 66d523e69c4068cdd8430788eda871d8da87ffce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 21:34:26 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E4=BD=BF=E7=94=A8service=E8=BF=87?= =?UTF-8?q?=E5=BA=A6error=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/http/http.service.ts | 59 ++++++++++++------- src/app/app.module.ts | 24 ++++++-- src/app/services/error.service.ts | 23 ++++++++ src/app/view/index/index.component.ts | 9 +-- .../registration/registration.component.ts | 4 +- 5 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 src/app/services/error.service.ts diff --git a/src/app/api/http/http.service.ts b/src/app/api/http/http.service.ts index 7d3171c..de3d82e 100644 --- a/src/app/api/http/http.service.ts +++ b/src/app/api/http/http.service.ts @@ -1,28 +1,34 @@ -import {Injectable} from '@angular/core'; +import {forwardRef, Inject, Injectable, Injector} from '@angular/core'; import {RequestObj} from '../../class/HttpReqAndResp'; import {HttpClient, HttpResponse} from '@angular/common/http'; import {environment} from '../../../environments/environment'; import {LocalStorageService} from '../../services/local-storage.service'; import {Response} from '../../class/HttpReqAndResp'; -import {Observable, Observer, Subject} from 'rxjs'; -import {ErrDispatch} from '../../class/ErrDispatch'; +import {Observable, Observer, Subscription} from 'rxjs'; +import {ErrorService} from '../../services/error.service'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class HttpService { constructor(private httpClient: HttpClient, - protected localStorageService: LocalStorageService) { + protected localStorageService: LocalStorageService, + private injector: Injector) { } - private errorDispatch: ErrDispatch; + private subscriptionQueue: Subscription[] = []; - setErrDispatch(errDispatch: ErrDispatch) { - this.errorDispatch = errDispatch; - } + // private errorDispatch: ErrDispatch; + + public getSubscriptionQueue = () => this.subscriptionQueue; + + // setErrDispatch(errDispatch: ErrDispatch) { + // this.errorDispatch = errDispatch; + // } Service(request: RequestObj) { + const errorService = this.injector.get(ErrorService); request.url = null; // 设置默认值 request.contentType = request.contentType == null ? 'application/x-www-form-urlencoded' : request.contentType; @@ -55,21 +61,30 @@ export class HttpService { const oob = new Observable>(o => observer = o); - observable.subscribe(o => { - const tokenFromReps = o.headers.get('Authorization'); - if (tokenFromReps) { - this.localStorageService.setToken(tokenFromReps); - } - if (o.body.code !== 0) { - observer.error(o.body); - if (this.errorDispatch) { - this.errorDispatch.errHandler(o.body.code, o.body.msg, request); + const subscription = observable.subscribe({ + next: o => { + const tokenFromReps = o.headers.get('Authorization'); + if (tokenFromReps) { + this.localStorageService.setToken(tokenFromReps); } - } else { - observer.next(o.body); - } - observer.complete(); + if (o.body.code !== 0) { + observer.error(o.body); + errorService.httpException(o.body) + // if (this.errorDispatch) { + // this.errorDispatch.errHandler(o.body.code, o.body.msg, request); + // } + } else { + observer.next(o.body); + } + observer.complete(); + }, + error: err => { + errorService.httpError(err); + this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) + }, + complete: () => this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) }); + this.subscriptionQueue.push(subscription); return oob; } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 0e762e0..ae58dc1 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,5 +1,5 @@ import {BrowserModule} from '@angular/platform-browser'; -import {NgModule} from '@angular/core'; +import {forwardRef, NgModule} from '@angular/core'; import {AppComponent} from './app.component'; import {NgZorroAntdModule, NZ_I18N, zh_CN} from 'ng-zorro-antd'; import {FormsModule} from '@angular/forms'; @@ -12,8 +12,14 @@ import {FooterComponent} from './components/footer/footer.component'; import {AppRoutingModule} from './app-routing.module'; import {LoginRegistrationModule} from './view/login-registration/login-registration.module'; import {AdminModule} from './view/admin/admin.module'; -import { ServiceWorkerModule } from '@angular/service-worker'; -import { environment } from '../environments/environment'; +import {ServiceWorkerModule} from '@angular/service-worker'; +import {environment} from '../environments/environment'; +import {HttpService} from './api/http/http.service'; +import {ErrorService} from './services/error.service'; +import {ComponentStateService} from './services/component-state.service'; +import {GlobalUserService} from './services/global-user.service'; +import {LocalStorageService} from './services/local-storage.service'; +import {ApiService} from './api/api.service'; registerLocaleData(zh); @@ -33,9 +39,17 @@ registerLocaleData(zh); BrowserAnimationsModule, LoginRegistrationModule, AdminModule, - ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }) + ServiceWorkerModule.register('ngsw-worker.js', {enabled: environment.production}) + ], + providers: [ + ComponentStateService, + GlobalUserService, + LocalStorageService, + HttpService, + ApiService, + ErrorService, + {provide: NZ_I18N, useValue: zh_CN}, ], - providers: [{provide: NZ_I18N, useValue: zh_CN}], exports: [], bootstrap: [AppComponent] }) diff --git a/src/app/services/error.service.ts b/src/app/services/error.service.ts new file mode 100644 index 0000000..6c52778 --- /dev/null +++ b/src/app/services/error.service.ts @@ -0,0 +1,23 @@ +import {Injectable, Injector} from '@angular/core'; +import {Response} from '../class/HttpReqAndResp'; +import {HttpService} from '../api/http/http.service'; + +@Injectable({ + providedIn: 'root' +}) +export class ErrorService { + + constructor(private httpService: HttpService) { + } + + public httpError(err: any) { + console.log('error=>', err) + // this.httpService.getSubscriptionQueue().map(a => a.unsubscribe()) + } + + public httpException(response: Response) { + console.log('exception=>', response) + } + + +} diff --git a/src/app/view/index/index.component.ts b/src/app/view/index/index.component.ts index e839d74..5d543a2 100644 --- a/src/app/view/index/index.component.ts +++ b/src/app/view/index/index.component.ts @@ -3,11 +3,9 @@ import {ApiService} from '../../api/api.service'; import {Article} from '../../class/Article'; import {NzIconService, NzMessageService} from 'ng-zorro-antd'; import {SvgIconUtil} from '../../utils/svgIconUtil'; -import {PageList} from '../../class/HttpReqAndResp'; -import {ErrDispatch} from '../../class/ErrDispatch'; -import {RequestObj} from '../../class/HttpReqAndResp'; +import {PageList, RequestObj} from '../../class/HttpReqAndResp'; import {Router} from '@angular/router'; -import {Category, Tag} from '../../class/Tag'; +import {Category} from '../../class/Tag'; import {Title} from '@angular/platform-browser'; @Component({ @@ -16,7 +14,7 @@ import {Title} from '@angular/platform-browser'; styleUrls: ['./index.component.less'], providers: [ApiService] }) -export class IndexComponent implements OnInit, ErrDispatch { +export class IndexComponent implements OnInit { constructor(private apiService: ApiService, private iconService: NzIconService, @@ -24,7 +22,6 @@ export class IndexComponent implements OnInit, ErrDispatch { private router: Router, private title: Title) { this.iconService.addIconLiteral('blog:location', SvgIconUtil.locationIcon); - // apiService.setErrDispatch(this); title.setTitle('小海博客'); } diff --git a/src/app/view/login-registration/components/registration/registration.component.ts b/src/app/view/login-registration/components/registration/registration.component.ts index 85d03f1..2e1439c 100644 --- a/src/app/view/login-registration/components/registration/registration.component.ts +++ b/src/app/view/login-registration/components/registration/registration.component.ts @@ -3,7 +3,6 @@ import {environment} from '../../../../../environments/environment'; import {ApiService} from '../../../../api/api.service'; import {NzMessageService} from 'ng-zorro-antd'; import {Router} from '@angular/router'; -import {ErrDispatch} from '../../../../class/ErrDispatch'; import {RequestObj} from '../../../../class/HttpReqAndResp'; import {LoginReq} from '../../../../class/User'; import {Title} from '@angular/platform-browser'; @@ -14,13 +13,12 @@ import {Title} from '@angular/platform-browser'; styleUrls: ['./registration.component.less'], providers: [ApiService] }) -export class RegistrationComponent implements OnInit, ErrDispatch { +export class RegistrationComponent implements OnInit { constructor(private apiService: ApiService, private nzMessageService: NzMessageService, private router: Router, private title: Title) { - // apiService.setErrDispatch(this); this.title.setTitle('小海博客 | 注册'); } From 01c21f57327a7bb1361eb9a2df88736cd34acea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 21:35:10 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=88=A0=E9=99=A4ErrDispatch.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/class/ErrDispatch.ts | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 src/app/class/ErrDispatch.ts diff --git a/src/app/class/ErrDispatch.ts b/src/app/class/ErrDispatch.ts deleted file mode 100644 index 79c04fa..0000000 --- a/src/app/class/ErrDispatch.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {RequestObj} from './HttpReqAndResp'; - -export interface ErrDispatch { - errHandler(code: number, msg: string, request?: RequestObj): void; -} From 6fb62adb9110d0af2cd23ea1632a76db0c55141d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 21:38:21 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/http/http.service.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/app/api/http/http.service.ts b/src/app/api/http/http.service.ts index de3d82e..909f072 100644 --- a/src/app/api/http/http.service.ts +++ b/src/app/api/http/http.service.ts @@ -1,9 +1,8 @@ -import {forwardRef, Inject, Injectable, Injector} from '@angular/core'; -import {RequestObj} from '../../class/HttpReqAndResp'; +import {Injectable, Injector} from '@angular/core'; +import {RequestObj, Response} from '../../class/HttpReqAndResp'; import {HttpClient, HttpResponse} from '@angular/common/http'; import {environment} from '../../../environments/environment'; import {LocalStorageService} from '../../services/local-storage.service'; -import {Response} from '../../class/HttpReqAndResp'; import {Observable, Observer, Subscription} from 'rxjs'; import {ErrorService} from '../../services/error.service'; @@ -19,14 +18,8 @@ export class HttpService { private subscriptionQueue: Subscription[] = []; - // private errorDispatch: ErrDispatch; - public getSubscriptionQueue = () => this.subscriptionQueue; - // setErrDispatch(errDispatch: ErrDispatch) { - // this.errorDispatch = errDispatch; - // } - Service(request: RequestObj) { const errorService = this.injector.get(ErrorService); request.url = null; @@ -70,9 +63,6 @@ export class HttpService { if (o.body.code !== 0) { observer.error(o.body); errorService.httpException(o.body) - // if (this.errorDispatch) { - // this.errorDispatch.errHandler(o.body.code, o.body.msg, request); - // } } else { observer.next(o.body); } From 44f251135c71b48b8dc90e96d683f3259eb3ee20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 22:16:30 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/http/http.service.ts | 11 +++++---- src/app/services/error.service.ts | 41 ++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/app/api/http/http.service.ts b/src/app/api/http/http.service.ts index 909f072..9f38d52 100644 --- a/src/app/api/http/http.service.ts +++ b/src/app/api/http/http.service.ts @@ -12,7 +12,7 @@ import {ErrorService} from '../../services/error.service'; export class HttpService { constructor(private httpClient: HttpClient, - protected localStorageService: LocalStorageService, + private localStorageService: LocalStorageService, private injector: Injector) { } @@ -70,6 +70,7 @@ export class HttpService { }, error: err => { errorService.httpError(err); + errorService.checkConnection(); this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) }, complete: () => this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) @@ -78,7 +79,7 @@ export class HttpService { return oob; } - private get(request: RequestObj) { + get(request: RequestObj) { return this.httpClient.get(request.url, { headers: request.header, @@ -87,7 +88,7 @@ export class HttpService { }); } - private post(request: RequestObj) { + post(request: RequestObj) { return this.httpClient.post(request.url, request.data, { headers: request.header, @@ -96,7 +97,7 @@ export class HttpService { }); } - private put(request: RequestObj) { + put(request: RequestObj) { return this.httpClient.put(request.url, request.data, { headers: request.header, @@ -105,7 +106,7 @@ export class HttpService { }); } - private delete(request: RequestObj) { + delete(request: RequestObj) { return this.httpClient.delete(request.url, { headers: request.header, diff --git a/src/app/services/error.service.ts b/src/app/services/error.service.ts index 6c52778..b68b35a 100644 --- a/src/app/services/error.service.ts +++ b/src/app/services/error.service.ts @@ -1,23 +1,52 @@ -import {Injectable, Injector} from '@angular/core'; -import {Response} from '../class/HttpReqAndResp'; +import {Injectable} from '@angular/core'; +import {RequestObj, Response} from '../class/HttpReqAndResp'; import {HttpService} from '../api/http/http.service'; +import {environment} from '../../environments/environment'; +import {Router} from '@angular/router'; +import {ComponentStateService} from './component-state.service'; @Injectable({ providedIn: 'root' }) export class ErrorService { - constructor(private httpService: HttpService) { + constructor(private httpService: HttpService, private router: Router, + private componentStateService: ComponentStateService) { } + private static HTTP_ERROR_COUNT: number = 0; + private readonly MAINTAIN_PAGE_PREFIX = '/maintain' + public httpError(err: any) { - console.log('error=>', err) + if (!environment.production) { + console.log('error=>', err) + } + ErrorService.HTTP_ERROR_COUNT++; // this.httpService.getSubscriptionQueue().map(a => a.unsubscribe()) } public httpException(response: Response) { - console.log('exception=>', response) + if (!environment.production) + console.log('exception=>', response) } - + public checkConnection() { + // The HTTP_ERROR_COUNT is start with 1 in this function + if (ErrorService.HTTP_ERROR_COUNT === 1) { + const req: RequestObj = { + path: '/headerInfo', + method: 'GET', + url: environment.host + '/headerInfo' + } + this.httpService.get(req).subscribe({ + next: data => null, + error: err => { + if (this.componentStateService.currentPath !== this.MAINTAIN_PAGE_PREFIX) { + this.router.navigateByUrl(this.MAINTAIN_PAGE_PREFIX) + } + ErrorService.HTTP_ERROR_COUNT = 0; + } + }) + } + } } From 53ddfe6e4c932dbb5af055aeaecd0626ea3f3582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 22:30:07 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/services/error.service.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/app/services/error.service.ts b/src/app/services/error.service.ts index b68b35a..a1a00f6 100644 --- a/src/app/services/error.service.ts +++ b/src/app/services/error.service.ts @@ -4,6 +4,7 @@ import {HttpService} from '../api/http/http.service'; import {environment} from '../../environments/environment'; import {Router} from '@angular/router'; import {ComponentStateService} from './component-state.service'; +import {NzNotificationService} from 'ng-zorro-antd'; @Injectable({ providedIn: 'root' @@ -11,11 +12,13 @@ import {ComponentStateService} from './component-state.service'; export class ErrorService { constructor(private httpService: HttpService, private router: Router, - private componentStateService: ComponentStateService) { + private componentStateService: ComponentStateService, + private notification: NzNotificationService) { } private static HTTP_ERROR_COUNT: number = 0; private readonly MAINTAIN_PAGE_PREFIX = '/maintain' + private readonly ADMIN_PAGE_PREFIX = '/admin' public httpError(err: any) { if (!environment.production) { @@ -28,6 +31,10 @@ export class ErrorService { public httpException(response: Response) { if (!environment.production) console.log('exception=>', response) + if (response.code === -1 && response.msg === '请求重复') return + if (this.componentStateService.currentPath === this.ADMIN_PAGE_PREFIX) { + this.notification.create('error', `请求失败<${response.code}>`, `${response.msg}`); + } } public checkConnection() { @@ -39,8 +46,8 @@ export class ErrorService { url: environment.host + '/headerInfo' } this.httpService.get(req).subscribe({ - next: data => null, - error: err => { + next: () => null, + error: () => { if (this.componentStateService.currentPath !== this.MAINTAIN_PAGE_PREFIX) { this.router.navigateByUrl(this.MAINTAIN_PAGE_PREFIX) } From 86378a46f2836a41ccaa8d623164ac8f916d8a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 22:33:53 +0800 Subject: [PATCH 10/11] typo --- src/app/services/error.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/services/error.service.ts b/src/app/services/error.service.ts index a1a00f6..748fee3 100644 --- a/src/app/services/error.service.ts +++ b/src/app/services/error.service.ts @@ -31,7 +31,7 @@ export class ErrorService { public httpException(response: Response) { if (!environment.production) console.log('exception=>', response) - if (response.code === -1 && response.msg === '请求重复') return + if (response.code === -1 && response.msg === '重复请求') return if (this.componentStateService.currentPath === this.ADMIN_PAGE_PREFIX) { this.notification.create('error', `请求失败<${response.code}>`, `${response.msg}`); } From a81f41e15cdab990b6ea55abccc683e793ded110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 5 Aug 2020 22:36:09 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E5=8F=8B=E9=93=BE=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../view/admin/admin-link/admin-link.component.html | 1 + src/app/view/admin/admin-link/admin-link.module.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/app/view/admin/admin-link/admin-link.component.html b/src/app/view/admin/admin-link/admin-link.component.html index 664abab..7d68b1c 100644 --- a/src/app/view/admin/admin-link/admin-link.component.html +++ b/src/app/view/admin/admin-link/admin-link.component.html @@ -4,6 +4,7 @@ [headData]="headData" [template]="{open:{temp:open,param:{true:'可见',false:'不可见'}},delete:{temp:deleteTemp,param:{true:'已删除',false:'未删除'}}}" > + diff --git a/src/app/view/admin/admin-link/admin-link.module.ts b/src/app/view/admin/admin-link/admin-link.module.ts index 5c78133..954d661 100644 --- a/src/app/view/admin/admin-link/admin-link.module.ts +++ b/src/app/view/admin/admin-link/admin-link.module.ts @@ -3,7 +3,15 @@ import {CommonModule} from '@angular/common'; import {RouterModule} from '@angular/router'; import {AdminLinkComponent} from './admin-link.component'; import {CommonTableModule} from '../components/common-table/common-table.module'; -import {NzCheckboxModule, NzFormModule, NzInputModule, NzModalModule, NzSelectModule, NzTagModule} from 'ng-zorro-antd'; +import { + NzButtonModule, + NzCheckboxModule, + NzFormModule, + NzInputModule, + NzModalModule, + NzSelectModule, + NzTagModule +} from 'ng-zorro-antd'; import {FormsModule, ReactiveFormsModule} from '@angular/forms'; @@ -23,6 +31,7 @@ import {FormsModule, ReactiveFormsModule} from '@angular/forms'; NzInputModule, NzSelectModule, NzTagModule, + NzButtonModule, ] })