camera.vue 34 KB

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