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. }, 3e3);
  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. });
  376. }
  377. }, 1500);
  378. }
  379. }
  380. const min = Math.floor(seconds / 60).toString().padStart(2, "0");
  381. const sec = (seconds % 60).toString().padStart(2, "0");
  382. this.remainingTime = `${min}:${sec}`;
  383. }, 1e3);
  384. },
  385. resetTimer() {
  386. clearInterval(this.timerInterval);
  387. },
  388. selectOption(index) {
  389. if (this.currentQuestionIndex === this.questions.length - 1 && this.showContinueButton) {
  390. common_vendor.index.showToast({
  391. title: "该题目已完成,请继续下一部分",
  392. icon: "none",
  393. duration: 2e3
  394. });
  395. return;
  396. }
  397. if (this.showResult)
  398. return;
  399. console.log("selectOption", index);
  400. if (this.currentQuestion.questionType === 2) {
  401. const optionIndex = this.selectedOptions.indexOf(index);
  402. if (optionIndex > -1) {
  403. this.selectedOptions.splice(optionIndex, 1);
  404. } else {
  405. this.selectedOptions.push(index);
  406. }
  407. } else if (this.currentQuestion.questionType === 1 || this.currentQuestion.questionType === 3 || this.currentQuestion.questionType === 4) {
  408. this.selectedOption = index;
  409. this.playAiSpeaking();
  410. setTimeout(() => {
  411. this.checkAnswer();
  412. this.saveAnswer();
  413. this.submitCurrentAnswer().then(() => {
  414. setTimeout(() => {
  415. this.goToNextQuestion();
  416. }, 800);
  417. }).catch((error) => {
  418. console.error("提交答案失败:", error);
  419. setTimeout(() => {
  420. this.goToNextQuestion();
  421. }, 1e3);
  422. });
  423. }, 500);
  424. } else {
  425. this.selectedOption = index;
  426. const answer = {
  427. questionId: this.currentQuestion.id,
  428. questionType: this.currentQuestion.questionType,
  429. answer: index
  430. };
  431. const existingIndex = this.answers.findIndex((a) => a.questionId === answer.questionId);
  432. if (existingIndex > -1) {
  433. this.answers[existingIndex] = answer;
  434. } else {
  435. this.answers.push(answer);
  436. }
  437. this.playAiSpeaking();
  438. setTimeout(() => {
  439. this.checkAnswer();
  440. this.saveAnswer();
  441. this.submitCurrentAnswer().then(() => {
  442. setTimeout(() => {
  443. this.goToNextQuestion();
  444. }, 800);
  445. }).catch((error) => {
  446. console.error("提交答案失败:", error);
  447. setTimeout(() => {
  448. this.goToNextQuestion();
  449. }, 1e3);
  450. });
  451. }, 500);
  452. }
  453. },
  454. checkAnswer() {
  455. if (this.showResult)
  456. return;
  457. clearInterval(this.timerInterval);
  458. if (!this.currentQuestion) {
  459. console.error("当前问题不存在");
  460. return;
  461. }
  462. if (this.currentQuestion.questionType === 0) {
  463. this.isAnswerCorrect = true;
  464. } else if (this.currentQuestion.questionType === 2) {
  465. const sortedSelected = [...this.selectedOptions].sort();
  466. const sortedCorrect = [...this.currentQuestion.correctAnswers].sort();
  467. if (sortedSelected.length !== sortedCorrect.length) {
  468. this.isAnswerCorrect = false;
  469. } else {
  470. this.isAnswerCorrect = sortedSelected.every((value, index) => value === sortedCorrect[index]);
  471. }
  472. } else {
  473. this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
  474. }
  475. this.showResult = true;
  476. this.saveAnswer();
  477. if (this.currentQuestionIndex === this.questions.length - 1) {
  478. this.$nextTick(() => {
  479. this.$forceUpdate();
  480. });
  481. }
  482. },
  483. // 修改 nextQuestion 方法,只处理多选题
  484. nextQuestion() {
  485. if (this.currentQuestion.questionType !== 2)
  486. return;
  487. this.checkAnswer();
  488. this.saveAnswer();
  489. this.submitCurrentAnswer().then(() => {
  490. setTimeout(() => {
  491. if (this.currentQuestionIndex >= this.questions.length - 1) {
  492. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  493. this.showContinueButton = true;
  494. setTimeout(() => {
  495. this.scrollTop = 0;
  496. this.$nextTick(() => {
  497. setTimeout(() => {
  498. this.scrollTop = 1e4;
  499. }, 100);
  500. });
  501. }, 200);
  502. } else {
  503. common_vendor.index.navigateTo({
  504. url: "/pages/posture-guide/posture-guide"
  505. });
  506. }
  507. } else {
  508. this.goToNextQuestion();
  509. }
  510. }, 500);
  511. }).catch((error) => {
  512. console.error("提交答案失败:", error);
  513. setTimeout(() => {
  514. if (this.currentQuestionIndex >= this.questions.length - 1) {
  515. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  516. this.showContinueButton = true;
  517. setTimeout(() => {
  518. this.scrollTop = 1e4;
  519. }, 200);
  520. } else {
  521. common_vendor.index.navigateTo({
  522. url: "/pages/posture-guide/posture-guide"
  523. });
  524. }
  525. } else {
  526. this.goToNextQuestion();
  527. }
  528. }, 1e3);
  529. });
  530. },
  531. toggleSettings() {
  532. common_vendor.index.showToast({
  533. title: "设置功能开发中",
  534. icon: "none"
  535. });
  536. },
  537. back(target) {
  538. if (this.timerInterval) {
  539. clearInterval(this.timerInterval);
  540. }
  541. if (target) {
  542. common_vendor.index.navigateTo({
  543. url: target
  544. });
  545. return;
  546. }
  547. },
  548. error(e) {
  549. console.error(e.detail);
  550. common_vendor.index.showToast({
  551. title: "相机启动失败,请检查权限设置",
  552. icon: "none"
  553. });
  554. },
  555. playAiSpeaking() {
  556. if (this.useVideo && this.aiVideoContext) {
  557. this.aiVideoContext.play();
  558. }
  559. if (this.digitalHumanUrl) {
  560. const speakText = this.currentQuestion ? this.currentQuestion.text : "";
  561. this.interactWithDigitalHuman(speakText);
  562. }
  563. },
  564. pauseAiSpeaking() {
  565. if (this.useVideo && this.aiVideoContext) {
  566. this.aiVideoContext.pause();
  567. }
  568. },
  569. // 修改 restartTest 方法,添加可选的跳转目标
  570. restartTest(target) {
  571. this.currentQuestionIndex = 0;
  572. this.score = 0;
  573. this.showEndModal = false;
  574. this.showResult = false;
  575. this.selectedOption = null;
  576. this.selectedOptions = [];
  577. this.resetTimer();
  578. if (target) {
  579. common_vendor.index.navigateTo({
  580. url: target
  581. });
  582. }
  583. },
  584. // 在methods中添加测试方法
  585. testEndScreen() {
  586. this.interviewCompleted = true;
  587. this.showEndModal = false;
  588. },
  589. // 初始化数字人
  590. initDigitalHuman() {
  591. this.digitalHumanUrl = "";
  592. },
  593. // 与数字人交互的方法
  594. interactWithDigitalHuman(message) {
  595. const webview = this.$mp.page.$getAppWebview().children()[0];
  596. if (webview) {
  597. webview.evalJS(`receiveMessage('${message}')`);
  598. }
  599. },
  600. // 添加 navigateToInterview 方法
  601. navigateToInterview() {
  602. this.showEndModal = false;
  603. common_vendor.index.navigateTo({
  604. url: "/pages/interview/interview",
  605. success: () => {
  606. console.log("成功跳转到interview页面");
  607. },
  608. fail: (err) => {
  609. console.error("跳转失败:", err);
  610. common_vendor.index.showToast({
  611. title: "跳转失败,请手动返回首页",
  612. icon: "none"
  613. });
  614. }
  615. });
  616. },
  617. // 添加 handleCameraError 方法
  618. handleCameraError(e) {
  619. console.error("相机错误:", e.detail);
  620. common_vendor.index.showToast({
  621. title: "相机启动失败,请检查权限设置",
  622. icon: "none"
  623. });
  624. },
  625. // 修改 isOptionSelected 方法
  626. isOptionSelected(question, qIndex, optionIndex) {
  627. const answer = this.answers.find((a) => a.questionId === question.id);
  628. if (qIndex === this.currentQuestionIndex) {
  629. if (question.questionType === 1 || question.questionType === 3 || question.questionType === 4) {
  630. return this.selectedOption === optionIndex;
  631. } else if (question.questionType === 2) {
  632. return this.selectedOptions.includes(optionIndex);
  633. }
  634. } else if (answer) {
  635. if (question.questionType === 1 || question.questionType === 3 || question.questionType === 4) {
  636. return answer.answer === optionIndex;
  637. } else if (question.questionType === 2 && Array.isArray(answer.answer)) {
  638. return answer.answer.includes(optionIndex);
  639. }
  640. }
  641. return false;
  642. },
  643. // 修改 handleOptionClick 方法
  644. handleOptionClick(qIndex, index) {
  645. if (qIndex < this.currentQuestionIndex - 1) {
  646. common_vendor.index.showToast({
  647. title: "该题目已完成,无法修改",
  648. icon: "none",
  649. duration: 2e3
  650. });
  651. return;
  652. }
  653. if (qIndex === this.questions.length - 1 && this.showContinueButton) {
  654. common_vendor.index.showToast({
  655. title: "该题目已完成,请继续下一部分",
  656. icon: "none",
  657. duration: 2e3
  658. });
  659. return;
  660. }
  661. if (qIndex > this.currentQuestionIndex) {
  662. const firstUnansweredIndex = this.currentQuestionIndex;
  663. common_vendor.index.showToast({
  664. title: `请先完成第 ${firstUnansweredIndex + 1} 题`,
  665. icon: "none",
  666. duration: 2e3
  667. });
  668. this.currentScrollId = "question-" + firstUnansweredIndex;
  669. return;
  670. }
  671. if (this._isClicking) {
  672. return;
  673. }
  674. this._isClicking = true;
  675. if (this._clickTimer) {
  676. clearTimeout(this._clickTimer);
  677. }
  678. try {
  679. if (qIndex === this.currentQuestionIndex - 1) {
  680. this.currentQuestionIndex = qIndex;
  681. this.currentScrollId = "question-" + qIndex;
  682. this.showResult = false;
  683. this.selectedOption = null;
  684. this.selectedOptions = [];
  685. }
  686. this.selectOption(index);
  687. this._clickTimer = setTimeout(() => {
  688. this._isClicking = false;
  689. }, 1e3);
  690. } catch (error) {
  691. console.error("处理选项点击失败:", error);
  692. common_vendor.index.showToast({
  693. title: "操作失败,请重试",
  694. icon: "none"
  695. });
  696. this._isClicking = false;
  697. }
  698. },
  699. // 处理题目分组
  700. processQuestionGroups(questions) {
  701. const group1 = [];
  702. const group2 = [];
  703. questions.forEach((q) => {
  704. if (q.category === 5) {
  705. group1.push(q);
  706. } else {
  707. group2.push(q);
  708. }
  709. });
  710. this.questionGroups = [group2, group1].filter((group) => group.length > 0);
  711. this.questions = this.questionGroups[0] || [];
  712. },
  713. // 处理继续按钮点击
  714. handleContinue() {
  715. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  716. if (this.currentGroupIndex === 0) {
  717. this.showPromptModal = true;
  718. } else {
  719. this.proceedToNextGroup();
  720. }
  721. } else {
  722. common_vendor.index.navigateTo({
  723. url: "/pages/posture-guide/posture-guide"
  724. });
  725. }
  726. },
  727. // 添加新的方法处理弹窗确认
  728. handlePromptConfirm() {
  729. this.showPromptModal = false;
  730. this.proceedToNextGroup();
  731. },
  732. // 添加新的方法处理组切换逻辑
  733. proceedToNextGroup() {
  734. this.currentGroupIndex++;
  735. this.questions = this.questionGroups[this.currentGroupIndex];
  736. this.currentQuestionIndex = 0;
  737. this.showContinueButton = false;
  738. this.currentScrollId = "question-0";
  739. this.scrollTop = 0;
  740. this.selectedOption = null;
  741. this.selectedOptions = [];
  742. this.showResult = false;
  743. },
  744. // 保存当前题目的答案
  745. saveAnswer() {
  746. let answer;
  747. if (this.currentQuestion.questionType === 0) {
  748. answer = {
  749. questionId: this.currentQuestion.id,
  750. questionType: this.currentQuestion.questionType,
  751. answer: this.openQuestionAnswer,
  752. answerDuration: this.getAnswerDuration()
  753. // 添加答题时长
  754. };
  755. } else if (this.currentQuestion.questionType === 1 || this.currentQuestion.questionType === 3 || this.currentQuestion.questionType === 4) {
  756. answer = {
  757. questionId: this.currentQuestion.id,
  758. questionType: this.currentQuestion.questionType,
  759. answer: this.selectedOption,
  760. answerDuration: this.getAnswerDuration()
  761. // 添加答题时长
  762. };
  763. } else {
  764. answer = {
  765. questionId: this.currentQuestion.id,
  766. questionType: this.currentQuestion.questionType,
  767. answer: this.selectedOptions,
  768. answerDuration: this.getAnswerDuration()
  769. // 添加答题时长
  770. };
  771. }
  772. const existingIndex = this.answers.findIndex((a) => a.questionId === answer.questionId);
  773. if (existingIndex > -1) {
  774. this.answers[existingIndex] = answer;
  775. } else {
  776. this.answers.push(answer);
  777. }
  778. this.currentAnswer = answer;
  779. console.log("已保存答案:", this.answers);
  780. },
  781. // 获取答题时长(秒)
  782. getAnswerDuration() {
  783. const remainingTimeArr = this.remainingTime.split(":");
  784. const remainingSeconds = parseInt(remainingTimeArr[0]) * 60 + parseInt(remainingTimeArr[1]);
  785. return 30 - remainingSeconds;
  786. },
  787. // 提交当前答案
  788. async submitCurrentAnswer() {
  789. if (!this.currentAnswer || this.isSubmitting)
  790. return;
  791. try {
  792. this.isSubmitting = true;
  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);