init commit
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpRequest, HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
import { HttpObserve } from '@angular/common/http/src/client';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
/**
|
||||
* httpclient服务
|
||||
*/
|
||||
@Injectable()
|
||||
export class HttpClientService {
|
||||
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
) { }
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* request
|
||||
*
|
||||
* @return any
|
||||
*/
|
||||
request(method: string, url: string, options?: {
|
||||
body?: any;
|
||||
headers?: HttpHeaders | {
|
||||
[header: string]: string | string[];
|
||||
};
|
||||
params?: HttpParams | {
|
||||
[param: string]: string | string[];
|
||||
};
|
||||
observe?: HttpObserve;
|
||||
reportProgress?: boolean;
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
|
||||
withCredentials?: boolean;
|
||||
}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.request(method, url, options = {}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* delete
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
delete(url: string, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.delete(url, options = {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* get
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
get(url: string, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.get(url, options = {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* head
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
head(url: string, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.head(url, options = {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* options
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
options(url: string, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.options(url, options = {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* patch
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
patch(url: string, body: any | null, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.patch(url, body, options = {}));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* post
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
post(url: string, body: any | null, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.post(url, body, options = {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* put
|
||||
* @param url
|
||||
* @param options
|
||||
*/
|
||||
put(url: string, body: any | null, options: {
|
||||
headers?: HttpHeaders | { [header: string]: string | string[] },
|
||||
observe?: HttpObserve,
|
||||
params?: HttpParams | { [param: string]: string | string[] },
|
||||
reportProgress?: boolean,
|
||||
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
|
||||
withCredentials?: boolean,
|
||||
} = {}): Observable<any> {
|
||||
return this.commonProcess(this.httpClient.put(url, body, options = {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共处理
|
||||
* @param observable
|
||||
*/
|
||||
commonProcess(observable: Observable<any>): Observable<any> {
|
||||
// this.spinService.spin(true);
|
||||
return Observable.create((observer) => {
|
||||
observable.subscribe(res => {
|
||||
// this.spinService.spin(false);
|
||||
observer.next(res);
|
||||
}, (err) => {
|
||||
// this.spinService.spin(false);
|
||||
observer.error(err);
|
||||
}, () => {
|
||||
// this.spinService.spin(false);
|
||||
observer.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
Http, Response, Headers, RequestOptions, URLSearchParams, RequestOptionsArgs, RequestMethod
|
||||
} from '@angular/http';
|
||||
|
||||
|
||||
/**
|
||||
* http服务
|
||||
*/
|
||||
@Injectable()
|
||||
export class HttpService {
|
||||
|
||||
constructor(private http: Http) {}
|
||||
|
||||
public request(url: string, options: RequestOptionsArgs, success: Function, error: Function): any {
|
||||
// this.spinService.spin(true);
|
||||
this.http.request(url, options).subscribe(res => {
|
||||
// this.spinService.spin(false);
|
||||
success(res.ok, res.json(), res);
|
||||
}, err => {
|
||||
// this.spinService.spin(false);
|
||||
//处理请求失败
|
||||
let msg = this.requestFailed(url, options, err);
|
||||
error(err.ok, msg, err);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public get(url: string, paramMap: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Get,
|
||||
search: HttpService.buildURLSearchParams(paramMap)
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public post(url: string, body: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Post,
|
||||
body: body,
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json; charset=UTF-8'
|
||||
})
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public postFormData(url: string, paramMap: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Post,
|
||||
search: HttpService.buildURLSearchParams(paramMap).toString(),
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
})
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public put(url: string, body: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Put,
|
||||
body: body
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public delete(url: string, paramMap: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Delete,
|
||||
search: HttpService.buildURLSearchParams(paramMap).toString()
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public patch(url: string, body: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Patch,
|
||||
body: body
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public head(url: string, paramMap: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Head,
|
||||
search: HttpService.buildURLSearchParams(paramMap).toString()
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
public options(url: string, paramMap: any = null, success: Function=function(successful, data, res){}, error: Function=function(successful, msg, err){}): any {
|
||||
return this.request(url, new RequestOptions({
|
||||
method: RequestMethod.Options,
|
||||
search: HttpService.buildURLSearchParams(paramMap).toString()
|
||||
}), success, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象转为查询参数
|
||||
* @param paramMap
|
||||
* @returns {URLSearchParams}
|
||||
*/
|
||||
private static buildURLSearchParams(paramMap): URLSearchParams {
|
||||
let params = new URLSearchParams();
|
||||
if (!paramMap) {
|
||||
return params;
|
||||
}
|
||||
for (let key in paramMap) {
|
||||
let val = paramMap[key];
|
||||
// if (val instanceof Date) {
|
||||
// val = Utils.dateFormat(val, 'yyyy-MM-dd hh:mm:ss')
|
||||
// }
|
||||
params.set(key, val);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求失败事件
|
||||
* @param url
|
||||
* @param options
|
||||
* @param err
|
||||
*/
|
||||
private requestFailed(url: string, options: RequestOptionsArgs, err) {
|
||||
let msg = '请求发生异常', status = err.status;
|
||||
if (status === 0) {
|
||||
msg = '请求失败,请求响应出错';
|
||||
} else if (status === 404) {
|
||||
msg = '请求失败,未找到请求地址';
|
||||
} else if (status === 500) {
|
||||
msg = '请求失败,服务器出错,请稍后再试';
|
||||
} else {
|
||||
msg = "未知错误,请检查网络";
|
||||
}
|
||||
|
||||
return msg;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, Router, ActivatedRouteSnapshot } from '@angular/router';
|
||||
import getCookie from '../utils/get-cookie';
|
||||
|
||||
@Injectable()
|
||||
export class LoginAuthService implements CanActivate {
|
||||
constructor(
|
||||
private router: Router,
|
||||
) { }
|
||||
canActivate(route: ActivatedRouteSnapshot): boolean {
|
||||
if (!this.isLogin()) {
|
||||
if (route.routeConfig.path === 'login') {
|
||||
return true
|
||||
} else {
|
||||
this.router.navigate(['login'])
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if (route.routeConfig.path === 'login') {
|
||||
this.router.navigate(['app'])
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public isLogin(): boolean {
|
||||
if (this.getToken()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
getToken(): string {
|
||||
return getCookie('token');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user