Files
airport-chengdu-web/src/app/services/http-client.service.ts
T

158 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-10-26 11:33:25 +08:00
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
2018-10-26 11:33:25 +08:00
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';
2018-10-26 11:33:25 +08:00
import { Observable } from 'rxjs';
/**
* httpclient服务
*/
@Injectable()
export class HttpClientService {
constructor(
private router: Router,
2018-10-26 11:33:25 +08:00
private httpClient: HttpClient,
private operateSpinServiceService: OperateSpinServiceService,
private toastService: ToastService,
2018-10-26 11:33:25 +08:00
) { }
header: HttpHeaders = new HttpHeaders().set('x-requested-with', 'XMLHttpRequest')
2018-10-26 11:33:25 +08:00
/**
* request
*
* @return any
*/
2018-11-21 18:55:57 +08:00
request(method: string, url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.request(method, url, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* delete
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
delete(url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.delete(url, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* get
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
get(url: string, params?: object, options?): Observable<any> {
2018-11-15 18:29:55 +08:00
let paramsString = [];
url = url + '?'
let t = Math.random();
2018-11-21 18:55:57 +08:00
if (params && params) {
2018-11-15 18:29:55 +08:00
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 }));
2018-11-21 18:55:57 +08:00
} else {
return this.commonProcess(this.httpClient.get(url + 't=' + t, { headers: this.header }));
2018-11-15 18:29:55 +08:00
}
2018-10-26 11:33:25 +08:00
}
/**
* head
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
head(url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.head(url, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* options
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
options(url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.options(url, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* patch
* @param url
* @param options
*/
2018-11-27 18:16:25 +08:00
patch(url: string, body?, options?): Observable<any> {
return this.commonProcess(this.httpClient.patch(url, body, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* post
* @param url
2018-11-21 18:55:57 +08:00
* @param body
2018-10-26 11:33:25 +08:00
* @param options
*/
2018-11-27 18:16:25 +08:00
post(url: string, body?, options?): Observable<any> {
return this.commonProcess(this.httpClient.post(url, body, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* put
* @param url
* @param options
*/
2018-11-27 18:16:25 +08:00
put(url: string, body?, options?): Observable<any> {
return this.commonProcess(this.httpClient.put(url, body, { headers: this.header }));
2018-10-26 11:33:25 +08:00
}
/**
* 公共处理
* @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;
}
}
2018-10-26 11:33:25 +08:00
observer.error(err);
}, () => {
observer.complete();
});
});
}
delCookie($name) {
let myDate = new Date();
myDate.setTime(-1000);
2018-12-26 15:34:34 +08:00
document.cookie = $name + "=''; path=/; expires=" + myDate.toUTCString();
}
2018-10-26 11:33:25 +08:00
}