import { HttpClient , HttpContext , HttpEvent , HttpHeaders , HttpParams , HttpResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable , of } from 'rxjs' ; import { delay , finalize , switchMap , tap } from 'rxjs/operators' ; import { AlainConfigService , AlainThemeHttpClientConfig } from '@delon/util/config' ; import type { NzSafeAny } from 'ng-zorro-antd/core/types' ; export type _HttpHeaders = HttpHeaders | { [ header ] : string | string [ ] ; } ; export type HttpObserve = 'body' | 'events' | 'response' ; /**
 * 封装HttpClient，主要解决：
 * + 优化HttpClient在参数上便利性
 * + 统一实现 loading
 * + 统一处理时间格式问题
 */ @ Injectable ( { providedIn : 'root' } ) export class _HttpClient { private cog : AlainThemeHttpClientConfig ; constructor ( private http , cogSrv ) { this . cog = cogSrv . merge ( 'themeHttp' , { nullValueHandling : 'include' , dateValueHandling : 'timestamp' } ) ! ; } private lc = 0 ; /**
     * Get whether it's loading
     *
     * 获取是否正在加载中
     */ get loading ( ) : boolean { return this . lc > 0 ; } /**
     * Get the currently loading count
     *
     * 获取当前加载中的数量
     */ get loadingCount ( ) : number { return this . lc ; } parseParams ( params ) { const newParams = { } ; if ( params instanceof HttpParams ) { return params ; } Object . keys ( params ) . forEach ( key => { let _data = params [ key ] ; if ( this . cog . nullValueHandling === 'ignore' && _data == null ) return ; if ( this . cog . dateValueHandling === 'timestamp' && _data instanceof Date ) { _data = _data . valueOf ( ) ; } newParams [ key ] = _data ; } ) ; return new HttpParams ( { fromObject : newParams } ) ; } appliedUrl ( url , params ? ) { if ( ! params ) return url ; url += ~ url . indexOf ( '?' ) ? '' : '?' ; const arr = [ ] ; Object . keys ( params ) . forEach ( key => { arr . push ( `${ key }=${ params [ key ] }` ) ; } ) ; return url + arr . join ( '&' ) ; } private setCount ( count ) { Promise . resolve ( null ) . then ( ( ) => ( this . lc = count <= 0 ? 0 : count ) ) ; } private push ( ) { this . setCount ( ++ this . lc ) ; } private pop ( ) { this . setCount ( -- this . lc ) ; } /**
     * Clean loading count
     *
     * 清空加载中
     */ cleanLoading ( ) { this . setCount ( 0 ) ; } /**
     * **GET Request** Return a `string` type / 返回一个 `string` 类型
     */ get ( url , params , options ) ; /**
     * **GET Request** Return a `HttpEvent<T>` type / 返回一个 `HttpEvent<T>` 类型
     */ get < T > ( url , params , options ) ; /**
     * **GET Request** Return a `HttpResponse<any>` type / 返回一个 `HttpResponse<any>` 类型
     */ get ( url , params , options ) ; /**
     * **GET Request** Return a `HttpResponse<T>` type / 返回一个 `HttpResponse<T>` 类型
     */ get < T > ( url , params , options ) ; /**
     * **GET Request** Return a `any` type / 返回一个 `any` 类型
     */ get ( url , params ? , options ? ) ; /**
     * **GET Request** Return a generic type / 返回一个泛类型
     */ get < T > ( url , params ? , options ? ) ; get ( url , params , options = { } ) { return this . request ( 'GET' , url , { params , ... options } ) ; } /**
     * **POST Request** Return a `string` type / 返回一个 `string` 类型
     */ post ( url , body , params , options ) ; /**
     * **POST Request** Return a `HttpEvent<T>` type / 返回一个 `HttpEvent<T>` 类型
     */ post < T > ( url , body , params , options ) ; /**
     * **POST Request** Return a `HttpResponse<any>` type / 返回一个 `HttpResponse<any>` 类型
     */ post ( url , body , params , options ) ; /**
     * **POST Request** Return a `any` type / 返回一个 `any` 类型
     */ post ( url , body ? , params ? , options ? ) ; /**
     * **POST Request** Return a JSON type / 返回一个 `JSON` 类型
     */ post < T > ( url , body ? , params ? , options ? ) ; post ( url , body , params , options = { } ) { return this . request ( 'POST' , url , { body , params , ... options } ) ; } /**
     * **DELETE Request** Return a `string` type / 返回一个 `string` 类型
     */ delete ( url , params , options ) ; /**
     * **DELETE Request** Return a `JSON` type / 返回一个 `JSON` 类型
     */ delete ( url , params , options ) ; /**
     * **DELETE Request** Return a `any` type / 返回一个 `any` 类型
     */ delete ( url , params ? , options ? ) ; /**
     * c返回一个泛类型
     */ delete < T > ( url , params ? , options ? ) ; delete ( url , params , options = { } ) { return this . request ( 'DELETE' , url , { params , ... options } ) ; } param callbackParam jsonp ( url , params ? , callbackParam = 'JSONP_CALLBACK' ) { return of ( null ) . pipe ( delay ( 0 ) , tap ( ( ) => this . push ( ) ) , switchMap ( ( ) => this . http . jsonp ( this . appliedUrl ( url , params ) , callbackParam ) ) , finalize ( ( ) => this . pop ( ) ) ) ; } /**
     * **PATCH Request** Return a `string` type / 返回一个 `string` 类型
     */ patch ( url , body , params , options ) ; /**
     * **PATCH Request** Return a `HttpResponse<JSON>` type / 返回一个 `HttpResponse<JSON>` 类型
     */ patch ( url , body , params , options ) ; /**
     * **PATCH Request** Return a `any` type / 返回一个 `any` 类型
     */ patch ( url , body ? , params ? , options ? ) ; /**
     * **PATCH Request** Return a `JSON` type / 返回一个 `JSON` 类型
     */ patch < T > ( url , body ? , params ? , options ? ) ; patch ( url , body , params , options = { } ) { return this . request ( 'PATCH' , url , { body , params , ... options } ) ; } /**
     * **PUT Request** Return a `string` type / 返回一个 `string` 类型
     */ put ( url , body , params , options ) ; /**
     * **PUT Request** Return a `HttpResponse<JSON>` type / 返回一个 `HttpResponse<JSON>` 类型
     */ put ( url , body , params , options ) ; /**
     * **PUT Request** Return a `any` type / 返回一个 `any` 类型
     */ put ( url , body ? , params ? , options ? ) ; /**
     * **PUT Request** Return a `JSON` type / 返回一个 `JSON` 类型
     */ put < T > ( url , body ? , params ? , options ? ) ; put ( url , body , params , options = { } ) { return this . request ( 'PUT' , url , { body , params , ... options } ) ; } /**
     * **Form Request** Return a `string` type / 返回一个 `string` 类型
     */ form ( url , body , params , options ) ; /**
     * **Form Request** Return a `HttpEvent<T>` type / 返回一个 `HttpEvent<T>` 类型
     */ form < T > ( url , body , params , options ) ; /**
     * **Form Request** Return a `HttpResponse<JSON>` type / 返回一个 `HttpResponse<JSON>` 类型
     */ form ( url , body , params , options ) ; /**
     * **Form Request** Return a `any` type / 返回一个 `any` 类型
     */ form ( url , body ? , params ? , options ? ) ; /**
     * **Form Request** Return a `JSON` type / 返回一个 `JSON` 类型
     */ form < T > ( url , body ? , params ? , options ? ) ; form ( url , body , params , options = { } ) { return this . request ( 'POST' , url , { body , params , ... options , headers : { 'content-type' : `application/x-www-form-urlencoded` } } ) ; } /**
     * **Request** Return a `ArrayBuffer` type / 返回一个 `ArrayBuffer` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `Blob` type / 返回一个 `Blob` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `string` type / 返回一个 `string` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpEvent<ArrayBuffer>` type / 返回一个 `HttpEvent<ArrayBuffer>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpEvent<Blob>` type / 返回一个 `HttpEvent<Blob>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpEvent<string>` type / 返回一个 `HttpEvent<string>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpEvent<any>` type / 返回一个 `HttpEvent<any>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpEvent<R>` type / 返回一个 `HttpEvent<R>` 类型
     */ request < R > ( method , url , options ) ; /**
     * **Request** Return a `HttpResponse<ArrayBuffer>` type / 返回一个 `HttpResponse<ArrayBuffer>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpResponse<Blob>` type / 返回一个 `HttpResponse<Blob>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpResponse<string>` type / 返回一个 `HttpResponse<string>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpResponse<Object>` type / 返回一个 `HttpResponse<Object>` 类型
     */ request ( method , url , options ) ; /**
     * **Request** Return a `HttpResponse<R>` type / 返回一个 `HttpResponse<R>` 类型
     */ request < R > ( method , url , options ) ; /**
     * **Request** Return a `HttpResponse<Object>` type / 返回一个 `HttpResponse<Object>` 类型
     */ request ( method , url , options ? ) ; /**
     * **Request** Return a `R` type / 返回一个 `R` 类型
     */ request < R > ( method , url , options ? ) ; /**
     * **Request** Return a `any` type / 返回一个 `any` 类型
     */ request ( method , url , options ? ) ; request ( method , url , options = { } ) { if ( options . params ) options . params = this . parseParams ( options . params ) ; return of ( null ) . pipe ( delay ( 0 ) , tap ( ( ) => this . push ( ) ) , switchMap ( ( ) => this . http . request ( method , url , options ) ) , finalize ( ( ) => this . pop ( ) ) ) ; } }