From 9869c6c2ee60f06a0d0507088fbf60c967e8287a Mon Sep 17 00:00:00 2001 From: xuqm Date: Thu, 3 Apr 2025 10:52:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(imagePreview):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=A2=84=E8=A7=88=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 ImagePreviewController 从 MessageHelper 移动到 ConfigManager - 更新 ChatItemView 和 ArticleDetailView 中的图片预览逻辑 - 优化 HttpHelper 中的请求处理逻辑 --- src/main/ets/http/HttpHelper.ets | 86 ++++++++------------------------ 1 file changed, 22 insertions(+), 64 deletions(-) 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 {