camera.vue 36 KB

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