从"Blog"仓库中分离出来

This commit is contained in:
小海
2019-11-28 19:26:45 +08:00
commit c2aaf280db
616 changed files with 104128 additions and 0 deletions

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,97 @@
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) {
this.getUserInfo();
}
userPage: Page<User>[] = [];
currentUserPage: Page<User>;
/**
* 获取用户信息
*/
getUserInfo() {
const observable = this.http.get('/user/userInfo');
observable.subscribe((data: any) => {
if (data.code === 0) {
this.userInfo = data.result;
}
});
return observable;
}
/**
* 注销登录
*/
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}`);
}
}