diff --git a/Index.ets b/Index.ets index 59f9d95..e90cbba 100644 --- a/Index.ets +++ b/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' /** * 正则相关 diff --git a/src/main/ets/http/HttpHelper.ts b/src/main/ets/http/HttpHelper.ts index 6c72fdf..4e51756 100644 --- a/src/main/ets/http/HttpHelper.ts +++ b/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(); + 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') diff --git a/src/main/ets/utils/LocalStorageHelper.ts b/src/main/ets/utils/LocalStorageHelper.ts new file mode 100644 index 0000000..38ee486 --- /dev/null +++ b/src/main/ets/utils/LocalStorageHelper.ts @@ -0,0 +1,3 @@ +export class LocalStorageHelper{ + public static storage: LocalStorage = new LocalStorage(); +} \ No newline at end of file diff --git a/src/main/ets/utils/LocalStorageKeys.ts b/src/main/ets/utils/LocalStorageKeys.ts new file mode 100644 index 0000000..9e2ee03 --- /dev/null +++ b/src/main/ets/utils/LocalStorageKeys.ts @@ -0,0 +1,5 @@ + +export class LocalStorageKeys{ + public static HttpHandlerList: string = 'httpHandlerList'; + public static HttpHandlerListLength: string = 'HttpHandlerListLength'; +} \ No newline at end of file