import { Injectable } from "@angular/core" ; export interface FileParam { chunk : Blob ; hash : string ; filename : string ; fileHash : string ; } @ Injectable ( { providedIn : "root" , } ) export class RequestService { constructor ( ) { } request ( { url , method = "post" , data , headers , onProgress = ( e ) => e , requestList = [ ] , } ) { return new Promise ( ( resolve ) => { const xhr = new XMLHttpRequest ( ) ; xhr . upload . onprogress = onProgress ; xhr . open ( method , url ) ; Object . keys ( headers ) . forEach ( ( key ) => xhr . setRequestHeader ( key , headers [ key ] ) ) ; xhr . send ( data ) ; xhr . onload = ( e ) => { if ( requestList ) { const xhrIndex = requestList . findIndex ( ( item ) => item === xhr ) ; requestList . splice ( xhrIndex , 1 ) ; } const payload = ( xhr . status == 200 ? ( e . target as any ) . response : { message : "上传失败" , code : xhr . status } ) ; resolve ( JSON . parse ( payload ) ) ; } ; requestList && requestList . push ( xhr ) ; } ) ; } }