camera.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. mode: "normal",
  52. // 相机模式
  53. currentScrollId: "question-0",
  54. // 当前滚动到的问题ID
  55. questionGroups: [],
  56. // 存储分组后的题目
  57. currentGroupIndex: 0,
  58. // 当前显示的组索引
  59. showContinueButton: false,
  60. // 是否显示继续按钮
  61. scrollToBottom: false,
  62. // 新增:是否需要滚动到底部
  63. scrollTop: 0,
  64. // 控制 scroll-view 的滚动位置
  65. _clickTimer: null,
  66. _isClicking: false,
  67. showPromptModal: false,
  68. // 添加新的数据属性
  69. personDetectionSocket: null,
  70. // WebSocket对象
  71. personDetectionInterval: null,
  72. // 定时器对象
  73. showCameraWarning: false,
  74. // 添加新的数据属性
  75. showPageWarning: false
  76. // 添加新的数据属性
  77. };
  78. },
  79. computed: {
  80. currentQuestion() {
  81. console.log(this.questions[this.currentQuestionIndex]);
  82. return this.questions[this.currentQuestionIndex];
  83. }
  84. },
  85. onLoad(options) {
  86. if (options && options.id) {
  87. this.interviewId = options.id;
  88. this.fetchInterviewData();
  89. } else {
  90. this.fetchInterviewList();
  91. }
  92. },
  93. onReady() {
  94. this.cameraContext = common_vendor.index.createCameraContext();
  95. if (this.useVideo) {
  96. this.aiVideoContext = common_vendor.index.createVideoContext("aiInterviewer");
  97. }
  98. this.initDigitalHuman();
  99. },
  100. mounted() {
  101. common_vendor.index.onUserCaptureScreen(() => {
  102. console.log("User captured screen");
  103. this.screenCaptureCount++;
  104. if (this.screenCaptureCount === 1) {
  105. common_vendor.index.showModal({
  106. title: "Warning",
  107. content: "检测到屏幕截图。如果你再次捕捉屏幕,你的面试将无效.",
  108. showCancel: false,
  109. confirmText: "OK"
  110. });
  111. } else if (this.screenCaptureCount >= 2) {
  112. common_vendor.index.showModal({
  113. title: "Interview Invalid",
  114. content: "由于多次屏幕截图,您的面试已无效。请联系招聘人员寻求帮助.",
  115. showCancel: false,
  116. confirmText: "OK",
  117. success: () => {
  118. this.invalidateInterview();
  119. }
  120. });
  121. }
  122. });
  123. },
  124. beforeDestroy() {
  125. this.cleanupPersonDetectionWebSocket();
  126. },
  127. methods: {
  128. initPersonDetectionWebSocket() {
  129. if (this.personDetectionSocket) {
  130. this.cleanupPersonDetectionWebSocket();
  131. }
  132. try {
  133. this.personDetectionSocket = common_vendor.index.connectSocket({
  134. url: `${common_config.personDetectionWsUrl}/ws/interview-room/room_${common_vendor.index.getStorageSync("appId")}/${common_vendor.index.getStorageSync("appId")}/`,
  135. success: () => {
  136. console.log("WebSocket connection initiated");
  137. },
  138. fail: (error) => {
  139. console.error("WebSocket connection failed:", error);
  140. }
  141. });
  142. this.personDetectionSocket.onOpen(() => {
  143. console.log("WebSocket connection opened");
  144. this.startPersonDetectionInterval();
  145. });
  146. this.personDetectionSocket.onError((error) => {
  147. console.error("WebSocket error:", error);
  148. this.cleanupPersonDetectionWebSocket();
  149. });
  150. this.personDetectionSocket.onClose(() => {
  151. console.log("WebSocket connection closed");
  152. this.cleanupPersonDetectionWebSocket();
  153. });
  154. this.personDetectionSocket.onMessage((res2) => {
  155. try {
  156. const data = JSON.parse(res2.data);
  157. console.log(data);
  158. if (data.type === "person_detection_result") {
  159. this.handlePersonDetectionResult(data);
  160. }
  161. } catch (error) {
  162. console.error("Error parsing WebSocket message:", error);
  163. }
  164. });
  165. } catch (error) {
  166. console.error("Error initializing WebSocket:", error);
  167. this.cleanupPersonDetectionWebSocket();
  168. }
  169. },
  170. startPersonDetectionInterval() {
  171. if (this.personDetectionInterval) {
  172. clearInterval(this.personDetectionInterval);
  173. }
  174. this.personDetectionInterval = setInterval(() => {
  175. if (this.personDetectionSocket && this.cameraContext) {
  176. this.cameraContext.takePhoto({
  177. quality: "low",
  178. success: (res2) => {
  179. const tempFilePath = res2.tempImagePath;
  180. common_vendor.index.getFileSystemManager().readFile({
  181. filePath: tempFilePath,
  182. encoding: "base64",
  183. success: (res3) => {
  184. const base64Image = res3.data;
  185. this.personDetectionSocket.send({
  186. data: JSON.stringify({
  187. type: "person_detection",
  188. image_data: base64Image
  189. })
  190. });
  191. },
  192. fail: (error) => {
  193. console.error("Error reading image file:", error);
  194. }
  195. });
  196. },
  197. fail: (error) => {
  198. console.error("Error taking photo:", error);
  199. }
  200. });
  201. }
  202. }, 3e3);
  203. },
  204. cleanupPersonDetectionWebSocket() {
  205. if (this.personDetectionInterval) {
  206. clearInterval(this.personDetectionInterval);
  207. this.personDetectionInterval = null;
  208. }
  209. if (this.personDetectionSocket) {
  210. try {
  211. this.personDetectionSocket.close();
  212. } catch (error) {
  213. console.error("Error closing WebSocket:", error);
  214. }
  215. this.personDetectionSocket = null;
  216. }
  217. },
  218. handlePersonDetectionResult(data) {
  219. console.log(data.data.detection.has_person);
  220. console.log(data.data.identity.status);
  221. if (data.data.identity.status !== "identity_verified") {
  222. this.showPageWarning = true;
  223. common_vendor.index.showToast({
  224. title: data.data.identity.message,
  225. icon: "none",
  226. duration: 3e3
  227. });
  228. common_vendor.index.vibrateLong({
  229. success: function() {
  230. console.log("Vibration successful");
  231. },
  232. fail: function(err) {
  233. console.error("Vibration failed:", err);
  234. }
  235. });
  236. setTimeout(() => {
  237. this.showPageWarning = false;
  238. }, 3e3);
  239. }
  240. },
  241. // 添加作废面试的方法
  242. invalidateInterview() {
  243. if (this.isRecording) {
  244. this.stopRecordingAnswer();
  245. }
  246. if (this.recordingTimer) {
  247. clearInterval(this.recordingTimer);
  248. }
  249. this.isRecording = false;
  250. this.showStopRecordingButton = false;
  251. this.showStartRecordingButton = false;
  252. setTimeout(() => {
  253. common_vendor.index.switchTab({
  254. url: "/pages/index/index"
  255. });
  256. }, 500);
  257. const systemInfo = common_vendor.index.getSystemInfoSync();
  258. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  259. if (isMiniProgram) {
  260. this.initPersonDetectionWebSocket();
  261. }
  262. },
  263. // 获取面试列表
  264. async fetchInterviewList() {
  265. try {
  266. this.loading = true;
  267. const res2 = await api_user.getQuestions({
  268. position_id: JSON.parse(common_vendor.index.getStorageSync("selectedJob")).id,
  269. page: 1,
  270. limit: 999,
  271. tenant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || 1
  272. });
  273. console.log(res2.items);
  274. this.interviewId = res2.items;
  275. this.fetchInterviewData(res2.items);
  276. } catch (error) {
  277. console.error("获取面试列表失败:", error);
  278. this.handleLoadError("获取面试列表失败");
  279. }
  280. },
  281. // 获取面试详情数据
  282. async fetchInterviewData(data) {
  283. try {
  284. this.loading = true;
  285. if (data && Array.isArray(data)) {
  286. const formattedQuestions = data.filter((q) => q.question_form !== 0).map((q, index) => ({
  287. id: q.id || index + 1,
  288. text: q.question || "未知问题",
  289. options: q.options || [],
  290. correctAnswer: q.correctAnswer || 0,
  291. isImportant: q.is_system || false,
  292. explanation: q.explanation || "",
  293. questionType: q.question_form,
  294. questionTypeName: q.question_form_name || "单选题",
  295. is_required_correct: q.is_required_correct || false,
  296. correctAnswers: q.correct_answers || [],
  297. difficulty: q.difficulty || 1,
  298. difficultyName: q.difficulty_name || "初级",
  299. imageUrl: q.question_image_url || "",
  300. // 添加图片URL字段
  301. category: q.category || 0
  302. // 添加 category 字段
  303. }));
  304. this.processQuestionGroups(formattedQuestions);
  305. } else {
  306. this.processInterviewData(res);
  307. }
  308. console.log(this.questions);
  309. this.totalQuestions = this.questions.length;
  310. } catch (error) {
  311. console.error("获取面试详情失败:", error);
  312. this.handleLoadError("获取面试详情失败");
  313. } finally {
  314. this.loading = false;
  315. }
  316. },
  317. // 处理面试数据
  318. processInterviewData(data) {
  319. this.questions = [];
  320. if (data && data.question_form !== 0) {
  321. const formattedQuestion = {
  322. id: data.id || 1,
  323. text: data.question || "未知问题",
  324. options: data.options || [],
  325. correctAnswer: data.correctAnswer || 0,
  326. isImportant: data.is_system || false,
  327. explanation: data.explanation || "",
  328. questionType: data.question_form,
  329. // 1-单选题,2-多选题,3-看图答题
  330. questionTypeName: data.question_form_name || "单选题",
  331. is_required_correct: data.is_required_correct || false,
  332. correctAnswers: data.correct_answers || [],
  333. difficulty: data.difficulty || 1,
  334. difficultyName: data.difficulty_name || "初级",
  335. imageUrl: data.question_image_url || ""
  336. // 添加图片URL字段
  337. };
  338. this.questions.push(formattedQuestion);
  339. this.totalQuestions = this.questions.length;
  340. } else {
  341. this.handleLoadError("没有可用的选择题");
  342. }
  343. },
  344. // 处理加载错误
  345. handleLoadError(message) {
  346. this.loadError = true;
  347. this.loading = false;
  348. this.errorMessage = message || "加载失败";
  349. common_vendor.index.showToast({
  350. title: message || "加载失败",
  351. icon: "none",
  352. duration: 2e3
  353. });
  354. },
  355. startTimer() {
  356. if (this.questions.length === 0)
  357. return;
  358. this.questionStartTime = /* @__PURE__ */ new Date();
  359. let seconds = 60;
  360. this.remainingTime = `01:00`;
  361. this.timerInterval = setInterval(() => {
  362. seconds--;
  363. if (seconds <= 0) {
  364. clearInterval(this.timerInterval);
  365. if (!this.showResult) {
  366. this.checkAnswer();
  367. setTimeout(() => {
  368. if (this.currentQuestionIndex < this.questions.length - 1) {
  369. this.goToNextQuestion();
  370. } else {
  371. common_vendor.index.navigateTo({
  372. url: "/pages/interview-question/interview-question"
  373. });
  374. }
  375. }, 1500);
  376. }
  377. }
  378. const min = Math.floor(seconds / 60).toString().padStart(2, "0");
  379. const sec = (seconds % 60).toString().padStart(2, "0");
  380. this.remainingTime = `${min}:${sec}`;
  381. }, 1e3);
  382. },
  383. resetTimer() {
  384. clearInterval(this.timerInterval);
  385. },
  386. selectOption(index) {
  387. if (this.currentQuestionIndex === this.questions.length - 1 && this.showContinueButton) {
  388. common_vendor.index.showToast({
  389. title: "该题目已完成,请继续下一部分",
  390. icon: "none",
  391. duration: 2e3
  392. });
  393. return;
  394. }
  395. if (this.showResult)
  396. return;
  397. console.log("selectOption", index);
  398. if (this.currentQuestion.questionType === 2) {
  399. const optionIndex = this.selectedOptions.indexOf(index);
  400. if (optionIndex > -1) {
  401. this.selectedOptions.splice(optionIndex, 1);
  402. } else {
  403. this.selectedOptions.push(index);
  404. }
  405. } else if (this.currentQuestion.questionType === 1 || this.currentQuestion.questionType === 3 || this.currentQuestion.questionType === 4) {
  406. this.selectedOption = index;
  407. this.playAiSpeaking();
  408. setTimeout(() => {
  409. this.checkAnswer();
  410. this.saveAnswer();
  411. this.submitCurrentAnswer().then(() => {
  412. setTimeout(() => {
  413. this.goToNextQuestion();
  414. }, 800);
  415. }).catch((error) => {
  416. console.error("提交答案失败:", error);
  417. setTimeout(() => {
  418. this.goToNextQuestion();
  419. }, 1e3);
  420. });
  421. }, 500);
  422. } else {
  423. this.selectedOption = index;
  424. const answer = {
  425. questionId: this.currentQuestion.id,
  426. questionType: this.currentQuestion.questionType,
  427. answer: index
  428. };
  429. const existingIndex = this.answers.findIndex((a) => a.questionId === answer.questionId);
  430. if (existingIndex > -1) {
  431. this.answers[existingIndex] = answer;
  432. } else {
  433. this.answers.push(answer);
  434. }
  435. this.playAiSpeaking();
  436. setTimeout(() => {
  437. this.checkAnswer();
  438. this.saveAnswer();
  439. this.submitCurrentAnswer().then(() => {
  440. setTimeout(() => {
  441. this.goToNextQuestion();
  442. }, 800);
  443. }).catch((error) => {
  444. console.error("提交答案失败:", error);
  445. setTimeout(() => {
  446. this.goToNextQuestion();
  447. }, 1e3);
  448. });
  449. }, 500);
  450. }
  451. },
  452. checkAnswer() {
  453. if (this.showResult)
  454. return;
  455. clearInterval(this.timerInterval);
  456. if (!this.currentQuestion) {
  457. console.error("当前问题不存在");
  458. return;
  459. }
  460. if (this.currentQuestion.questionType === 0) {
  461. this.isAnswerCorrect = true;
  462. } else if (this.currentQuestion.questionType === 2) {
  463. const sortedSelected = [...this.selectedOptions].sort();
  464. const sortedCorrect = [...this.currentQuestion.correctAnswers].sort();
  465. if (sortedSelected.length !== sortedCorrect.length) {
  466. this.isAnswerCorrect = false;
  467. } else {
  468. this.isAnswerCorrect = sortedSelected.every((value, index) => value === sortedCorrect[index]);
  469. }
  470. } else {
  471. this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
  472. }
  473. this.showResult = true;
  474. this.saveAnswer();
  475. if (this.currentQuestionIndex === this.questions.length - 1) {
  476. this.$nextTick(() => {
  477. this.$forceUpdate();
  478. });
  479. }
  480. },
  481. // 修改 nextQuestion 方法,只处理多选题
  482. nextQuestion() {
  483. if (this.currentQuestion.questionType !== 2)
  484. return;
  485. this.checkAnswer();
  486. this.saveAnswer();
  487. this.submitCurrentAnswer().then(() => {
  488. setTimeout(() => {
  489. if (this.currentQuestionIndex >= this.questions.length - 1) {
  490. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  491. this.showContinueButton = true;
  492. setTimeout(() => {
  493. this.scrollTop = 0;
  494. this.$nextTick(() => {
  495. setTimeout(() => {
  496. this.scrollTop = 1e4;
  497. }, 100);
  498. });
  499. }, 200);
  500. } else {
  501. common_vendor.index.navigateTo({
  502. url: "/pages/interview-question/interview-question"
  503. });
  504. }
  505. } else {
  506. this.goToNextQuestion();
  507. }
  508. }, 500);
  509. }).catch((error) => {
  510. console.error("提交答案失败:", error);
  511. setTimeout(() => {
  512. if (this.currentQuestionIndex >= this.questions.length - 1) {
  513. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  514. this.showContinueButton = true;
  515. setTimeout(() => {
  516. this.scrollTop = 1e4;
  517. }, 200);
  518. } else {
  519. common_vendor.index.navigateTo({
  520. url: "/pages/interview-question/interview-question"
  521. });
  522. }
  523. } else {
  524. this.goToNextQuestion();
  525. }
  526. }, 1e3);
  527. });
  528. },
  529. toggleSettings() {
  530. common_vendor.index.showToast({
  531. title: "设置功能开发中",
  532. icon: "none"
  533. });
  534. },
  535. back(target) {
  536. if (this.timerInterval) {
  537. clearInterval(this.timerInterval);
  538. }
  539. if (target) {
  540. common_vendor.index.navigateTo({
  541. url: target
  542. });
  543. return;
  544. }
  545. },
  546. error(e) {
  547. console.error(e.detail);
  548. common_vendor.index.showToast({
  549. title: "相机启动失败,请检查权限设置",
  550. icon: "none"
  551. });
  552. },
  553. playAiSpeaking() {
  554. if (this.useVideo && this.aiVideoContext) {
  555. this.aiVideoContext.play();
  556. }
  557. if (this.digitalHumanUrl) {
  558. const speakText = this.currentQuestion ? this.currentQuestion.text : "";
  559. this.interactWithDigitalHuman(speakText);
  560. }
  561. },
  562. pauseAiSpeaking() {
  563. if (this.useVideo && this.aiVideoContext) {
  564. this.aiVideoContext.pause();
  565. }
  566. },
  567. // 修改 restartTest 方法,添加可选的跳转目标
  568. restartTest(target) {
  569. this.currentQuestionIndex = 0;
  570. this.score = 0;
  571. this.showEndModal = false;
  572. this.showResult = false;
  573. this.selectedOption = null;
  574. this.selectedOptions = [];
  575. this.resetTimer();
  576. if (target) {
  577. common_vendor.index.navigateTo({
  578. url: target
  579. });
  580. }
  581. },
  582. // 在methods中添加测试方法
  583. testEndScreen() {
  584. this.interviewCompleted = true;
  585. this.showEndModal = false;
  586. },
  587. // 初始化数字人
  588. initDigitalHuman() {
  589. this.digitalHumanUrl = "";
  590. },
  591. // 与数字人交互的方法
  592. interactWithDigitalHuman(message) {
  593. const webview = this.$mp.page.$getAppWebview().children()[0];
  594. if (webview) {
  595. webview.evalJS(`receiveMessage('${message}')`);
  596. }
  597. },
  598. // 添加 navigateToInterview 方法
  599. navigateToInterview() {
  600. this.showEndModal = false;
  601. common_vendor.index.navigateTo({
  602. url: "/pages/interview/interview",
  603. success: () => {
  604. console.log("成功跳转到interview页面");
  605. },
  606. fail: (err) => {
  607. console.error("跳转失败:", err);
  608. common_vendor.index.showToast({
  609. title: "跳转失败,请手动返回首页",
  610. icon: "none"
  611. });
  612. }
  613. });
  614. },
  615. // 添加 handleCameraError 方法
  616. handleCameraError(e) {
  617. console.error("相机错误:", e.detail);
  618. common_vendor.index.showToast({
  619. title: "相机启动失败,请检查权限设置",
  620. icon: "none"
  621. });
  622. },
  623. // 修改 isOptionSelected 方法
  624. isOptionSelected(question, qIndex, optionIndex) {
  625. const answer = this.answers.find((a) => a.questionId === question.id);
  626. if (qIndex === this.currentQuestionIndex) {
  627. if (question.questionType === 1 || question.questionType === 3 || question.questionType === 4) {
  628. return this.selectedOption === optionIndex;
  629. } else if (question.questionType === 2) {
  630. return this.selectedOptions.includes(optionIndex);
  631. }
  632. } else if (answer) {
  633. if (question.questionType === 1 || question.questionType === 3 || question.questionType === 4) {
  634. return answer.answer === optionIndex;
  635. } else if (question.questionType === 2 && Array.isArray(answer.answer)) {
  636. return answer.answer.includes(optionIndex);
  637. }
  638. }
  639. return false;
  640. },
  641. // 修改 handleOptionClick 方法
  642. handleOptionClick(qIndex, index) {
  643. if (qIndex < this.currentQuestionIndex) {
  644. common_vendor.index.showToast({
  645. title: "该题目已完成,无法修改",
  646. icon: "none",
  647. duration: 2e3
  648. });
  649. return;
  650. }
  651. if (qIndex === this.questions.length - 1 && this.showContinueButton) {
  652. common_vendor.index.showToast({
  653. title: "该题目已完成,请继续下一部分",
  654. icon: "none",
  655. duration: 2e3
  656. });
  657. return;
  658. }
  659. if (qIndex > this.currentQuestionIndex) {
  660. const firstUnansweredIndex = this.currentQuestionIndex;
  661. common_vendor.index.showToast({
  662. title: `请先完成第 ${firstUnansweredIndex + 1} 题`,
  663. icon: "none",
  664. duration: 2e3
  665. });
  666. this.currentScrollId = "question-" + firstUnansweredIndex;
  667. return;
  668. }
  669. if (this._isClicking) {
  670. return;
  671. }
  672. this._isClicking = true;
  673. if (this._clickTimer) {
  674. clearTimeout(this._clickTimer);
  675. }
  676. try {
  677. this.selectOption(index);
  678. this._clickTimer = setTimeout(() => {
  679. this._isClicking = false;
  680. }, 1e3);
  681. } catch (error) {
  682. console.error("处理选项点击失败:", error);
  683. common_vendor.index.showToast({
  684. title: "操作失败,请重试",
  685. icon: "none"
  686. });
  687. this._isClicking = false;
  688. }
  689. },
  690. // 处理题目分组
  691. processQuestionGroups(questions) {
  692. const group1 = [];
  693. const group2 = [];
  694. questions.forEach((q) => {
  695. if (q.category === 5) {
  696. group1.push(q);
  697. } else {
  698. group2.push(q);
  699. }
  700. });
  701. this.questionGroups = [group2, group1].filter((group) => group.length > 0);
  702. this.questions = this.questionGroups[0] || [];
  703. },
  704. // 处理继续按钮点击
  705. handleContinue() {
  706. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  707. if (this.currentGroupIndex === 0) {
  708. this.showPromptModal = true;
  709. } else {
  710. this.proceedToNextGroup();
  711. }
  712. } else {
  713. common_vendor.index.navigateTo({
  714. url: "/pages/interview-question/interview-question"
  715. });
  716. }
  717. },
  718. // 添加新的方法处理弹窗确认
  719. handlePromptConfirm() {
  720. this.showPromptModal = false;
  721. this.proceedToNextGroup();
  722. },
  723. // 添加新的方法处理组切换逻辑
  724. proceedToNextGroup() {
  725. this.currentGroupIndex++;
  726. this.questions = this.questionGroups[this.currentGroupIndex];
  727. this.currentQuestionIndex = 0;
  728. this.showContinueButton = false;
  729. this.currentScrollId = "question-0";
  730. this.scrollTop = 0;
  731. this.selectedOption = null;
  732. this.selectedOptions = [];
  733. this.showResult = false;
  734. },
  735. // 保存当前题目的答案
  736. saveAnswer() {
  737. let answer;
  738. if (this.currentQuestion.questionType === 0) {
  739. answer = {
  740. questionId: this.currentQuestion.id,
  741. questionType: this.currentQuestion.questionType,
  742. answer: this.openQuestionAnswer,
  743. answerDuration: this.getAnswerDuration()
  744. // 添加答题时长
  745. };
  746. } else if (this.currentQuestion.questionType === 1 || this.currentQuestion.questionType === 3 || this.currentQuestion.questionType === 4) {
  747. answer = {
  748. questionId: this.currentQuestion.id,
  749. questionType: this.currentQuestion.questionType,
  750. answer: this.selectedOption,
  751. answerDuration: this.getAnswerDuration()
  752. // 添加答题时长
  753. };
  754. } else {
  755. answer = {
  756. questionId: this.currentQuestion.id,
  757. questionType: this.currentQuestion.questionType,
  758. answer: this.selectedOptions,
  759. answerDuration: this.getAnswerDuration()
  760. // 添加答题时长
  761. };
  762. }
  763. const existingIndex = this.answers.findIndex((a) => a.questionId === answer.questionId);
  764. if (existingIndex > -1) {
  765. this.answers[existingIndex] = answer;
  766. } else {
  767. this.answers.push(answer);
  768. }
  769. this.currentAnswer = answer;
  770. console.log("已保存答案:", this.answers);
  771. },
  772. // 获取答题时长(秒)
  773. getAnswerDuration() {
  774. const remainingTimeArr = this.remainingTime.split(":");
  775. const remainingSeconds = parseInt(remainingTimeArr[0]) * 60 + parseInt(remainingTimeArr[1]);
  776. return 30 - remainingSeconds;
  777. },
  778. // 提交当前答案
  779. async submitCurrentAnswer() {
  780. if (!this.currentAnswer || this.isSubmitting)
  781. return;
  782. try {
  783. this.isSubmitting = true;
  784. common_vendor.index.showLoading({
  785. title: "正在提交答案..."
  786. });
  787. let answerContent = "";
  788. let answerOptions = [];
  789. if (this.currentAnswer.questionType === 0) {
  790. answerContent = this.currentAnswer.answer;
  791. answerOptions = [];
  792. } else if (this.currentAnswer.questionType === 1 || this.currentAnswer.questionType === 3 || this.currentAnswer.questionType === 4) {
  793. const selectedIndex = this.currentAnswer.answer;
  794. const selectedOption = this.currentQuestion.options[selectedIndex];
  795. console.log("selectedOption", selectedOption);
  796. const optionId = selectedOption.id ? selectedOption.id : selectedIndex;
  797. answerContent = optionId.toString();
  798. answerOptions = [optionId];
  799. } else if (this.currentAnswer.questionType === 2) {
  800. const selectedIndices = this.currentAnswer.answer;
  801. const selectedOptionIds = selectedIndices.map((index) => {
  802. const option = this.currentQuestion.options[index];
  803. return option.id ? option.id : index;
  804. });
  805. answerOptions = selectedOptionIds;
  806. }
  807. const submitData = {
  808. position_id: JSON.parse(common_vendor.index.getStorageSync("selectedJob")).id,
  809. applicant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).id,
  810. job_position_question_id: this.currentAnswer.questionId,
  811. // answer_content: answerContent,
  812. answer_options: answerOptions,
  813. answer_duration: this.currentAnswer.answerDuration || 0,
  814. tenant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || 1
  815. };
  816. console.log("提交数据:", submitData);
  817. const res2 = await this.$http.post(`${common_config.apiBaseUrl}/api/job/submit_answer`, submitData);
  818. console.log("提交答案响应:", res2);
  819. return res2;
  820. } catch (error) {
  821. console.error("提交答案失败:", error);
  822. common_vendor.index.showToast({
  823. title: "提交答案失败,请重试",
  824. icon: "none"
  825. });
  826. throw error;
  827. } finally {
  828. common_vendor.index.hideLoading();
  829. this.isSubmitting = false;
  830. }
  831. },
  832. // 修改 goToNextQuestion 方法
  833. async goToNextQuestion() {
  834. if (this.currentQuestionIndex >= this.questions.length - 1) {
  835. this.showContinueButton = true;
  836. setTimeout(() => {
  837. this.scrollTop = 0;
  838. this.$nextTick(() => {
  839. setTimeout(() => {
  840. this.scrollTop = 1e4;
  841. }, 100);
  842. });
  843. }, 200);
  844. return;
  845. }
  846. this.currentQuestionIndex++;
  847. this.currentScrollId = "question-" + this.currentQuestionIndex;
  848. this.showResult = false;
  849. this.selectedOption = null;
  850. this.selectedOptions = [];
  851. this.openQuestionAnswer = "";
  852. if (this.questions[this.currentQuestionIndex]) {
  853. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  854. this.resetTimer();
  855. this.playAiSpeaking();
  856. setTimeout(() => {
  857. this.pauseAiSpeaking();
  858. }, 2e3);
  859. }
  860. }
  861. },
  862. // 添加生命周期钩子,确保在组件销毁时清除计时器
  863. beforeDestroy() {
  864. if (this.timerInterval) {
  865. clearInterval(this.timerInterval);
  866. }
  867. }
  868. };
  869. if (!Array) {
  870. const _component_uni_load_more = common_vendor.resolveComponent("uni-load-more");
  871. _component_uni_load_more();
  872. }
  873. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  874. return common_vendor.e({
  875. a: !$data.digitalHumanUrl
  876. }, !$data.digitalHumanUrl ? {
  877. b: $data.mode,
  878. c: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  879. } : $data.digitalHumanUrl ? {
  880. e: $data.digitalHumanUrl
  881. } : {
  882. f: common_assets._imports_0
  883. }, {
  884. d: $data.digitalHumanUrl,
  885. g: $data.showPageWarning ? 1 : "",
  886. h: common_vendor.f($data.questions, (question, qIndex, i0) => {
  887. return common_vendor.e({
  888. a: common_vendor.t(qIndex + 1),
  889. b: common_vendor.t(question.questionTypeName),
  890. c: common_vendor.t(question.text),
  891. d: question.is_required_correct
  892. }, question.is_required_correct ? {} : {}, {
  893. e: question.questionType === 3 && question.imageUrl
  894. }, question.questionType === 3 && question.imageUrl ? {
  895. f: question.imageUrl
  896. } : {}, {
  897. g: question.questionType !== 0
  898. }, question.questionType !== 0 ? {
  899. h: common_vendor.f(question.options, (option, index, i1) => {
  900. return {
  901. a: common_vendor.t(option.option_text || (typeof option === "string" ? option : JSON.stringify(option))),
  902. b: index,
  903. c: $options.isOptionSelected(question, qIndex, index) ? 1 : "",
  904. d: common_vendor.o(($event) => $options.handleOptionClick(qIndex, index), index)
  905. };
  906. }),
  907. i: qIndex !== $data.currentQuestionIndex ? 1 : ""
  908. } : {}, {
  909. j: qIndex === $data.currentQuestionIndex && question.questionType === 2
  910. }, qIndex === $data.currentQuestionIndex && question.questionType === 2 ? {
  911. k: common_vendor.t("下一题"),
  912. l: common_vendor.o(($event) => $options.nextQuestion(), question.id),
  913. m: $data.selectedOptions.length === 0
  914. } : {}, {
  915. n: question.id,
  916. o: "question-" + qIndex
  917. });
  918. }),
  919. i: common_vendor.t($data.questions.length),
  920. j: $data.showContinueButton
  921. }, $data.showContinueButton ? {
  922. k: common_vendor.t($data.currentGroupIndex < $data.questionGroups.length - 1 ? "继续下一部分" : "完成测试"),
  923. l: common_vendor.o((...args) => $options.handleContinue && $options.handleContinue(...args))
  924. } : {}, {
  925. m: $data.currentScrollId,
  926. n: $data.scrollTop,
  927. o: $data.showEndModal
  928. }, $data.showEndModal ? {
  929. p: common_vendor.o((...args) => $options.navigateToInterview && $options.navigateToInterview(...args))
  930. } : {}, {
  931. q: $data.interviewCompleted
  932. }, $data.interviewCompleted ? {
  933. r: common_assets._imports_0,
  934. s: common_vendor.o((...args) => $options.back && $options.back(...args))
  935. } : {}, {
  936. t: $data.loading
  937. }, $data.loading ? {
  938. v: common_vendor.p({
  939. status: "loading",
  940. contentText: {
  941. contentdown: "加载中..."
  942. }
  943. })
  944. } : {}, {
  945. w: !$data.loading && $data.loadError
  946. }, !$data.loading && $data.loadError ? {
  947. x: common_vendor.o((...args) => $options.fetchInterviewData && $options.fetchInterviewData(...args))
  948. } : {}, {
  949. y: $data.showPromptModal
  950. }, $data.showPromptModal ? {
  951. z: common_vendor.o((...args) => $options.handlePromptConfirm && $options.handlePromptConfirm(...args))
  952. } : {});
  953. }
  954. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  955. wx.createPage(MiniProgramPage);