camera.vue 36 KB

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