camera.js 39 KB

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