description export enum HttpErrorType { UrlError , TimeOut , RequestError } export interface HttpError { type : HttpErrorType ; reason : any ; } export enum HttpRequestType { POST = "POST" , GET = "GET" } class HttpPackageData { data : any = null ; url : string = null ; description timeout : number = 10000 ; description type : HttpRequestType = HttpRequestType . GET ; requestHeader : { name : string ; value : string ; } [ ] | { name : string ; value : string ; } = null ; description example isAutoAttachCurrentTime = false ; private _responseType : XMLHttpRequestResponseType = "" ; public set responseType ( type ) { this . _responseType = type ; } public get responseType ( ) { if ( CC_JSB ) { if ( this . _responseType == "" ) { this . _responseType = "text" ; } } return this . _responseType ; } } description export class HttpPackage { description public static crossProxy = { } ; description public static location = { host : "" , pathname : "" , protocol : "" } ; private _data : HttpPackageData = new HttpPackageData ( ) ; public set data ( data ) { this . _data = data ; } public get data ( ) : HttpPackageData { return this . _data ; } private _params : Object = null ; description example public set params ( value ) { this . _params = value ; } public get params ( ) { return this . _params ; } description param cb param errorcb public send ( cb ? , errorcb ? ) { HttpClient . request ( this , cb , errorcb ) ; } } class HttpClient { static crossProxy ( url ) { if ( cc . sys . isBrowser && ! CC_PREVIEW && HttpPackage . crossProxy ) { let config = HttpPackage . crossProxy ; let location = HttpPackage . location ; let keys = Object . keys ( config ) ; for ( let i = 0 ; i < keys . length ; i ++ ) { let key = keys [ i ] ; let value = config [ key ] ; if ( url . indexOf ( key ) > - 1 ) { if ( value . protocol && value . api ) { if ( location . protocol != value . protocol ) { url = url . replace ( value . protocol , location . protocol ) ; } return url . replace ( key , `${ location . host }/${ value . api }` ) ; } } } return url ; } else { return url ; } } private static convertParams ( url , params ) { if ( params == null || params == undefined ) { return url ; } let result = "&" ; if ( url . indexOf ( "?" ) < 0 ) { result = "?" ; } let keys = Object . keys ( params ) ; for ( let i = 0 ; i < keys . length ; i ++ ) { if ( i == 0 ) { result += `${ keys [ i ] }=${ params [ keys [ i ] ] }` ; } else { result += `&${ keys [ i ] }=${ params [ keys [ i ] ] }` ; } } result = url + result ; return result ; } static request ( httpPackage , cb ? , errorcb ? ) { let url = httpPackage . data . url ; if ( ! url ) { if ( CC_DEBUG ) { cc . error ( `reuqest url error` ) ; } if ( errorcb ) errorcb ( { type : HttpErrorType . UrlError , reason : "错误的Url地址" } ) ; return ; } let xhr = new XMLHttpRequest ( ) ; xhr . onreadystatechange = function ( ) { if ( xhr . readyState === 4 ) { if ( ( xhr . status >= 200 && xhr . status < 300 ) ) { if ( xhr . responseType == "arraybuffer" || xhr . responseType == "blob" ) { if ( cb ) cb ( xhr . response ) ; } else { if ( CC_DEBUG ) cc . log ( `htpp res(${ xhr . responseText })` ) ; if ( cb ) cb ( xhr . responseText ) ; } } else { let reason = `请求错误,错误状态:${ xhr . status }` ; cc . error ( `request error status : ${ xhr . status } url : ${ url } ` ) ; if ( errorcb ) errorcb ( { type : HttpErrorType . RequestError , reason : reason } ) ; } } else { } } ; xhr . responseType = httpPackage . data . responseType ; xhr . timeout = httpPackage . data . timeout ; xhr . ontimeout = ( ) => { xhr . abort ( ) ; if ( CC_DEBUG ) cc . warn ( `request timeout : ${ url }` ) ; if ( errorcb ) errorcb ( { type : HttpErrorType . TimeOut , reason : "连接超时" } ) ; } ; xhr . onerror = ( ) => { cc . error ( `request error : ${ url } ` ) ; if ( errorcb ) errorcb ( { type : HttpErrorType . RequestError , reason : "请求错误" } ) ; } ; if ( CC_DEBUG ) cc . log ( `[send http request] url : ${ url } request type : ${ httpPackage . data . type } , responseType : ${ xhr . responseType }` ) ; url = this . crossProxy ( url ) ; url = this . convertParams ( url , httpPackage . params ) ; if ( cc . sys . isBrowser ) { if ( httpPackage . data . isAutoAttachCurrentTime ) { if ( url . indexOf ( "?" ) >= 0 ) { url = `${ url }&cur_loc_t=${ Date . timeNow ( ) }` ; } else { url = `${ url }?cur_loc_t=${ Date . timeNow ( ) }` ; } } } if ( cc . sys . isBrowser && ! CC_PREVIEW ) { if ( CC_DEBUG ) cc . log ( `[send http request] corss prox url : ${ url } request type : ${ httpPackage . data . type } , responseType : ${ xhr . responseType }` ) ; } if ( httpPackage . data . type === HttpRequestType . POST ) { xhr . open ( HttpRequestType . POST , url ) ; if ( httpPackage . data . requestHeader ) { if ( httpPackage . data . requestHeader instanceof Array ) { httpPackage . data . requestHeader . forEach ( ( header ) => { xhr . setRequestHeader ( header . name , header . value ) ; } ) ; } else { let header = httpPackage . data . requestHeader ; xhr . setRequestHeader ( header . name , header . value ) ; } } else { xhr . setRequestHeader ( "Content-Type" , "text/plain;charset=UTF-8" ) ; } xhr . send ( httpPackage . data . data ) ; } else { xhr . open ( HttpRequestType . GET , url , true ) ; if ( httpPackage . data . requestHeader ) { if ( httpPackage . data . requestHeader instanceof Array ) { httpPackage . data . requestHeader . forEach ( ( header ) => { xhr . setRequestHeader ( header . name , header . value ) ; } ) ; } else { let header = httpPackage . data . requestHeader ; xhr . setRequestHeader ( header . name , header . value ) ; } } xhr . send ( ) ; } } }