camera.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_user = require("../../api/user.js");
  4. const common_config = require("../../common/config.js");
  5. const common_assets = require("../../common/assets.js");
  6. const _sfc_main = {
  7. data() {
  8. return {
  9. cameraContext: null,
  10. devicePosition: "front",
  11. interviewStarted: true,
  12. showEndModal: false,
  13. currentQuestionIndex: 0,
  14. currentStep: 1,
  15. stepTexts: ["测试准备", "回答问题", "测试结束"],
  16. progressWidth: 50,
  17. remainingTime: "00:27",
  18. selectedOption: null,
  19. selectedOptions: [],
  20. showResult: false,
  21. isAnswerCorrect: false,
  22. questions: [],
  23. // 改为空数组,将通过API获取
  24. interviewId: null,
  25. // 存储当前面试ID
  26. useVideo: false,
  27. timerInterval: null,
  28. score: 0,
  29. totalQuestions: 0,
  30. interviewCompleted: false,
  31. digitalHumanUrl: "",
  32. // 数字人URL
  33. loading: true,
  34. // 添加加载状态
  35. loadError: false,
  36. // 添加加载错误状态
  37. errorMessage: "",
  38. // 添加错误消息
  39. answers: [],
  40. // 存储用户的所有答案
  41. currentQuestionDetail: null,
  42. // 当前题目详情
  43. isSubmitting: false,
  44. // 是否正在提交答案
  45. openQuestionAnswer: "",
  46. // 存储开放问题的答案
  47. currentAnswer: null,
  48. // 存储当前答案以便提交
  49. questionStartTime: null
  50. // 存储问题开始时间
  51. };
  52. },
  53. computed: {
  54. currentQuestion() {
  55. console.log(this.questions[this.currentQuestionIndex]);
  56. return this.questions[this.currentQuestionIndex];
  57. }
  58. },
  59. onLoad(options) {
  60. if (options && options.id) {
  61. this.interviewId = options.id;
  62. this.fetchInterviewData();
  63. } else {
  64. this.fetchInterviewList();
  65. }
  66. },
  67. onReady() {
  68. this.cameraContext = common_vendor.index.createCameraContext();
  69. if (this.useVideo) {
  70. this.aiVideoContext = common_vendor.index.createVideoContext("aiInterviewer");
  71. }
  72. this.initDigitalHuman();
  73. },
  74. methods: {
  75. // 获取面试列表
  76. async fetchInterviewList() {
  77. try {
  78. this.loading = true;
  79. const res2 = await api_user.getInterviewList({ job_id: JSON.parse(common_vendor.index.getStorageSync("selectedJob")).id });
  80. console.log(res2);
  81. this.interviewId = res2;
  82. this.fetchInterviewData(res2);
  83. } catch (error) {
  84. console.error("获取面试列表失败:", error);
  85. this.handleLoadError("获取面试列表失败");
  86. }
  87. },
  88. // 获取面试详情数据
  89. async fetchInterviewData(data) {
  90. try {
  91. this.loading = true;
  92. if (data && Array.isArray(data)) {
  93. this.questions = data.map((q, index) => ({
  94. id: q.id || index + 1,
  95. text: q.question || "未知问题",
  96. options: q.options || [],
  97. correctAnswer: q.correctAnswer || 0,
  98. isImportant: q.is_system || false,
  99. explanation: q.explanation || "",
  100. questionType: q.question_form,
  101. questionTypeName: q.question_form_name || "单选题",
  102. correctAnswers: q.correct_answers || [],
  103. difficulty: q.difficulty || 1,
  104. difficultyName: q.difficulty_name || "初级"
  105. }));
  106. } else {
  107. this.processInterviewData(res);
  108. }
  109. console.log(this.questions);
  110. this.totalQuestions = this.questions.length;
  111. if (this.questions.length > 0) {
  112. this.startTimer();
  113. }
  114. } catch (error) {
  115. console.error("获取面试详情失败:", error);
  116. this.handleLoadError("获取面试详情失败");
  117. } finally {
  118. this.loading = false;
  119. }
  120. },
  121. // 处理面试数据
  122. processInterviewData(data) {
  123. this.questions = [];
  124. if (data) {
  125. const formattedQuestion = {
  126. id: data.id || 1,
  127. text: data.question || "未知问题",
  128. options: data.options || [],
  129. correctAnswer: data.correctAnswer || 0,
  130. isImportant: data.is_system || false,
  131. explanation: data.explanation || "",
  132. questionType: data.question_form,
  133. // 1-单选题,2-多选题
  134. questionTypeName: data.question_form_name || "单选题",
  135. correctAnswers: data.correct_answers || [],
  136. difficulty: data.difficulty || 1,
  137. difficultyName: data.difficulty_name || "初级"
  138. };
  139. this.questions.push(formattedQuestion);
  140. this.totalQuestions = this.questions.length;
  141. this.startTimer();
  142. } else {
  143. this.handleLoadError("面试中没有问题");
  144. }
  145. },
  146. // 处理加载错误
  147. handleLoadError(message) {
  148. this.loadError = true;
  149. this.loading = false;
  150. this.errorMessage = message || "加载失败";
  151. common_vendor.index.showToast({
  152. title: message || "加载失败",
  153. icon: "none",
  154. duration: 2e3
  155. });
  156. },
  157. startTimer() {
  158. if (this.questions.length === 0)
  159. return;
  160. this.questionStartTime = /* @__PURE__ */ new Date();
  161. let seconds = 30;
  162. this.timerInterval = setInterval(() => {
  163. seconds--;
  164. if (seconds <= 0) {
  165. clearInterval(this.timerInterval);
  166. if (!this.showResult) {
  167. this.checkAnswer();
  168. }
  169. }
  170. const min = Math.floor(seconds / 60).toString().padStart(2, "0");
  171. const sec = (seconds % 60).toString().padStart(2, "0");
  172. this.remainingTime = `${min}:${sec}`;
  173. }, 1e3);
  174. },
  175. resetTimer() {
  176. clearInterval(this.timerInterval);
  177. this.startTimer();
  178. },
  179. selectOption(index) {
  180. if (this.showResult)
  181. return;
  182. if (this.currentQuestion.questionType === 2) {
  183. const optionIndex = this.selectedOptions.indexOf(index);
  184. if (optionIndex > -1) {
  185. this.selectedOptions.splice(optionIndex, 1);
  186. } else {
  187. this.selectedOptions.push(index);
  188. }
  189. } else if (this.currentQuestion.questionType === 1) {
  190. this.selectedOption = index;
  191. }
  192. this.playAiSpeaking();
  193. },
  194. checkAnswer() {
  195. clearInterval(this.timerInterval);
  196. if (!this.currentQuestion) {
  197. console.error("当前问题不存在");
  198. return;
  199. }
  200. if (this.currentQuestion.questionType === 0) {
  201. this.isAnswerCorrect = true;
  202. } else if (this.currentQuestion.questionType === 2) {
  203. const sortedSelected = [...this.selectedOptions].sort();
  204. const sortedCorrect = [...this.currentQuestion.correctAnswers].sort();
  205. if (sortedSelected.length !== sortedCorrect.length) {
  206. this.isAnswerCorrect = false;
  207. } else {
  208. this.isAnswerCorrect = sortedSelected.every((value, index) => value === sortedCorrect[index]);
  209. }
  210. } else {
  211. this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
  212. }
  213. this.showResult = true;
  214. },
  215. nextQuestion(data) {
  216. if (!this.showResult) {
  217. this.checkAnswer();
  218. this.saveAnswer(data);
  219. this.submitCurrentAnswer(data).then(() => {
  220. this.showResult = true;
  221. }).catch((error) => {
  222. console.error("提交答案失败:", error);
  223. common_vendor.index.showToast({
  224. title: "提交答案失败,请重试",
  225. icon: "none"
  226. });
  227. this.showResult = true;
  228. });
  229. return;
  230. }
  231. if (this.currentQuestionIndex >= this.questions.length - 1) {
  232. common_vendor.index.navigateTo({
  233. url: "/pages/interview/interview",
  234. // 假设这是手部照片采集页面的路径
  235. success: () => {
  236. console.log("成功跳转到手部照片采集页面");
  237. },
  238. fail: (err) => {
  239. console.error("跳转失败:", err);
  240. common_vendor.index.showToast({
  241. title: "跳转失败,请手动返回首页",
  242. icon: "none"
  243. });
  244. }
  245. });
  246. return;
  247. }
  248. this.goToNextQuestion();
  249. },
  250. // 保存当前题目的答案
  251. saveAnswer() {
  252. let answer;
  253. if (this.currentQuestion.questionType === 0) {
  254. answer = {
  255. questionId: this.currentQuestion.id,
  256. questionType: this.currentQuestion.questionType,
  257. answer: this.openQuestionAnswer,
  258. answerDuration: this.getAnswerDuration()
  259. // 添加答题时长
  260. };
  261. } else {
  262. answer = {
  263. questionId: this.currentQuestion.id,
  264. questionType: this.currentQuestion.questionType,
  265. answer: this.currentQuestion.questionType === 1 ? this.selectedOption : this.selectedOptions,
  266. answerDuration: this.getAnswerDuration()
  267. // 添加答题时长
  268. };
  269. }
  270. const existingIndex = this.answers.findIndex((a) => a.questionId === answer.questionId);
  271. if (existingIndex > -1) {
  272. this.answers[existingIndex] = answer;
  273. } else {
  274. this.answers.push(answer);
  275. }
  276. this.currentAnswer = answer;
  277. console.log("已保存答案:", this.answers);
  278. },
  279. // 获取答题时长(秒)
  280. getAnswerDuration() {
  281. const remainingTimeArr = this.remainingTime.split(":");
  282. const remainingSeconds = parseInt(remainingTimeArr[0]) * 60 + parseInt(remainingTimeArr[1]);
  283. return 30 - remainingSeconds;
  284. },
  285. // 提交当前答案
  286. async submitCurrentAnswer() {
  287. if (!this.currentAnswer)
  288. return;
  289. try {
  290. common_vendor.index.showLoading({
  291. title: "正在提交答案..."
  292. });
  293. let answerContent = "";
  294. if (this.currentAnswer.questionType === 0) {
  295. answerContent = this.currentAnswer.answer;
  296. } else if (this.currentAnswer.questionType === 1) {
  297. const selectedIndex = this.currentAnswer.answer;
  298. const selectedOption = this.currentQuestion.options[selectedIndex];
  299. answerContent = selectedOption.id ? selectedOption.id.toString() : selectedIndex.toString();
  300. } else if (this.currentAnswer.questionType === 2) {
  301. const selectedIndices = this.currentAnswer.answer;
  302. const selectedOptionIds = selectedIndices.map((index) => {
  303. const option = this.currentQuestion.options[index];
  304. return option.id ? option.id : index;
  305. });
  306. answerContent = selectedOptionIds.join(",");
  307. }
  308. const submitData = {
  309. application_id: 1,
  310. // 或者使用其他合适的ID
  311. question_id: this.currentAnswer.questionId,
  312. answer_content: answerContent,
  313. answer_duration: this.currentAnswer.answerDuration || 0,
  314. // 如果需要tenant_id,请在这里添加
  315. tenant_id: 1
  316. };
  317. console.log("提交数据:", submitData);
  318. const res2 = await this.$http.post(`${common_config.apiBaseUrl}/api/job/submit_answer`, submitData);
  319. console.log("提交答案响应:", res2);
  320. common_vendor.index.hideLoading();
  321. return res2;
  322. } catch (error) {
  323. console.error("提交答案失败:", error);
  324. common_vendor.index.hideLoading();
  325. common_vendor.index.showToast({
  326. title: "提交答案失败,请重试",
  327. icon: "none"
  328. });
  329. throw error;
  330. }
  331. },
  332. // 修改 goToNextQuestion 方法,添加 async 关键字
  333. async goToNextQuestion() {
  334. this.showResult = false;
  335. this.selectedOption = null;
  336. this.selectedOptions = [];
  337. this.openQuestionAnswer = "";
  338. this.currentQuestionIndex++;
  339. if (this.questions[this.currentQuestionIndex]) {
  340. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  341. this.resetTimer();
  342. this.playAiSpeaking();
  343. setTimeout(() => {
  344. this.pauseAiSpeaking();
  345. }, 2e3);
  346. return;
  347. }
  348. try {
  349. this.loading = true;
  350. await new Promise((resolve) => setTimeout(resolve, 1e3));
  351. const res2 = {
  352. /* 模拟的题目数据 */
  353. };
  354. if (res2) {
  355. const formattedQuestion = {
  356. id: res2.id || this.currentQuestionIndex + 1,
  357. text: res2.question || "未知问题",
  358. options: res2.options || [],
  359. correctAnswer: res2.correctAnswer || 0,
  360. isImportant: res2.is_system || false,
  361. explanation: res2.explanation || "",
  362. questionType: res2.question_form || 1,
  363. questionTypeName: res2.question_form_name || "单选题",
  364. correctAnswers: res2.correct_answers || [],
  365. difficulty: res2.difficulty || 1,
  366. difficultyName: res2.difficulty_name || "初级"
  367. };
  368. this.questions.push(formattedQuestion);
  369. }
  370. } catch (error) {
  371. console.error("获取题目详情失败:", error);
  372. common_vendor.index.showToast({
  373. title: "获取题目失败,请重试",
  374. icon: "none"
  375. });
  376. this.currentQuestionIndex--;
  377. } finally {
  378. this.loading = false;
  379. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  380. this.resetTimer();
  381. }
  382. },
  383. toggleSettings() {
  384. common_vendor.index.showToast({
  385. title: "设置功能开发中",
  386. icon: "none"
  387. });
  388. },
  389. back(target) {
  390. if (this.timerInterval) {
  391. clearInterval(this.timerInterval);
  392. }
  393. if (target) {
  394. common_vendor.index.navigateTo({
  395. url: target
  396. });
  397. return;
  398. }
  399. try {
  400. const pages = getCurrentPages();
  401. if (pages.length > 1) {
  402. common_vendor.index.reLaunch({
  403. url: "/pages/index/index"
  404. });
  405. } else {
  406. common_vendor.index.reLaunch({
  407. url: "/pages/index/index"
  408. });
  409. }
  410. } catch (e) {
  411. console.error("导航错误:", e);
  412. common_vendor.index.reLaunch({
  413. url: "/pages/index/index"
  414. });
  415. }
  416. },
  417. error(e) {
  418. console.error(e.detail);
  419. common_vendor.index.showToast({
  420. title: "相机启动失败,请检查权限设置",
  421. icon: "none"
  422. });
  423. },
  424. playAiSpeaking() {
  425. if (this.useVideo && this.aiVideoContext) {
  426. this.aiVideoContext.play();
  427. }
  428. if (this.digitalHumanUrl) {
  429. const speakText = this.currentQuestion ? this.currentQuestion.text : "";
  430. this.interactWithDigitalHuman(speakText);
  431. }
  432. },
  433. pauseAiSpeaking() {
  434. if (this.useVideo && this.aiVideoContext) {
  435. this.aiVideoContext.pause();
  436. }
  437. },
  438. // 修改 restartTest 方法,添加可选的跳转目标
  439. restartTest(target) {
  440. this.currentQuestionIndex = 0;
  441. this.score = 0;
  442. this.showEndModal = false;
  443. this.showResult = false;
  444. this.selectedOption = null;
  445. this.selectedOptions = [];
  446. this.resetTimer();
  447. if (target) {
  448. common_vendor.index.navigateTo({
  449. url: target
  450. });
  451. }
  452. },
  453. // 在methods中添加测试方法
  454. testEndScreen() {
  455. this.interviewCompleted = true;
  456. this.showEndModal = false;
  457. },
  458. // 初始化数字人
  459. initDigitalHuman() {
  460. this.digitalHumanUrl = "";
  461. },
  462. // 与数字人交互的方法
  463. interactWithDigitalHuman(message) {
  464. const webview = this.$mp.page.$getAppWebview().children()[0];
  465. if (webview) {
  466. webview.evalJS(`receiveMessage('${message}')`);
  467. }
  468. },
  469. // 添加 navigateToInterview 方法
  470. navigateToInterview() {
  471. this.showEndModal = false;
  472. common_vendor.index.navigateTo({
  473. url: "/pages/interview/interview",
  474. success: () => {
  475. console.log("成功跳转到interview页面");
  476. },
  477. fail: (err) => {
  478. console.error("跳转失败:", err);
  479. common_vendor.index.showToast({
  480. title: "跳转失败,请手动返回首页",
  481. icon: "none"
  482. });
  483. }
  484. });
  485. }
  486. },
  487. // 添加生命周期钩子,确保在组件销毁时清除计时器
  488. beforeDestroy() {
  489. if (this.timerInterval) {
  490. clearInterval(this.timerInterval);
  491. }
  492. }
  493. };
  494. if (!Array) {
  495. const _component_uni_load_more = common_vendor.resolveComponent("uni-load-more");
  496. _component_uni_load_more();
  497. }
  498. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  499. return common_vendor.e({
  500. a: $data.digitalHumanUrl
  501. }, $data.digitalHumanUrl ? {
  502. b: $data.digitalHumanUrl
  503. } : {
  504. c: common_assets._imports_0
  505. }, {
  506. d: common_vendor.t($data.currentQuestionIndex + 1),
  507. e: common_vendor.t($data.questions.length),
  508. f: $options.currentQuestion.isImportant
  509. }, $options.currentQuestion.isImportant ? {} : {}, {
  510. g: common_vendor.t($options.currentQuestion.questionTypeName),
  511. h: common_vendor.t($options.currentQuestion.text),
  512. i: $options.currentQuestion.questionType == 0
  513. }, $options.currentQuestion.questionType == 0 ? {
  514. j: $data.openQuestionAnswer,
  515. k: common_vendor.o(($event) => $data.openQuestionAnswer = $event.detail.value),
  516. l: common_vendor.t($data.openQuestionAnswer.length)
  517. } : {
  518. m: common_vendor.f($options.currentQuestion.options, (option, index, i0) => {
  519. return {
  520. a: common_vendor.t(option.option_text || (typeof option === "string" ? option : JSON.stringify(option))),
  521. b: index,
  522. c: ($options.currentQuestion.questionType === 1 ? $data.selectedOption === index : $data.selectedOptions.includes(index)) ? 1 : "",
  523. d: $data.showResult && ($options.currentQuestion.questionType === 1 ? index === $options.currentQuestion.correctAnswer : $options.currentQuestion.correctAnswers.includes(index)) ? 1 : "",
  524. e: $data.showResult && ($options.currentQuestion.questionType === 1 ? $data.selectedOption === index && index !== $options.currentQuestion.correctAnswer : $data.selectedOptions.includes(index) && !$options.currentQuestion.correctAnswers.includes(index)) ? 1 : "",
  525. f: common_vendor.o(($event) => $options.selectOption(index), index)
  526. };
  527. }),
  528. n: common_vendor.t($options.currentQuestion.questionType === 1 ? "●" : "☐")
  529. }, {
  530. o: common_vendor.t($data.showResult ? "下一题" : "提交答案"),
  531. p: common_vendor.o(($event) => $options.nextQuestion(_ctx.option)),
  532. q: $options.currentQuestion.questionType === 0 && $data.openQuestionAnswer.trim() === "" || $options.currentQuestion.questionType === 1 && $data.selectedOption === null || $options.currentQuestion.questionType === 2 && $data.selectedOptions.length === 0,
  533. r: $data.showEndModal
  534. }, $data.showEndModal ? {
  535. s: common_vendor.o((...args) => $options.navigateToInterview && $options.navigateToInterview(...args))
  536. } : {}, {
  537. t: $data.interviewCompleted
  538. }, $data.interviewCompleted ? {
  539. v: common_assets._imports_0,
  540. w: common_vendor.o((...args) => $options.back && $options.back(...args))
  541. } : {}, {
  542. x: $data.loading
  543. }, $data.loading ? {
  544. y: common_vendor.p({
  545. status: "loading",
  546. contentText: {
  547. contentdown: "加载中..."
  548. }
  549. })
  550. } : {}, {
  551. z: !$data.loading && $data.loadError
  552. }, !$data.loading && $data.loadError ? {
  553. A: common_vendor.o((...args) => $options.fetchInterviewData && $options.fetchInterviewData(...args))
  554. } : {});
  555. }
  556. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  557. wx.createPage(MiniProgramPage);