diff --git a/commons/basic/Index.ets b/commons/basic/Index.ets index 7f24ccb..59820dc 100644 --- a/commons/basic/Index.ets +++ b/commons/basic/Index.ets @@ -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' + diff --git a/commons/basic/src/main/ets/components/HdWeb.ets b/commons/basic/src/main/ets/components/HdWeb.ets deleted file mode 100644 index c3ac490..0000000 --- a/commons/basic/src/main/ets/components/HdWeb.ets +++ /dev/null @@ -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) - } -} \ No newline at end of file diff --git a/commons/basic/src/main/ets/utils/expert_request.ets b/commons/basic/src/main/ets/utils/expert_request.ets deleted file mode 100644 index 9e9219e..0000000 --- a/commons/basic/src/main/ets/utils/expert_request.ets +++ /dev/null @@ -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 - -export interface HdResponse { - code: number - message: string - data: T -} - -class xx_HdHttp { - baseURL: string - - constructor(options: HdRequestOptions) { - this.baseURL = options.baseURL || '' - } - - private request(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 - 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 - // 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(url: string, data?: Object): Promise> { - return this.request(url, http.RequestMethod.GET, data) - } - - post(url: string, data?: Object): Promise> { - return this.request(url, http.RequestMethod.POST, data) - } - - put(url: string, data?: Object): Promise> { - return this.request(url, http.RequestMethod.PUT, data) - } - - delete(url: string, data?: Object): Promise> { - return this.request(url, http.RequestMethod.DELETE, data) - } -} - - -export const hdHttps = new xx_HdHttp({ baseURL: '' }) \ No newline at end of file diff --git a/commons/basic/src/main/ets/utils/request.ets b/commons/basic/src/main/ets/utils/request.ets index 278c1c5..c20b78b 100644 --- a/commons/basic/src/main/ets/utils/request.ets +++ b/commons/basic/src/main/ets/utils/request.ets @@ -31,56 +31,6 @@ class HdHttp { this.baseURL = options.baseURL || '' } - private request1(path: string, method: http.RequestMethod = http.RequestMethod.GET, extraDatas:HashMap) { - 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 - 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(path: string, method: http.RequestMethod = http.RequestMethod.POST, extraDatas :HashMap) { const httpInstance = http.createHttp() @@ -155,6 +105,8 @@ class HdHttp { } + + get(url: string, data?: Object): Promise> { return this.requestafter(url, http.RequestMethod.GET, data) } diff --git a/features/mypage/src/main/ets/view/EditUserDataComp.ets b/features/mypage/src/main/ets/view/EditUserDataComp.ets index 347f178..a363c37 100644 --- a/features/mypage/src/main/ets/view/EditUserDataComp.ets +++ b/features/mypage/src/main/ets/view/EditUserDataComp.ets @@ -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(updateDataUrl, { - uuid: authStore.getUser().uuid, - userName: authStore.getUser().userName, - photo: base64String, - type:'2' - } as updateExtraData).then(async (res: HdResponse) => { - 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(updateDataUrl, { + // uuid: authStore.getUser().uuid, + // userName: authStore.getUser().userName, + // photo: base64String, + // type:'2' + // } as updateExtraData).then(async (res: HdResponse) => { + // 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,