camera.vue 32 KB

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