test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. data() {
  5. return {
  6. questions: [
  7. { id: 1, question: "问题1", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  8. { id: 2, question: "问题2", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  9. { id: 3, question: "问题3", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  10. { id: 4, question: "问题4", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  11. { id: 5, question: "问题5", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  12. { id: 6, question: "问题6", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  13. { id: 7, question: "问题7", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  14. { id: 8, question: "问题8", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  15. { id: 9, question: "问题9", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  16. { id: 10, question: "问题10", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  17. { id: 11, question: "问题11", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  18. { id: 12, question: "问题12", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  19. { id: 13, question: "问题13", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] },
  20. { id: 14, question: "问题14", options: [{ value: "A", label: "选项A" }, { value: "B", label: "选项B" }, { value: "c", label: "选项c" }, { value: "D", label: "选项D" }] }
  21. // 更多问题...
  22. ],
  23. selectedAnswers: [],
  24. scrollTop: 0,
  25. currentQuestionIndex: 0,
  26. scrollHeight: 0,
  27. scrollDelay: 500,
  28. questionPositions: []
  29. // 用于存储每个问题的位置
  30. };
  31. },
  32. onLoad() {
  33. const res = common_vendor.index.getSystemInfoSync();
  34. this.scrollHeight = res.windowHeight;
  35. setTimeout(() => {
  36. this.calculateQuestionPositions();
  37. }, 300);
  38. },
  39. methods: {
  40. selectAnswer(index, value) {
  41. this.$set(this.selectedAnswers, index, value);
  42. if (index < this.questions.length - 1) {
  43. setTimeout(() => {
  44. this.currentQuestionIndex = index + 1;
  45. this.scrollToQuestion(this.currentQuestionIndex);
  46. }, this.scrollDelay);
  47. } else {
  48. setTimeout(() => {
  49. this.scrollToBottom();
  50. }, this.scrollDelay);
  51. }
  52. },
  53. goToNext(index) {
  54. if (index < this.questions.length - 1) {
  55. this.currentQuestionIndex = index + 1;
  56. this.scrollToQuestion(this.currentQuestionIndex);
  57. }
  58. },
  59. calculateQuestionPositions() {
  60. common_vendor.index.createSelectorQuery().selectAll(".question-item").boundingClientRect((rects) => {
  61. if (rects) {
  62. let accumulatedHeight = 0;
  63. this.questionPositions = rects.map((rect) => {
  64. accumulatedHeight += rect.height;
  65. return accumulatedHeight - rect.height;
  66. });
  67. }
  68. }).exec();
  69. },
  70. scrollToQuestion(index) {
  71. if (this.questionPositions[index] !== void 0) {
  72. this.scrollTop = this.questionPositions[index];
  73. }
  74. },
  75. scrollToBottom() {
  76. const lastIndex = this.questions.length - 1;
  77. if (this.questionPositions[lastIndex] !== void 0) {
  78. this.scrollTop = this.questionPositions[lastIndex] + 200;
  79. }
  80. },
  81. submit() {
  82. common_vendor.index.showToast({
  83. title: "提交成功",
  84. icon: "success"
  85. });
  86. }
  87. }
  88. };
  89. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  90. return {
  91. a: common_vendor.t($data.currentQuestionIndex + 1),
  92. b: common_vendor.t($data.questions.length),
  93. c: common_vendor.f($data.questions, (item, index, i0) => {
  94. return common_vendor.e({
  95. a: common_vendor.t(item.question),
  96. b: common_vendor.f(item.options, (option, k1, i1) => {
  97. return {
  98. a: common_vendor.t(option.label),
  99. b: option.value,
  100. c: common_vendor.o(($event) => $options.selectAnswer(index, option.value), option.value),
  101. d: $data.selectedAnswers[index] === option.value ? 1 : ""
  102. };
  103. }),
  104. c: index === $data.questions.length - 1 && $data.selectedAnswers[index]
  105. }, index === $data.questions.length - 1 && $data.selectedAnswers[index] ? {
  106. d: common_vendor.o((...args) => $options.submit && $options.submit(...args), item.id)
  107. } : {}, {
  108. e: item.id,
  109. f: "question-" + index
  110. });
  111. }),
  112. d: $data.scrollTop,
  113. e: $data.scrollHeight + "px"
  114. };
  115. }
  116. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  117. wx.createPage(MiniProgramPage);