camera.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. interviewCompleted: false
  65. };
  66. },
  67. computed: {
  68. currentQuestion() {
  69. return this.questions[this.currentQuestionIndex];
  70. }
  71. },
  72. onReady() {
  73. this.cameraContext = common_vendor.index.createCameraContext();
  74. if (this.useVideo) {
  75. this.aiVideoContext = common_vendor.index.createVideoContext("aiInterviewer");
  76. }
  77. this.startTimer();
  78. this.totalQuestions = this.questions.length;
  79. },
  80. methods: {
  81. startTimer() {
  82. let seconds = 30;
  83. this.timerInterval = setInterval(() => {
  84. seconds--;
  85. if (seconds <= 0) {
  86. clearInterval(this.timerInterval);
  87. if (!this.showResult) {
  88. this.checkAnswer();
  89. }
  90. }
  91. const min = Math.floor(seconds / 60).toString().padStart(2, "0");
  92. const sec = (seconds % 60).toString().padStart(2, "0");
  93. this.remainingTime = `${min}:${sec}`;
  94. }, 1e3);
  95. },
  96. resetTimer() {
  97. clearInterval(this.timerInterval);
  98. this.startTimer();
  99. },
  100. selectOption(index) {
  101. if (this.showResult)
  102. return;
  103. this.selectedOption = index;
  104. this.playAiSpeaking();
  105. },
  106. checkAnswer() {
  107. clearInterval(this.timerInterval);
  108. if (!this.currentQuestion) {
  109. console.error("当前问题不存在");
  110. return;
  111. }
  112. this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
  113. if (this.isAnswerCorrect) {
  114. this.score++;
  115. }
  116. if (this.currentQuestionIndex === this.questions.length - 1) {
  117. this.showEndModal = false;
  118. this.interviewCompleted = true;
  119. return;
  120. }
  121. this.goToNextQuestion();
  122. },
  123. nextQuestion() {
  124. if (this.selectedOption !== null) {
  125. this.checkAnswer();
  126. return;
  127. }
  128. },
  129. // 新增方法,处理进入下一题的逻辑
  130. goToNextQuestion() {
  131. this.showResult = false;
  132. this.selectedOption = null;
  133. this.currentQuestionIndex++;
  134. if (this.currentQuestionIndex >= this.questions.length) {
  135. this.showEndModal = false;
  136. this.interviewCompleted = true;
  137. if (this.timerInterval) {
  138. clearInterval(this.timerInterval);
  139. }
  140. return;
  141. }
  142. this.resetTimer();
  143. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  144. this.playAiSpeaking();
  145. setTimeout(() => {
  146. this.pauseAiSpeaking();
  147. }, 2e3);
  148. },
  149. toggleSettings() {
  150. common_vendor.index.showToast({
  151. title: "设置功能开发中",
  152. icon: "none"
  153. });
  154. },
  155. back() {
  156. if (this.timerInterval) {
  157. clearInterval(this.timerInterval);
  158. }
  159. try {
  160. const pages = getCurrentPages();
  161. if (pages.length > 1) {
  162. common_vendor.index.reLaunch({
  163. url: "/pages/index/index"
  164. });
  165. } else {
  166. common_vendor.index.reLaunch({
  167. url: "/pages/index/index"
  168. });
  169. }
  170. } catch (e) {
  171. console.error("导航错误:", e);
  172. common_vendor.index.reLaunch({
  173. url: "/pages/index/index"
  174. });
  175. }
  176. },
  177. error(e) {
  178. console.error(e.detail);
  179. common_vendor.index.showToast({
  180. title: "相机启动失败,请检查权限设置",
  181. icon: "none"
  182. });
  183. },
  184. playAiSpeaking() {
  185. if (this.useVideo && this.aiVideoContext) {
  186. this.aiVideoContext.play();
  187. }
  188. },
  189. pauseAiSpeaking() {
  190. if (this.useVideo && this.aiVideoContext) {
  191. this.aiVideoContext.pause();
  192. }
  193. },
  194. // 重新开始测试
  195. restartTest() {
  196. this.currentQuestionIndex = 0;
  197. this.score = 0;
  198. this.showEndModal = false;
  199. this.showResult = false;
  200. this.selectedOption = null;
  201. this.resetTimer();
  202. },
  203. // 在methods中添加测试方法
  204. testEndScreen() {
  205. this.interviewCompleted = true;
  206. this.showEndModal = false;
  207. }
  208. },
  209. // 添加生命周期钩子,确保在组件销毁时清除计时器
  210. beforeDestroy() {
  211. if (this.timerInterval) {
  212. clearInterval(this.timerInterval);
  213. }
  214. }
  215. };
  216. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  217. return common_vendor.e({
  218. a: common_assets._imports_0,
  219. b: common_vendor.t($data.currentQuestionIndex + 1),
  220. c: common_vendor.t($options.currentQuestion.id),
  221. d: common_vendor.t($data.questions.length),
  222. e: $options.currentQuestion.isImportant
  223. }, $options.currentQuestion.isImportant ? {} : {}, {
  224. f: common_vendor.t($options.currentQuestion.text),
  225. g: common_vendor.f($options.currentQuestion.options, (option, index, i0) => {
  226. return {
  227. a: common_vendor.t(option),
  228. b: index,
  229. c: $data.selectedOption === index ? 1 : "",
  230. d: $data.showResult && index === $options.currentQuestion.correctAnswer ? 1 : "",
  231. e: $data.showResult && $data.selectedOption === index && index !== $options.currentQuestion.correctAnswer ? 1 : "",
  232. f: common_vendor.o(($event) => $options.selectOption(index), index)
  233. };
  234. }),
  235. h: common_vendor.t($data.remainingTime),
  236. i: common_vendor.o((...args) => $options.nextQuestion && $options.nextQuestion(...args)),
  237. j: $data.selectedOption === null,
  238. k: $data.showEndModal
  239. }, $data.showEndModal ? {
  240. l: common_vendor.t($data.score),
  241. m: common_vendor.t($data.totalQuestions),
  242. n: common_vendor.t($data.score),
  243. o: common_vendor.t($data.totalQuestions),
  244. p: common_vendor.o((...args) => $options.restartTest && $options.restartTest(...args)),
  245. q: common_vendor.o((...args) => $options.back && $options.back(...args))
  246. } : {}, {
  247. r: $data.interviewCompleted
  248. }, $data.interviewCompleted ? {
  249. s: common_assets._imports_0,
  250. t: common_vendor.o((...args) => $options.back && $options.back(...args))
  251. } : {});
  252. }
  253. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  254. wx.createPage(MiniProgramPage);