camera.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_assets = require("../../common/assets.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. cameraContext: null,
  8. devicePosition: "front",
  9. interviewStarted: true,
  10. showEndModal: false,
  11. currentQuestionIndex: 0,
  12. currentStep: 1,
  13. stepTexts: ["测试准备", "回答问题", "测试结束"],
  14. progressWidth: 50,
  15. remainingTime: "00:27",
  16. selectedOption: null,
  17. showResult: false,
  18. isAnswerCorrect: false,
  19. questions: [
  20. {
  21. id: 6,
  22. text: "以下不属于中国传统节日的是( )。",
  23. options: [
  24. "A. 春节",
  25. "B. 端午节",
  26. "C. 重阳节",
  27. "D. 元旦"
  28. ],
  29. correctAnswer: 3,
  30. isImportant: true,
  31. explanation: "元旦是公历新年,属于现代节日,而春节、端午节和重阳节都是中国传统节日。"
  32. },
  33. {
  34. id: 7,
  35. text: "下列哪个是中国四大名著之一( )。",
  36. options: [
  37. "A. 聊斋志异",
  38. "B. 西游记",
  39. "C. 世说新语",
  40. "D. 聊斋志异"
  41. ],
  42. correctAnswer: 1,
  43. isImportant: false,
  44. explanation: "中国四大名著是《红楼梦》、《西游记》、《水浒传》和《三国演义》。"
  45. },
  46. {
  47. id: 8,
  48. text: '中国传统文化中"仁义礼智信"五常不包括( )。',
  49. options: [
  50. "A. 忠",
  51. "B. 孝",
  52. "C. 礼",
  53. "D. 信"
  54. ],
  55. correctAnswer: 1,
  56. isImportant: true,
  57. explanation: '儒家的五常是"仁、义、礼、智、信",不包括"孝"。'
  58. }
  59. ],
  60. useVideo: false,
  61. timerInterval: null,
  62. score: 0,
  63. totalQuestions: 0
  64. };
  65. },
  66. computed: {
  67. currentQuestion() {
  68. return this.questions[this.currentQuestionIndex];
  69. }
  70. },
  71. onReady() {
  72. this.cameraContext = common_vendor.index.createCameraContext();
  73. if (this.useVideo) {
  74. this.aiVideoContext = common_vendor.index.createVideoContext("aiInterviewer");
  75. }
  76. this.startTimer();
  77. this.totalQuestions = this.questions.length;
  78. },
  79. methods: {
  80. startTimer() {
  81. let seconds = 30;
  82. this.timerInterval = setInterval(() => {
  83. seconds--;
  84. if (seconds <= 0) {
  85. clearInterval(this.timerInterval);
  86. if (!this.showResult) {
  87. this.checkAnswer();
  88. }
  89. }
  90. const min = Math.floor(seconds / 60).toString().padStart(2, "0");
  91. const sec = (seconds % 60).toString().padStart(2, "0");
  92. this.remainingTime = `${min}:${sec}`;
  93. }, 1e3);
  94. },
  95. resetTimer() {
  96. clearInterval(this.timerInterval);
  97. this.startTimer();
  98. },
  99. selectOption(index) {
  100. if (this.showResult)
  101. return;
  102. this.selectedOption = index;
  103. this.playAiSpeaking();
  104. },
  105. checkAnswer() {
  106. clearInterval(this.timerInterval);
  107. if (!this.currentQuestion) {
  108. console.error("当前问题不存在");
  109. return;
  110. }
  111. this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
  112. if (this.isAnswerCorrect) {
  113. this.score++;
  114. }
  115. this.goToNextQuestion();
  116. },
  117. nextQuestion() {
  118. if (this.selectedOption !== null) {
  119. this.checkAnswer();
  120. return;
  121. }
  122. },
  123. // 新增方法,处理进入下一题的逻辑
  124. goToNextQuestion() {
  125. this.showResult = false;
  126. this.selectedOption = null;
  127. this.currentQuestionIndex++;
  128. if (this.currentQuestionIndex >= this.questions.length) {
  129. this.showEndModal = true;
  130. return;
  131. }
  132. this.resetTimer();
  133. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  134. this.playAiSpeaking();
  135. setTimeout(() => {
  136. this.pauseAiSpeaking();
  137. }, 2e3);
  138. },
  139. toggleSettings() {
  140. common_vendor.index.showToast({
  141. title: "设置功能开发中",
  142. icon: "none"
  143. });
  144. },
  145. back() {
  146. clearInterval(this.timerInterval);
  147. common_vendor.index.navigateBack();
  148. },
  149. error(e) {
  150. console.error(e.detail);
  151. common_vendor.index.showToast({
  152. title: "相机启动失败,请检查权限设置",
  153. icon: "none"
  154. });
  155. },
  156. playAiSpeaking() {
  157. if (this.useVideo && this.aiVideoContext) {
  158. this.aiVideoContext.play();
  159. }
  160. },
  161. pauseAiSpeaking() {
  162. if (this.useVideo && this.aiVideoContext) {
  163. this.aiVideoContext.pause();
  164. }
  165. },
  166. // 重新开始测试
  167. restartTest() {
  168. this.currentQuestionIndex = 0;
  169. this.score = 0;
  170. this.showEndModal = false;
  171. this.showResult = false;
  172. this.selectedOption = null;
  173. this.resetTimer();
  174. }
  175. },
  176. // 添加生命周期钩子,确保在组件销毁时清除计时器
  177. beforeDestroy() {
  178. if (this.timerInterval) {
  179. clearInterval(this.timerInterval);
  180. }
  181. }
  182. };
  183. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  184. return common_vendor.e({
  185. a: common_assets._imports_0,
  186. b: common_vendor.t($data.currentQuestionIndex + 1),
  187. c: common_vendor.t($options.currentQuestion.id),
  188. d: common_vendor.t($data.questions.length),
  189. e: $options.currentQuestion.isImportant
  190. }, $options.currentQuestion.isImportant ? {} : {}, {
  191. f: common_vendor.t($options.currentQuestion.text),
  192. g: common_vendor.f($options.currentQuestion.options, (option, index, i0) => {
  193. return {
  194. a: common_vendor.t(option),
  195. b: index,
  196. c: $data.selectedOption === index ? 1 : "",
  197. d: $data.showResult && index === $options.currentQuestion.correctAnswer ? 1 : "",
  198. e: $data.showResult && $data.selectedOption === index && index !== $options.currentQuestion.correctAnswer ? 1 : "",
  199. f: common_vendor.o(($event) => $options.selectOption(index), index)
  200. };
  201. }),
  202. h: common_vendor.t($data.remainingTime),
  203. i: common_vendor.o((...args) => $options.nextQuestion && $options.nextQuestion(...args)),
  204. j: $data.selectedOption === null,
  205. k: $data.showEndModal
  206. }, $data.showEndModal ? {
  207. l: common_vendor.t($data.score),
  208. m: common_vendor.t($data.totalQuestions),
  209. n: common_vendor.t($data.score),
  210. o: common_vendor.t($data.totalQuestions),
  211. p: common_vendor.o((...args) => $options.restartTest && $options.restartTest(...args)),
  212. q: common_vendor.o((...args) => $options.back && $options.back(...args))
  213. } : {});
  214. }
  215. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  216. wx.createPage(MiniProgramPage);