// import { Get } from "../../../../lib/request/method"; import { getQuestionnaireMethod, submitQuestionnaireMethod, } from "../../request"; export interface Questionnaire { id: number; // 满意度调研问卷ID name: string; //满意度调研问卷名称 startSentence: string; //开头语 endSentence: string; //结束语 suggestion: string; //其他意见 groups: Array<{ name: string; // 问题组名称 items: Array<{ name: string; score?: string; // 原始分数(可选) selectedScore?: number | string; // 用户选择的分数 }>; }>; } Page({ onLoad(options: { id?: string }) { console.log(options, "options"); // 如果没有传递 id,使用默认 id 1,或者可以根据实际情况调整 const questionnaireId = options.id ? Number(options.id) : 1; this.setData({ "followDetail.id": questionnaireId, }); console.log(this.data.followDetail.id, "this.data.followDetail.id"); console.log(questionnaireId, "questionnaireId"); if (questionnaireId) { this.getQuestionnaire(questionnaireId); } else { wx.showToast({ title: "问卷ID不能为空", icon: "none", duration: 2000, }); } }, data: { scoreList: [ { label: "1分", value: "1", }, { label: "2分", value: "2", }, { label: "3分", value: "3", }, { label: "4分", value: "4", }, { label: "5分", value: "5", }, ], followDetail: { id: 0, } as Questionnaire, loading: true, // 加载状态 showEmpty: false, // 显示空数据 saving: false, // 保存状态 suggestion: "", // 其他意见 }, async getQuestionnaire(id: number) { // 模拟后端数据(根据图片内容) // const mockData: Questionnaire = { // id: id, // name: "就诊体验满意度", // startSentence: // "感谢您选择我院就诊,为了持续改进医疗服务质量,我们诚挚地邀请您参与本次满意度调研。您的真实反馈对我们非常重要,我们将严格保密您的个人信息,请您根据实际就诊体验,花费几分钟时间完成本次调研。", // endSentence: // "再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量。再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量", // suggestion: "其他意见", // groups: [ // { // name: "一、就诊环境评价", // items: [ // { // name: "医院门诊/住院部的整洁度", // score: "1", // }, // { // name: "医院的通风情况", // score: "2", // }, // { // name: "医院的采光情况", // score: "3", // }, // { // name: "医院的噪音控制", // score: "4", // }, // { // name: "候诊区域的舒适度(座椅、空间等)", // score: "5", // }, // ], // }, // ], // }; // 模拟网络延迟 // await new Promise((resolve) => setTimeout(resolve, 500)); // this.setData({ // followDetail: mockData, // loading: false, // }); // 实际接口调用 let list = await getQuestionnaireMethod(id); console.log(list, "list"); const questionnaireData = list?.data as Questionnaire; // 确保 id 字段存在,如果后端没有返回 id,使用传入的 id const finalData = questionnaireData ? { ...questionnaireData, id: questionnaireData.id || id } : ({ id } as Questionnaire); this.setData({ loading: false, followDetail: finalData, showEmpty: JSON.stringify(list?.data) === "{}" ? true : false, }); console.log(this.data.followDetail, "this.data.followDetail"); }, // 选择分数(radio-group change 事件) onScoreChange(e: WechatMiniprogram.CustomEvent) { console.log(e, "改变分数"); const { groupIndex, itemIndex } = e.currentTarget.dataset; const score = e.detail.value; const groups = [...this.data.followDetail.groups]; groups[groupIndex].items[itemIndex].selectedScore = score; this.setData({ "followDetail.groups": groups, }); }, // 输入其他意见 onCommentInput(e: WechatMiniprogram.Input) { this.setData({ suggestion: e.detail.value, }); }, // 保存问卷 async onSave() { // 检查是否所有问题都已回答 const groups = this.data.followDetail?.groups || []; // let allAnswered = true; // let unansweredQuestions: string[] = []; console.log(groups, "groups"); // for (const group of groups) { // for (const item of group.items) { // if (!item.selectedScore) { // allAnswered = false; // unansweredQuestions.push(item.name); // } // } // } // if (!allAnswered) { // wx.showToast({ // title: "请完成所有问题的评分", // icon: "none", // duration: 2000, // }); // return; // } console.log(this.data.followDetail.id, "提交====="); // 准备提交数据 const submitData = { groups: groups.map((group) => ({ name: group.name, items: group.items.map((item) => ({ name: item.name, score: item.selectedScore ? String(item.selectedScore) : "", })), })), startSentence: this.data.followDetail.startSentence, endSentence: this.data.followDetail.endSentence, suggestion: this.data.suggestion, name: this.data.followDetail.name, }; console.log(submitData, "提交的数据==submitData"); this.setData({ saving: true, }); try { // 实际接口调用 await submitQuestionnaireMethod( this.data.followDetail.id, submitData as AnyObject ); wx.showToast({ title: "提交成功", icon: "success", duration: 2000, }); setTimeout(() => { wx.redirectTo({ url: "/pages/home/home", }); }, 2000); } catch (error) { wx.showToast({ title: "保存失败,请重试", icon: "none", duration: 2000, }); this.setData({ saving: false, }); } finally { this.setData({ saving: false, }); } }, });