更新token

This commit is contained in:
禾几海
2020-05-18 08:28:30 +08:00
parent d3a122065e
commit cdc4f3ea0f

View File

@@ -1,6 +1,6 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {RequestObj} from '../../class/HttpReqAndResp'; import {RequestObj} from '../../class/HttpReqAndResp';
import {HttpClient} from '@angular/common/http'; import {HttpClient, HttpResponse} from '@angular/common/http';
import {environment} from '../../../environments/environment'; import {environment} from '../../../environments/environment';
import {LocalStorageService} from '../../services/local-storage.service'; import {LocalStorageService} from '../../services/local-storage.service';
import {Response} from '../../class/HttpReqAndResp'; import {Response} from '../../class/HttpReqAndResp';
@@ -34,7 +34,7 @@ export class HttpService {
} }
request.path = this.checkUrl(request); request.path = this.checkUrl(request);
let observable: Observable<Response<T>>; let observable: Observable<HttpResponse<Response<T>>>;
switch (request.method) { switch (request.method) {
case 'GET': case 'GET':
observable = this.get<Response<T>>(request); observable = this.get<Response<T>>(request);
@@ -55,13 +55,17 @@ export class HttpService {
const oob = new Observable<Response<T>>(o => observer = o); const oob = new Observable<Response<T>>(o => observer = o);
observable.subscribe(o => { observable.subscribe(o => {
if (o.code) { const tokenFromReps = o.headers.get('Authorization');
if (tokenFromReps) {
this.localStorageService.setToken(tokenFromReps);
}
if (o.body.code) {
observer.error(o); observer.error(o);
if (this.errorDispatch) { if (this.errorDispatch) {
this.errorDispatch.errHandler(o.code, o.msg, request); this.errorDispatch.errHandler(o.body.code, o.body.msg, request);
} }
} else { } else {
observer.next(o); observer.next(o.body);
} }
observer.complete(); observer.complete();
}); });
@@ -72,7 +76,8 @@ export class HttpService {
return this.httpClient.get<T>(request.path, return this.httpClient.get<T>(request.path,
{ {
headers: request.header, headers: request.header,
withCredentials: true withCredentials: true,
observe: 'response'
}); });
} }
@@ -80,7 +85,8 @@ export class HttpService {
return this.httpClient.post<T>(request.path, request.data, return this.httpClient.post<T>(request.path, request.data,
{ {
headers: request.header, headers: request.header,
withCredentials: true withCredentials: true,
observe: 'response'
}); });
} }
@@ -88,7 +94,8 @@ export class HttpService {
return this.httpClient.put<T>(request.path, request.data, return this.httpClient.put<T>(request.path, request.data,
{ {
headers: request.header, headers: request.header,
withCredentials: true withCredentials: true,
observe: 'response'
}); });
} }
@@ -96,7 +103,8 @@ export class HttpService {
return this.httpClient.delete<T>(request.path, return this.httpClient.delete<T>(request.path,
{ {
headers: request.header, headers: request.header,
withCredentials: true withCredentials: true,
observe: 'response'
}); });
} }