"use strict"; const common_vendor = require("../../common/vendor.js"); const api_user = require("../../api/user.js"); const common_assets = require("../../common/assets.js"); const _sfc_main = { data() { return { cameraContext: null, devicePosition: "front", interviewStarted: true, showEndModal: false, currentQuestionIndex: 0, currentStep: 1, stepTexts: ["测试准备", "回答问题", "测试结束"], progressWidth: 50, remainingTime: "00:27", selectedOption: null, selectedOptions: [], showResult: false, isAnswerCorrect: false, questions: [], // 改为空数组,将通过API获取 interviewId: null, // 存储当前面试ID useVideo: false, timerInterval: null, score: 0, totalQuestions: 0, interviewCompleted: false, digitalHumanUrl: "", // 数字人URL loading: true, // 添加加载状态 loadError: false, // 添加加载错误状态 errorMessage: "" // 添加错误消息 }; }, computed: { currentQuestion() { console.log(this.questions[this.currentQuestionIndex]); return this.questions[this.currentQuestionIndex]; } }, onLoad(options) { if (options && options.id) { this.interviewId = options.id; this.fetchInterviewData(); } else { this.fetchInterviewList(); } }, onReady() { this.cameraContext = common_vendor.index.createCameraContext(); if (this.useVideo) { this.aiVideoContext = common_vendor.index.createVideoContext("aiInterviewer"); } this.initDigitalHuman(); }, methods: { // 获取面试列表 async fetchInterviewList() { try { this.loading = true; const res = await api_user.getInterviewList(); console.log(res); this.interviewId = res.items[0].id; this.fetchInterviewData(); } catch (error) { console.error("获取面试列表失败:", error); this.handleLoadError("获取面试列表失败"); } }, // 获取面试详情数据 async fetchInterviewData() { try { this.loading = true; const res = await api_user.getInterviewDetail({ id: this.interviewId }); console.log("API返回数据:", res); if (res && Array.isArray(res.items)) { this.questions = res.items.map((q, index) => ({ id: q.id || index + 1, text: q.question || "未知问题", options: q.options || [], correctAnswer: q.correctAnswer || 0, isImportant: q.is_system || false, explanation: q.explanation || "", questionType: q.question_form || 1, questionTypeName: q.question_form_name || "单选题", correctAnswers: q.correct_answers || [], difficulty: q.difficulty || 1, difficultyName: q.difficulty_name || "初级" })); } else { this.processInterviewData(res); } console.log(this.questions); this.totalQuestions = this.questions.length; if (this.questions.length > 0) { this.startTimer(); } } catch (error) { console.error("获取面试详情失败:", error); this.handleLoadError("获取面试详情失败"); } finally { this.loading = false; } }, // 处理面试数据 processInterviewData(data) { this.questions = []; if (data) { const formattedQuestion = { id: data.id || 1, text: data.question || "未知问题", options: data.options || [], correctAnswer: data.correctAnswer || 0, isImportant: data.is_system || false, explanation: data.explanation || "", questionType: data.question_form || 1, // 1-单选题,2-多选题 questionTypeName: data.question_form_name || "单选题", correctAnswers: data.correct_answers || [], difficulty: data.difficulty || 1, difficultyName: data.difficulty_name || "初级" }; this.questions.push(formattedQuestion); this.totalQuestions = this.questions.length; this.startTimer(); } else { this.handleLoadError("面试中没有问题"); } }, // 处理加载错误 handleLoadError(message) { this.loadError = true; this.loading = false; this.errorMessage = message || "加载失败"; common_vendor.index.showToast({ title: message || "加载失败", icon: "none", duration: 2e3 }); }, startTimer() { if (this.questions.length === 0) return; let seconds = 30; this.timerInterval = setInterval(() => { seconds--; if (seconds <= 0) { clearInterval(this.timerInterval); if (!this.showResult) { this.checkAnswer(); } } const min = Math.floor(seconds / 60).toString().padStart(2, "0"); const sec = (seconds % 60).toString().padStart(2, "0"); this.remainingTime = `${min}:${sec}`; }, 1e3); }, resetTimer() { clearInterval(this.timerInterval); this.startTimer(); }, selectOption(index) { if (this.showResult) return; if (this.currentQuestion.questionType === 2) { const optionIndex = this.selectedOptions.indexOf(index); if (optionIndex > -1) { this.selectedOptions.splice(optionIndex, 1); } else { this.selectedOptions.push(index); } } else { this.selectedOption = index; } this.playAiSpeaking(); }, checkAnswer() { clearInterval(this.timerInterval); if (!this.currentQuestion) { console.error("当前问题不存在"); return; } if (this.currentQuestion.questionType === 2) { const sortedSelected = [...this.selectedOptions].sort(); const sortedCorrect = [...this.currentQuestion.correctAnswers].sort(); if (sortedSelected.length !== sortedCorrect.length) { this.isAnswerCorrect = false; } else { this.isAnswerCorrect = sortedSelected.every((value, index) => value === sortedCorrect[index]); } } else { this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer; } if (this.isAnswerCorrect) { this.score++; } this.showResult = true; if (this.currentQuestionIndex === this.questions.length - 1) { setTimeout(() => { this.interviewCompleted = false; }, 1500); return; } setTimeout(() => { this.goToNextQuestion(); }, 1500); }, nextQuestion() { if (this.currentQuestion.questionType === 2) { if (this.selectedOptions.length > 0 && !this.showResult) { this.checkAnswer(); } } else { if (this.selectedOption !== null && !this.showResult) { this.checkAnswer(); } else if (this.showResult) { this.goToNextQuestion(); } } }, // 新增方法,处理进入下一题的逻辑 goToNextQuestion() { this.showResult = false; this.selectedOption = null; this.selectedOptions = []; this.currentQuestionIndex++; if (this.currentQuestionIndex >= this.questions.length) { this.showEndModal = true; this.interviewCompleted = false; if (this.timerInterval) { clearInterval(this.timerInterval); } return; } this.resetTimer(); this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100; this.playAiSpeaking(); setTimeout(() => { this.pauseAiSpeaking(); }, 2e3); }, toggleSettings() { common_vendor.index.showToast({ title: "设置功能开发中", icon: "none" }); }, back() { if (this.timerInterval) { clearInterval(this.timerInterval); } try { const pages = getCurrentPages(); if (pages.length > 1) { common_vendor.index.reLaunch({ url: "/pages/index/index" }); } else { common_vendor.index.reLaunch({ url: "/pages/index/index" }); } } catch (e) { console.error("导航错误:", e); common_vendor.index.reLaunch({ url: "/pages/index/index" }); } }, error(e) { console.error(e.detail); common_vendor.index.showToast({ title: "相机启动失败,请检查权限设置", icon: "none" }); }, playAiSpeaking() { if (this.useVideo && this.aiVideoContext) { this.aiVideoContext.play(); } if (this.digitalHumanUrl) { const speakText = this.currentQuestion ? this.currentQuestion.text : ""; this.interactWithDigitalHuman(speakText); } }, pauseAiSpeaking() { if (this.useVideo && this.aiVideoContext) { this.aiVideoContext.pause(); } }, // 重新开始测试 restartTest() { this.currentQuestionIndex = 0; this.score = 0; this.showEndModal = false; this.showResult = false; this.selectedOption = null; this.selectedOptions = []; this.resetTimer(); }, // 在methods中添加测试方法 testEndScreen() { this.interviewCompleted = true; this.showEndModal = false; }, // 初始化数字人 initDigitalHuman() { this.digitalHumanUrl = ""; }, // 与数字人交互的方法 interactWithDigitalHuman(message) { const webview = this.$mp.page.$getAppWebview().children()[0]; if (webview) { webview.evalJS(`receiveMessage('${message}')`); } } }, // 添加生命周期钩子,确保在组件销毁时清除计时器 beforeDestroy() { if (this.timerInterval) { clearInterval(this.timerInterval); } } }; if (!Array) { const _component_uni_load_more = common_vendor.resolveComponent("uni-load-more"); _component_uni_load_more(); } function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: $data.digitalHumanUrl }, $data.digitalHumanUrl ? { b: $data.digitalHumanUrl } : { c: common_assets._imports_0 }, { d: common_vendor.t($data.currentQuestionIndex + 1), e: common_vendor.t($options.currentQuestion.id), f: common_vendor.t($data.questions.length), g: $options.currentQuestion.isImportant }, $options.currentQuestion.isImportant ? {} : {}, { h: common_vendor.t($options.currentQuestion.questionTypeName), i: common_vendor.t($options.currentQuestion.text), j: common_vendor.f($options.currentQuestion.options, (option, index, i0) => { return { a: common_vendor.t(option.option_text || (typeof option === "string" ? option : JSON.stringify(option))), b: index, c: ($options.currentQuestion.questionType === 1 ? $data.selectedOption === index : $data.selectedOptions.includes(index)) ? 1 : "", d: $data.showResult && ($options.currentQuestion.questionType === 1 ? index === $options.currentQuestion.correctAnswer : $options.currentQuestion.correctAnswers.includes(index)) ? 1 : "", e: $data.showResult && ($options.currentQuestion.questionType === 1 ? $data.selectedOption === index && index !== $options.currentQuestion.correctAnswer : $data.selectedOptions.includes(index) && !$options.currentQuestion.correctAnswers.includes(index)) ? 1 : "", f: common_vendor.o(($event) => $options.selectOption(index), index) }; }), k: common_vendor.t($data.remainingTime), l: common_vendor.t($data.showResult ? "下一题" : "提交答案"), m: common_vendor.o((...args) => $options.nextQuestion && $options.nextQuestion(...args)), n: $options.currentQuestion.questionType === 1 ? $data.selectedOption === null : $data.selectedOptions.length === 0, o: $data.showEndModal }, $data.showEndModal ? { p: common_vendor.t($data.score), q: common_vendor.t($data.totalQuestions), r: common_vendor.t($data.score), s: common_vendor.t($data.totalQuestions), t: common_vendor.o((...args) => $options.restartTest && $options.restartTest(...args)), v: common_vendor.o((...args) => $options.back && $options.back(...args)) } : {}, { w: $data.interviewCompleted }, $data.interviewCompleted ? { x: common_assets._imports_0, y: common_vendor.o((...args) => $options.back && $options.back(...args)) } : {}, { z: $data.loading }, $data.loading ? { A: common_vendor.p({ status: "loading", contentText: { contentdown: "加载中..." } }) } : {}, { B: !$data.loading && $data.loadError }, !$data.loading && $data.loadError ? { C: common_vendor.o((...args) => $options.fetchInterviewData && $options.fetchInterviewData(...args)) } : {}); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]); wx.createPage(MiniProgramPage);