From 1930fe2f76bc7218808caa0cc03566a0797ea39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=B5=B7?= Date: Sat, 16 May 2020 14:47:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B7=AF=E7=94=B1=E5=AE=88?= =?UTF-8?q?=E5=8D=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index/src/app/view/admin/admin.component.html | 18 ++--- index/src/app/view/admin/admin.component.ts | 20 +++--- index/src/app/view/admin/auth.guard.ts | 66 +++++++++++-------- 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/index/src/app/view/admin/admin.component.html b/index/src/app/view/admin/admin.component.html index 4d741ae..6db67a4 100644 --- a/index/src/app/view/admin/admin.component.html +++ b/index/src/app/view/admin/admin.component.html @@ -38,15 +38,15 @@ 分类标签管理 - - - - + + + + -
  • - - 修改信息 -
  • + + + +
  • @@ -89,7 +89,7 @@ + [nzTitle]="sayHelloTemp" (nzOnClose)="infoDrawerVisible = false" >

    您最近一次登录是在:

    {{user.recentlyLandedDate}}



    diff --git a/index/src/app/view/admin/admin.component.ts b/index/src/app/view/admin/admin.component.ts index b2fe207..d1c3602 100644 --- a/index/src/app/view/admin/admin.component.ts +++ b/index/src/app/view/admin/admin.component.ts @@ -13,6 +13,16 @@ import {ApiService} from '../../api/api.service'; export class AdminComponent implements OnInit { constructor(public gUserService: GlobalUserService, private apiService: ApiService, private messageService: NzMessageService) { + this.gUserService.watchUserInfo({ + complete: () => null, + error: (err) => null, + next: data => { + console.log('更新user') + this.user = data.result + this.initHelloWords() + } + } + ) } user: User; @@ -25,21 +35,11 @@ export class AdminComponent implements OnInit { showInfoDrawer = () => this.infoDrawerVisible = !this.infoDrawerVisible; ngOnInit(): void { - this.gUserService.watchUserInfo({ - complete: () => null, - error: (err) => null, - next: data => { - console.log('更新user') - this.user = data.result - } - } - ) this.editInfoFormGroup = new FormGroup({ desc: new FormControl(), displayName: new FormControl(), email: new FormControl({value: null, disabled: true}) }); - this.initHelloWords() } private initHelloWords() { diff --git a/index/src/app/view/admin/auth.guard.ts b/index/src/app/view/admin/auth.guard.ts index 3c12748..fb7cd72 100644 --- a/index/src/app/view/admin/auth.guard.ts +++ b/index/src/app/view/admin/auth.guard.ts @@ -1,6 +1,6 @@ import {Injectable} from '@angular/core'; import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router} from '@angular/router'; -import {Observable} from 'rxjs'; +import {Observable, Observer} from 'rxjs'; import {User} from '../../class/User'; import {GlobalUserService} from '../../services/global-user.service'; @@ -10,48 +10,58 @@ import {GlobalUserService} from '../../services/global-user.service'; export class AuthGuard implements CanActivate { constructor(private userService: GlobalUserService, private router: Router) { - userService.watchUserInfo({ - complete: () => null, - error: (err) => { - // 请求重复 - console.log(err); - if (err.code !== -1) { - this.userInfo = null; - this.router.navigateByUrl(this.loginPath); - } - }, - next: data => { - this.userInfo = data.result - } - }) + this.userService.refreshUserInfo(); } userInfo: User; private path: string; private readonly loginPath: string = '/user/login'; + private observable: Observable; - canActivate( - next: ActivatedRouteSnapshot, - state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { - const path = state.url.indexOf('?') > 0 ? state.url.substr(0, state.url.indexOf('?')) : state.url; - this.path = path - if (path.split('/')[1] === 'admin' && !this.userInfo) { - this.router.navigateByUrl(this.loginPath); - return false; - } - switch (path) { + canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { + this.path = state.url.indexOf('?') > 0 ? state.url.substr(0, state.url.indexOf('?')) : state.url; + this.observable = new Observable(observer => { + if (!this.userInfo) { + this.watchUserInfo(observer); + } else { + this.checkPath(observer); + } + }); + return this.observable; + } + + watchUserInfo(observer: Observer) { + this.userService.watchUserInfo({ + complete: null, + error: (err) => { + // 请求重复 + if (err.code !== -1) { + observer.next(false); + this.router.navigateByUrl(this.loginPath); + } + }, + next: data => { + this.userInfo = data.result; + this.checkPath(observer); + } + }) + } + + + checkPath(observer: Observer) { + switch (this.path) { case '/admin/article': - case '/admin/category': case '/admin/link': case '/admin/tag': case '/admin/update': case '/admin/user': case '/admin/visitor': if (this.userInfo.role !== 'admin') { - return false; + observer.next(false) + return; } } - return true; + observer.next(true); } }