//============== Http操作========================= //Copyright 2018 何镇汐 //Licensed under the MIT license //================================================ import { HttpClient, HttpHeaders, HttpParams, HttpErrorResponse } from '@angular/common/http'; import { IocHelper as ioc } from './ioc-helper'; import { formatDate, uuid } from '../common/helper'; /** * Http操作 */ export class HttpHelper { /** * 发送请求 * @param url 请求地址 * @param httpMethod http方法 * @param data 数据 */ static send(url: string, httpMethod: HttpMethod, data?): HttpRequest { switch (httpMethod) { case HttpMethod.Get: return this.get(url).param(data); case HttpMethod.Post: return this.post(url, data); case HttpMethod.Put: return this.put(url, data); case HttpMethod.Delete: return this.delete(url).param(data); default: return this.get(url).param(data); } } /** * get请求 * @param url 请求地址 */ static get(url: string): HttpRequest { return new HttpRequest(HttpMethod.Get, url); } /** * post请求 * @param url 请求地址 * @param body Http主体 */ static post(url: string, body?): HttpRequest { return new HttpRequest(HttpMethod.Post, url, body); } /** * put请求 * @param url 请求地址 * @param body Http主体 */ static put(url: string, body?): HttpRequest { return new HttpRequest(HttpMethod.Put, url, body); } /** * delete请求 * @param url 请求地址 */ static delete(url: string): HttpRequest { return new HttpRequest(HttpMethod.Delete, url); } } /** * Http请求操作 */ export class HttpRequest { /** * Http头集合 */ private headers: HttpHeaders; /** * 内容类型 */ private httpContentType: HttpContentType; /** * Http参数集合 */ private parameters: HttpParams; /** * 初始化Http请求操作 * @param httpMethod Http方法 * @param url 请求地址 * @param body Http主体 */ constructor(private httpMethod: HttpMethod, private url: string, private body?) { this.headers = new HttpHeaders(); this.parameters = new HttpParams(); } /** * 添加Http头 * @param name 名称 * @param value 值 */ header(name: string, value): HttpRequest { let stringValue = ""; if (value !== undefined && value !== null) stringValue = String(value); this.headers = this.headers.append(name, stringValue); return this; } /** * 设置内容类型 * @param contentType 内容类型 */ contentType(contentType: HttpContentType): HttpRequest { this.httpContentType = contentType; return this; } /** * 添加Http参数,添加到url查询字符串 * @param data 参数对象 */ param(data): HttpRequest; /** * 添加Http参数,添加到url查询字符串 * @param name 名称 * @param value 值 */ param(name: string, value: string): HttpRequest; param(data, value?: string): HttpRequest { if (typeof data === "object") { this.paramByObject(data); return this; } if (typeof data === "string" && value) this.parameters = this.parameters.append(data, value); return this; } /** * 添加Http参数 */ private paramByObject(data) { for (let key in data) { if (data.hasOwnProperty(key)) { let value = this.getValue(data[key]); if (value == null) value = ""; this.parameters = this.parameters.append(key, value); } } } /** * 获取值 * @param data 数据 */ private getValue(data): string { if (!data) return data; if (data.getYear) return formatDate(data, "YYYY-MM-DD HH:mm:ss"); return data; } /** * 处理响应 * @param handler 响应处理函数 * @param errorHandler 错误处理函数 * @param beforeHandler 发送前处理函数,返回false则取消发送 * @param completeHandler 请求完成处理函数 */ handle(handler: (value: T) => void, errorHandler?: (error: HttpErrorResponse) => void, beforeHandler?: () => boolean, completeHandler?: () => void) { if (beforeHandler && beforeHandler() === false) return; this.request().subscribe(handler, errorHandler, completeHandler); } /** * 处理响应 * @param handler 响应处理函数 * @param errorHandler 错误处理函数 * @param beforeHandler 发送前处理函数,返回false则取消发送 * @param completeHandler 请求完成处理函数 */ async handleAsync(handler: (value: T) => void, errorHandler?: (error: HttpErrorResponse) => void, beforeHandler?: () => boolean, completeHandler?: () => void): Promise { if (beforeHandler && beforeHandler() === false) return; return await this.request().toPromise().then(handler).catch(errorHandler).then(completeHandler); } /** * 发送请求 */ private request() { this.setContentType(); let httpClient = ioc.get(HttpClient); this.initParameters(); let options = { headers: this.headers, params: this.parameters }; switch (this.httpMethod) { case HttpMethod.Get: return httpClient.get(this.url, options); case HttpMethod.Post: return httpClient.post(this.url, this.getBody(), options); case HttpMethod.Put: return httpClient.put(this.url, this.getBody(), options); case HttpMethod.Delete: return httpClient.delete(this.url, options); default: return httpClient.get(this.url, options); } } /** * 初始化请求参数 */ private initParameters() { if (this.httpMethod === HttpMethod.Get) this.param("no-cache-tag", uuid()); } /** * 获取body */ private getBody() { if (typeof this.body === "string") return JSON.stringify(this.body); return this.body; } /** * 设置内容类型 */ private setContentType() { return this.header("Content-Type", this.getContentType(this.httpContentType)); } /** * 获取内容类型 * @param contentType */ private getContentType(contentType: HttpContentType) { switch (contentType) { case HttpContentType.FormUrlEncoded: return "application/x-www-form-urlencoded"; case HttpContentType.Json: return "application/json"; default: return "application/json"; } } } /** * Http方法 */ export enum HttpMethod { Get, Post, Put, Delete } /** * Http内容类型 */ export enum HttpContentType { /** * application/x-www-form-urlencoded */ FormUrlEncoded, /** * application/json */ Json }