import { ArrayList, HashMap } from '@kit.ArkTS'; import http from '@ohos.net.http'; 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(); // 并发白名单,这个名单里面的api,重复请求不会取消 private concurrentList = new ArrayList(); constructor() { this.httpHandlerList = new HashMap(); this.concurrentList.clear() } /** * 添加并发白名单 * @param apiNo */ public addConcurrent(apiNo: string) { 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 data 请求参数 * @param headers * @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用 * @returns */ public post(url: string, data: string | Object | ArrayBuffer | undefined, headers?: Record, apiNo?: string): Promise { return new Promise((resolve, reject) => { if (this.concurrentList.getIndexOf(apiNo ?? url) === -1 && this.httpHandlerList.hasKey(apiNo ?? url)) { this.httpHandlerList.get(apiNo ?? url).destroy() this.httpHandlerList.remove(apiNo ?? url) } let httpRequest = http.createHttp(); if (this.concurrentList.getIndexOf(apiNo ?? url) === -1) { this.httpHandlerList.set(apiNo ?? url, httpRequest) } const header = { "Content-Type": "application/json", "Accept": "application/json", ...headers } console.log('>>>>>', '接口请求', JSON.stringify(header)) console.log('>>>>>', '接口请求', data) console.log('>>>>>', '接口请求', url) httpRequest.request(url, { method: http.RequestMethod.POST, connectTimeout: 60000, readTimeout: 60000, header: header, extraData: data }) .then((data: http.HttpResponse) => { console.info('=====>' + 'Result:' + data.result as string); console.info('=====>' + 'code:' + data.responseCode); // console.info('=====>' + 'type:' + JSON.stringify(data.resultType)); // console.info('=====>' + 'header:' + JSON.stringify(data.header)); // console.info('=====>' + 'cookies:' + data.cookies); // 自API version 8开始支持cookie // console.info('=====>' + 'header.content-Type:' + JSON.stringify(data.header)); // console.info('=====>' + 'header.Status-Line:' + JSON.stringify(data.header)); if (this.httpHandlerList.hasKey(apiNo ?? url)) { this.httpHandlerList.remove(apiNo ?? url) } if (data.responseCode === 200) { resolve((typeof data.result === 'string'?JSON.parse(data.result):data.result) as T) } else { reject('服务异常') } }).catch((err: Error) => { if (this.httpHandlerList.hasKey(apiNo ?? url)) { this.httpHandlerList.remove(apiNo ?? url) } if (err.message === 'Failed writing received data to disk/application') { reject('cancel') } else reject(err) }); }); } /** * get请求 * @param url url地址 * @param data 请求参数 * @param headers * @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用 * @returns */ public get(url: string, data: string | undefined, headers?: Object, apiNo?: string): Promise { return new Promise((resolve, reject) => { if (this.concurrentList.getIndexOf(apiNo ?? url) === -1 && this.httpHandlerList.hasKey(apiNo ?? url)) { this.httpHandlerList.get(apiNo ?? url).destroy() this.httpHandlerList.remove(apiNo ?? url) } let httpRequest = http.createHttp(); if (this.concurrentList.getIndexOf(apiNo ?? url) === -1) { this.httpHandlerList.set(apiNo ?? url, httpRequest) } const header = { // "Content-Type": "application/json", // "Accept": "application/json", ...headers } // console.log('>>>>>', '接口请求', JSON.stringify(header)) // console.log('>>>>>', '接口请求', data) // console.log('>>>>>', '接口请求', url) if (data) { url = `${url}?` const json = JSON.parse(data) for (let jsonKey in json) { const value = json[jsonKey] if (value) { url = `${url}${jsonKey}=${json[jsonKey]}&` } } url = url.slice(0, url.length - 1) } httpRequest.request(url, { method: http.RequestMethod.GET, connectTimeout: 60000, readTimeout: 60000, header: header, }) .then((data: http.HttpResponse) => { // console.info('=====>' + 'Result:' + data.result as string); // console.info('=====>' + 'code:' + data.responseCode); // console.info('=====>' + 'type:' + JSON.stringify(data.resultType)); // console.info('=====>' + 'header:' + JSON.stringify(data.header)); // console.info('=====>' + 'cookies:' + data.cookies); // 自API version 8开始支持cookie // console.info('=====>' + 'header.content-Type:' + JSON.stringify(data.header)); // console.info('=====>' + 'header.Status-Line:' + JSON.stringify(data.header)); if (this.httpHandlerList.hasKey(apiNo ?? url)) { this.httpHandlerList.remove(apiNo ?? url) } if (data.responseCode === 200) { resolve((typeof data.result === 'string'?JSON.parse(data.result):data.result) as T) } else { reject('服务异常') } }).catch((err: Error) => { if (this.httpHandlerList.hasKey(apiNo ?? url)) { this.httpHandlerList.remove(apiNo ?? url) } if (err.message === 'Failed writing received data to disk/application') { reject('cancel') } else reject(err) }); }); } }