合并为一个项目 #14

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

View File

@@ -0,0 +1,26 @@
import {Observable, of} from 'rxjs';
import {PageList} from '../class/HttpReqAndResp';
/**
* 判断 一个Page<any>[] 中是否存在一条已查询的数据
* @param pageNum 页码
* @param pageSize 单页数量
* @param pageList 源数据
* @return 未查到null 查到:该条数据
*/
export function exist<T>(pageNum: number, pageSize: number, pageList: PageList<any>[]): Observable<PageList<T>> | null {
if (pageList === undefined || pageList == null || pageList.length === 0) {
return null;
}
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < pageList.length; i++) {
// tslint:disable-next-line:triple-equals
if (pageList[i].pageNum == pageNum && pageList[i].pageSize == pageSize) {
const ob: Observable<PageList<T>> = new Observable(o => {
o.next(pageList[i]);
o.complete();
})
}
}
return null;
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { ArticleService } from './article.service';
describe('ArticleService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: ArticleService = TestBed.get(ArticleService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,48 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {PageList} from "../../../../class/HttpReqAndResp";
import {Article} from "../../../../class/Article";
import {exist} from "../../../../utils/dataUtil";
@Injectable({
providedIn: 'root'
})
export class ArticleService {
constructor(public http: HttpService) {
}
// 存储所有已经请求过的数据<首页数据>
pageList: PageList<Article>[] = [];
currentPage: PageList<Article>;
/**
* 获取文章
* @param pageNum 页码数
* @param pageSize 单页数据量
*/
getArticle(pageNum: number, pageSize: number) {
const articlePage = exist<Article>(pageNum, pageSize, this.pageList);
if (articlePage) {
articlePage.subscribe(data => {
this.currentPage = data;
});
return articlePage;
}
const observable = this.http.get('/admin/articles?page=' + pageNum + '&count=' + pageSize);
observable.subscribe(data => {
if (data.code === 0) {
this.currentPage = data.result;
this.pageList.push(data.result);
}
});
return observable;
}
deleteArticle(id) {
return this.http.delete('/admin/article/del?articleID=' + id);
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { CategoryService } from './category.service';
describe('CategoryService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: CategoryService = TestBed.get(CategoryService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,56 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {Category} from '../../classes/category';
@Injectable({
providedIn: 'root'
})
export class CategoryService {
// FIXME : !!!!!!!!!!!!!!!!!!数据处理全部放到一个模块中!!!!!!!!!!!!!!!!!!
constructor(public http: HttpService) {
}
categories: Category[];
getAllCategory() {
const observable = this.http.get('/categories');
observable.subscribe((data) => {
if (data.code === 0) {
this.categories = data.result;
}
}
);
return observable;
}
update(submitBody: { id: number, name: string }) {
return this.http.put('/admin/category/update', submitBody, false);
}
create(nameStr: string) {
const observable = this.http.post('/admin/category/create', {name: nameStr}, false);
observable.subscribe(data => {
if (data.code === 0) {
this.categories.push(data.result);
}
});
return observable;
}
delete(id: number) {
const observable = this.http.delete(`/admin/category/del?id=${id}`);
observable.subscribe(data => {
if (data.code === 0) {
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < this.categories.length; i++) {
if (this.categories[i].id === id) {
this.categories.splice(i, 1);
}
}
}
});
return observable;
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { CommentService } from './comment.service';
describe('CommentService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: CommentService = TestBed.get(CommentService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,98 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {Page} from '../../classes/page';
import {Comment} from '../../classes/comment';
import {exist} from '../../utils/dataUtil';
import {CommentReq} from "../../classes/commentReq";
@Injectable({
providedIn: 'root'
})
export class CommentService {
constructor(public http: HttpService) {
}
// 存放
leaveMsgPage: Page<Comment>[] = [];
commentPage: Page<Comment>[] = [];
currentComment: Page<Comment>;
currentLeaveMsg: Page<Comment>;
/**
* 获取 评论
* @param pageNum 页码
* @param pageSize 单页数据数量
* @param isAdmin 是否是管理员
*/
getComments(pageNum: number, pageSize: number, isAdmin: boolean) {
const exist1 = exist<Comment>(pageNum, pageSize, this.commentPage);
if (exist1) {
exist1.subscribe(data => {
this.currentComment = data;
});
return exist1;
}
const observable = this.http.get(`/${isAdmin ? 'admin' : 'user'}/comment/type/1?count=${pageSize}&page=${pageNum}`);
observable.subscribe(data => {
if (data.code === 0) {
this.commentPage.unshift(data.result);
this.currentComment = data.result;
}
});
return observable;
}
/**
* 获取 留言
* @param pageNum 页码
* @param pageSize 单页数据数量
* @param isAdmin 是否是管理员
*/
getLeaveMsg(pageNum: number, pageSize: number, isAdmin: boolean) {
const exist1 = exist<Comment>(pageNum, pageSize, this.leaveMsgPage);
if (exist1) {
exist1.subscribe(data => {
this.currentLeaveMsg = data;
});
return exist1;
}
const observable = this.http.get(`/${isAdmin ? 'admin' : 'user'}/comment/type/0?count=${pageSize}&page=${pageNum}`);
observable.subscribe(data => {
if (data.code === 0) {
this.leaveMsgPage.unshift(data.result);
this.currentLeaveMsg = data.result;
}
});
return observable;
}
/**
* 回复评论/留言
* @param responseComment 请求体
*/
rely(responseComment: CommentReq) {
return this.http.post('/user/comment/create', responseComment, true);
}
/**
* 通过父评论 获取回复
* @param pid 父评论id
*/
getByPid(pid: number) {
return this.http.get('/comment/pid/' + pid + '?count=5&page=1');
}
update(subComment) {
return this.http.put('/user/comment/update', subComment);
}
delete(id: number) {
return this.http.delete('/user/comment/del?id=' + id);
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { LinkService } from './link.service';
describe('LinkService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: LinkService = TestBed.get(LinkService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,39 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {Link} from '../../classes/link';
import {Page} from '../../classes/page';
@Injectable({
providedIn: 'root'
})
export class LinkService {
constructor(public http: HttpService) {
}
// 不采取存储page[] :: 数据量较少
public currentPage: Page<Link>;
getLinks(pageNum: number, pageSize: number) {
const observable = this.http.get(`/admin/links?page=${pageNum}&count=${pageSize}`);
observable.subscribe(data => {
if (data.code === 0) {
this.currentPage = data.result;
}
});
return observable;
}
update(submitBody: Link) {
return this.http.put('/admin/links/update', submitBody, true);
}
create(submitBody: Link) {
submitBody.id = null;
return this.http.post('/admin/links/create', submitBody, true);
}
delete(id) {
return this.http.delete(`/admin/links/del/${id}`);
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { LogService } from './log.service';
describe('LogService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: LogService = TestBed.get(LogService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,22 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
@Injectable({
providedIn: 'root'
})
export class LogService {
constructor(private http: HttpService) {
}
logText: string;
getLog() {
// @ts-ignore
const observable = this.http.httpClient.get<string>('https://api.celess.cn/blog.log', {responseType: 'text'});
observable.subscribe(data => {
this.logText = data;
});
return observable;
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { TagService } from './tag.service';
describe('TagService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: TagService = TestBed.get(TagService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,43 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {Tag} from '../../classes/tag';
import {Page} from '../../classes/page';
import {exist} from '../../utils/dataUtil';
@Injectable({
providedIn: 'root'
})
export class TagService {
constructor(public http: HttpService) {
}
tagPages: Page<Tag>[] = [];
currentTagPage: Page<Tag>;
getTags(pageNum: number, pageSize: number) {
const exist1 = exist<Tag>(pageNum, pageSize, this.tagPages);
if (exist1) {
exist1.subscribe(data => {
this.currentTagPage = data;
});
return exist1;
}
const observable = this.http.get(`/tags?count=${pageSize}&page=${pageNum}`);
observable.subscribe(data => {
if (data.code === 0) {
this.currentTagPage = data.result;
this.tagPages.unshift(data.result);
}
});
}
update(id: number, name: string) {
return this.http.put(`/admin/tag/update?id=${id}&name=${name}`, null);
}
delete(id: number) {
return this.http.delete(`/admin/tag/del?id=${id}`);
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { WebUpdateService } from './web-update.service';
describe('WebUpdateService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: WebUpdateService = TestBed.get(WebUpdateService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,48 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {UpdateInfo} from '../../classes/updateInfo';
import {Page} from '../../classes/page';
@Injectable({
providedIn: 'root'
})
export class WebUpdateService {
constructor(public http: HttpService) {
}
public updateInfoList: Page<UpdateInfo>;
public lastestUpdateTime: string;
getUpdateInfo(pageNum: number, pageSize: number) {
const observable = this.http.get(`/webUpdate/pages?page=${pageNum}&count=${pageSize}`);
observable.subscribe((data: any) => {
if (data.code === 0) {
this.updateInfoList = data.result;
}
});
return observable;
}
getLastestUpdateTime() {
this.http.get('/lastestUpdateTime').subscribe(data => {
if (data.code === 0) {
this.lastestUpdateTime = data.result;
}
})
}
update(submitBody: { id: number, info: string }) {
return this.http.put('/admin/webUpdate/update', submitBody, false);
}
create(infoStr: string) {
return this.http.post('/admin/webUpdate/create', {info: infoStr}, false);
}
delete(id: number) {
return this.http.delete(`/admin/webUpdate/del/${id}`);
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { UserService } from './user.service';
describe('UserService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: UserService = TestBed.get(UserService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,90 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {User} from '../../classes/user';
import {Page} from '../../classes/page';
import {exist} from '../../utils/dataUtil';
@Injectable({
providedIn: 'root'
})
export class UserService {
userInfo: User;
avatarHost: string = 'http://cdn.celess.cn';
constructor(public http: HttpService) {
}
userPage: Page<User>[] = [];
currentUserPage: Page<User>;
/**
* 获取用户信息
*/
getUserInfo() {
return this.http.get('/user/userInfo');
}
/**
* 注销登录
*/
logout() {
this.http.get('/logout').subscribe((data: any) => {
if (data.code === 0) {
this.userInfo = null;
this.http.removeToken();
}
});
}
updateInfo(submitBody: { desc: string, displayName: string }) {
const observable = this.http.put('/user/userInfo/update', submitBody, false);
observable.subscribe(data => {
if (data.code === 0) {
this.userInfo.desc = submitBody.desc;
this.userInfo.displayName = submitBody.displayName;
}
});
return observable;
}
sendEmail() {
return this.http.post('/sendVerifyEmail', {email: this.userInfo.email}, false);
}
/**
* 获取分页数据
* @param pageNum 页码
* @param pageSize 单页数据量
* @param refresh 是否强制刷新
*/
getPageUser(pageNum: number, pageSize: number, refresh: boolean = false) {
const existData = exist<User>(pageNum, pageSize, this.userPage);
if (existData && !refresh) {
existData.subscribe(data => {
this.currentUserPage = data;
});
}
this.http.get(`/admin/users?page=${pageNum}&count=${pageSize}`).subscribe(data => {
if (data.code === 0) {
this.currentUserPage = data.result;
this.userPage.unshift(data.result);
}
});
}
delete(id: number) {
return this.http.delete(`/admin/user/delete/${id}`);
}
update(user: User) {
return this.http.put('/admin/user', user, true);
}
getExistOfEmail(email: string) {
return this.http.get(`/emailStatus/${email}`);
}
}

View File

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { VisitorService } from './visitor.service';
describe('VisitorService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: VisitorService = TestBed.get(VisitorService);
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,76 @@
import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {Page} from '../../classes/page';
import {Visitor} from '../../classes/visitor';
@Injectable({
providedIn: 'root'
})
export class VisitorService {
constructor(public http: HttpService) {
}
public pageList: Page<Visitor>[] = [];
public currentPage: Page<Visitor>;
public dayVisit: number;
public totalVisitCount: number;
private ipLocationList: { ip: string, location: string }[] = [];
getVisitor(pageNum: number, pageSize: number) {
const observable = this.http.get(`/admin/visitor/page?count=${pageSize}&page=${pageNum}&showLocation=false`);
observable.subscribe(data => {
if (data.code === 0) {
this.pageList.unshift(data.result);
this.currentPage = data.result;
}
});
}
getDayVisitor() {
this.http.get('/dayVisitCount').subscribe(data => {
if (data.code === 0) {
this.dayVisit = data.result;
}
});
}
getTotalVisitorCount() {
this.http.get('/visitor/count').subscribe(data => {
this.totalVisitCount = data.result;
});
}
getIp(ip: string) {
const location = this.exist(ip);
if (location) {
return location;
}
const observable = this.http.get(`/ip/${ip}`);
observable.subscribe(data => {
if (data.code === 0) {
this.ipLocationList.unshift(data.result);
}
});
return observable;
}
getLocalIp() {
return this.http.get('/ip');
}
private exist(ip): string {
if (this.ipLocationList.length === 0) {
return null;
}
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < this.ipLocationList.length; i++) {
if (this.ipLocationList[i].ip === ip) {
return this.ipLocationList[i].location;
}
}
}
}