From af83f07d670d0a42245df649028800e2761bc027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E7=8E=B2?= Date: Wed, 2 Apr 2025 17:24:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0upload=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/ets/http/HttpHelper.ets | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/main/ets/http/HttpHelper.ets b/src/main/ets/http/HttpHelper.ets index a4b64be..571ec71 100644 --- a/src/main/ets/http/HttpHelper.ets +++ b/src/main/ets/http/HttpHelper.ets @@ -307,6 +307,92 @@ export class HttpHelper { } + /** + * postJson请求 + * @param url url地址 + * @param headers + * @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用 + * @returns + */ + public upload(params: HttpParamsPost, apiNo?: string, showLog?: boolean): Promise { + + return new Promise((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)