|
@@ -2,9 +2,20 @@ import { ArrayList, HashMap } from '@kit.ArkTS';
|
|
|
import http from '@ohos.net.http';
|
|
|
|
|
|
|
|
|
-type HttpParams = {
|
|
|
+type HttpParamsGet = {
|
|
|
url: string
|
|
|
- data?: string | Object | ArrayBuffer
|
|
|
+ query?: Record<string, string> | Object
|
|
|
+ headers?: Record<string, string>
|
|
|
+}
|
|
|
+type HttpParamsPost = {
|
|
|
+ url: string
|
|
|
+ data?: Object
|
|
|
+ query?: Record<string, string> | Object
|
|
|
+ headers?: Record<string, string>
|
|
|
+}
|
|
|
+type HttpParamsForm = {
|
|
|
+ url: string
|
|
|
+ data?: Record<string, string> | Object
|
|
|
query?: Record<string, string> | Object
|
|
|
headers?: Record<string, string>
|
|
|
}
|
|
@@ -50,12 +61,11 @@ export class HttpHelper {
|
|
|
/**
|
|
|
* postJson请求
|
|
|
* @param url url地址
|
|
|
- * @param data 请求参数
|
|
|
* @param headers
|
|
|
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
* @returns
|
|
|
*/
|
|
|
- public postJson<T>(params: HttpParams, apiNo?: string): Promise<T> {
|
|
|
+ public postJson<T>(params: HttpParamsPost, apiNo?: string): Promise<T> {
|
|
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
|
@@ -74,9 +84,9 @@ export class HttpHelper {
|
|
|
// "Accept": "application/json",
|
|
|
...params.headers
|
|
|
}
|
|
|
- console.log('>>>>>', 'POST:', params.url)
|
|
|
- // console.log('>>>>>', '接口请求', JSON.stringify(header))
|
|
|
- // console.log('>>>>>', '接口请求', data)
|
|
|
+ console.log('=====>', 'POST:', JSON.stringify(params))
|
|
|
+ // console.log('=====>', '接口请求', JSON.stringify(header))
|
|
|
+ // console.log('=====>', '接口请求', data)
|
|
|
|
|
|
httpRequest.request(this.getUrl(params.url, params.query), {
|
|
|
method: http.RequestMethod.POST,
|
|
@@ -115,6 +125,74 @@ export class HttpHelper {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * postForm请求
|
|
|
+ * @param url url地址
|
|
|
+ * @param headers
|
|
|
+ * @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ public postForm<T>(params: HttpParamsForm, apiNo?: string): 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)
|
|
|
+ }
|
|
|
+ let httpRequest = http.createHttp();
|
|
|
+
|
|
|
+ if (this.concurrentList.getIndexOf(apiNo ?? params.url) === -1) {
|
|
|
+ this.httpHandlerList.set(apiNo ?? params.url, httpRequest)
|
|
|
+ }
|
|
|
+
|
|
|
+ const header = {
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
|
+ // "Accept": "application/json",
|
|
|
+ ...params.headers
|
|
|
+ }
|
|
|
+ let data = this.getContent(params.data)
|
|
|
+ console.log('=====>', 'POSTForm:', params.url)
|
|
|
+ console.log('=====>', 'POSTForm:', JSON.stringify(header))
|
|
|
+ console.log('=====>', 'POSTForm:', data)
|
|
|
+
|
|
|
+ httpRequest.request(this.getUrl(params.url, params.query), {
|
|
|
+ method: http.RequestMethod.POST,
|
|
|
+ connectTimeout: 20000,
|
|
|
+ readTimeout: 20000,
|
|
|
+ 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 ?? params.url)) {
|
|
|
+ this.httpHandlerList.remove(apiNo ?? params.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 ?? params.url)) {
|
|
|
+ this.httpHandlerList.remove(apiNo ?? params.url)
|
|
|
+ }
|
|
|
+ if (err.message === 'Failed writing received data to disk/application') {
|
|
|
+ reject('cancel')
|
|
|
+ } else
|
|
|
+ reject(err)
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* get请求
|
|
@@ -124,7 +202,7 @@ export class HttpHelper {
|
|
|
* @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用
|
|
|
* @returns
|
|
|
*/
|
|
|
- public get<T>(params: HttpParams, apiNo?: string): Promise<T> {
|
|
|
+ public get<T>(params: HttpParamsGet, apiNo?: string): Promise<T> {
|
|
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
|
@@ -141,14 +219,14 @@ export class HttpHelper {
|
|
|
const header = {
|
|
|
...params.headers
|
|
|
}
|
|
|
- console.log('>>>>>', 'GET:', this.getUrl(params.url, params.query))
|
|
|
+ console.log('=====>', 'GET:', this.getUrl(params.url, params.query))
|
|
|
|
|
|
httpRequest.request(this.getUrl(params.url, params.query), {
|
|
|
method: http.RequestMethod.GET,
|
|
|
connectTimeout: 20000,
|
|
|
readTimeout: 20000,
|
|
|
header: header,
|
|
|
- extraData: params.data
|
|
|
+ // extraData: params.data
|
|
|
})
|
|
|
.then((data: http.HttpResponse) => {
|
|
|
// console.info('=====>' + 'Result:' + data.result as string);
|
|
@@ -198,6 +276,21 @@ export class HttpHelper {
|
|
|
return u
|
|
|
}
|
|
|
|
|
|
+ private getContent(data: Record<string, string> | Object) {
|
|
|
+ let u = ''
|
|
|
+ let q = data
|
|
|
+ if (typeof data === 'object') {
|
|
|
+ q = this.classToRecord(data)
|
|
|
+ }
|
|
|
+ Object.entries(q).forEach((row) => {
|
|
|
+ if (row[1]) {
|
|
|
+ u = `${u}${row[0]}=${row[1] as string}&`
|
|
|
+ }
|
|
|
+ });
|
|
|
+ u = u.slice(0, u.length - 1)
|
|
|
+ return u
|
|
|
+ }
|
|
|
+
|
|
|
private classToRecord(obj: Object): Record<string, string> {
|
|
|
const record: Record<string, string> = {} as Record<string, string>;
|
|
|
for (const key in obj) {
|