import { NaHttpInterceptorPreparable, NaHttpRequestHandler } from '../na-http-interceptor-preparable'; import { HttpRequest, HttpParams } from '@angular/common/http'; import { INaHttpConfig, NullValueHandling, DateValueHandling, NaHttpConfig } from '../../na-http-config'; import { NaHttpService } from '../../na-http.service'; export class NaParamsInterceptorPreparable implements NaHttpInterceptorPreparable { private httpConfig: INaHttpConfig; constructor(protected naHttpService: NaHttpService) { } prepare(req: HttpRequest, next: NaHttpRequestHandler): HttpRequest { this.httpConfig = this.naHttpService.httpConfig(req); let newReq = req; if (NaHttpService.METHOD.indexOf(req.method) >= 0) { newReq = req.clone({ body: this.makeParams(req.body) }); } else { newReq = req.clone({ params: this.cloneParams(req.params) }); } return next.handle(newReq); } private cloneParams(params: HttpParams): HttpParams { const newParams = {}; params.keys().forEach(key => { newParams[key] = params.get(key); }); this.makeParams(newParams); return new HttpParams({ fromObject: newParams }); } private makeParams(data: any = {}) { for (const key in data) { if (data.hasOwnProperty(key)) { const value = data[key]; if (this.httpConfig.NULL_VALUE_HANDLING === NullValueHandling.IGNORE) { // 去空 if (value == null || value === 'undefined' || typeof value === 'undefined') { delete data[key]; } else { // const type = Object.prototype.toString.call(value).toLowerCase().slice(8, -1); // // console.log(type, value.toString()); // switch (type) { // case 'boolean': // case 'number': // case 'date': // case 'string': // if (value.toString().length.length <= 0) { // delete data[key]; // } // break; // case 'array': // console.log(value); // // Array.from(value).forEach(val => { // // console.log(val); // // }); // break; // case 'map': // console.log(value); // break; // case 'object': // console.log(value); // // if (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0) { // // return {}; // // } // // Object.keys(value).map(key => { // // value[key] = this.removeNullValues(value[key]); // // }); // break; // default: // console.warn( // `unknown type:${type}, value:${value}, please perfect the judgment logic` // ); // break; // } } } if (this.httpConfig.DATE_VALUE_HANDLING === DateValueHandling.TIMESTAMP && value instanceof Date) { data[key] = value.valueOf(); } const constant = NaHttpConfig.CONSTANT; const newKey = constant[key]; if (newKey) { delete data[newKey]; } } } return data; } private removeNullArray(array: Array) { array.forEach(arr => { }); } // 去掉对象中存在空值的属性 private removeNullValues (value: any) { if (value == null || value === 'undefined' || typeof value === 'undefined') { return {}; } const type = Object.prototype.toString.call(value).toLowerCase().slice(8, -1); switch (type) { // case 'boolean': // case 'number': // return false; // case 'string': // return !value.replace(/(^\s*)|(\s*$)/g, ''); case 'array': Array.from(value).forEach(val => { }); return !value.length; // case 'map': // return !value.size; case 'object': if (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0) { return {}; } Object.keys(value).map(key => { value[key] = this.removeNullValues(value[key]); }); return value; default: console.warn( `unknown type:${type}, value:${value}, please perfect the judgment logic` ); return value; // 其他对象均视作非空 } } }