import { Injectable } from '@angular/core' ; import { Http , Response , Headers , RequestOptions } from '@angular/http' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/toPromise' ; /**
 * HTTP请求服务
 */ @ Injectable ( ) export class HttpSerProvider { constructor ( public http ) { } param { string } url param paramObj return { Promise < never | { } > } get ( url , paramObj = { } ) { let timestamp = Math . floor ( new Date ( ) . getTime ( ) / 1000 - 1420070400 ) . toString ( ) ; let headers = new Headers ( { 'timestamp' : timestamp } ) ; let options = new RequestOptions ( { headers : headers } ) ; return this . http . get ( url + this . toQueryString ( paramObj ) , options ) . toPromise ( ) . then ( this . extractData ) . catch ( this . handleError ) ; } param { string } url param body param { string } contentType return { Promise < never | { } > } post ( url , body = { } , contentType = "application/x-www-form-urlencoded" ) { let headers = new Headers ( { 'Content-Type' : contentType } ) ; let options = new RequestOptions ( { headers : headers } ) ; return this . http . post ( url , this . toBodyString ( body ) , options ) . toPromise ( ) . then ( this . extractData ) . catch ( this . handleError ) ; } param obj return { string } example private toQueryString ( obj ) { let ret = [ ] ; for ( let key in obj ) { key = encodeURIComponent ( key ) ; let values = obj [ key ] ; if ( values && values . constructor == Array ) { let queryValues = [ ] ; for ( let i = 0 , len = values . length , value ; i < len ; i ++ ) { value = values [ i ] ; queryValues . push ( this . toQueryPair ( key , value ) ) ; } ret = ret . concat ( queryValues ) ; } else { ret . push ( this . toQueryPair ( key , values ) ) ; } } return '?' + ret . join ( '&' ) ; } param obj return { string } private toBodyString ( obj ) { let ret = [ ] ; for ( let key in obj ) { key = encodeURIComponent ( key ) ; let values = obj [ key ] ; if ( values && values . constructor == Array ) { let queryValues = [ ] ; for ( let i = 0 , len = values . length , value ; i < len ; i ++ ) { value = values [ i ] ; queryValues . push ( this . toQueryPair ( key , value ) ) ; } ret = ret . concat ( queryValues ) ; } else { ret . push ( this . toQueryPair ( key , values ) ) ; } } return ret . join ( '&' ) ; } private toQueryPair ( key , value ) { if ( typeof value == 'undefined' ) { return key ; } return key + '=' + encodeURIComponent ( value === null ? '' : String ( value ) ) ; } private toSignPair ( key , value ) { return key + '=' + ( value === null ? '' : String ( value ) ) ; } private extractData ( res ) { let body = res . json ( ) ; return body || { } ; } private handleError ( error ) { let errMsg ; if ( error instanceof Response ) { const body = error . json ( ) || '' ; const err = body . error || JSON . stringify ( body ) ; errMsg = `${ error . status } - ${ error . statusText || '' } ${ err }` ; } else { errMsg = error . message ? error . message : error . toString ( ) ; } console . error ( errMsg ) ; return Promise . reject ( errMsg ) ; } }