diff --git a/index/src/app/utils/dataUtil.ts b/index/src/app/utils/dataUtil.ts new file mode 100644 index 0000000..185f0ea --- /dev/null +++ b/index/src/app/utils/dataUtil.ts @@ -0,0 +1,26 @@ +import {Observable, of} from 'rxjs'; +import {PageList} from '../class/HttpReqAndResp'; + +/** + * 判断 一个Page[] 中是否存在一条已查询的数据 + * @param pageNum 页码 + * @param pageSize 单页数量 + * @param pageList 源数据 + * @return 未查到:null 查到:该条数据 + */ +export function exist(pageNum: number, pageSize: number, pageList: PageList[]): Observable> | 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> = new Observable(o => { + o.next(pageList[i]); + o.complete(); + }) + } + } + return null; +} diff --git a/index/src/app/view/admin/services/article/article.service.spec.ts b/index/src/app/view/admin/services/article/article.service.spec.ts new file mode 100644 index 0000000..f60e5f5 --- /dev/null +++ b/index/src/app/view/admin/services/article/article.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/article/article.service.ts b/index/src/app/view/admin/services/article/article.service.ts new file mode 100644 index 0000000..6d38494 --- /dev/null +++ b/index/src/app/view/admin/services/article/article.service.ts @@ -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
[] = []; + + currentPage: PageList
; + + /** + * 获取文章 + * @param pageNum 页码数 + * @param pageSize 单页数据量 + */ + getArticle(pageNum: number, pageSize: number) { + const articlePage = exist
(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); + } + +} diff --git a/index/src/app/view/admin/services/category/category.service.spec.ts b/index/src/app/view/admin/services/category/category.service.spec.ts new file mode 100644 index 0000000..7fd26e6 --- /dev/null +++ b/index/src/app/view/admin/services/category/category.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/category/category.service.ts b/index/src/app/view/admin/services/category/category.service.ts new file mode 100644 index 0000000..bfc7b9b --- /dev/null +++ b/index/src/app/view/admin/services/category/category.service.ts @@ -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; + } + +} diff --git a/index/src/app/view/admin/services/comment/comment.service.spec.ts b/index/src/app/view/admin/services/comment/comment.service.spec.ts new file mode 100644 index 0000000..ec3f811 --- /dev/null +++ b/index/src/app/view/admin/services/comment/comment.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/comment/comment.service.ts b/index/src/app/view/admin/services/comment/comment.service.ts new file mode 100644 index 0000000..5448ebc --- /dev/null +++ b/index/src/app/view/admin/services/comment/comment.service.ts @@ -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[] = []; + + commentPage: Page[] = []; + + currentComment: Page; + + currentLeaveMsg: Page; + + /** + * 获取 评论 + * @param pageNum 页码 + * @param pageSize 单页数据数量 + * @param isAdmin 是否是管理员 + */ + getComments(pageNum: number, pageSize: number, isAdmin: boolean) { + const exist1 = exist(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(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); + } + + +} diff --git a/index/src/app/view/admin/services/link/link.service.spec.ts b/index/src/app/view/admin/services/link/link.service.spec.ts new file mode 100644 index 0000000..d24d674 --- /dev/null +++ b/index/src/app/view/admin/services/link/link.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/link/link.service.ts b/index/src/app/view/admin/services/link/link.service.ts new file mode 100644 index 0000000..334900b --- /dev/null +++ b/index/src/app/view/admin/services/link/link.service.ts @@ -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; + + 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}`); + } +} diff --git a/index/src/app/view/admin/services/log/log.service.spec.ts b/index/src/app/view/admin/services/log/log.service.spec.ts new file mode 100644 index 0000000..caf56e9 --- /dev/null +++ b/index/src/app/view/admin/services/log/log.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/log/log.service.ts b/index/src/app/view/admin/services/log/log.service.ts new file mode 100644 index 0000000..6ba2892 --- /dev/null +++ b/index/src/app/view/admin/services/log/log.service.ts @@ -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('https://api.celess.cn/blog.log', {responseType: 'text'}); + observable.subscribe(data => { + this.logText = data; + }); + return observable; + } +} diff --git a/index/src/app/view/admin/services/tag/tag.service.spec.ts b/index/src/app/view/admin/services/tag/tag.service.spec.ts new file mode 100644 index 0000000..4db95b5 --- /dev/null +++ b/index/src/app/view/admin/services/tag/tag.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/tag/tag.service.ts b/index/src/app/view/admin/services/tag/tag.service.ts new file mode 100644 index 0000000..080a95c --- /dev/null +++ b/index/src/app/view/admin/services/tag/tag.service.ts @@ -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[] = []; + currentTagPage: Page; + + getTags(pageNum: number, pageSize: number) { + const exist1 = exist(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}`); + } + +} diff --git a/index/src/app/view/admin/services/update/web-update.service.spec.ts b/index/src/app/view/admin/services/update/web-update.service.spec.ts new file mode 100644 index 0000000..9d632b7 --- /dev/null +++ b/index/src/app/view/admin/services/update/web-update.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/update/web-update.service.ts b/index/src/app/view/admin/services/update/web-update.service.ts new file mode 100644 index 0000000..7042a14 --- /dev/null +++ b/index/src/app/view/admin/services/update/web-update.service.ts @@ -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; + + 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}`); + } +} diff --git a/index/src/app/view/admin/services/user/user.service.spec.ts b/index/src/app/view/admin/services/user/user.service.spec.ts new file mode 100644 index 0000000..9e7fd1c --- /dev/null +++ b/index/src/app/view/admin/services/user/user.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/user/user.service.ts b/index/src/app/view/admin/services/user/user.service.ts new file mode 100644 index 0000000..3da2711 --- /dev/null +++ b/index/src/app/view/admin/services/user/user.service.ts @@ -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[] = []; + currentUserPage: Page; + + /** + * 获取用户信息 + */ + 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(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}`); + } +} diff --git a/index/src/app/view/admin/services/visitor/visitor.service.spec.ts b/index/src/app/view/admin/services/visitor/visitor.service.spec.ts new file mode 100644 index 0000000..0e96b3e --- /dev/null +++ b/index/src/app/view/admin/services/visitor/visitor.service.spec.ts @@ -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(); + }); +}); diff --git a/index/src/app/view/admin/services/visitor/visitor.service.ts b/index/src/app/view/admin/services/visitor/visitor.service.ts new file mode 100644 index 0000000..2f0937d --- /dev/null +++ b/index/src/app/view/admin/services/visitor/visitor.service.ts @@ -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[] = []; + public currentPage: Page; + + 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; + } + } + } +}