Merge remote-tracking branch 'origin/master'
这个提交包含在:
当前提交
aff781423c
@ -307,6 +307,92 @@ export class HttpHelper {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* postJson请求
|
||||
* @param url url地址
|
||||
* @param headers
|
||||
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
||||
* @returns
|
||||
*/
|
||||
public upload<T>(params: HttpParamsPost, apiNo?: string, showLog?: boolean): Promise<T> {
|
||||
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
|
||||
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1 &&
|
||||
this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
||||
this.httpHandlerList.get(apiNo ?? params.url).destroy()
|
||||
this.httpHandlerList.remove(apiNo ?? params.url)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
||||
this.httpHandlerList.length)
|
||||
}
|
||||
let httpRequest = http.createHttp();
|
||||
|
||||
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
|
||||
this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
||||
this.httpHandlerList.length)
|
||||
}
|
||||
|
||||
const header = HttpHelperX.getHeaders("multipart/form-data", params.headers)
|
||||
|
||||
if (showLog) {
|
||||
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
||||
}
|
||||
|
||||
|
||||
const formData: http.MultiFormData[] = params.data as http.MultiFormData[]
|
||||
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
||||
method: http.RequestMethod.POST,
|
||||
connectTimeout: 20000,
|
||||
readTimeout: 20000,
|
||||
header: header,
|
||||
// extraData: params.data,
|
||||
usingCache: false,
|
||||
multiFormDataList: formData,
|
||||
})
|
||||
.then((data: http.HttpResponse) => {
|
||||
if (showLog) {
|
||||
LogHelper.debug(`${apiNo}:\n ${data.result as string}`)
|
||||
LogHelper.print(data)
|
||||
}
|
||||
|
||||
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
||||
this.httpHandlerList.remove(apiNo ?? params.url)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
||||
this.httpHandlerList.length)
|
||||
}
|
||||
if (data.responseCode === 200) {
|
||||
resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
|
||||
} else {
|
||||
const err: Error = new Error()
|
||||
err.name = data.responseCode.toString()
|
||||
err.message = '服务异常'
|
||||
reject(err)
|
||||
}
|
||||
}).catch((err: Error) => {
|
||||
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
||||
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
||||
this.httpHandlerList.remove(apiNo ?? params.url)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
||||
this.httpHandlerList.length)
|
||||
}
|
||||
if (err.message === 'Failed writing received data to disk/application') {
|
||||
const error: Error = new Error()
|
||||
error.name = 'cancel'
|
||||
error.message = err.message
|
||||
reject(error)
|
||||
} else {
|
||||
reject(err)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
clearHttp() {
|
||||
for (let handler of this.httpHandlerList.keys()) {
|
||||
this.cancel(handler)
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户