camera.vue 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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});
  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. if (this.currentAnswer.questionType === 0) {
  492. // 开放题直接使用文本答案
  493. answerContent = this.currentAnswer.answer;
  494. } else if (this.currentAnswer.questionType === 1 || this.currentAnswer.questionType === 3) {
  495. // 单选题或看图答题,获取选项ID而不是索引
  496. const selectedIndex = this.currentAnswer.answer;
  497. const selectedOption = this.currentQuestion.options[selectedIndex];
  498. console.log('selectedOption',selectedOption);
  499. // 使用选项的ID或其他唯一标识符
  500. answerContent = selectedOption.id ? selectedOption.id.toString() : selectedIndex.toString();
  501. } else if (this.currentAnswer.questionType === 2) {
  502. // 多选题,将选项ID数组转为逗号分隔的字符串
  503. const selectedIndices = this.currentAnswer.answer;
  504. const selectedOptionIds = selectedIndices.map(index => {
  505. const option = this.currentQuestion.options[index];
  506. // 使用选项的ID或其他唯一标识符
  507. return option.id ? option.id : index;
  508. });
  509. answerContent = selectedOptionIds.join(',');
  510. }
  511. const submitData = {
  512. job_id: JSON.parse(uni.getStorageSync('selectedJob')).id,
  513. applicant_id: JSON.parse(uni.getStorageSync('userInfo')).id,
  514. question_id: this.currentAnswer.questionId,
  515. answer_content: answerContent,
  516. answer_duration: this.currentAnswer.answerDuration || 0,
  517. tenant_id: 1
  518. };
  519. console.log('提交数据:', submitData);
  520. // 调用API提交答案
  521. const res = await this.$http.post(`${apiBaseUrl}/api/job/submit_answer`, submitData);
  522. console.log('提交答案响应:', res);
  523. return res;
  524. } catch (error) {
  525. console.error('提交答案失败:', error);
  526. // 显示错误提示
  527. uni.showToast({
  528. title: '提交答案失败,请重试',
  529. icon: 'none'
  530. });
  531. throw error;
  532. } finally {
  533. // 隐藏加载提示
  534. uni.hideLoading();
  535. // 重置提交状态
  536. this.isSubmitting = false;
  537. }
  538. },
  539. // 修改 goToNextQuestion 方法,添加 async 关键字
  540. async goToNextQuestion() {
  541. // 重置状态
  542. this.showResult = false;
  543. this.selectedOption = null;
  544. this.selectedOptions = [];
  545. this.openQuestionAnswer = ''; // 重置开放问题答案
  546. // 前往下一题
  547. this.currentQuestionIndex++;
  548. // 如果已经有下一题的详情,直接使用
  549. if (this.questions[this.currentQuestionIndex]) {
  550. // 更新进度
  551. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  552. // 重置计时器
  553. this.resetTimer();
  554. // 播放AI介绍下一题
  555. this.playAiSpeaking();
  556. setTimeout(() => {
  557. this.pauseAiSpeaking();
  558. }, 2000);
  559. return;
  560. }
  561. // 否则请求下一题详情
  562. try {
  563. this.loading = true;
  564. // 假设您有一个获取单个题目详情的API
  565. // const res = await getQuestionDetail({
  566. // interviewId: this.interviewId,
  567. // questionIndex: this.currentQuestionIndex
  568. // });
  569. // 模拟API调用
  570. await new Promise(resolve => setTimeout(resolve, 1000));
  571. const res = { /* 模拟的题目数据 */ };
  572. // 处理题目数据
  573. if (res) {
  574. const formattedQuestion = {
  575. id: res.id || this.currentQuestionIndex + 1,
  576. text: res.question || '未知问题',
  577. options: res.options || [],
  578. correctAnswer: res.correctAnswer || 0,
  579. isImportant: res.is_system || false,
  580. explanation: res.explanation || '',
  581. questionType: res.question_form || 1,
  582. questionTypeName: res.question_form_name || '单选题',
  583. correctAnswers: res.correct_answers || [],
  584. difficulty: res.difficulty || 1,
  585. difficultyName: res.difficulty_name || '初级'
  586. };
  587. // 添加到问题列表
  588. this.questions.push(formattedQuestion);
  589. }
  590. } catch (error) {
  591. console.error('获取题目详情失败:', error);
  592. uni.showToast({
  593. title: '获取题目失败,请重试',
  594. icon: 'none'
  595. });
  596. // 出错时回到上一题
  597. this.currentQuestionIndex--;
  598. } finally {
  599. this.loading = false;
  600. // 更新进度
  601. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  602. // 重置计时器
  603. this.resetTimer();
  604. }
  605. },
  606. toggleSettings() {
  607. // 打开设置面板
  608. uni.showToast({
  609. title: '设置功能开发中',
  610. icon: 'none'
  611. });
  612. },
  613. back(target) {
  614. // 清除计时器
  615. if (this.timerInterval) {
  616. clearInterval(this.timerInterval);
  617. }
  618. // 如果指定了跳转目标
  619. if (target) {
  620. uni.navigateTo({
  621. url: target
  622. });
  623. return;
  624. }
  625. // 否则尝试返回上一页,如果失败则跳转到首页
  626. // try {
  627. // const pages = getCurrentPages();
  628. // if (pages.length > 1) {
  629. // uni.reLaunch({
  630. // url: '/pages/index/index'
  631. // });
  632. // } else {
  633. // // 如果当前是第一页,则跳转到首页
  634. // uni.reLaunch({
  635. // url: '/pages/index/index'
  636. // });
  637. // }
  638. // } catch (e) {
  639. // console.error('导航错误:', e);
  640. // // 出错时也跳转到首页
  641. // uni.reLaunch({
  642. // url: '/pages/index/index'
  643. // });
  644. // }
  645. },
  646. error(e) {
  647. // 相机错误事件
  648. console.error(e.detail);
  649. uni.showToast({
  650. title: '相机启动失败,请检查权限设置',
  651. icon: 'none'
  652. });
  653. },
  654. playAiSpeaking() {
  655. if (this.useVideo && this.aiVideoContext) {
  656. this.aiVideoContext.play();
  657. }
  658. // 触发数字人说话动画
  659. if (this.digitalHumanUrl) {
  660. // 假设当前问题的文本作为数字人要说的内容
  661. const speakText = this.currentQuestion ? this.currentQuestion.text : '';
  662. this.interactWithDigitalHuman(speakText);
  663. }
  664. },
  665. pauseAiSpeaking() {
  666. if (this.useVideo && this.aiVideoContext) {
  667. this.aiVideoContext.pause();
  668. }
  669. },
  670. // 修改 restartTest 方法,添加可选的跳转目标
  671. restartTest(target) {
  672. this.currentQuestionIndex = 0;
  673. this.score = 0;
  674. this.showEndModal = false;
  675. this.showResult = false;
  676. this.selectedOption = null;
  677. this.selectedOptions = [];
  678. this.resetTimer();
  679. // 如果指定了跳转目标
  680. if (target) {
  681. uni.navigateTo({
  682. url: target
  683. });
  684. }
  685. },
  686. // 在methods中添加测试方法
  687. testEndScreen() {
  688. this.interviewCompleted = true;
  689. this.showEndModal = false;
  690. },
  691. // 初始化数字人
  692. initDigitalHuman() {
  693. // 移除不可用的示例URL
  694. // this.digitalHumanUrl = 'https://your-digital-human-service.com/avatar?id=123';
  695. // 暂时不使用数字人服务,直接使用本地图片
  696. this.digitalHumanUrl = '';
  697. // 如果您有实际可用的数字人服务,可以在这里设置
  698. // 例如:this.digitalHumanUrl = 'https://your-actual-service.com/avatar';
  699. // 或者使用本地HTML文件(如果有的话)
  700. // this.digitalHumanUrl = '/hybrid/html/digital-human.html';
  701. },
  702. // 与数字人交互的方法
  703. interactWithDigitalHuman(message) {
  704. // 如果数字人是通过web-view加载的,可以使用postMessage与其通信
  705. const webview = this.$mp.page.$getAppWebview().children()[0];
  706. if (webview) {
  707. webview.evalJS(`receiveMessage('${message}')`);
  708. }
  709. },
  710. // 添加 navigateToInterview 方法
  711. navigateToInterview() {
  712. // 关闭模态框
  713. this.showEndModal = false;
  714. // 跳转到interview页面
  715. uni.navigateTo({
  716. url: '/pages/interview/interview',
  717. success: () => {
  718. console.log('成功跳转到interview页面');
  719. },
  720. fail: (err) => {
  721. console.error('跳转失败:', err);
  722. uni.showToast({
  723. title: '跳转失败,请手动返回首页',
  724. icon: 'none'
  725. });
  726. }
  727. });
  728. },
  729. // 添加 handleCameraError 方法
  730. handleCameraError(e) {
  731. console.error('相机错误:', e.detail);
  732. uni.showToast({
  733. title: '相机启动失败,请检查权限设置',
  734. icon: 'none'
  735. });
  736. },
  737. },
  738. // 添加生命周期钩子,确保在组件销毁时清除计时器
  739. beforeDestroy() {
  740. if (this.timerInterval) {
  741. clearInterval(this.timerInterval);
  742. }
  743. }
  744. }
  745. </script>
  746. <style>
  747. .camera-container {
  748. position: relative;
  749. width: 100%;
  750. height: 100vh;
  751. background-color: #ffffff;
  752. display: flex;
  753. flex-direction: column;
  754. overflow: hidden; /* 防止内容溢出 */
  755. }
  756. .header {
  757. height: 90rpx;
  758. /* background-color: #000000; */
  759. display: flex;
  760. align-items: center;
  761. padding: 0 30rpx;
  762. /* color: #ffffff; */
  763. }
  764. .back-button {
  765. width: 60rpx;
  766. height: 60rpx;
  767. display: flex;
  768. align-items: center;
  769. justify-content: center;
  770. }
  771. .title {
  772. flex: 1;
  773. text-align: center;
  774. font-size: 32rpx;
  775. font-weight: bold;
  776. }
  777. .controls {
  778. display: flex;
  779. gap: 30rpx;
  780. }
  781. .iconfont {
  782. font-size: 40rpx;
  783. }
  784. .content {
  785. flex: 1;
  786. display: flex;
  787. flex-direction: column;
  788. position: relative;
  789. padding: 50rpx 20rpx 0;
  790. }
  791. .progress-container {
  792. display: flex;
  793. flex-direction: column;
  794. justify-content: space-between;
  795. margin-bottom: 20rpx;
  796. }
  797. .progress-step {
  798. display: flex;
  799. align-items: center;
  800. margin-bottom: 10rpx;
  801. }
  802. .step-circle {
  803. width: 40rpx;
  804. height: 40rpx;
  805. border-radius: 50%;
  806. background-color: #e0e0e0;
  807. display: flex;
  808. align-items: center;
  809. justify-content: center;
  810. font-size: 24rpx;
  811. margin-right: 10rpx;
  812. }
  813. .step-circle.active {
  814. background-color: #ff9500;
  815. color: #ffffff;
  816. }
  817. .step-text {
  818. font-size: 24rpx;
  819. color: #666666;
  820. }
  821. .progress-bar {
  822. height: 10rpx;
  823. background-color: #e0e0e0;
  824. border-radius: 5rpx;
  825. overflow: hidden;
  826. margin-bottom: 20rpx;
  827. }
  828. .progress-fill {
  829. height: 100%;
  830. background-color: #00c853;
  831. transition: width 0.3s;
  832. }
  833. .interview-content {
  834. flex: 1;
  835. display: flex;
  836. flex-direction: column;
  837. }
  838. .video-area {
  839. display: flex;
  840. /* flex-direction: column; */
  841. justify-content: space-between;
  842. height: auto;
  843. margin-bottom: 20rpx;
  844. }
  845. .video-camera {
  846. display: flex;
  847. flex-direction: row;
  848. align-items: center;
  849. justify-content: space-between;
  850. width: 65%;
  851. }
  852. .interviewer {
  853. background-color: #f5f5f5;
  854. display: flex;
  855. align-items: center;
  856. justify-content: center;
  857. position: relative;
  858. width: 48% !important;
  859. height: 300rpx;
  860. }
  861. .interviewer-video {
  862. width: 100%;
  863. height: 100%;
  864. object-fit: cover;
  865. }
  866. .interviewer-avatar {
  867. width: 80%;
  868. height: 80%;
  869. object-fit: contain;
  870. }
  871. .user-camera-container {
  872. width: 48%;
  873. height: 300rpx;
  874. border-radius: 10rpx;
  875. overflow: hidden;
  876. border: 4rpx solid #ffffff;
  877. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
  878. }
  879. .question-area {
  880. flex: 1;
  881. display: flex;
  882. flex-direction: column;
  883. }
  884. .question-number {
  885. font-size: 28rpx;
  886. color: #666;
  887. margin-bottom: 20rpx;
  888. }
  889. .question-importance {
  890. background-color: #f9f9f9;
  891. padding: 20rpx;
  892. border-radius: 10rpx;
  893. margin-bottom: 20rpx;
  894. font-size: 28rpx;
  895. color: #333;
  896. }
  897. .question-importance text {
  898. color: #ff0000;
  899. margin-right: 10rpx;
  900. }
  901. .options {
  902. display: flex;
  903. flex-direction: column;
  904. gap: 20rpx;
  905. margin-bottom: 30rpx;
  906. }
  907. .option-item {
  908. padding: 20rpx;
  909. background-color: #f5f5f5;
  910. border-radius: 10rpx;
  911. font-size: 28rpx;
  912. transition: all 0.3s;
  913. border: 1px solid #e0e0e0;
  914. display: flex;
  915. align-items: center;
  916. }
  917. .option-selected {
  918. background-color: #e6f7ff;
  919. border: 1px solid #1890ff;
  920. }
  921. .option-correct {
  922. background-color: #d4f7d4;
  923. border: 1px solid #52c41a;
  924. }
  925. .option-wrong {
  926. background-color: #fff1f0;
  927. border: 1px solid #ff4d4f;
  928. }
  929. .option-text {
  930. color: #333;
  931. }
  932. .timer-container {
  933. text-align: right;
  934. margin: 20rpx 0;
  935. }
  936. .timer-text {
  937. font-size: 24rpx;
  938. color: #666;
  939. background-color: #f0f0f0;
  940. padding: 6rpx 12rpx;
  941. border-radius: 6rpx;
  942. }
  943. .bottom-button-container {
  944. position: fixed;
  945. bottom: 50px;
  946. left: 5%;
  947. right: 0;
  948. padding: 20rpx;
  949. display: flex;
  950. justify-content: center;
  951. z-index: 10;
  952. }
  953. .bottom-button-wrapper {
  954. display: flex;
  955. width: 100%;
  956. justify-content: space-between;
  957. align-items: center;
  958. }
  959. .timer-display {
  960. font-size: 28rpx;
  961. color: #666;
  962. padding: 10rpx 20rpx;
  963. border-radius: 6rpx;
  964. }
  965. .next-button {
  966. background-color: #0039b3;
  967. color: #ffffff;
  968. border-radius: 10rpx;
  969. font-size: 30rpx;
  970. height: 80rpx;
  971. line-height: 80rpx;
  972. width: 90%;
  973. text-align: center;
  974. margin: 0;
  975. }
  976. .footer {
  977. height: 100rpx;
  978. background-color: #000000;
  979. display: flex;
  980. justify-content: space-between;
  981. align-items: center;
  982. padding: 0 30rpx;
  983. color: #ffffff;
  984. }
  985. .timer {
  986. font-size: 28rpx;
  987. }
  988. .next-step {
  989. font-size: 28rpx;
  990. }
  991. .interview-end-modal {
  992. position: fixed;
  993. top: 0;
  994. left: 0;
  995. right: 0;
  996. bottom: 0;
  997. background-color: rgba(0, 0, 0, 0.5);
  998. display: flex;
  999. align-items: center;
  1000. justify-content: center;
  1001. z-index: 999;
  1002. }
  1003. .modal-content {
  1004. width: 80%;
  1005. background-color: #ffffff;
  1006. border-radius: 20rpx;
  1007. padding: 40rpx;
  1008. display: flex;
  1009. flex-direction: column;
  1010. align-items: center;
  1011. }
  1012. .modal-title {
  1013. font-size: 36rpx;
  1014. font-weight: bold;
  1015. margin-bottom: 30rpx;
  1016. }
  1017. .score-display {
  1018. font-size: 48rpx;
  1019. font-weight: bold;
  1020. color: #0039b3;
  1021. margin: 20rpx 0;
  1022. }
  1023. .modal-message {
  1024. font-size: 28rpx;
  1025. color: #666;
  1026. text-align: center;
  1027. margin-bottom: 40rpx;
  1028. }
  1029. .modal-buttons {
  1030. display: flex;
  1031. justify-content: space-between;
  1032. width: 100%;
  1033. margin-top: 20rpx;
  1034. }
  1035. .modal-button {
  1036. width: 45%;
  1037. }
  1038. @keyframes speaking {
  1039. 0% {
  1040. opacity: 0.8;
  1041. }
  1042. 50% {
  1043. opacity: 1;
  1044. }
  1045. 100% {
  1046. opacity: 0.8;
  1047. }
  1048. }
  1049. .speaking {
  1050. animation: speaking 1.5s infinite;
  1051. }
  1052. .digital-avatar {
  1053. display: flex;
  1054. flex-direction: row;
  1055. align-items: flex-start;
  1056. justify-content: space-between;
  1057. width: 100%;
  1058. margin-bottom: 20rpx;
  1059. }
  1060. .message-box {
  1061. width: 65%;
  1062. min-height: 120rpx;
  1063. background-color: #f5f5f5;
  1064. border-radius: 10rpx;
  1065. padding: 15rpx;
  1066. margin-right: 20rpx;
  1067. display: flex;
  1068. align-items: center;
  1069. }
  1070. .message-text {
  1071. font-size: 24rpx;
  1072. color: #333;
  1073. line-height: 1.5;
  1074. }
  1075. .user-avatar {
  1076. width: 110px;
  1077. height: 160px;
  1078. border-radius: 10rpx;
  1079. overflow: hidden;
  1080. }
  1081. .camera {
  1082. width: 100%;
  1083. height: 100%;
  1084. border-radius: 10rpx;
  1085. border: 2rpx solid #e0e0e0;
  1086. }
  1087. .avatar-image {
  1088. width: 100%;
  1089. height: 100%;
  1090. border-radius: 10rpx;
  1091. border: 2rpx solid #e0e0e0;
  1092. }
  1093. .digital-human-webview {
  1094. width: 100%;
  1095. height: 100%;
  1096. border-radius: 10rpx;
  1097. border: 2rpx solid #e0e0e0;
  1098. }
  1099. .interview-complete-screen {
  1100. position: fixed;
  1101. top: 0;
  1102. left: 0;
  1103. right: 0;
  1104. bottom: 0;
  1105. background-color: #f0f5ff;
  1106. display: flex;
  1107. flex-direction: column;
  1108. align-items: center;
  1109. justify-content: center;
  1110. z-index: 1000;
  1111. padding: 40rpx;
  1112. }
  1113. .digital-avatar-large {
  1114. width: 200rpx;
  1115. height: 200rpx;
  1116. margin-bottom: 60rpx;
  1117. }
  1118. .avatar-image-large {
  1119. width: 100%;
  1120. height: 100%;
  1121. border-radius: 30rpx;
  1122. }
  1123. .complete-message {
  1124. font-size: 40rpx;
  1125. font-weight: bold;
  1126. color: #333;
  1127. margin-bottom: 30rpx;
  1128. }
  1129. .complete-description {
  1130. font-size: 28rpx;
  1131. color: #666;
  1132. text-align: center;
  1133. margin-bottom: 80rpx;
  1134. line-height: 1.5;
  1135. }
  1136. .complete-button {
  1137. background-color: #0039b3;
  1138. color: #ffffff;
  1139. border-radius: 10rpx;
  1140. font-size: 32rpx;
  1141. padding: 20rpx 60rpx;
  1142. margin-top: 40rpx;
  1143. }
  1144. /* 添加加载状态样式 */
  1145. .loading-container {
  1146. position: absolute;
  1147. top: 0;
  1148. left: 0;
  1149. right: 0;
  1150. bottom: 0;
  1151. display: flex;
  1152. flex-direction: column;
  1153. align-items: center;
  1154. justify-content: center;
  1155. background-color: rgba(255, 255, 255, 0.9);
  1156. z-index: 100;
  1157. }
  1158. .loading-text {
  1159. margin-top: 20rpx;
  1160. font-size: 28rpx;
  1161. color: #666;
  1162. }
  1163. .error-container {
  1164. text-align: center;
  1165. }
  1166. .error-message {
  1167. font-size: 28rpx;
  1168. color: #ff4d4f;
  1169. margin-bottom: 30rpx;
  1170. }
  1171. .retry-button {
  1172. background-color: #0039b3;
  1173. color: #ffffff;
  1174. border-radius: 10rpx;
  1175. font-size: 28rpx;
  1176. padding: 10rpx 30rpx;
  1177. }
  1178. .question-type {
  1179. display: inline-block;
  1180. /* background-color: #0039b3; */
  1181. color: white;
  1182. font-size: 24rpx;
  1183. padding: 4rpx 10rpx;
  1184. border-radius: 6rpx;
  1185. margin-right: 10rpx;
  1186. }
  1187. /* 新增的开放问题样式 */
  1188. .open-question-container {
  1189. display: flex;
  1190. flex-direction: column;
  1191. width: 100%;
  1192. margin-bottom: 20rpx;
  1193. }
  1194. .open-question-input {
  1195. width: 100%;
  1196. height: 300rpx;
  1197. background-color: #f9f9f9;
  1198. border: 1px solid #e0e0e0;
  1199. border-radius: 10rpx;
  1200. padding: 20rpx;
  1201. font-size: 28rpx;
  1202. color: #333;
  1203. }
  1204. .word-count {
  1205. text-align: right;
  1206. font-size: 24rpx;
  1207. color: #999;
  1208. margin-top: 10rpx;
  1209. }
  1210. .option-prefix {
  1211. margin-right: 10rpx;
  1212. font-weight: bold;
  1213. color: #333;
  1214. min-width: 30rpx;
  1215. }
  1216. /* 新增的图片显示样式 */
  1217. .question-image-container {
  1218. width: 100%;
  1219. margin: 20rpx 0;
  1220. display: flex;
  1221. justify-content: center;
  1222. align-items: center;
  1223. }
  1224. .question-image {
  1225. width: 100%;
  1226. max-width: 600rpx;
  1227. border-radius: 10rpx;
  1228. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  1229. }
  1230. /* 添加滚动视图样式 */
  1231. .content-scroll {
  1232. flex: 1;
  1233. height: calc(100vh - 130px); /* 减去底部按钮的高度 */
  1234. overflow-y: auto;
  1235. }
  1236. /* 添加底部空白区域,防止内容被固定按钮遮挡 */
  1237. .bottom-space {
  1238. height: 130px; /* 与底部按钮高度一致 */
  1239. }
  1240. </style>