import PageContainerBehavior from "../../../../core/behavior/page-container.behavior"; import { upload } from "../../../../lib/request/upload"; import { Post } from "../../../../lib/request/method"; // module/chats/pages/analysis/analysis.ts interface IAnalysisData { _lastResetTime: number; uploadList: Array<{ target: string; required: boolean; label: string; src: string; }>; thumbnail: string[]; original: string[]; status: boolean[]; _queue: Record; activeObj: Record; followObj: Record; workId: number; } interface IAnalysisProperties { messageType: number; } type IAnalysisInstance = WechatMiniprogram.Component.Instance< IAnalysisData, IAnalysisProperties, { handle(event: WechatMiniprogram.TouchEvent): void; _chooseMedia(index: number): Promise; _deleteMedia(index: number): void; _uploadMedia(index: number, src?: string): void; onSubmit(): Promise; } >; Component({ behaviors: [PageContainerBehavior], options: {}, properties: { messageType: { type: Number, value: 0 }, }, data: { _lastResetTime: 0, uploadList: [ { target: "tongueImgUrl", required: true, label: "舌面图", src: "../../assets/tongue-1.png", }, { target: "tongueBackImgUrl", required: true, label: "舌下图", src: "../../assets/tongue-2.png", }, { target: "faceImgUrl", required: false, label: "正面面部图", src: "../../assets/face-1.png", }, ], thumbnail: [] as string[], original: [] as string[], status: [false, false, false], _queue: {} as AnyObject, // 随访上传参数 activeObj: {}, followObj: {}, workId: 0, }, attached() { // 从存储中拿到之前的对话信息 this.setData({ followObj: wx.getStorageSync("followObj"), workId: wx.getStorageSync("workId"), }); }, methods: { handle(event: WechatMiniprogram.TouchEvent) { const { handle, index } = event.mark as AnyObject; console.log(handle, index, handle === "upload:delete", event.mark); switch (handle) { case "preview": break; case "upload": this._chooseMedia(index).then( (src) => src && this._uploadMedia(index, src) ); break; case "upload:delete": this._deleteMedia(index); break; } }, _chooseMedia(index: number) { return wx .chooseMedia({ count: 1, mediaType: ["image"], sourceType: ["album", "camera"], camera: "front", }) .then((res) => { const src = res.tempFiles[0].tempFilePath; this.setData({ [`thumbnail.${index}`]: src }); return src; }) .catch(() => null); }, _deleteMedia(index: number) { this.setData({ [`thumbnail.${index}`]: "", [`original.${index}`]: "", }); }, _uploadMedia(index: number, src?: string) { this.setData({ [`_queue.${index}`]: true }); src ??= this.data.thumbnail[index]; upload({ params: { name: "file", file: src! }, transform({ data }) { return (data).url; }, }) .then( (src) => { this.setData({ [`original.${index}`]: src }); }, (error) => { wx.showToast({ title: error?.errMsg ?? "上传失败", icon: "error" }); this.setData({ [`thumbnail.${index}`]: "", [`original.${index}`]: "", }); } ) .then(() => { this.setData({ [`_queue.${index}`]: false }); }); }, async onSubmit() { const submitBtn = this.selectComponent('#submitBtn'); const data = { thumbnail: [] as any, source: [] as any, }; for (let index = 0; index < this.data.uploadList.length; index++) { const item = this.data.uploadList[index]; if (this.data._queue[index]) { console.log('[Analysis] Upload in progress, resetting button'); wx.showToast({ title: `请等待图片上传完毕`, icon: "none" }); submitBtn?.resetState(); return; } else if (item.required && !this.data.original[index]) { console.log('[Analysis] Missing required image, resetting button'); wx.showToast({ title: `请上传${item.label}`, icon: "none" }); submitBtn?.resetState(); return; } if (this.data.original[index]) data.source.push({ target: item.target, src: this.data.original[index], }); if (this.data.thumbnail[index]) data.thumbnail.push({ target: item.target, src: this.data.thumbnail[index], }); } let imageObj: any = { upImg: this.data.original[0], downImg: this.data.original[1], faceImg: this.data.original[2], }; // console.log(this.data.followObj, "this.data.followObj"); this.setData({ activeObj: { ...imageObj, ...this.data.followObj }, }); // console.log({ ...this.data.activeObj }, "activeObj"); let isAnalysis: number; if (this.data.messageType === 2) { isAnalysis = wx.getStorageSync("isAnalysis"); } // console.log(this.data, "messageType", isAnalysis); if (isAnalysis === 2) { // 提交随访提醒 try { const res = await Post( `/followupTaskManage/updateFollowupTaskFillin/${this.data?.workId}`, { ...this.data.activeObj }, { transform({ data }: any) { return data; }, } ); // 存储舌面象分析报告id,用于跳转页面时最后产生结果查看 wx.setStorageSync( "tonguefaceAnalysisReportId", res.tonguefaceAnalysisReportId ); this.getOpenerEventChannel().emit("update", data); wx.navigateBack(); } catch (error) { console.log('[Analysis] Submit failed, resetting button'); wx.showToast({ title: error?.errMsg ?? "提交失败", icon: "none" }); submitBtn?.resetState(); } } else { console.log("对话管家",data) // 对话管家 this.getOpenerEventChannel().emit("update", data); wx.navigateBack(); } }, }, });