- 在GlobalValue中新增envStatus属性用于记录环境状态- 支持TEST环境的自定义域名配置- 优化环境切换逻辑,支持多域名自动适配 - 新增EnvManager类统一管理环境同步和检测 - 实现用户登录前的环境状态同步机制 - 完善登录流程中的用户存在性校验 - 注册流程支持多环境验证码发送 - 首页及各个业务模块集成环境同步机制- HttpHelper支持批量添加并发请求白名单 - 新增checkUserLocal接口用于用户本地化判断 -修复环境URL获取逻辑,提高系统稳定性
403 行
14 KiB
Plaintext
403 行
14 KiB
Plaintext
import { ArrayList, HashMap } from '@kit.ArkTS';
|
||
import http from '@ohos.net.http';
|
||
import { LogHelper } from '../../../../Index';
|
||
import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper';
|
||
import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
|
||
import { HttpHelperX, HttpParamsForm, HttpParamsGet, HttpParamsPost, HttpParamsUpload } from './HttpHelperX';
|
||
import { BusinessError } from '@kit.BasicServicesKit';
|
||
import { image } from '@kit.ImageKit';
|
||
|
||
|
||
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>();
|
||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList)
|
||
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, this.httpHandlerList.length)
|
||
this.concurrentList.clear()
|
||
}
|
||
|
||
/**
|
||
* 添加并发白名单
|
||
* @param apiNo
|
||
*/
|
||
public addConcurrent(apiNo?: string) {
|
||
if (!apiNo) {
|
||
return
|
||
}
|
||
if (this.concurrentList.getIndexOf(apiNo) === -1) {
|
||
this.concurrentList.add(apiNo)
|
||
}
|
||
}
|
||
public addConcurrents(apiNo: string[]) {
|
||
for (let apiNoElement of apiNo) {
|
||
if (this.concurrentList.getIndexOf(apiNoElement) === -1) {
|
||
this.concurrentList.add(apiNoElement)
|
||
}
|
||
}
|
||
}
|
||
|
||
public removeConcurrent(apiNo: string) {
|
||
if (this.concurrentList.getIndexOf(apiNo) !== -1) {
|
||
this.concurrentList.remove(apiNo)
|
||
}
|
||
}
|
||
public removeConcurrents(apiNo: string[]) {
|
||
for (let apiNoElement of apiNo) {
|
||
if (this.concurrentList.getIndexOf(apiNoElement) !== -1) {
|
||
this.concurrentList.remove(apiNoElement)
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* postJson请求
|
||
* @param url url地址
|
||
* @param headers
|
||
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
||
* @returns
|
||
*/
|
||
public postJson<T>(params: HttpParamsPost, apiNo?: string, showLog?: boolean): Promise<T> {
|
||
|
||
return new Promise<T>((resolve, reject) => {
|
||
|
||
let httpRequest = http.createHttp();
|
||
this.setHandler(apiNo ?? params.url, httpRequest)
|
||
|
||
|
||
const header = HttpHelperX.getHeaders("application/json;charset=UTF-8", params.headers)
|
||
|
||
if (showLog) {
|
||
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
||
}
|
||
|
||
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
||
method: http.RequestMethod.POST,
|
||
connectTimeout: 20000,
|
||
readTimeout: 20000,
|
||
header: header,
|
||
extraData: params.data,
|
||
usingCache: false,
|
||
})
|
||
.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()
|
||
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)
|
||
}
|
||
});
|
||
});
|
||
|
||
}
|
||
|
||
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地址
|
||
* @param headers
|
||
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
||
* @returns
|
||
*/
|
||
public postForm<T>(params: HttpParamsForm, apiNo?: string, showLog?: boolean): Promise<T> {
|
||
|
||
return new Promise<T>((resolve, reject) => {
|
||
|
||
let httpRequest = http.createHttp();
|
||
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)
|
||
|
||
if (showLog) {
|
||
LogHelper.debug(`postForm:${apiNo}\n`, JSON.stringify(params))
|
||
}
|
||
|
||
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
||
method: http.RequestMethod.POST,
|
||
connectTimeout: 20000,
|
||
readTimeout: 20000,
|
||
header: header,
|
||
usingCache: false,
|
||
extraData: data ? encodeURI(data) : undefined
|
||
})
|
||
.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()
|
||
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)
|
||
}
|
||
});
|
||
});
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* get请求
|
||
* @param url url地址
|
||
* @param data 请求参数
|
||
* @param headers
|
||
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
||
* @returns
|
||
*/
|
||
public get<T>(params: HttpParamsGet, apiNo?: string, showLog?: boolean): Promise<T> {
|
||
|
||
return new Promise<T>((resolve, reject) => {
|
||
|
||
let httpRequest = http.createHttp();
|
||
this.setHandler(apiNo ?? params.url, httpRequest)
|
||
|
||
if (showLog) {
|
||
LogHelper.debug(`GET:${apiNo}\n`, HttpHelperX.getUrl(params.url, params.query) + '\n',
|
||
JSON.stringify(params.headers))
|
||
}
|
||
|
||
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
||
method: http.RequestMethod.GET,
|
||
connectTimeout: 20000,
|
||
readTimeout: 20000,
|
||
header: params.headers,
|
||
usingCache: false,
|
||
// extraData: params.data
|
||
})
|
||
.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) {
|
||
|
||
if (typeof data.result === 'string') {
|
||
resolve(JSON.parse(data.result) as T)
|
||
} else {
|
||
resolve(data.result as T)
|
||
}
|
||
} else {
|
||
const err: Error = new Error()
|
||
err.name = data.responseCode.toString()
|
||
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: HttpParamsUpload, apiNo?: string, showLog?: boolean): Promise<T> {
|
||
|
||
return new Promise<T>((resolve, reject) => {
|
||
|
||
let httpRequest = http.createHttp();
|
||
this.setHandler(apiNo ?? params.url, httpRequest)
|
||
|
||
if (showLog) {
|
||
LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params))
|
||
}
|
||
httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => {
|
||
if (showLog) {
|
||
LogHelper.debug(`${apiNo}:\n${JSON.stringify(data)}`)
|
||
}
|
||
params.onProgress && params.onProgress(data.sendSize, data.totalSize)
|
||
});
|
||
httpRequest.request(HttpHelperX.getUrl(params.url, params.query), {
|
||
method: http.RequestMethod.POST,
|
||
connectTimeout: 20000,
|
||
readTimeout: 20000,
|
||
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 {
|
||
const err: Error = new Error()
|
||
err.name = data.responseCode.toString()
|
||
err.message = '服务异常'
|
||
reject(err)
|
||
}
|
||
}).catch((err: Error) => {
|
||
|
||
LogHelper.error(JSON.stringify({ err: err, url: params.url, }))
|
||
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)
|
||
}
|
||
}).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)
|
||
}
|
||
});
|
||
});
|
||
|
||
}
|
||
|
||
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)
|
||
}
|
||
}
|
||
|
||
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('下载图片失败'))
|
||
}
|
||
})
|
||
})
|
||
}
|
||
} |