questionnaire.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {
  2. getQuestionnaireMethod,
  3. submitQuestionnaireMethod,
  4. } from "../../request";
  5. export interface Questionnaire {
  6. id: number; // 满意度调研问卷ID
  7. name: string; //满意度调研问卷名称
  8. startSentence: string; //开头语
  9. endSentence: string; //结束语
  10. suggestion: string; //其他意见
  11. groups: Array<{
  12. name: string; // 问题组名称
  13. items: Array<{
  14. name: string;
  15. score?: string; // 原始分数(可选)
  16. selectedScore?: number | string; // 用户选择的分数
  17. }>;
  18. }>;
  19. }
  20. Page({
  21. onLoad(options: { id?: string }) {
  22. // 如果没有传递 id,使用默认 id 1,或者可以根据实际情况调整
  23. const questionnaireId = options.id ? Number(options.id) : 1;
  24. this.setData({
  25. "followDetail.id": questionnaireId,
  26. });
  27. if (questionnaireId) {
  28. this.getQuestionnaire(questionnaireId);
  29. } else {
  30. wx.showToast({
  31. title: "问卷ID不能为空",
  32. icon: "none",
  33. duration: 2000,
  34. });
  35. }
  36. },
  37. data: {
  38. scoreList: [
  39. {
  40. label: "1分",
  41. value: "1",
  42. },
  43. {
  44. label: "2分",
  45. value: "2",
  46. },
  47. {
  48. label: "3分",
  49. value: "3",
  50. },
  51. {
  52. label: "4分",
  53. value: "4",
  54. },
  55. {
  56. label: "5分",
  57. value: "5",
  58. },
  59. ],
  60. followDetail: {
  61. id: 0,
  62. } as Questionnaire,
  63. loading: true, // 加载状态
  64. showEmpty: false, // 显示空数据
  65. saving: false, // 保存状态
  66. suggestion: "", // 其他意见
  67. },
  68. async getQuestionnaire(id: number) {
  69. // 模拟后端数据(根据图片内容)
  70. // const mockData: Questionnaire = {
  71. // id: id,
  72. // name: "就诊体验满意度",
  73. // startSentence:
  74. // "感谢您选择我院就诊,为了持续改进医疗服务质量,我们诚挚地邀请您参与本次满意度调研。您的真实反馈对我们非常重要,我们将严格保密您的个人信息,请您根据实际就诊体验,花费几分钟时间完成本次调研。",
  75. // endSentence:
  76. // "再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量。再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量再次感谢您的参与,我们会根据您的反馈持续改进服务质量",
  77. // suggestion: "其他意见",
  78. // groups: [
  79. // {
  80. // name: "一、就诊环境评价",
  81. // items: [
  82. // {
  83. // name: "医院门诊/住院部的整洁度",
  84. // score: "1",
  85. // },
  86. // {
  87. // name: "医院的通风情况",
  88. // score: "2",
  89. // },
  90. // {
  91. // name: "医院的采光情况",
  92. // score: "3",
  93. // },
  94. // {
  95. // name: "医院的噪音控制",
  96. // score: "4",
  97. // },
  98. // {
  99. // name: "候诊区域的舒适度(座椅、空间等)",
  100. // score: "5",
  101. // },
  102. // ],
  103. // },
  104. // ],
  105. // };
  106. // 模拟网络延迟
  107. // await new Promise((resolve) => setTimeout(resolve, 500));
  108. // this.setData({
  109. // followDetail: mockData,
  110. // loading: false,
  111. // });
  112. // 实际接口调用
  113. let list = await getQuestionnaireMethod(id);
  114. const questionnaireData = list?.data as Questionnaire;
  115. // 确保 id 字段存在,如果后端没有返回 id,使用传入的 id
  116. const finalData = questionnaireData
  117. ? { ...questionnaireData, id: questionnaireData.id || id }
  118. : ({ id } as Questionnaire);
  119. this.setData({
  120. loading: false,
  121. followDetail: finalData,
  122. showEmpty: JSON.stringify(list?.data) === "{}" ? true : false,
  123. });
  124. },
  125. // 选择分数(radio-group change 事件)
  126. onScoreChange(e: WechatMiniprogram.CustomEvent) {
  127. const { groupIndex, itemIndex } = e.currentTarget.dataset;
  128. const score = e.detail.value;
  129. const groups = [...this.data.followDetail.groups];
  130. groups[groupIndex].items[itemIndex].selectedScore = score;
  131. this.setData({
  132. "followDetail.groups": groups,
  133. });
  134. },
  135. // 输入其他意见
  136. onCommentInput(e: WechatMiniprogram.Input) {
  137. this.setData({
  138. suggestion: e.detail.value,
  139. });
  140. },
  141. // 保存问卷
  142. async onSave() {
  143. // 检查是否所有问题都已回答
  144. const groups = this.data.followDetail?.groups || [];
  145. // let allAnswered = true;
  146. // let unansweredQuestions: string[] = [];
  147. // for (const group of groups) {
  148. // for (const item of group.items) {
  149. // if (!item.selectedScore) {
  150. // allAnswered = false;
  151. // unansweredQuestions.push(item.name);
  152. // }
  153. // }
  154. // }
  155. // if (!allAnswered) {
  156. // wx.showToast({
  157. // title: "请完成所有问题的评分",
  158. // icon: "none",
  159. // duration: 2000,
  160. // });
  161. // return;
  162. // }
  163. // 准备提交数据
  164. const submitData = {
  165. groups: groups.map((group) => ({
  166. name: group.name,
  167. items: group.items.map((item) => ({
  168. name: item.name,
  169. score: item.selectedScore ? String(item.selectedScore) : "",
  170. })),
  171. })),
  172. startSentence: this.data.followDetail.startSentence,
  173. endSentence: this.data.followDetail.endSentence,
  174. suggestion: this.data.suggestion,
  175. name: this.data.followDetail.name,
  176. };
  177. this.setData({
  178. saving: true,
  179. });
  180. try {
  181. // 实际接口调用
  182. await submitQuestionnaireMethod(
  183. this.data.followDetail.id,
  184. submitData as AnyObject
  185. );
  186. wx.showToast({
  187. title: "提交成功",
  188. icon: "success",
  189. duration: 2000,
  190. });
  191. setTimeout(() => {
  192. wx.redirectTo({
  193. url: "/pages/home/home",
  194. });
  195. }, 2000);
  196. } catch (error) {
  197. wx.showToast({
  198. title: "保存失败,请重试",
  199. icon: "none",
  200. duration: 2000,
  201. });
  202. this.setData({
  203. saving: false,
  204. });
  205. } finally {
  206. this.setData({
  207. saving: false,
  208. });
  209. }
  210. },
  211. });