import { authStore, BasicConstant, ChangePhotoGrids, ChangeUtil, hdHttp, HdLoadingDialog, HdNav, HdResponse, preferenceStore, ViewImageInfo } from "@itcast/basic" import { PhotoActionSheet } from '@itcast/basic' import { AnswerListBean } from "../model/ConsulModel" import { promptAction, router } from "@kit.ArkUI" import { PerfactInputSheet } from "@itcast/basic/src/main/ets/Views/PerfactInputSheet" import { BusinessError } from "@kit.BasicServicesKit" import { HashMap } from "@kit.ArkTS" import { StringIsEmpty } from "@nimkit/common" import { http } from "@kit.NetworkKit" import { TimestampBean } from "@itcast/basic/src/main/ets/utils/request" import { bundleManager } from "@kit.AbilityKit" import { rcp } from "@kit.RemoteCommunicationKit" import { CryptoJS } from '@ohos/crypto-js' @Component export struct MyOpinionComp { @State photos: string[] = [] @State base64Array:string[] = [] @State previewIndex: number = -1 @State maxSelectNumber: number = 6 private photoSheetDialog!: CustomDialogController; @State @Watch('onRemoveImg') removeImg: boolean=false @State text: string = '' @State @Watch('onAddImg') addImg: boolean=false @State params:param = router.getParams() as param; @State removeIndex: number=0 hashMap: HashMap = new HashMap(); hashMapImg: HashMap = new HashMap(); onAddImg() { this.photoSheetDialog.open() } onRemoveImg() { this.photos.splice(this.removeIndex, 1) this.maxSelectNumber =6- this.photos.length; ChangeUtil.convertUrisOrUrlsToBase64(this.photos).then(base64Array => { console.info('转换结果:', base64Array+'转换个数:'+base64Array.length) this.base64Array = base64Array }).catch((err:BusinessError) => { console.error('批量转换失败:', err) }) } private custom!:CustomDialogController; dialog: CustomDialogController = new CustomDialogController({ builder: HdLoadingDialog({ message: '加载中...' }), customStyle: true, alignment: DialogAlignment.Center }) initDialog() { this.custom = new CustomDialogController({ builder:PerfactInputSheet({ controller:this.custom, inputTitle:'提示', okText:'保存', needcancelCallBack:true, okColor:$r('app.color.top_title'), inputPlaceholder:'您有未发布的内容,是否保存?', style:'2', inputCallBack:(input: string,title:string)=>{ if(title=='needcancelCallBack') { preferenceStore.setItemString('MyOpinionComp'+this.params.uuid,'') } else { preferenceStore.setItemString('MyOpinionComp'+this.params.uuid,this.text) } router.back() } }), alignment: DialogAlignment.Center, customStyle: true, autoCancel: false, backgroundColor: ('rgba(0,0,0,0.5)'), height: '100%' }) } private initPhotoDialog() { this.photoSheetDialog = new CustomDialogController({ builder: PhotoActionSheet({ controller: this.photoSheetDialog, maxSelectNumber:this.maxSelectNumber, // 修改为支持多选 onPhotoSelected: async (uris: string[] | string) => { let selectedUris: string[] = []; if (Array.isArray(uris)) { selectedUris = uris; } else if (typeof uris === 'string') { selectedUris = [uris]; } this.photos.push(...selectedUris); this.maxSelectNumber = 6- this.photos.length; ChangeUtil.convertUrisOrUrlsToBase64(this.photos).then(base64Array => { console.info('转换结果:', base64Array+'转换个数:'+base64Array.length) this.base64Array = base64Array }).catch((err:BusinessError) => { console.error('批量转换失败:', err) }) } }), alignment: DialogAlignment.Bottom, customStyle: true, autoCancel: false, backgroundColor: ('rgba(0,0,0,0.5)'), height: '100%' }); } aboutToAppear(): void { console.log('Response aboutToAppear') this.initPhotoDialog() this.initDialog() if(this.params.isHistory =='true') { this.text=this.params.myAnswer.note if(this.params.myAnswer.imgs!=null) { this.photos.push(...this.changeToImgs(this.params.myAnswer.imgs.split(","))) this.maxSelectNumber = 6- this.photos.length; ChangeUtil.convertUrisOrUrlsToBase64(this.changeToImgs(this.params.myAnswer.imgs.split(","))).then(base64Array => { console.info('转换结果:', base64Array+'转换个数:'+base64Array.length) this.base64Array = base64Array }).catch((err:BusinessError) => { console.error('批量转换失败:', err) }) } } else { this.text=preferenceStore.getItemString('MyOpinionComp'+this.params.uuid) } } build() { Column() { HdNav({ title: '我的意见', showRightIcon: false, showLeftIcon: true ,isLeftAction:true,leftItemAction:()=>{ if(this.params.isHistory =='false') { if(this.text.length>0) { this.custom.open() } else { router.back() } } else { router.back() } }}) Text('我的意见 *').fontSize(17).fontColor($r('app.color.top_title')).padding(10).width('100%').textAlign(TextAlign.Start) TextArea({ placeholder: '请依据患者的个人信息、疾病资料及患者所咨询的问题详细解答患者的问题(信息仅提问患者及医生可见、最多输入300个字)', text: $$this.text }) .fontColor($r('app.color.common_gray_03')) .height(100) .textAlign(TextAlign.Start) .fontSize(14).padding(9).margin({left:10,right:10,bottom:10}) .backgroundColor($r('app.color.f6f6f6')).borderRadius(8) Text('相关图片').fontSize(17).fontColor($r('app.color.top_title')).padding({left:10,right:10,bottom:10}).width('100%').textAlign(TextAlign.Start) Text('可以用部分科普或文献来协助问答问题,最多6张').fontSize(14).fontColor($r('app.color.999999')).padding({left:10,right:10,bottom:10}).width('100%').textAlign(TextAlign.Start) ChangePhotoGrids({imgList:this.changeToImg(this.photos),maxSelectNumber:6 ,addImg:this.addImg,removeImg:this.removeImg,removeIndex:this.removeIndex}).layoutWeight(1) Column() { Button({ type: ButtonType.Normal }){ Text('提交') } .width('100%') .height(53) .backgroundColor($r('app.color.patient_theme')) .fontColor(Color.White) .onClick(() => { if(StringIsEmpty(this.text)||this.text.length<5) { promptAction.showToast({ message: '意见至少5个字' }) return } if(this.params.isHistory =='false') { this.addInterrogationAnswer() } else { this.updateInterrogationAnswer() } }) } .backgroundColor(Color.White) .width('100%') } .height('100%') .width('100%') } async getUploadImg() { this.hashMapImg.clear(); if(this.photos.length>0) { for (let index = 0; index < this.photos.length; index++) { if(this.photos[index].includes('http')) { this.hashMapImg.set('img'+index+1,await ChangeUtil.getImageBase64(this.photos[index])) } else { this.hashMapImg.set('img'+index+1, await ChangeUtil.convertUriToBase64(this.photos[index])) } } } return this.hashMapImg } async updateInterrogationAnswer() { this.dialog.open() this.hashMap.clear(); this.hashMap.set("note",this.text); this.hashMap.set('uuid', this.params.uuid) this.hashMap.set('imgsBean',this.assembleBase64Images(this.base64Array)) hdHttp.httpReqObject(BasicConstant.updateInterrogationAnswer,this.hashMap).then(async (res: HdResponse) => { this.dialog.close() let json:Record = JSON.parse(res+'') as Record; promptAction.showToast({ message: json.message }) if(json.code == '200') { router.back() } }).catch((err: BusinessError) => { this.dialog.close() }) // let req = new rcp.Request("http://example.com", "POST", headers, simpleForm, cookies, transferRange, configuration); } addInterrogationAnswer() { this.dialog.open() this.hashMap.clear(); this.hashMap.set("note",this.text); this.hashMap.set('step1_uuid', this.params.step1_uuid) this.hashMap.set('imgsBean',this.assembleBase64Images(this.base64Array)) hdHttp.httpReqObject(BasicConstant.addInterrogationAnswer,this.hashMap).then(async (res: HdResponse) => { this.dialog.close() let json:Record = JSON.parse(res+'') as Record; promptAction.showToast({ message: json.message }) if(json.code == '200') { router.back() } }).catch((err: BusinessError) => { this.dialog.close() }) // this.httpReqImg(BasicConstant.addInterrogationAnswer) } changeToImg( imgListurl:string[]) { let imgListtmps:ViewImageInfo[]=[] imgListurl.forEach((url: string) => { let item = {uri:url} as ViewImageInfo imgListtmps.push(item) }) return imgListtmps } changeToImgs( imgListurl:string[]) { let imgListtmps:string[]=[] imgListurl.forEach((url: string) => { let item = BasicConstant.urlHtml + url imgListtmps.push(item) }) return imgListtmps } /** * 将base64数组组装为对象 * @param base64Array base64字符串数组 * @param maxImages 最大图片数量,默认8 * @returns 包含img1~imgN字段的对象 */ assembleBase64Images( base64Array: string[], maxImages: number = 6 ): Record { const imgObject: Record = {}; // 确定实际处理的图片数量 const processCount = Math.min(base64Array.length, maxImages); for (let i = 0; i < processCount; i++) { const originalBase64 = base64Array[i]; // 清理base64前缀[9,10](@ref) const cleanBase64 = originalBase64.replace( /^data:image\/\w+;base64,/i, '' ); // 生成字段名 const fieldName = `img${i + 1}`; imgObject[fieldName] = cleanBase64; } return imgObject; } async getVersion() { let res = await bundleManager.getBundleInfoForSelf( bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA ) return res.versionName } } interface param{ uuid:string, isHistory:string, myAnswer:AnswerListBean step1_uuid:string }