提交网络请求

This commit is contained in:
XiuYun CHEN
2025-05-15 15:15:40 +08:00
parent 15f7f114c4
commit 7f8c81dc66
5 changed files with 42 additions and 215 deletions
@@ -1,24 +0,0 @@
import { webview } from '@kit.ArkWeb'
import { logger } from '../utils/logger'
@Component
export struct HdWeb {
layoutMode: WebLayoutMode = WebLayoutMode.NONE
src: ResourceStr = $rawfile('detail.html')
onLoad: () => void = () => {
}
controller: webview.WebviewController = new webview.WebviewController()
build() {
Web({ src: this.src, controller: this.controller })
.javaScriptAccess(true)
.onPageEnd(() => {
this.onLoad()
})
.onErrorReceive(event => {
logger.error(event!.error.getErrorInfo())
})
.layoutMode(this.layoutMode)
.layoutWeight(1)
}
}
@@ -1,121 +0,0 @@
import { http } from '@kit.NetworkKit';
import { authStore } from './auth';
import { promptAction, router } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';
import { logger } from './logger';
import uri from '@ohos.uri';
interface HdRequestOptions {
baseURL?: string
}
type HdParams = Record<string, string | number | boolean>
export interface HdResponse<T> {
code: number
message: string
data: T
}
class xx_HdHttp {
baseURL: string
constructor(options: HdRequestOptions) {
this.baseURL = options.baseURL || ''
}
private request<T>(path: string, method: http.RequestMethod = http.RequestMethod.GET, extraData?: Object) {
// 创建httpRequest对象。
let httpRequest = http.createHttp();
let promise = httpRequest.request(
// 请求url地址
path,
{
// 请求方式
method: method,
// 可选,默认为60s
connectTimeout: 60000,
// 可选,默认为60s
readTimeout: 60000,
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
},
extraData:extraData
});
// 处理响应结果。
return promise.then((data) => {
logger.info('Response httpReqSimply:' + JSON.stringify(data));
const result = data.result as HdResponse<T>
return result
}
).catch((err:BusinessError) => {
logger.info('Response httpReq error:' + JSON.stringify(err));
return Promise.reject(err);
}).finally(() => {
httpRequest.destroy()
})
// const httpInstance = http.createHttp()
// const options: http.HttpRequestOptions = {
// header: {
// 'Content-Type': 'application/json;charset=utf-8',
// 'Content-Language': 'zh_CN',
// connectTimeout: 60000,
// readTimeout: 60000,
// },
// method
// }
// let fullUrl = this.baseURL + path
// if (method === http.RequestMethod.GET && extraData) {
// const strArr = Object.keys(extraData)
// .filter(key => (extraData as HdParams)[key] !== undefined)
// .map(key => `${key}=${(extraData as HdParams)[key]}`)
// fullUrl += `?${strArr.join('&')}`
// } else {
// options.extraData = JSON.stringify(extraData);
// }
// return httpInstance.request(fullUrl, options).then((res) => {
// if (res.result) {
// const result = res.result as HdResponse<T>
// console.log('请求成功:',JSON.stringify(res.result));
// if (result.code === 10000) {
// logger.info(fullUrl, JSON.stringify(res.result).substring(0, 200))
// return result
// }
// if (result.code === 401) {
// authStore.delUser()
// router.pushUrl({
// url: 'pages/LoginPage/LoginPage'
// }, router.RouterMode.Single)
// throw new Error('登录过期')
// }
// }
// return Promise.reject(res.result)
// }).catch((err: BusinessError) => {
// logger.error(fullUrl, err.code?.toString(), err.message)
// promptAction.showToast({ message: err.message || '网络错误' })
// return Promise.reject(err)
// }).finally(() => {
// httpInstance.destroy()
// })
}
get<T>(url: string, data?: Object): Promise<HdResponse<T>> {
return this.request<T>(url, http.RequestMethod.GET, data)
}
post<T>(url: string, data?: Object): Promise<HdResponse<T>> {
return this.request<T>(url, http.RequestMethod.POST, data)
}
put<T>(url: string, data?: Object): Promise<HdResponse<T>> {
return this.request<T>(url, http.RequestMethod.PUT, data)
}
delete<T>(url: string, data?: Object): Promise<HdResponse<T>> {
return this.request<T>(url, http.RequestMethod.DELETE, data)
}
}
export const hdHttps = new xx_HdHttp({ baseURL: '' })
+2 -50
View File
@@ -31,56 +31,6 @@ class HdHttp {
this.baseURL = options.baseURL || ''
}
private request1<T>(path: string, method: http.RequestMethod = http.RequestMethod.GET, extraDatas:HashMap<string, string>) {
const httpInstance = http.createHttp()
let fullUrl = this.baseURL + path
let promise = httpInstance.request(
// 请求url地址
fullUrl,
{
// 请求方式
method: http.RequestMethod.POST,
// 可选,默认为60s
connectTimeout: 60000,
// 可选,默认为60s
readTimeout: 60000,
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json',
'sign':this.getSign(extraDatas)
},
extraData:ChangeUtil.map2Json(extraDatas)
});
logger.info('Response JSON.stringify(extraDatas)' + ChangeUtil.map2Json(extraDatas))
return promise.then((data) => {
logger.info('Response request:' + data.result);
if (data.result) {
const result = data.result as HdResponse<T>
logger.info('Response result:' + result);
return result
}
return Promise.reject(data.result)
// if (data.responseCode === http.ResponseCode.OK) {
// console.info('Response request:' + data.result);
//
// }
// else
// {
//
// }
// return Promise.reject(data.result)
}
).catch((err:BusinessError) => {
logger.info('Response httpReq request:' + JSON.stringify(err));
return Promise.reject(err)
}).finally(() => {
httpInstance.destroy()
})
}
private request<T>(path: string, method: http.RequestMethod = http.RequestMethod.POST, extraDatas :HashMap<string, string>) {
const httpInstance = http.createHttp()
@@ -155,6 +105,8 @@ class HdHttp {
}
get<T>(url: string, data?: Object): Promise<HdResponse<T>> {
return this.requestafter<T>(url, http.RequestMethod.GET, data)
}