camera.vue 32 KB

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