合并为一个项目 #14

Merged
xiaohai2271 merged 56 commits from issue11 into master 2020-05-16 22:35:08 +08:00
5 changed files with 76 additions and 46 deletions
Showing only changes of commit e96e2c2f0d - Show all commits

View File

@@ -4,6 +4,7 @@ import {windowWidthChange} from '../../utils/util';
import {ApiService} from '../../api/api.service'; import {ApiService} from '../../api/api.service';
import {User} from '../../class/User'; import {User} from '../../class/User';
import {ComponentStateService} from '../../services/component-state.service'; import {ComponentStateService} from '../../services/component-state.service';
import {UserService} from '../../services/user.service';
@Component({ @Component({
selector: 'app-header', selector: 'app-header',
@@ -13,8 +14,8 @@ import {ComponentStateService} from '../../services/component-state.service';
export class HeaderComponent implements OnInit { export class HeaderComponent implements OnInit {
constructor(private router: Router, constructor(private router: Router,
private apiService: ApiService, public componentStateService: ComponentStateService,
public componentStateService: ComponentStateService) { private userService: UserService) {
this.pageList = [ this.pageList = [
{name: '首页', path: '/', icon: 'home', iconType: 'fill', show: true}, {name: '首页', path: '/', icon: 'home', iconType: 'fill', show: true},
{name: '分类', path: '/categories', icon: 'project', iconType: 'fill', show: true}, {name: '分类', path: '/categories', icon: 'project', iconType: 'fill', show: true},
@@ -26,7 +27,7 @@ export class HeaderComponent implements OnInit {
{name: '注册', path: '/registration', icon: 'user', iconType: 'outline', show: false} {name: '注册', path: '/registration', icon: 'user', iconType: 'outline', show: false}
]; ];
this.getInfo();
this.showList = window.innerWidth > this.mobileMaxWidth; this.showList = window.innerWidth > this.mobileMaxWidth;
this.changeLoginButtonV(); this.changeLoginButtonV();
// 监听宽度变化 // 监听宽度变化
@@ -41,7 +42,6 @@ export class HeaderComponent implements OnInit {
} else { } else {
this.size = 'large'; this.size = 'large';
} }
this.getInfo();
}); });
} }
@@ -116,27 +116,27 @@ export class HeaderComponent implements OnInit {
} }
getInfo() { getInfo() {
this.apiService.userInfo().subscribe(data => { this.userService.watchUserInfo({
complete: () => null,
error: (err) => null,
next: data => {
this.userInfo = data.result; this.userInfo = data.result;
this.changeLoginButtonV(); this.changeLoginButtonV();
}, }
error => {
} }
); );
} }
logout() { logout() {
this.apiService.logout().subscribe(data => { this.userService.logout({
location.reload(); next: data => null,
}, error: err => null,
error => { complete: () => null
} });
);
this.userInfo = null;
} }
toAdminPage() { toAdminPage() {
window.location.href = '/admin'; this.router.navigateByUrl('/admin')
} }
} }

View File

