camera.vue 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. <template>
  2. <view class="camera-container">
  3. <!-- 顶部标题栏 -->
  4. <!-- <view class="header">
  5. <view class="back-button" @click="back">
  6. <text class="iconfont icon-back">&#xe679;</text>
  7. </view>
  8. <view class="title">中国传统文化测试</view>
  9. <view class="controls">
  10. <text class="iconfont icon-mic">&#xe677;</text>
  11. <text class="iconfont icon-settings" @click="toggleSettings">&#xe678;</text>
  12. </view>
  13. </view> -->
  14. <!-- 主要内容区域 -->
  15. <view class="content">
  16. <!-- 数字人头像 -->
  17. <view class="digital-avatar">
  18. <web-view v-if="digitalHumanUrl" :src="digitalHumanUrl" class="digital-human-webview"></web-view>
  19. <image v-else src="/static/avatar.png" mode="aspectFill" class="avatar-image"></image>
  20. </view>
  21. <!-- 进度指示器 -->
  22. <!-- <view class="progress-container">
  23. <view class="progress-step" v-for="(step, index) in 3" :key="index">
  24. <view class="step-circle" :class="{'active': currentStep >= index+1}">{{index+1}}</view>
  25. <view class="step-text">{{stepTexts[index]}}</view>
  26. </view>
  27. <view class="progress-bar">
  28. <view class="progress-fill" :style="{width: progressWidth + '%'}"></view>
  29. </view>
  30. </view> -->
  31. <!-- 面试内容区域 -->
  32. <view class="interview-content">
  33. <!-- 问题编号和进度 -->
  34. <view class="question-number">
  35. <text>{{currentQuestionIndex + 1}}/{{questions.length}}</text>
  36. </view>
  37. <!-- 问题和选项区域 -->
  38. <view class="question-area">
  39. <view class="question-importance">
  40. <text v-if="currentQuestion.isImportant">重点题</text>
  41. <text class="question-type">[{{currentQuestion.questionTypeName}}]</text>
  42. {{currentQuestion.text}}
  43. </view>
  44. <view class="options">
  45. <!-- 开放问题 (question_form: 0) - 显示文本输入框 -->
  46. <!-- {{ currentQuestion }} -->
  47. <view v-if="currentQuestion.questionType == 0" class="open-question-container">
  48. <textarea
  49. class="open-question-input"
  50. v-model="openQuestionAnswer"
  51. placeholder="请输入您的回答..."
  52. maxlength="500"
  53. ></textarea>
  54. <view class="word-count">{{openQuestionAnswer.length}}/500</view>
  55. </view>
  56. <!-- 单选题和多选题 (question_form: 1 或 2) -->
  57. <view
  58. v-else
  59. v-for="(option, index) in currentQuestion.options"
  60. :key="index"
  61. class="option-item"
  62. :class="{
  63. 'option-selected': currentQuestion.questionType === 1 ? selectedOption === index : selectedOptions.includes(index),
  64. 'option-correct': showResult && (
  65. currentQuestion.questionType === 1 ? index === currentQuestion.correctAnswer : currentQuestion.correctAnswers.includes(index)
  66. ),
  67. 'option-wrong': showResult && (
  68. currentQuestion.questionType === 1 ?
  69. (selectedOption === index && index !== currentQuestion.correctAnswer) :
  70. (selectedOptions.includes(index) && !currentQuestion.correctAnswers.includes(index))
  71. )
  72. }"
  73. @click="selectOption(index)">
  74. <!-- 选项前添加单选/多选标识 -->
  75. <text class="option-prefix">{{currentQuestion.questionType === 1 ? '●' : '☐'}}</text>
  76. <text class="option-text">{{ option.option_text || (typeof option === 'string' ? option : JSON.stringify(option)) }}</text>
  77. </view>
  78. </view>
  79. <!-- <view class="timer-container">
  80. <text class="timer-text">本题剩余时间 {{remainingTime}}</text>
  81. </view> -->
  82. <button class="next-button" @click="nextQuestion(option)"
  83. :disabled="
  84. (currentQuestion.questionType === 0 && openQuestionAnswer.trim() === '') ||
  85. (currentQuestion.questionType === 1 && selectedOption === null) ||
  86. (currentQuestion.questionType === 2 && selectedOptions.length === 0)
  87. ">
  88. {{showResult ? '下一题' : '提交答案'}}
  89. </button>
  90. </view>
  91. </view>
  92. </view>
  93. <!-- 底部控制栏 -->
  94. <!-- <view class="footer">
  95. <view class="timer">本题剩余 {{remainingTime}}</view>
  96. <view class="next-step">进入下一题</view>
  97. </view> -->
  98. <!-- 面试结束弹窗 -->
  99. <view class="interview-end-modal" v-if="showEndModal">
  100. <view class="modal-content">
  101. <view class="modal-title">测试已完成</view>
  102. <!-- <view class="score-display">{{score}}/{{totalQuestions}}</view>
  103. <view class="modal-message">您在本次测试中答了{{score}}道题目,共{{totalQuestions}}道题目。</view> -->
  104. <view class="modal-buttons">
  105. <!-- <button type="default" class="modal-button" @click="restartTest">重新测试</button> -->
  106. <button type="primary" class="modal-button" @click="navigateToInterview">进入面试</button>
  107. </view>
  108. </view>
  109. </view>
  110. <!-- AI面试结束页面 -->
  111. <view class="interview-complete-screen" v-if="interviewCompleted">
  112. <view class="digital-avatar-large">
  113. <image src="/static/avatar.png" mode="aspectFill" class="avatar-image-large"></image>
  114. </view>
  115. <view class="complete-message">AI面试已结束</view>
  116. <view class="complete-description">本次面试的全部环节已结束。非常感谢您的参与。</view>
  117. <button class="complete-button" @click="back">我知道了</button>
  118. </view>
  119. <!-- 在模板中添加一个测试按钮(开发时使用,发布前删除) -->
  120. <!-- <button @click="testEndScreen" style="position: absolute; top: 10px; right: 10px; z-index: 9999;">测试结束页</button> -->
  121. <!-- 添加加载状态 -->
  122. <view class="loading-container" v-if="loading">
  123. <uni-load-more status="loading" :contentText="{contentdown: '加载中...'}" />
  124. <text class="loading-text">正在加载面试题目...</text>
  125. </view>
  126. <!-- 添加加载错误提示 -->
  127. <view class="loading-container" v-if="!loading && loadError">
  128. <view class="error-container">
  129. <text class="error-message">加载面试题目失败</text>
  130. <button class="retry-button" @click="fetchInterviewData">重试</button>
  131. </view>
  132. </view>
  133. </view>
  134. </template>
  135. <script>
  136. import { getInterviewList, getInterviewDetail } from '@/api/user.js';
  137. import { apiBaseUrl } from '@/common/config.js';
  138. export default {
  139. data() {
  140. return {
  141. cameraContext: null,
  142. devicePosition: 'front',
  143. interviewStarted: true,
  144. showEndModal: false,
  145. currentQuestionIndex: 0,
  146. currentStep: 1,
  147. stepTexts: ['测试准备', '回答问题', '测试结束'],
  148. progressWidth: 50,
  149. remainingTime: '00:27',
  150. selectedOption: null,
  151. selectedOptions: [],
  152. showResult: false,
  153. isAnswerCorrect: false,
  154. questions: [], // 改为空数组,将通过API获取
  155. interviewId: null, // 存储当前面试ID
  156. useVideo: false,
  157. timerInterval: null,
  158. score: 0,
  159. totalQuestions: 0,
  160. interviewCompleted: false,
  161. digitalHumanUrl: '', // 数字人URL
  162. loading: true, // 添加加载状态
  163. loadError: false, // 添加加载错误状态
  164. errorMessage: '', // 添加错误消息
  165. answers: [], // 存储用户的所有答案
  166. currentQuestionDetail: null, // 当前题目详情
  167. isSubmitting: false, // 是否正在提交答案
  168. openQuestionAnswer: '', // 存储开放问题的答案
  169. currentAnswer: null, // 存储当前答案以便提交
  170. questionStartTime: null, // 存储问题开始时间
  171. }
  172. },
  173. computed: {
  174. currentQuestion() {
  175. console.log(this.questions[this.currentQuestionIndex]);
  176. return this.questions[this.currentQuestionIndex];
  177. }
  178. },
  179. onLoad(options) {
  180. // 从路由参数中获取面试ID
  181. if (options && options.id) {
  182. this.interviewId = options.id;
  183. this.fetchInterviewData();
  184. } else {
  185. // 如果没有ID,可以获取面试列表并使用第一个
  186. this.fetchInterviewList();
  187. }
  188. },
  189. onReady() {
  190. // 创建相机上下文
  191. this.cameraContext = uni.createCameraContext();
  192. // 创建AI视频上下文
  193. if (this.useVideo) {
  194. this.aiVideoContext = uni.createVideoContext('aiInterviewer');
  195. }
  196. // 初始化数字人
  197. this.initDigitalHuman();
  198. },
  199. methods: {
  200. // 获取面试列表
  201. async fetchInterviewList() {
  202. try {
  203. this.loading = true;
  204. const res = await getInterviewList({job_id:JSON.parse(uni.getStorageSync('selectedJob')).id});
  205. console.log(res);
  206. // 使用第一个面试
  207. this.interviewId = res//[0].id;
  208. this.fetchInterviewData(res);
  209. } catch (error) {
  210. console.error('获取面试列表失败:', error);
  211. this.handleLoadError('获取面试列表失败');
  212. }
  213. },
  214. // 获取面试详情数据
  215. async fetchInterviewData(data) {
  216. try {
  217. this.loading = true;
  218. // const res = await getInterviewDetail({ id: this.interviewId });
  219. // console.log('API返回数据:', res);
  220. // 如果返回的是问题列表
  221. if (data && Array.isArray(data)) {
  222. // 处理多个问题
  223. this.questions =data.map((q, index) => ({
  224. id: q.id || index + 1,
  225. text: q.question || '未知问题',
  226. options:q.options || [],
  227. correctAnswer: q.correctAnswer || 0,
  228. isImportant: q.is_system || false,
  229. explanation: q.explanation || '',
  230. questionType: q.question_form,
  231. questionTypeName: q.question_form_name || '单选题',
  232. correctAnswers: q.correct_answers || [],
  233. difficulty: q.difficulty || 1,
  234. difficultyName: q.difficulty_name || '初级'
  235. }));
  236. } else {
  237. // 处理单个问题
  238. this.processInterviewData(res);
  239. }
  240. console.log(this.questions);
  241. // 设置总题目数
  242. this.totalQuestions = this.questions.length;
  243. // 启动倒计时
  244. if (this.questions.length > 0) {
  245. this.startTimer();
  246. }
  247. } catch (error) {
  248. console.error('获取面试详情失败:', error);
  249. this.handleLoadError('获取面试详情失败');
  250. } finally {
  251. this.loading = false;
  252. }
  253. },
  254. // 处理面试数据
  255. processInterviewData(data) {
  256. // 清空现有问题列表
  257. this.questions = [];
  258. // 检查数据格式并处理
  259. if (data) {
  260. // 创建一个格式化的问题对象
  261. const formattedQuestion = {
  262. id: data.id || 1,
  263. text: data.question || '未知问题',
  264. options: data.options || [],
  265. correctAnswer: data.correctAnswer || 0,
  266. isImportant: data.is_system || false,
  267. explanation: data.explanation || '',
  268. questionType: data.question_form, // 1-单选题,2-多选题
  269. questionTypeName: data.question_form_name || '单选题',
  270. correctAnswers: data.correct_answers || [],
  271. difficulty: data.difficulty || 1,
  272. difficultyName: data.difficulty_name || '初级'
  273. };
  274. // 添加到问题列表
  275. this.questions.push(formattedQuestion);
  276. // 设置总题目数
  277. this.totalQuestions = this.questions.length;
  278. // 启动倒计时
  279. this.startTimer();
  280. } else {
  281. this.handleLoadError('面试中没有问题');
  282. }
  283. },
  284. // 处理加载错误
  285. handleLoadError(message) {
  286. this.loadError = true;
  287. this.loading = false;
  288. this.errorMessage = message || '加载失败';
  289. uni.showToast({
  290. title: message || '加载失败',
  291. icon: 'none',
  292. duration: 2000
  293. });
  294. },
  295. startTimer() {
  296. // 确保问题已加载
  297. if (this.questions.length === 0) return;
  298. // 记录开始时间
  299. this.questionStartTime = new Date();
  300. // 模拟倒计时
  301. let seconds = 30; // 每题30秒
  302. this.timerInterval = setInterval(() => {
  303. seconds--;
  304. if (seconds <= 0) {
  305. clearInterval(this.timerInterval);
  306. // 时间到,自动提交答案
  307. if (!this.showResult) {
  308. this.checkAnswer();
  309. }
  310. }
  311. const min = Math.floor(seconds / 60).toString().padStart(2, '0');
  312. const sec = (seconds % 60).toString().padStart(2, '0');
  313. this.remainingTime = `${min}:${sec}`;
  314. }, 1000);
  315. },
  316. resetTimer() {
  317. clearInterval(this.timerInterval);
  318. this.startTimer();
  319. },
  320. selectOption(index) {
  321. if (this.showResult) return; // 已显示结果时不能再选择
  322. // 判断当前题目类型
  323. if (this.currentQuestion.questionType === 2) { // 多选题
  324. // 如果已经选中,则取消选中
  325. const optionIndex = this.selectedOptions.indexOf(index);
  326. if (optionIndex > -1) {
  327. this.selectedOptions.splice(optionIndex, 1);
  328. } else {
  329. // 否则添加到已选中数组
  330. this.selectedOptions.push(index);
  331. }
  332. } else if (this.currentQuestion.questionType === 1) { // 单选题
  333. this.selectedOption = index;
  334. }
  335. this.playAiSpeaking();
  336. },
  337. checkAnswer() {
  338. clearInterval(this.timerInterval); // 停止计时
  339. // 确保当前问题存在
  340. if (!this.currentQuestion) {
  341. console.error('当前问题不存在');
  342. return;
  343. }
  344. // 根据题目类型检查答案是否正确
  345. if (this.currentQuestion.questionType === 0) { // 开放问题
  346. // 开放问题没有标准答案,可以根据需要设置为正确或待评估
  347. this.isAnswerCorrect = true; // 或者设置为 null 表示待评估
  348. } else if (this.currentQuestion.questionType === 2) { // 多选题
  349. const sortedSelected = [...this.selectedOptions].sort();
  350. const sortedCorrect = [...this.currentQuestion.correctAnswers].sort();
  351. if (sortedSelected.length !== sortedCorrect.length) {
  352. this.isAnswerCorrect = false;
  353. } else {
  354. this.isAnswerCorrect = sortedSelected.every((value, index) => value === sortedCorrect[index]);
  355. }
  356. } else { // 单选题
  357. this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
  358. }
  359. // 显示结果
  360. this.showResult = true;
  361. },
  362. nextQuestion(data) {
  363. // 如果还没有显示结果,先检查答案并提交
  364. if (!this.showResult) {
  365. this.checkAnswer();
  366. // 保存当前题目的答案并立即提交
  367. this.saveAnswer(data);
  368. this.submitCurrentAnswer(data).then(() => {
  369. // 提交成功后显示结果
  370. this.showResult = true;
  371. }).catch(error => {
  372. console.error('提交答案失败:', error);
  373. uni.showToast({
  374. title: '提交答案失败,请重试',
  375. icon: 'none'
  376. });
  377. // 即使提交失败也显示结果
  378. this.showResult = true;
  379. });
  380. return;
  381. }
  382. // 如果已经显示结果,点击"下一题"按钮时执行
  383. // 如果是最后一题,直接跳转到手部照片采集页面
  384. if (this.currentQuestionIndex >= this.questions.length - 1) {
  385. // 直接跳转到手部照片采集页面,不显示结束模态框
  386. uni.navigateTo({
  387. url: '/pages/interview/interview', // 假设这是手部照片采集页面的路径
  388. success: () => {
  389. console.log('成功跳转到手部照片采集页面');
  390. },
  391. fail: (err) => {
  392. console.error('跳转失败:', err);
  393. uni.showToast({
  394. title: '跳转失败,请手动返回首页',
  395. icon: 'none'
  396. });
  397. }
  398. });
  399. return;
  400. }
  401. // 前往下一题
  402. this.goToNextQuestion();
  403. },
  404. // 保存当前题目的答案
  405. saveAnswer() {
  406. let answer;
  407. if (this.currentQuestion.questionType === 0) { // 开放问题
  408. answer = {
  409. questionId: this.currentQuestion.id,
  410. questionType: this.currentQuestion.questionType,
  411. answer: this.openQuestionAnswer,
  412. answerDuration: this.getAnswerDuration() // 添加答题时长
  413. };
  414. } else { // 单选或多选
  415. answer = {
  416. questionId: this.currentQuestion.id,
  417. questionType: this.currentQuestion.questionType,
  418. answer: this.currentQuestion.questionType === 1 ?
  419. this.selectedOption : this.selectedOptions,
  420. answerDuration: this.getAnswerDuration() // 添加答题时长
  421. };
  422. }
  423. // 检查是否已存在该题目的答案,如果存在则更新
  424. const existingIndex = this.answers.findIndex(a => a.questionId === answer.questionId);
  425. if (existingIndex > -1) {
  426. this.answers[existingIndex] = answer;
  427. } else {
  428. this.answers.push(answer);
  429. }
  430. // 保存当前答案以便提交
  431. this.currentAnswer = answer;
  432. console.log('已保存答案:', this.answers);
  433. },
  434. // 获取答题时长(秒)
  435. getAnswerDuration() {
  436. // 这里假设每题默认30秒,根据剩余时间计算用时
  437. // 您可以根据实际情况调整计算逻辑
  438. const remainingTimeArr = this.remainingTime.split(':');
  439. const remainingSeconds = parseInt(remainingTimeArr[0]) * 60 + parseInt(remainingTimeArr[1]);
  440. return 30 - remainingSeconds; // 假设每题30秒
  441. },
  442. // 提交当前答案
  443. async submitCurrentAnswer() {
  444. if (!this.currentAnswer) return;
  445. try {
  446. // 显示提交中提示
  447. uni.showLoading({
  448. title: '正在提交答案...'
  449. });
  450. // 构建提交数据
  451. let answerContent = '';
  452. if (this.currentAnswer.questionType === 0) {
  453. // 开放题直接使用文本答案
  454. answerContent = this.currentAnswer.answer;
  455. } else if (this.currentAnswer.questionType === 1) {
  456. // 单选题,获取选项ID而不是索引
  457. const selectedIndex = this.currentAnswer.answer;
  458. const selectedOption = this.currentQuestion.options[selectedIndex];
  459. // 使用选项的ID或其他唯一标识符
  460. answerContent = selectedOption.id ? selectedOption.id.toString() : selectedIndex.toString();
  461. } else if (this.currentAnswer.questionType === 2) {
  462. // 多选题,将选项ID数组转为逗号分隔的字符串
  463. const selectedIndices = this.currentAnswer.answer;
  464. const selectedOptionIds = selectedIndices.map(index => {
  465. const option = this.currentQuestion.options[index];
  466. // 使用选项的ID或其他唯一标识符
  467. return option.id ? option.id : index;
  468. });
  469. answerContent = selectedOptionIds.join(',');
  470. }
  471. const submitData = {
  472. application_id: 1, // 或者使用其他合适的ID
  473. question_id: this.currentAnswer.questionId,
  474. answer_content: answerContent,
  475. answer_duration: this.currentAnswer.answerDuration || 0,
  476. // 如果需要tenant_id,请在这里添加
  477. tenant_id: 1
  478. };
  479. console.log('提交数据:', submitData);
  480. // 调用API提交答案
  481. const res = await this.$http.post(`${apiBaseUrl}/api/job/submit_answer`, submitData);
  482. console.log('提交答案响应:', res);
  483. // 隐藏加载提示
  484. uni.hideLoading();
  485. return res;
  486. } catch (error) {
  487. console.error('提交答案失败:', error);
  488. // 隐藏加载提示
  489. uni.hideLoading();
  490. // 显示错误提示
  491. uni.showToast({
  492. title: '提交答案失败,请重试',
  493. icon: 'none'
  494. });
  495. throw error;
  496. }
  497. },
  498. // 修改 goToNextQuestion 方法,添加 async 关键字
  499. async goToNextQuestion() {
  500. // 重置状态
  501. this.showResult = false;
  502. this.selectedOption = null;
  503. this.selectedOptions = [];
  504. this.openQuestionAnswer = ''; // 重置开放问题答案
  505. // 前往下一题
  506. this.currentQuestionIndex++;
  507. // 如果已经有下一题的详情,直接使用
  508. if (this.questions[this.currentQuestionIndex]) {
  509. // 更新进度
  510. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  511. // 重置计时器
  512. this.resetTimer();
  513. // 播放AI介绍下一题
  514. this.playAiSpeaking();
  515. setTimeout(() => {
  516. this.pauseAiSpeaking();
  517. }, 2000);
  518. return;
  519. }
  520. // 否则请求下一题详情
  521. try {
  522. this.loading = true;
  523. // 假设您有一个获取单个题目详情的API
  524. // const res = await getQuestionDetail({
  525. // interviewId: this.interviewId,
  526. // questionIndex: this.currentQuestionIndex
  527. // });
  528. // 模拟API调用
  529. await new Promise(resolve => setTimeout(resolve, 1000));
  530. const res = { /* 模拟的题目数据 */ };
  531. // 处理题目数据
  532. if (res) {
  533. const formattedQuestion = {
  534. id: res.id || this.currentQuestionIndex + 1,
  535. text: res.question || '未知问题',
  536. options: res.options || [],
  537. correctAnswer: res.correctAnswer || 0,
  538. isImportant: res.is_system || false,
  539. explanation: res.explanation || '',
  540. questionType: res.question_form || 1,
  541. questionTypeName: res.question_form_name || '单选题',
  542. correctAnswers: res.correct_answers || [],
  543. difficulty: res.difficulty || 1,
  544. difficultyName: res.difficulty_name || '初级'
  545. };
  546. // 添加到问题列表
  547. this.questions.push(formattedQuestion);
  548. }
  549. } catch (error) {
  550. console.error('获取题目详情失败:', error);
  551. uni.showToast({
  552. title: '获取题目失败,请重试',
  553. icon: 'none'
  554. });
  555. // 出错时回到上一题
  556. this.currentQuestionIndex--;
  557. } finally {
  558. this.loading = false;
  559. // 更新进度
  560. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  561. // 重置计时器
  562. this.resetTimer();
  563. }
  564. },
  565. toggleSettings() {
  566. // 打开设置面板
  567. uni.showToast({
  568. title: '设置功能开发中',
  569. icon: 'none'
  570. });
  571. },
  572. back(target) {
  573. // 清除计时器
  574. if (this.timerInterval) {
  575. clearInterval(this.timerInterval);
  576. }
  577. // 如果指定了跳转目标
  578. if (target) {
  579. uni.navigateTo({
  580. url: target
  581. });
  582. return;
  583. }
  584. // 否则尝试返回上一页,如果失败则跳转到首页
  585. try {
  586. const pages = getCurrentPages();
  587. if (pages.length > 1) {
  588. uni.reLaunch({
  589. url: '/pages/index/index'
  590. });
  591. } else {
  592. // 如果当前是第一页,则跳转到首页
  593. uni.reLaunch({
  594. url: '/pages/index/index'
  595. });
  596. }
  597. } catch (e) {
  598. console.error('导航错误:', e);
  599. // 出错时也跳转到首页
  600. uni.reLaunch({
  601. url: '/pages/index/index'
  602. });
  603. }
  604. },
  605. error(e) {
  606. // 相机错误事件
  607. console.error(e.detail);
  608. uni.showToast({
  609. title: '相机启动失败,请检查权限设置',
  610. icon: 'none'
  611. });
  612. },
  613. playAiSpeaking() {
  614. if (this.useVideo && this.aiVideoContext) {
  615. this.aiVideoContext.play();
  616. }
  617. // 触发数字人说话动画
  618. if (this.digitalHumanUrl) {
  619. // 假设当前问题的文本作为数字人要说的内容
  620. const speakText = this.currentQuestion ? this.currentQuestion.text : '';
  621. this.interactWithDigitalHuman(speakText);
  622. }
  623. },
  624. pauseAiSpeaking() {
  625. if (this.useVideo && this.aiVideoContext) {
  626. this.aiVideoContext.pause();
  627. }
  628. },
  629. // 修改 restartTest 方法,添加可选的跳转目标
  630. restartTest(target) {
  631. this.currentQuestionIndex = 0;
  632. this.score = 0;
  633. this.showEndModal = false;
  634. this.showResult = false;
  635. this.selectedOption = null;
  636. this.selectedOptions = [];
  637. this.resetTimer();
  638. // 如果指定了跳转目标
  639. if (target) {
  640. uni.navigateTo({
  641. url: target
  642. });
  643. }
  644. },
  645. // 在methods中添加测试方法
  646. testEndScreen() {
  647. this.interviewCompleted = true;
  648. this.showEndModal = false;
  649. },
  650. // 初始化数字人
  651. initDigitalHuman() {
  652. // 移除不可用的示例URL
  653. // this.digitalHumanUrl = 'https://your-digital-human-service.com/avatar?id=123';
  654. // 暂时不使用数字人服务,直接使用本地图片
  655. this.digitalHumanUrl = '';
  656. // 如果您有实际可用的数字人服务,可以在这里设置
  657. // 例如:this.digitalHumanUrl = 'https://your-actual-service.com/avatar';
  658. // 或者使用本地HTML文件(如果有的话)
  659. // this.digitalHumanUrl = '/hybrid/html/digital-human.html';
  660. },
  661. // 与数字人交互的方法
  662. interactWithDigitalHuman(message) {
  663. // 如果数字人是通过web-view加载的,可以使用postMessage与其通信
  664. const webview = this.$mp.page.$getAppWebview().children()[0];
  665. if (webview) {
  666. webview.evalJS(`receiveMessage('${message}')`);
  667. }
  668. },
  669. // 添加 navigateToInterview 方法
  670. navigateToInterview() {
  671. // 关闭模态框
  672. this.showEndModal = false;
  673. // 跳转到interview页面
  674. uni.navigateTo({
  675. url: '/pages/interview/interview',
  676. success: () => {
  677. console.log('成功跳转到interview页面');
  678. },
  679. fail: (err) => {
  680. console.error('跳转失败:', err);
  681. uni.showToast({
  682. title: '跳转失败,请手动返回首页',
  683. icon: 'none'
  684. });
  685. }
  686. });
  687. }
  688. },
  689. // 添加生命周期钩子,确保在组件销毁时清除计时器
  690. beforeDestroy() {
  691. if (this.timerInterval) {
  692. clearInterval(this.timerInterval);
  693. }
  694. }
  695. }
  696. </script>
  697. <style>
  698. .camera-container {
  699. position: relative;
  700. width: 100%;
  701. height: 100vh;
  702. background-color: #ffffff;
  703. display: flex;
  704. flex-direction: column;
  705. }
  706. .header {
  707. height: 90rpx;
  708. /* background-color: #000000; */
  709. display: flex;
  710. align-items: center;
  711. padding: 0 30rpx;
  712. /* color: #ffffff; */
  713. }
  714. .back-button {
  715. width: 60rpx;
  716. height: 60rpx;
  717. display: flex;
  718. align-items: center;
  719. justify-content: center;
  720. }
  721. .title {
  722. flex: 1;
  723. text-align: center;
  724. font-size: 32rpx;
  725. font-weight: bold;
  726. }
  727. .controls {
  728. display: flex;
  729. gap: 30rpx;
  730. }
  731. .iconfont {
  732. font-size: 40rpx;
  733. }
  734. .content {
  735. flex: 1;
  736. display: flex;
  737. flex-direction: column;
  738. position: relative;
  739. padding: 50rpx 20rpx 0;
  740. }
  741. .progress-container {
  742. display: flex;
  743. flex-direction: column;
  744. justify-content: space-between;
  745. margin-bottom: 20rpx;
  746. }
  747. .progress-step {
  748. display: flex;
  749. align-items: center;
  750. margin-bottom: 10rpx;
  751. }
  752. .step-circle {
  753. width: 40rpx;
  754. height: 40rpx;
  755. border-radius: 50%;
  756. background-color: #e0e0e0;
  757. display: flex;
  758. align-items: center;
  759. justify-content: center;
  760. font-size: 24rpx;
  761. margin-right: 10rpx;
  762. }
  763. .step-circle.active {
  764. background-color: #ff9500;
  765. color: #ffffff;
  766. }
  767. .step-text {
  768. font-size: 24rpx;
  769. color: #666666;
  770. }
  771. .progress-bar {
  772. height: 10rpx;
  773. background-color: #e0e0e0;
  774. border-radius: 5rpx;
  775. overflow: hidden;
  776. margin-bottom: 20rpx;
  777. }
  778. .progress-fill {
  779. height: 100%;
  780. background-color: #00c853;
  781. transition: width 0.3s;
  782. }
  783. .interview-content {
  784. flex: 1;
  785. display: flex;
  786. flex-direction: column;
  787. }
  788. .video-area {
  789. display: flex;
  790. /* flex-direction: column; */
  791. justify-content: space-between;
  792. height: auto;
  793. margin-bottom: 20rpx;
  794. }
  795. .video-camera {
  796. display: flex;
  797. flex-direction: row;
  798. align-items: center;
  799. justify-content: space-between;
  800. width: 65%;
  801. }
  802. .interviewer {
  803. background-color: #f5f5f5;
  804. display: flex;
  805. align-items: center;
  806. justify-content: center;
  807. position: relative;
  808. width: 48% !important;
  809. height: 300rpx;
  810. }
  811. .interviewer-video {
  812. width: 100%;
  813. height: 100%;
  814. object-fit: cover;
  815. }
  816. .interviewer-avatar {
  817. width: 80%;
  818. height: 80%;
  819. object-fit: contain;
  820. }
  821. .user-camera-container {
  822. width: 48%;
  823. height: 300rpx;
  824. border-radius: 10rpx;
  825. overflow: hidden;
  826. border: 4rpx solid #ffffff;
  827. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
  828. }
  829. .question-area {
  830. flex: 1;
  831. display: flex;
  832. flex-direction: column;
  833. }
  834. .question-number {
  835. font-size: 28rpx;
  836. color: #666;
  837. margin-bottom: 20rpx;
  838. }
  839. .question-importance {
  840. background-color: #f9f9f9;
  841. padding: 20rpx;
  842. border-radius: 10rpx;
  843. margin-bottom: 20rpx;
  844. font-size: 28rpx;
  845. color: #333;
  846. }
  847. .question-importance text {
  848. color: #ff0000;
  849. margin-right: 10rpx;
  850. }
  851. .options {
  852. display: flex;
  853. flex-direction: column;
  854. gap: 20rpx;
  855. margin-bottom: 30rpx;
  856. }
  857. .option-item {
  858. padding: 20rpx;
  859. background-color: #f5f5f5;
  860. border-radius: 10rpx;
  861. font-size: 28rpx;
  862. transition: all 0.3s;
  863. border: 1px solid #e0e0e0;
  864. }
  865. .option-selected {
  866. background-color: #e6f7ff;
  867. border: 1px solid #1890ff;
  868. }
  869. .option-correct {
  870. background-color: #d4f7d4;
  871. border: 1px solid #52c41a;
  872. }
  873. .option-wrong {
  874. background-color: #fff1f0;
  875. border: 1px solid #ff4d4f;
  876. }
  877. .option-text {
  878. color: #333;
  879. }
  880. .timer-container {
  881. text-align: center;
  882. margin: 20rpx 0;
  883. }
  884. .timer-text {
  885. font-size: 24rpx;
  886. color: #666;
  887. }
  888. .next-button {
  889. background-color: #6c5ce7;
  890. color: #ffffff;
  891. border-radius: 10rpx;
  892. font-size: 30rpx;
  893. margin: 20rpx auto;
  894. height: 80rpx;
  895. line-height: 80rpx;
  896. width: 90%;
  897. text-align: center;
  898. }
  899. .next-button[disabled] {
  900. background-color: #cccccc;
  901. color: #999999;
  902. }
  903. .footer {
  904. height: 100rpx;
  905. background-color: #000000;
  906. display: flex;
  907. justify-content: space-between;
  908. align-items: center;
  909. padding: 0 30rpx;
  910. color: #ffffff;
  911. }
  912. .timer {
  913. font-size: 28rpx;
  914. }
  915. .next-step {
  916. font-size: 28rpx;
  917. }
  918. .interview-end-modal {
  919. position: fixed;
  920. top: 0;
  921. left: 0;
  922. right: 0;
  923. bottom: 0;
  924. background-color: rgba(0, 0, 0, 0.5);
  925. display: flex;
  926. align-items: center;
  927. justify-content: center;
  928. z-index: 999;
  929. }
  930. .modal-content {
  931. width: 80%;
  932. background-color: #ffffff;
  933. border-radius: 20rpx;
  934. padding: 40rpx;
  935. display: flex;
  936. flex-direction: column;
  937. align-items: center;
  938. }
  939. .modal-title {
  940. font-size: 36rpx;
  941. font-weight: bold;
  942. margin-bottom: 30rpx;
  943. }
  944. .score-display {
  945. font-size: 48rpx;
  946. font-weight: bold;
  947. color: #6c5ce7;
  948. margin: 20rpx 0;
  949. }
  950. .modal-message {
  951. font-size: 28rpx;
  952. color: #666;
  953. text-align: center;
  954. margin-bottom: 40rpx;
  955. }
  956. .modal-buttons {
  957. display: flex;
  958. justify-content: space-between;
  959. width: 100%;
  960. margin-top: 20rpx;
  961. }
  962. .modal-button {
  963. width: 45%;
  964. }
  965. @keyframes speaking {
  966. 0% {
  967. opacity: 0.8;
  968. }
  969. 50% {
  970. opacity: 1;
  971. }
  972. 100% {
  973. opacity: 0.8;
  974. }
  975. }
  976. .speaking {
  977. animation: speaking 1.5s infinite;
  978. }
  979. .digital-avatar {
  980. width: 120rpx;
  981. height: 120rpx;
  982. z-index: 10;
  983. overflow: hidden;
  984. }
  985. .avatar-image {
  986. width: 120rpx;
  987. height: 120rpx;
  988. border-radius: 20rpx;
  989. border: 2rpx solid #e0e0e0;
  990. }
  991. .digital-human-webview {
  992. width: 120rpx;
  993. height: 120rpx;
  994. border-radius: 20rpx;
  995. border: 2rpx solid #e0e0e0;
  996. }
  997. .interview-complete-screen {
  998. position: fixed;
  999. top: 0;
  1000. left: 0;
  1001. right: 0;
  1002. bottom: 0;
  1003. background-color: #f0f5ff;
  1004. display: flex;
  1005. flex-direction: column;
  1006. align-items: center;
  1007. justify-content: center;
  1008. z-index: 1000;
  1009. padding: 40rpx;
  1010. }
  1011. .digital-avatar-large {
  1012. width: 200rpx;
  1013. height: 200rpx;
  1014. margin-bottom: 60rpx;
  1015. }
  1016. .avatar-image-large {
  1017. width: 100%;
  1018. height: 100%;
  1019. border-radius: 30rpx;
  1020. }
  1021. .complete-message {
  1022. font-size: 40rpx;
  1023. font-weight: bold;
  1024. color: #333;
  1025. margin-bottom: 30rpx;
  1026. }
  1027. .complete-description {
  1028. font-size: 28rpx;
  1029. color: #666;
  1030. text-align: center;
  1031. margin-bottom: 80rpx;
  1032. line-height: 1.5;
  1033. }
  1034. .complete-button {
  1035. background-color: #6c5ce7;
  1036. color: #ffffff;
  1037. border-radius: 10rpx;
  1038. font-size: 32rpx;
  1039. padding: 20rpx 60rpx;
  1040. margin-top: 40rpx;
  1041. }
  1042. /* 添加加载状态样式 */
  1043. .loading-container {
  1044. position: absolute;
  1045. top: 0;
  1046. left: 0;
  1047. right: 0;
  1048. bottom: 0;
  1049. display: flex;
  1050. flex-direction: column;
  1051. align-items: center;
  1052. justify-content: center;
  1053. background-color: rgba(255, 255, 255, 0.9);
  1054. z-index: 100;
  1055. }
  1056. .loading-text {
  1057. margin-top: 20rpx;
  1058. font-size: 28rpx;
  1059. color: #666;
  1060. }
  1061. .error-container {
  1062. text-align: center;
  1063. }
  1064. .error-message {
  1065. font-size: 28rpx;
  1066. color: #ff4d4f;
  1067. margin-bottom: 30rpx;
  1068. }
  1069. .retry-button {
  1070. background-color: #6c5ce7;
  1071. color: #ffffff;
  1072. border-radius: 10rpx;
  1073. font-size: 28rpx;
  1074. padding: 10rpx 30rpx;
  1075. }
  1076. .question-type {
  1077. display: inline-block;
  1078. background-color: #6c5ce7;
  1079. color: white;
  1080. font-size: 24rpx;
  1081. padding: 4rpx 10rpx;
  1082. border-radius: 6rpx;
  1083. margin-right: 10rpx;
  1084. }
  1085. /* 新增的开放问题样式 */
  1086. .open-question-container {
  1087. display: flex;
  1088. flex-direction: column;
  1089. width: 100%;
  1090. margin-bottom: 20rpx;
  1091. }
  1092. .open-question-input {
  1093. width: 100%;
  1094. height: 300rpx;
  1095. background-color: #f9f9f9;
  1096. border: 1px solid #e0e0e0;
  1097. border-radius: 10rpx;
  1098. padding: 20rpx;
  1099. font-size: 28rpx;
  1100. color: #333;
  1101. }
  1102. .word-count {
  1103. text-align: right;
  1104. font-size: 24rpx;
  1105. color: #999;
  1106. margin-top: 10rpx;
  1107. }
  1108. .option-prefix {
  1109. margin-right: 10rpx;
  1110. color: #6c5ce7;
  1111. font-weight: bold;
  1112. }
  1113. </style>