158 lines
4.6 KiB
TypeScript
158 lines
4.6 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { HttpClient, HttpRequest, HttpHeaders, HttpParams } from '@angular/common/http';
|
|
import { HttpObserve } from '@angular/common/http/src/client';
|
|
import { OperateSpinServiceService } from './operate-spin-service.service';
|
|
import { ToastService } from '../components/toast/toast.service';
|
|
import { ToastConfig, ToastType } from '../components/toast/toast-model';
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
/**
|
|
* httpclient服务
|
|
*/
|
|
@Injectable()
|
|
export class HttpClientService {
|
|
|
|
constructor(
|
|
private router: Router,
|
|
private httpClient: HttpClient,
|
|
private operateSpinServiceService: OperateSpinServiceService,
|
|
private toastService: ToastService,
|
|
) { }
|
|
|
|
header: HttpHeaders = new HttpHeaders().set('x-requested-with', 'XMLHttpRequest')
|
|
|
|
/**
|
|
* request
|
|
*
|
|
* @return any
|
|
*/
|
|
request(method: string, url: string, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.request(method, url, { headers: this.header }));
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* delete
|
|
* @param url
|
|
* @param options
|
|
*/
|
|
delete(url: string, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.delete(url, { headers: this.header }));
|
|
}
|
|
|
|
/**
|
|
* get
|
|
* @param url
|
|
* @param options
|
|
*/
|
|
get(url: string, params?: object, options?): Observable<any> {
|
|
let paramsString = [];
|
|
url = url + '?'
|
|
let t = Math.random();
|
|
if (params && params) {
|
|
for (const key in params) {
|
|
if (params[key] !== '') {
|
|
paramsString.push([key, params[key]].join('='))
|
|
}
|
|
}
|
|
return this.commonProcess(this.httpClient.get(encodeURI(url + paramsString.join('&') + '&t=' + t), { headers: this.header }));
|
|
} else {
|
|
return this.commonProcess(this.httpClient.get(url + 't=' + t, { headers: this.header }));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* head
|
|
* @param url
|
|
* @param options
|
|
*/
|
|
head(url: string, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.head(url, { headers: this.header }));
|
|
}
|
|
|
|
/**
|
|
* options
|
|
* @param url
|
|
* @param options
|
|
*/
|
|
options(url: string, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.options(url, { headers: this.header }));
|
|
}
|
|
|
|
/**
|
|
* patch
|
|
* @param url
|
|
* @param options
|
|
*/
|
|
patch(url: string, body?, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.patch(url, body, { headers: this.header }));
|
|
}
|
|
|
|
|
|
/**
|
|
* post
|
|
* @param url
|
|
* @param body
|
|
* @param options
|
|
*/
|
|
post(url: string, body?, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.post(url, body, { headers: this.header }));
|
|
}
|
|
|
|
/**
|
|
* put
|
|
* @param url
|
|
* @param options
|
|
*/
|
|
put(url: string, body?, options?): Observable<any> {
|
|
return this.commonProcess(this.httpClient.put(url, body, { headers: this.header }));
|
|
}
|
|
|
|
/**
|
|
* 公共处理
|
|
* @param observable
|
|
*/
|
|
commonProcess(observable: Observable<any>): Observable<any> {
|
|
return Observable.create((observer) => {
|
|
observable.subscribe(res => {
|
|
observer.next(res);
|
|
}, (err) => {
|
|
switch (err.status) {
|
|
case 401: {
|
|
this.operateSpinServiceService.hideSpin();
|
|
let cookieMsg = ['SESSION', 'token']
|
|
let sessionStorageMsg = ['userInfo']
|
|
sessionStorageMsg.forEach(element => {
|
|
sessionStorage.removeItem(element)
|
|
})
|
|
cookieMsg.forEach(element => {
|
|
this.delCookie(element)
|
|
})
|
|
this.router.navigate(['/login']);
|
|
break;
|
|
};
|
|
case 500: {
|
|
this.operateSpinServiceService.hideSpin();
|
|
const toastCfg = new ToastConfig(ToastType.ERROR, '', '数据请求失败!', 5000);
|
|
this.toastService.toast(toastCfg);
|
|
break;
|
|
}
|
|
}
|
|
observer.error(err);
|
|
}, () => {
|
|
observer.complete();
|
|
});
|
|
});
|
|
}
|
|
|
|
delCookie($name) {
|
|
let myDate = new Date();
|
|
myDate.setTime(-1000);
|
|
document.cookie = $name + "=''; path=/; expires=" + myDate.toUTCString();
|
|
}
|
|
|
|
}
|