refactor(basic): 优化 HTTP 请求处理和日志记录
- 移除了不必要的注释和未使用的代码 - 添加了数据发送进度的监听和日志记录- 优化了 HTTP 请求的 error 处理 - 调整了请求完成后的清理逻辑,移到 finally 块中执行- 引入了 ToolsHelper用于日志记录
这个提交包含在:
父节点
3c1dcb0782
当前提交
0a9b2f02e8
@ -1,6 +1,6 @@
|
|||||||
import { ArrayList, HashMap } from '@kit.ArkTS';
|
import { ArrayList, HashMap } from '@kit.ArkTS';
|
||||||
import http from '@ohos.net.http';
|
import http from '@ohos.net.http';
|
||||||
import { LogHelper } from '../../../../Index';
|
import { LogHelper, ToolsHelper } from '../../../../Index';
|
||||||
import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper';
|
import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper';
|
||||||
import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
|
import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
|
||||||
import { HttpHelperX, HttpParamsForm, HttpParamsGet, HttpParamsPost, HttpParamsUpload } from './HttpHelperX';
|
import { HttpHelperX, HttpParamsForm, HttpParamsGet, HttpParamsPost, HttpParamsUpload } from './HttpHelperX';
|
||||||
@ -340,31 +340,24 @@ export class HttpHelper {
|
|||||||
if (showLog) {
|
if (showLog) {
|
||||||
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
||||||
}
|
}
|
||||||
// let data = HttpHelperX.getContent(params.data)
|
httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => {
|
||||||
|
ToolsHelper.log(`${apiNo}:\n${JSON.stringify(data)}`)
|
||||||
|
});
|
||||||
// const formData: http.MultiFormData[] = params.data as http.MultiFormData[]
|
|
||||||
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
||||||
method: http.RequestMethod.POST,
|
method: http.RequestMethod.POST,
|
||||||
connectTimeout: 20000,
|
connectTimeout: 20000,
|
||||||
readTimeout: 20000,
|
readTimeout: 20000,
|
||||||
header: header,
|
header: header,
|
||||||
extraData: params.data,
|
|
||||||
usingCache: false,
|
usingCache: false,
|
||||||
multiFormDataList: params.data,
|
multiFormDataList: params.data,
|
||||||
})
|
})
|
||||||
.then((data: http.HttpResponse) => {
|
.then((data: http.HttpResponse) => {
|
||||||
|
|
||||||
if (showLog) {
|
if (showLog) {
|
||||||
LogHelper.debug(`${apiNo}:\n ${data.result as string}`)
|
LogHelper.debug(`${apiNo}:\n ${data.result as string}`)
|
||||||
LogHelper.print(data)
|
LogHelper.print(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
||||||
this.httpHandlerList.remove(apiNo ?? params.url)
|
|
||||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
|
||||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
||||||
this.httpHandlerList.length)
|
|
||||||
}
|
|
||||||
if (data.responseCode === 200) {
|
if (data.responseCode === 200) {
|
||||||
resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
|
resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
|
||||||
} else {
|
} else {
|
||||||
@ -374,13 +367,8 @@ export class HttpHelper {
|
|||||||
reject(err)
|
reject(err)
|
||||||
}
|
}
|
||||||
}).catch((err: Error) => {
|
}).catch((err: Error) => {
|
||||||
|
|
||||||
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
||||||
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
||||||
this.httpHandlerList.remove(apiNo ?? params.url)
|
|
||||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
|
||||||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
||||||
this.httpHandlerList.length)
|
|
||||||
}
|
|
||||||
if (err.message === 'Failed writing received data to disk/application') {
|
if (err.message === 'Failed writing received data to disk/application') {
|
||||||
const error: Error = new Error()
|
const error: Error = new Error()
|
||||||
error.name = 'cancel'
|
error.name = 'cancel'
|
||||||
@ -389,6 +377,14 @@ export class HttpHelper {
|
|||||||
} else {
|
} else {
|
||||||
reject(err)
|
reject(err)
|
||||||
}
|
}
|
||||||
|
}).finally(() => {
|
||||||
|
httpRequest.off("dataSendProgress");
|
||||||
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
||||||
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
||||||
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
||||||
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
||||||
|
this.httpHandlerList.length)
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户