Browse Source

refactor(imagePreview): 重构图片预览功能

- 将 ImagePreviewController 从 MessageHelper 移动到 ConfigManager
- 更新 ChatItemView 和 ArticleDetailView 中的图片预览逻辑
- 优化 HttpHelper 中的请求处理逻辑
xuqm 1 week ago
parent
commit
9869c6c2ee
1 changed files with 22 additions and 64 deletions
  1. 22 64
      src/main/ets/http/HttpHelper.ets

+ 22 - 64
src/main/ets/http/HttpHelper.ets

@@ -62,22 +62,9 @@ export class HttpHelper {
 
     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();
+      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<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)
-      }
+      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<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)
-      }
+      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<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)
+      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 {