questionnaire.ts 6.8 KB

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