2024-05-07 17:41:21 +08:00
|
|
|
|
import { ArrayList, HashMap } from '@kit.ArkTS';
|
|
|
|
|
|
import http from '@ohos.net.http';
|
2024-10-31 12:23:46 +08:00
|
|
|
|
import { LogHelper } from '../../../../Index';
|
2024-10-20 17:59:30 +08:00
|
|
|
|
import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper';
|
|
|
|
|
|
import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
|
2024-10-31 12:23:46 +08:00
|
|
|
|
import { HttpHelperX, HttpParamsForm, HttpParamsGet, HttpParamsPost } from './HttpHelperX';
|
2025-03-13 19:04:11 +08:00
|
|
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
|
|
|
|
import { image } from '@kit.ImageKit';
|
2024-10-31 12:23:46 +08:00
|
|
|
|
|
2024-05-08 16:20:49 +08:00
|
|
|
|
|
2024-05-07 17:41:21 +08:00
|
|
|
|
export class HttpHelper {
|
|
|
|
|
|
private static instance: HttpHelper | null = null
|
|
|
|
|
|
|
|
|
|
|
|
// 单例模式
|
|
|
|
|
|
static get() {
|
|
|
|
|
|
// 判断系统是否已经有单例了
|
|
|
|
|
|
if (HttpHelper.instance === null) {
|
|
|
|
|
|
HttpHelper.instance = new HttpHelper()
|
|
|
|
|
|
}
|
|
|
|
|
|
return HttpHelper.instance
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//请求中队列
|
|
|
|
|
|
private httpHandlerList = new HashMap<string, http.HttpRequest>();
|
|
|
|
|
|
// 并发白名单,这个名单里面的api,重复请求不会取消
|
|
|
|
|
|
private concurrentList = new ArrayList<string>();
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.httpHandlerList = new HashMap<string, http.HttpRequest>();
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
|
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
this.concurrentList.clear()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加并发白名单
|
|
|
|
|
|
* @param apiNo
|
|
|
|
|
|
*/
|
2024-10-31 12:23:46 +08:00
|
|
|
|
public addConcurrent(apiNo?: string) {
|
|
|
|
|
|
if (!apiNo) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo) === -1) {
|
|
|
|
|
|
this.concurrentList.add(apiNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public removeConcurrent(apiNo: string) {
|
|
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo) !== -1) {
|
|
|
|
|
|
this.concurrentList.remove(apiNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* postJson请求
|
|
|
|
|
|
* @param url url地址
|
|
|
|
|
|
* @param headers
|
|
|
|
|
|
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
2024-10-31 12:23:46 +08:00
|
|
|
|
public postJson<T>(params: HttpParamsPost, apiNo?: string, showLog?: boolean): Promise<T> {
|
2024-05-07 17:41:21 +08:00
|
|
|
|
|
|
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
|
|
|
2024-10-18 20:30:27 +08:00
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1 &&
|
|
|
|
|
|
this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
2024-05-08 16:20:49 +08:00
|
|
|
|
this.httpHandlerList.get(apiNo ?? params.url).destroy()
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
let httpRequest = http.createHttp();
|
|
|
|
|
|
|
2024-05-08 16:20:49 +08:00
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
|
|
|
|
|
|
this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
const header = HttpHelperX.getHeaders("application/json;charset=UTF-8", params.headers)
|
|
|
|
|
|
|
|
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
2024-05-07 17:41:21 +08:00
|
|
|
|
method: http.RequestMethod.POST,
|
2024-05-08 16:20:49 +08:00
|
|
|
|
connectTimeout: 20000,
|
|
|
|
|
|
readTimeout: 20000,
|
2024-05-07 17:41:21 +08:00
|
|
|
|
header: header,
|
2024-11-26 16:39:19 +08:00
|
|
|
|
extraData: params.data,
|
|
|
|
|
|
usingCache: false,
|
2024-05-07 17:41:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then((data: http.HttpResponse) => {
|
2024-10-31 12:23:46 +08:00
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`${apiNo}:\n ${data.result as string}`)
|
|
|
|
|
|
LogHelper.print(data)
|
|
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
|
2024-05-08 16:20:49 +08:00
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (data.responseCode === 200) {
|
2024-05-08 16:20:49 +08:00
|
|
|
|
resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
} else {
|
2024-10-15 17:39:02 +08:00
|
|
|
|
const err: Error = new Error()
|
|
|
|
|
|
err.name = data.responseCode.toString()
|
|
|
|
|
|
err.message = '服务异常'
|
|
|
|
|
|
reject(err)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).catch((err: Error) => {
|
2024-10-31 12:23:46 +08:00
|
|
|
|
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
2024-05-08 16:20:49 +08:00
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (err.message === 'Failed writing received data to disk/application') {
|
2024-11-05 17:43:12 +08:00
|
|
|
|
const error: Error = new Error()
|
|
|
|
|
|
error.name = 'cancel'
|
|
|
|
|
|
error.message = err.message
|
|
|
|
|
|
reject(error)
|
2024-10-18 20:30:27 +08:00
|
|
|
|
} else {
|
2024-05-07 17:41:21 +08:00
|
|
|
|
reject(err)
|
2024-10-18 20:30:27 +08:00
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 19:22:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* postForm请求
|
|
|
|
|
|
* @param url url地址
|
|
|
|
|
|
* @param headers
|
|
|
|
|
|
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
2024-10-31 12:23:46 +08:00
|
|
|
|
public postForm<T>(params: HttpParamsForm, apiNo?: string, showLog?: boolean): Promise<T> {
|
2024-10-10 19:22:29 +08:00
|
|
|
|
|
|
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
|
|
|
2024-10-18 20:30:27 +08:00
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1 &&
|
|
|
|
|
|
this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
2024-10-10 19:22:29 +08:00
|
|
|
|
this.httpHandlerList.get(apiNo ?? params.url).destroy()
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-10-10 19:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
let httpRequest = http.createHttp();
|
|
|
|
|
|
|
|
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
|
|
|
|
|
|
this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-10-10 19:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
const header = HttpHelperX.getHeaders("application/x-www-form-urlencoded;charset=UTF-8", params.headers)
|
|
|
|
|
|
let data = HttpHelperX.getContent(params.data)
|
2024-10-11 17:56:56 +08:00
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`postForm:${apiNo}\n`, JSON.stringify(params))
|
|
|
|
|
|
}
|
2024-10-10 19:22:29 +08:00
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
2024-10-10 19:22:29 +08:00
|
|
|
|
method: http.RequestMethod.POST,
|
|
|
|
|
|
connectTimeout: 20000,
|
|
|
|
|
|
readTimeout: 20000,
|
|
|
|
|
|
header: header,
|
2024-11-26 16:39:19 +08:00
|
|
|
|
usingCache: false,
|
2024-10-31 12:23:46 +08:00
|
|
|
|
extraData: data ? encodeURI(data) : undefined
|
2024-10-10 19:22:29 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then((data: http.HttpResponse) => {
|
2024-10-31 12:23:46 +08:00
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`${apiNo}:\n ${data.result as string}`)
|
|
|
|
|
|
LogHelper.print(data)
|
|
|
|
|
|
}
|
2024-10-10 19:22:29 +08:00
|
|
|
|
|
|
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-10-10 19:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (data.responseCode === 200) {
|
|
|
|
|
|
resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
|
|
|
|
|
|
} else {
|
2024-10-15 17:39:02 +08:00
|
|
|
|
const err: Error = new Error()
|
|
|
|
|
|
err.name = data.responseCode.toString()
|
|
|
|
|
|
err.message = '服务异常'
|
|
|
|
|
|
reject(err)
|
2024-10-10 19:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).catch((err: Error) => {
|
2024-10-31 12:23:46 +08:00
|
|
|
|
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
2024-10-10 19:22:29 +08:00
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-10-10 19:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (err.message === 'Failed writing received data to disk/application') {
|
2024-11-05 17:43:12 +08:00
|
|
|
|
const error: Error = new Error()
|
|
|
|
|
|
error.name = 'cancel'
|
|
|
|
|
|
error.message = err.message
|
|
|
|
|
|
reject(error)
|
2024-10-18 20:30:27 +08:00
|
|
|
|
} else {
|
2024-10-10 19:22:29 +08:00
|
|
|
|
reject(err)
|
2024-10-18 20:30:27 +08:00
|
|
|
|
}
|
2024-10-10 19:22:29 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-08 16:20:49 +08:00
|
|
|
|
|
2024-05-07 17:41:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* get请求
|
|
|
|
|
|
* @param url url地址
|
|
|
|
|
|
* @param data 请求参数
|
|
|
|
|
|
* @param headers
|
|
|
|
|
|
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
2024-10-31 12:23:46 +08:00
|
|
|
|
public get<T>(params: HttpParamsGet, apiNo?: string, showLog?: boolean): Promise<T> {
|
2024-05-07 17:41:21 +08:00
|
|
|
|
|
|
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
|
|
|
2024-10-18 20:30:27 +08:00
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1 &&
|
|
|
|
|
|
this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
2024-05-08 16:20:49 +08:00
|
|
|
|
this.httpHandlerList.get(apiNo ?? params.url).destroy()
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
let httpRequest = http.createHttp();
|
|
|
|
|
|
|
2024-05-08 16:20:49 +08:00
|
|
|
|
if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
|
|
|
|
|
|
this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`GET:${apiNo}\n`, HttpHelperX.getUrl(params.url, params.query) + '\n',
|
|
|
|
|
|
JSON.stringify(params.headers))
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-31 12:23:46 +08:00
|
|
|
|
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
2024-05-07 17:41:21 +08:00
|
|
|
|
method: http.RequestMethod.GET,
|
2024-05-08 16:20:49 +08:00
|
|
|
|
connectTimeout: 20000,
|
|
|
|
|
|
readTimeout: 20000,
|
2024-10-31 12:23:46 +08:00
|
|
|
|
header: params.headers,
|
2024-11-26 16:39:19 +08:00
|
|
|
|
usingCache: false,
|
2024-10-10 19:22:29 +08:00
|
|
|
|
// extraData: params.data
|
2024-05-07 17:41:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then((data: http.HttpResponse) => {
|
2024-10-31 12:23:46 +08:00
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`${apiNo}:\n${data.result as string}`)
|
|
|
|
|
|
LogHelper.print(data)
|
|
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
|
2024-05-08 16:20:49 +08:00
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (data.responseCode === 200) {
|
2025-03-10 16:42:44 +08:00
|
|
|
|
|
|
|
|
|
|
if (typeof data.result === 'string') {
|
|
|
|
|
|
resolve(JSON.parse(data.result) as T)
|
2025-03-13 19:04:11 +08:00
|
|
|
|
} else {
|
2025-03-10 16:42:44 +08:00
|
|
|
|
resolve(data.result as T)
|
|
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
} else {
|
2024-10-15 17:39:02 +08:00
|
|
|
|
const err: Error = new Error()
|
|
|
|
|
|
err.name = data.responseCode.toString()
|
2025-04-02 17:24:48 +08:00
|
|
|
|
err.message = '服务异常'
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}).catch((err: Error) => {
|
|
|
|
|
|
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') {
|
|
|
|
|
|
const error: Error = new Error()
|
|
|
|
|
|
error.name = 'cancel'
|
|
|
|
|
|
error.message = err.message
|
|
|
|
|
|
reject(error)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* postJson请求
|
|
|
|
|
|
* @param url url地址
|
|
|
|
|
|
* @param headers
|
|
|
|
|
|
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
public upload<T>(params: HttpParamsPost, apiNo?: string, showLog?: boolean): Promise<T> {
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const formData: http.MultiFormData[] = params.data as http.MultiFormData[]
|
|
|
|
|
|
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
|
|
|
|
|
method: http.RequestMethod.POST,
|
|
|
|
|
|
connectTimeout: 20000,
|
|
|
|
|
|
readTimeout: 20000,
|
|
|
|
|
|
header: header,
|
|
|
|
|
|
// extraData: params.data,
|
|
|
|
|
|
usingCache: false,
|
|
|
|
|
|
multiFormDataList: formData,
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((data: http.HttpResponse) => {
|
|
|
|
|
|
if (showLog) {
|
|
|
|
|
|
LogHelper.debug(`${apiNo}:\n ${data.result as string}`)
|
|
|
|
|
|
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) {
|
|
|
|
|
|
resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const err: Error = new Error()
|
|
|
|
|
|
err.name = data.responseCode.toString()
|
2024-10-15 17:39:02 +08:00
|
|
|
|
err.message = '服务异常'
|
|
|
|
|
|
reject(err)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).catch((err: Error) => {
|
2024-10-31 12:23:46 +08:00
|
|
|
|
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
2024-05-08 16:20:49 +08:00
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo ?? params.url)) {
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo ?? params.url)
|
2024-10-20 17:59:30 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
2024-10-31 12:23:46 +08:00
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (err.message === 'Failed writing received data to disk/application') {
|
2024-11-05 17:43:12 +08:00
|
|
|
|
const error: Error = new Error()
|
|
|
|
|
|
error.name = 'cancel'
|
|
|
|
|
|
error.message = err.message
|
|
|
|
|
|
reject(error)
|
2024-10-18 20:30:27 +08:00
|
|
|
|
} else {
|
2024-05-07 17:41:21 +08:00
|
|
|
|
reject(err)
|
2024-10-18 20:30:27 +08:00
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-11-12 17:12:11 +08:00
|
|
|
|
|
|
|
|
|
|
clearHttp() {
|
|
|
|
|
|
for (let handler of this.httpHandlerList.keys()) {
|
|
|
|
|
|
this.cancel(handler)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cancel(apiNo: string) {
|
|
|
|
|
|
if (this.httpHandlerList.hasKey(apiNo)) {
|
|
|
|
|
|
this.httpHandlerList.get(apiNo).destroy()
|
|
|
|
|
|
this.httpHandlerList.remove(apiNo)
|
|
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
|
|
|
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength,
|
|
|
|
|
|
this.httpHandlerList.length)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-13 19:04:11 +08:00
|
|
|
|
|
|
|
|
|
|
downloadImage(url: string): Promise<image.ImageSource> {
|
|
|
|
|
|
return new Promise<image.ImageSource>((resolve, reject) => {
|
|
|
|
|
|
http.createHttp()
|
|
|
|
|
|
.request(url, (error: BusinessError, data: http.HttpResponse) => {
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
reject(error)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (data.result instanceof ArrayBuffer) {
|
|
|
|
|
|
let imageData: ArrayBuffer = data.result as ArrayBuffer;
|
|
|
|
|
|
let imageSource: image.ImageSource = image.createImageSource(imageData);
|
|
|
|
|
|
resolve(imageSource)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reject(new Error('下载图片失败'))
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-05-07 17:41:21 +08:00
|
|
|
|
}
|