diff --git a/src/main/ets/http/HttpHelper.ets b/src/main/ets/http/HttpHelper.ets index 85b9f20..0d999aa 100644 --- a/src/main/ets/http/HttpHelper.ets +++ b/src/main/ets/http/HttpHelper.ets @@ -62,22 +62,9 @@ export class HttpHelper { 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(); + this.setHandler(apiNo ?? params.url, httpRequest) - 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("application/json;charset=UTF-8", params.headers) @@ -134,6 +121,23 @@ export class HttpHelper { } + setHandler(key: string, httpRequest: http.HttpRequest) { + if (this.concurrentList.getIndexOf(key) === -1 && + this.httpHandlerList.hasKey(key)) { + this.httpHandlerList.get(key).destroy() + this.httpHandlerList.remove(key) + SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList) + SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, + this.httpHandlerList.length) + } + if (this.concurrentList.getIndexOf(key) === -1) { + this.httpHandlerList.set(key, httpRequest) + SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList) + SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, + this.httpHandlerList.length) + } + } + /** * postForm请求 * @param url url地址 @@ -145,22 +149,8 @@ export class HttpHelper { 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) - } + this.setHandler(apiNo ?? params.url, httpRequest) const header = HttpHelperX.getHeaders("application/x-www-form-urlencoded;charset=UTF-8", params.headers) let data = HttpHelperX.getContent(params.data) @@ -231,22 +221,8 @@ export class HttpHelper { 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) - } + this.setHandler(apiNo ?? params.url, httpRequest) if (showLog) { LogHelper.debug(`GET:${apiNo}\n`, HttpHelperX.getUrl(params.url, params.query) + '\n', @@ -318,24 +294,8 @@ export class HttpHelper { 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) + this.setHandler(apiNo ?? params.url, httpRequest) if (showLog) { LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params)) @@ -350,17 +310,15 @@ export class HttpHelper { method: http.RequestMethod.POST, connectTimeout: 20000, readTimeout: 20000, - header: header, + header: HttpHelperX.getHeaders("multipart/form-data", params.headers), usingCache: false, multiFormDataList: params.data, }) .then((data: http.HttpResponse) => { - if (showLog) { LogHelper.debug(`${apiNo}:\n ${data.result as string}`) LogHelper.print(data) } - if (data.responseCode === 200) { resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T) } else {