camera.js 33 KB

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