@@ -2,21 +2,33 @@ import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree} from '@angular/router'; import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {User} from '../../class/User'; import {User} from '../../class/User';
import {UserService} from '../../services/user.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthGuard implements CanActivate { export class AuthGuard implements CanActivate {
constructor() { constructor(private userService: UserService) {
userService.watchUserInfo({
complete: () => null,
error: (err) => null,
next: data => {
this.userInfo = data.result
console.log(this.path);
// todo :用户信息更新时 重新判断下path
}
})
} }
userInfo: User; userInfo: User;
private path: string;
canActivate( canActivate(
next: ActivatedRouteSnapshot, next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const path = state.url.indexOf('?') > 0 ? state.url.substr(0, state.url.indexOf('?')) : state.url; const path = state.url.indexOf('?') > 0 ? state.url.substr(0, state.url.indexOf('?')) : state.url;
this.path = path
switch (path) { switch (path) {
case '/admin/article': case '/admin/article':
case '/admin/category': case '/admin/category':

View File

@@ -7,6 +7,7 @@ import {User} from '../../class/User';
import {CommentReq} from '../../class/Comment'; import {CommentReq} from '../../class/Comment';
import {PageList} from '../../class/HttpReqAndResp'; import {PageList} from '../../class/HttpReqAndResp';
import {Comment} from '../../class/Comment'; import {Comment} from '../../class/Comment';
import {UserService} from '../../services/user.service';
declare var editormd; declare var editormd;
declare var $; declare var $;
@@ -20,6 +21,7 @@ export class ArticleComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute, constructor(private activatedRoute: ActivatedRoute,
private apiService: ApiService, private apiService: ApiService,
private userService: UserService,
private titleService: Title, private titleService: Title,
private router: Router) { private router: Router) {
this.articleId = +activatedRoute.snapshot.paramMap.get('id'); this.articleId = +activatedRoute.snapshot.paramMap.get('id');
@@ -41,10 +43,13 @@ export class ArticleComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.toArticle(this.articleId); this.toArticle(this.articleId);
this.apiService.userInfo().subscribe(data => { this.userService.watchUserInfo({
complete: () => null,
error: (err) => null,
next: data => {
this.user = data.result; this.user = data.result;
this.avatarImgUrl = data.result.avatarImgUrl; this.avatarImgUrl = data.result.avatarImgUrl;
}, error => { }
}); });
this.comment = new CommentReq(true); this.comment = new CommentReq(true);
} }

View File

@@ -1,11 +1,10 @@
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd'; import {NzMessageService} from 'ng-zorro-antd';
import {ApiService} from '../../../../api/api.service';
import {LoginReq} from '../../../../class/User'; import {LoginReq} from '../../../../class/User';
import {LocalStorageService} from '../../../../services/local-storage.service';
import {ActivatedRoute, Router} from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import {LoginRegistrationService} from '../../service/login-registration.service'; import {LoginRegistrationService} from '../../service/login-registration.service';
import {Title} from '@angular/platform-browser'; import {Title} from '@angular/platform-browser';
import {UserService} from '../../../../services/user.service';
@Component({ @Component({
selector: 'c-login', selector: 'c-login',
@@ -15,7 +14,7 @@ import {Title} from '@angular/platform-browser';
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
constructor(private nzMessageService: NzMessageService, constructor(private nzMessageService: NzMessageService,
private apiService: ApiService, private userService: UserService,
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
private router: Router, private router: Router,
private loginRegistrationService: LoginRegistrationService, private loginRegistrationService: LoginRegistrationService,
@@ -54,20 +53,24 @@ export class LoginComponent implements OnInit {
return; return;
} }
this.apiService.login(this.loginReq).subscribe( this.userService.login(this.loginReq, {
data => { complete: () => null,
error: (err) => {
this.nzMessageService.error(err.msg);
this.submitting = false;
this.loginStatus.emit(false);
},
next: data => {
this.submitting = false; this.submitting = false;
this.nzMessageService.success('登录成功,欢迎你' + data.result.displayName); this.nzMessageService.success('登录成功,欢迎你' + data.result.displayName);
this.loginStatus.emit(true); this.loginStatus.emit(true);
if (this.url) { if (this.url) {
this.router.navigateByUrl(this.url); this.router.navigateByUrl(this.url);
} else { } else {
window.location.href = '/admin/'; // window.location.href = '/admin/';
this.router.navigateByUrl('/admin')
}
} }
}, error => {
this.nzMessageService.error(error.msg);
this.submitting = false;
this.loginStatus.emit(false);
} }
); );
} }

View File

@@ -7,6 +7,7 @@ import {NzMessageService} from 'ng-zorro-antd';
import {User} from '../../class/User'; import {User} from '../../class/User';
import {Tag} from '../../class/Tag'; import {Tag} from '../../class/Tag';
import {Title} from '@angular/platform-browser'; import {Title} from '@angular/platform-browser';
import {UserService} from '../../services/user.service';
@Component({ @Component({
selector: 'view-write', selector: 'view-write',
@@ -18,6 +19,7 @@ export class WriteComponent implements OnInit {
constructor(private router: Router, constructor(private router: Router,
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
private apiService: ApiService, private apiService: ApiService,
private userService: UserService,
private message: NzMessageService, private message: NzMessageService,
private titleService: Title) { private titleService: Title) {
this.titleService.setTitle('小海博客 | 创作'); this.titleService.setTitle('小海博客 | 创作');
@@ -37,6 +39,8 @@ export class WriteComponent implements OnInit {
// 发布新文章时,文章相同会被拦回 此处判断一下 // 发布新文章时,文章相同会被拦回 此处判断一下
title: string; title: string;
private lastShowTime: number;
// 同步属性内容 // 同步属性内容
syncModel(str): void { syncModel(str): void {
this.article.mdContent = str; this.article.mdContent = str;
@@ -54,17 +58,23 @@ export class WriteComponent implements OnInit {
} }
this.setSuitableHeight(); this.setSuitableHeight();
// 用户权限判断 // 用户权限判断
this.apiService.userInfo().subscribe({ this.userService.watchUserInfo({
next: data => { complete: () => null,
this.userInfo = data.result; error: (err) => {
if (data.result.role !== 'admin') { if (!this.lastShowTime || Date.now() - this.lastShowTime > 1000) {
this.message.info('你暂时无发布文章的权限,所写文章将暂存在本地'); this.message.info('你暂时还没有登录,请点击右上角登录后开始创作');
this.lastShowTime = Date.now();
} }
}, },
error: e => { next: data => {
this.message.info('你暂时还没有登录,请点击右上角登录后开始创作'); this.userInfo = data.result;
if ((!data.result || data.result.role !== 'admin')
&& (!this.lastShowTime || Date.now() - this.lastShowTime > 1000)) {
this.message.info('你暂时无发布文章的权限,所写文章将暂存在本地');
} }
}); }
})
;
this.apiService.tagsNac().subscribe(data => { this.apiService.tagsNac().subscribe(data => {
this.tagNacList = data.result; this.tagNacList = data.result;
this.tagNacList.sort((a, b) => a.name.length - b.name.length); this.tagNacList.sort((a, b) => a.name.length - b.name.length);