将登录,获取用户信息和注销的数据处理移到service层
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {HttpClient} from '@angular/common/http';
|
import {HttpClient} from '@angular/common/http';
|
||||||
import {Observable, Observer, of} from 'rxjs';
|
|
||||||
|
|
||||||
import {Article} from '../class/Article';
|
import {Article} from '../class/Article';
|
||||||
import {HttpService} from './http/http.service';
|
import {HttpService} from './http/http.service';
|
||||||
@@ -13,7 +12,6 @@ import {CommentReq} from '../class/Comment';
|
|||||||
import {Link} from '../class/Link';
|
import {Link} from '../class/Link';
|
||||||
import {User} from '../class/User';
|
import {User} from '../class/User';
|
||||||
import {LoginReq} from '../class/User';
|
import {LoginReq} from '../class/User';
|
||||||
import {Response} from '../class/HttpReqAndResp';
|
|
||||||
|
|
||||||
import {LocalStorageService} from '../services/local-storage.service';
|
import {LocalStorageService} from '../services/local-storage.service';
|
||||||
|
|
||||||
@@ -202,34 +200,15 @@ export class ApiService extends HttpService {
|
|||||||
|
|
||||||
|
|
||||||
login(loginReq: LoginReq) {
|
login(loginReq: LoginReq) {
|
||||||
const observable = super.Service<User>({
|
return super.Service<User>({
|
||||||
path: '/login',
|
path: '/login',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
data: loginReq
|
data: loginReq
|
||||||
});
|
});
|
||||||
let observer: Observer<Response<User>>;
|
|
||||||
const oob = new Observable<Response<User>>(o => observer = o);
|
|
||||||
observable.subscribe({
|
|
||||||
next: o => {
|
|
||||||
if (o.code === 0) {
|
|
||||||
// 登录成功
|
|
||||||
this.localStorageService.setToken(o.result.token);
|
|
||||||
this.localStorageService.setUser(o.result);
|
|
||||||
}
|
|
||||||
observer.next(o);
|
|
||||||
observer.complete();
|
|
||||||
},
|
|
||||||
error: err => {
|
|
||||||
observer.error(err);
|
|
||||||
observer.complete();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return oob;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
this.localStorageService.clear();
|
|
||||||
return super.Service<string>({
|
return super.Service<string>({
|
||||||
path: '/logout',
|
path: '/logout',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@@ -288,32 +267,10 @@ export class ApiService extends HttpService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userInfo() {
|
userInfo() {
|
||||||
// 判断本地缓存的用户信息是否符合要求,符合要求返回本地缓存
|
return super.Service<User>({
|
||||||
const user = this.localStorageService.getUser();
|
|
||||||
if (this.localStorageService.isLogin() && user && !this.localStorageService.checkNeedNet()) {
|
|
||||||
return of<Response<User>>(new Response<User>(user));
|
|
||||||
}
|
|
||||||
// 不符合 请求网络数据并更新缓存
|
|
||||||
const observable = super.Service<User>({
|
|
||||||
path: '/user/userInfo',
|
path: '/user/userInfo',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
});
|
});
|
||||||
let observer: Observer<Response<User>>;
|
|
||||||
const oob = new Observable<Response<User>>(o => observer = o);
|
|
||||||
observable.subscribe({
|
|
||||||
next: o => {
|
|
||||||
this.localStorageService.setUser(o.result);
|
|
||||||
observer.next(o);
|
|
||||||
observer.complete();
|
|
||||||
},
|
|
||||||
error: err => {
|
|
||||||
// console.debug('登录过期 token错误 等等');
|
|
||||||
this.localStorageService.removeToken();
|
|
||||||
observer.error(err);
|
|
||||||
observer.complete();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return oob;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
visit() {
|
visit() {
|
||||||
|
|||||||
16
index/src/app/services/user.service.spec.ts
Normal file
16
index/src/app/services/user.service.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { UserService } from './user.service';
|
||||||
|
|
||||||
|
describe('UserService', () => {
|
||||||
|
let service: UserService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(UserService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
96
index/src/app/services/user.service.ts
Normal file
96
index/src/app/services/user.service.ts
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
import {Injectable} from '@angular/core';
|
||||||
|
import {LoginReq, User} from '../class/User';
|
||||||
|
import {ApiService} from '../api/api.service';
|
||||||
|
import {Observable, Observer} from 'rxjs';
|
||||||
|
import {Response} from '../class/HttpReqAndResp';
|
||||||
|
import {LocalStorageService} from './local-storage.service';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class UserService {
|
||||||
|
|
||||||
|
constructor(private apiService: ApiService,
|
||||||
|
private localStorageService: LocalStorageService) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存储订阅者
|
||||||
|
private userObserverArray: Observer<Response<User>>[] = [];
|
||||||
|
|
||||||
|
watchUserInfo(observer: Observer<Response<User>>) {
|
||||||
|
if (this.userObserverArray.indexOf(observer) < 0) this.userObserverArray.push(observer);
|
||||||
|
const user = this.localStorageService.getUser();
|
||||||
|
// 判断本地缓存的用户信息是否符合要求,符合要求返回本地缓存
|
||||||
|
if (this.localStorageService.isLogin() && user && !this.localStorageService.checkNeedNet()) {
|
||||||
|
observer.next(new Response<User>(user));
|
||||||
|
return {
|
||||||
|
unsubscribe() {
|
||||||
|
observer.complete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 不符合 请求网络数据并更新缓存
|
||||||
|
// 向订阅者传数据
|
||||||
|
const subscription = this.apiService.userInfo().subscribe({
|
||||||
|
next: o => {
|
||||||
|
this.localStorageService.setUser(o.result);
|
||||||
|
observer.next(o);
|
||||||
|
},
|
||||||
|
error: err => {
|
||||||
|
// console.debug('登录过期 token错误 等等');
|
||||||
|
this.localStorageService.removeToken();
|
||||||
|
observer.next(new Response<User>(null));
|
||||||
|
observer.error(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
unsubscribe() {
|
||||||
|
observer.complete();
|
||||||
|
subscription.unsubscribe()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
login(loginReq: LoginReq, observer: Observer<Response<User>>) {
|
||||||
|
const oob = new Observable<Response<User>>(o => observer = o);
|
||||||
|
const subscription = this.apiService.login(loginReq).subscribe({
|
||||||
|
next: o => {
|
||||||
|
// 登录成功
|
||||||
|
this.localStorageService.setToken(o.result.token);
|
||||||
|
this.localStorageService.setUser(o.result);
|
||||||
|
// this.userObserver.next(o);
|
||||||
|
this.userObserverArray.forEach(ob => ob.next(o))
|
||||||
|
observer.next(o);
|
||||||
|
observer.complete();
|
||||||
|
},
|
||||||
|
error: err => {
|
||||||
|
observer.error(err);
|
||||||
|
observer.complete();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
unsubscribe() {
|
||||||
|
observer.complete();
|
||||||
|
subscription.unsubscribe();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
logout(observer?: Observer<Response<string>>) {
|
||||||
|
// 如果不需要返回消息也ok
|
||||||
|
this.apiService.logout().subscribe(data => {
|
||||||
|
this.localStorageService.clear();
|
||||||
|
this.userObserverArray.forEach(ob => ob.next(new Response<User>(null)))
|
||||||
|
if (observer) {
|
||||||
|
observer.next(data);
|
||||||
|
observer.complete();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
if (observer) {
|
||||||
|
observer.error(error);
|
||||||
|
observer.complete();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user