徐勤民 6 mesiacov pred
rodič
commit
87ef142944

+ 2 - 0
Index.ets

@@ -10,6 +10,8 @@ export { AlgorithmHelper } from './src/main/ets/utils/AlgorithmHelper'
 export { AppStorageHelper } from './src/main/ets/utils/AppStorageHelper'
 
 export { PreferencesHelper } from './src/main/ets/utils/PreferencesHelper'
+export { LocalStorageKeys } from './src/main/ets/utils/LocalStorageKeys'
+export { LocalStorageHelper } from './src/main/ets/utils/LocalStorageHelper'
 
 /**
  * 正则相关

+ 28 - 0
src/main/ets/http/HttpHelper.ts

@@ -1,5 +1,7 @@
 import { ArrayList, HashMap } from '@kit.ArkTS';
 import http from '@ohos.net.http';
+import { LocalStorageHelper } from '../utils/LocalStorageHelper';
+import { LocalStorageKeys } from '../utils/LocalStorageKeys';
 
 
 type HttpParamsGet = {
@@ -39,6 +41,8 @@ export class HttpHelper {
 
   constructor() {
     this.httpHandlerList = new HashMap<string, http.HttpRequest>();
+    LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+    LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
     this.concurrentList.clear()
   }
 
@@ -72,11 +76,15 @@ export class HttpHelper {
       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)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
       }
       let httpRequest = http.createHttp();
 
       if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
         this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
       }
 
       const header = {
@@ -106,6 +114,8 @@ export class HttpHelper {
 
           if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
             this.httpHandlerList.remove(apiNo ?? params.url)
+            LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+            LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
           }
           if (data.responseCode === 200) {
             resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
@@ -118,6 +128,8 @@ export class HttpHelper {
         }).catch((err: Error) => {
         if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
           this.httpHandlerList.remove(apiNo ?? params.url)
+          LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+          LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
         }
         if (err.message === 'Failed writing received data to disk/application') {
           reject('cancel')
@@ -142,11 +154,15 @@ export class HttpHelper {
       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)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
       }
       let httpRequest = http.createHttp();
 
       if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
         this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
       }
 
       const header = {
@@ -178,6 +194,8 @@ export class HttpHelper {
 
           if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
             this.httpHandlerList.remove(apiNo ?? params.url)
+            LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+            LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
           }
           if (data.responseCode === 200) {
             resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
@@ -190,6 +208,8 @@ export class HttpHelper {
         }).catch((err: Error) => {
         if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
           this.httpHandlerList.remove(apiNo ?? params.url)
+          LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+          LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
         }
         if (err.message === 'Failed writing received data to disk/application') {
           reject('cancel')
@@ -216,11 +236,15 @@ export class HttpHelper {
       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)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
       }
       let httpRequest = http.createHttp();
 
       if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
         this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+        LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
       }
 
       const header = {
@@ -247,6 +271,8 @@ export class HttpHelper {
 
           if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
             this.httpHandlerList.remove(apiNo ?? params.url)
+            LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+            LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
           }
           if (data.responseCode === 200) {
             resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
@@ -259,6 +285,8 @@ export class HttpHelper {
         }).catch((err: Error) => {
         if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
           this.httpHandlerList.remove(apiNo ?? params.url)
+          LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerList,this.httpHandlerList)
+          LocalStorageHelper.storage.setOrCreate(LocalStorageKeys.HttpHandlerListLength,this.httpHandlerList.length)
         }
         if (err.message === 'Failed writing received data to disk/application') {
           reject('cancel')

+ 3 - 0
src/main/ets/utils/LocalStorageHelper.ts

@@ -0,0 +1,3 @@
+export class LocalStorageHelper{
+  public static storage: LocalStorage = new LocalStorage();
+}

+ 5 - 0
src/main/ets/utils/LocalStorageKeys.ts

@@ -0,0 +1,5 @@
+
+export class LocalStorageKeys{
+  public static HttpHandlerList: string = 'httpHandlerList';
+  public static HttpHandlerListLength: string = 'HttpHandlerListLength';
+}