提交网络请求
This commit is contained in:
parent
15f7f114c4
commit
7f8c81dc66
@ -28,4 +28,4 @@ export { RequestDefaultModel,ExpertData } from './src/main/ets/models/RequestDef
|
||||
|
||||
export { HdList, HdListController } from './src/main/ets/components/HdList'
|
||||
|
||||
export { hdHttps } from './src/main/ets/utils/expert_request'
|
||||
|
||||
|
||||
@ -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: '' })
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ import { OfficeSelectedSheet } from './OfficeSelectedSheet'
|
||||
import { PositionSelectedSheet } from './PositionSelectedSheet'
|
||||
import { SpecialitySelectedSheet } from './SpecialitySelectedSheet'
|
||||
import { http } from '@kit.NetworkKit';
|
||||
|
||||
import { rcp } from '@kit.RemoteCommunicationKit';
|
||||
interface extraData {
|
||||
uuid: string
|
||||
}
|
||||
@ -146,26 +146,46 @@ export struct EditUserDataComp {
|
||||
onPhotoSelected: async (uri: string) => {
|
||||
this.photoPath = uri;
|
||||
console.info('Selected image URI:', uri);
|
||||
const base64String = await ChangeUtil.imageToBase64(uri);
|
||||
const base64String = await ChangeUtil.convertUriToBase64(uri);
|
||||
const updateDataUrl:string = BasicConstant.urlExpert + 'modify';
|
||||
hdHttp.post<string>(updateDataUrl, {
|
||||
uuid: authStore.getUser().uuid,
|
||||
userName: authStore.getUser().userName,
|
||||
photo: base64String,
|
||||
type:'2'
|
||||
} as updateExtraData).then(async (res: HdResponse<string>) => {
|
||||
let json:callBackData = JSON.parse(res+'') as callBackData;
|
||||
if(json.code == 1 && json.data && typeof json.data === 'object') {
|
||||
authStore.updateUser(json.data)
|
||||
console.log('更新图片成功:', authStore.getUser().email);
|
||||
promptAction.showToast({message:'头像修改成功', duration: 1000})
|
||||
} else {
|
||||
console.error('更新图片失败:'+json.message)
|
||||
promptAction.showToast({ message: json.message, duration: 1000 })
|
||||
}
|
||||
}).catch((err: BusinessError) => {
|
||||
console.error(`更新图片请求失败: ${err}`);
|
||||
// 定义content,请根据实际情况选择
|
||||
const postContent = new rcp.MultipartForm({
|
||||
'uuid': authStore.getUser().uuid,
|
||||
'userName': authStore.getUser().userName,
|
||||
'photo': base64String,
|
||||
'type':'2'
|
||||
})
|
||||
// 创建session
|
||||
const session = rcp.createSession();
|
||||
// 使用post发起请求,使用Promise进行异步回调;其中content以及destination为可选参数,可根据实际情况选择
|
||||
session.post(updateDataUrl, postContent)
|
||||
.then((response) => {
|
||||
// console.info(`Response succeeded: ${JSON.stringify(response.headers)}`);
|
||||
console.info(`Response succeeded: ${JSON.stringify(response.statusCode)}`);
|
||||
console.info(`Response succeeded22: ${JSON.stringify(response)}`);
|
||||
// console.info(`Response succeeded: ${JSON.stringify(postContent)}`);
|
||||
})
|
||||
.catch((err: BusinessError) => {
|
||||
console.error(`Response err: Code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err)}`);
|
||||
})
|
||||
// hdHttp.post<string>(updateDataUrl, {
|
||||
// uuid: authStore.getUser().uuid,
|
||||
// userName: authStore.getUser().userName,
|
||||
// photo: base64String,
|
||||
// type:'2'
|
||||
// } as updateExtraData).then(async (res: HdResponse<string>) => {
|
||||
// let json:callBackData = JSON.parse(res+'') as callBackData;
|
||||
// if(json.code == 1 && json.data && typeof json.data === 'object') {
|
||||
// authStore.updateUser(json.data)
|
||||
// console.log('更新图片成功:', authStore.getUser().email);
|
||||
// promptAction.showToast({message:'头像修改成功', duration: 1000})
|
||||
// } else {
|
||||
// console.error('更新图片失败:'+json.message)
|
||||
// promptAction.showToast({ message: json.message, duration: 1000 })
|
||||
// }
|
||||
// }).catch((err: BusinessError) => {
|
||||
// console.error(`更新图片请求失败: ${err}`);
|
||||
// })
|
||||
}
|
||||
}),
|
||||
alignment: DialogAlignment.Bottom,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user