camera.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. questions.forEach((q) => {
  752. if (q.category === 5) {
  753. group1.push(q);
  754. } else {
  755. group2.push(q);
  756. }
  757. });
  758. if (this.blankQuestions && this.blankQuestions.length > 0) {
  759. this.blankQuestions.forEach((q) => {
  760. if (q.category === 5) {
  761. group1.push(q);
  762. } else {
  763. group2.push(q);
  764. }
  765. });
  766. }
  767. this.questionGroups = [group2, group1].filter((group) => group.length > 0);
  768. this.questions = this.questionGroups[0] || [];
  769. },
  770. // 处理继续按钮点击
  771. handleContinue() {
  772. if (this.currentGroupIndex < this.questionGroups.length - 1) {
  773. if (this.currentGroupIndex === 0) {
  774. this.showPromptModal = true;
  775. } else {
  776. this.proceedToNextGroup();
  777. }
  778. } else {
  779. if (this.positionConfig.enable_posture_check) {
  780. common_vendor.index.navigateTo({
  781. url: "/pages/posture-guide/posture-guide"
  782. });
  783. } else {
  784. common_vendor.index.navigateTo({
  785. url: "/pages/interview-question/interview-question"
  786. });
  787. }
  788. }
  789. },
  790. // 添加新的方法处理弹窗确认
  791. handlePromptConfirm() {
  792. this.showPromptModal = false;
  793. this.proceedToNextGroup();
  794. },
  795. // 添加新的方法处理组切换逻辑
  796. proceedToNextGroup() {
  797. this.currentGroupIndex++;
  798. this.questions = this.questionGroups[this.currentGroupIndex];
  799. this.currentQuestionIndex = 0;
  800. this.showContinueButton = false;
  801. this.currentScrollId = "question-0";
  802. this.scrollTop = 0;
  803. this.selectedOption = null;
  804. this.selectedOptions = [];
  805. this.showResult = false;
  806. },
  807. // 保存当前题目的答案
  808. saveAnswer() {
  809. let answer;
  810. if (this.currentQuestion.questionType === 0) {
  811. answer = {
  812. questionId: this.currentQuestion.id,
  813. questionType: this.currentQuestion.questionType,
  814. answer: this.openQuestionAnswer,
  815. answerDuration: this.getAnswerDuration()
  816. // 添加答题时长
  817. };
  818. } else if (this.currentQuestion.questionType === 1 || this.currentQuestion.questionType === 3 || this.currentQuestion.questionType === 4) {
  819. answer = {
  820. questionId: this.currentQuestion.id,
  821. questionType: this.currentQuestion.questionType,
  822. answer: this.selectedOption,
  823. answerDuration: this.getAnswerDuration()
  824. // 添加答题时长
  825. };
  826. } else if (this.currentQuestion.questionType === 6) {
  827. answer = {
  828. questionId: this.currentQuestion.id,
  829. questionType: this.currentQuestion.questionType,
  830. answer: this.currentBlankAnswers[this.currentQuestion.id] || {},
  831. answerDuration: this.getAnswerDuration()
  832. // 添加答题时长
  833. };
  834. } else {
  835. answer = {
  836. questionId: this.currentQuestion.id,
  837. questionType: this.currentQuestion.questionType,
  838. answer: this.selectedOptions,
  839. answerDuration: this.getAnswerDuration()
  840. // 添加答题时长
  841. };
  842. }
  843. const existingIndex = this.answers.findIndex((a) => a.questionId === answer.questionId);
  844. if (existingIndex > -1) {
  845. this.answers[existingIndex] = answer;
  846. } else {
  847. this.answers.push(answer);
  848. }
  849. this.currentAnswer = answer;
  850. console.log("已保存答案:", this.answers);
  851. },
  852. // 获取答题时长(秒)
  853. getAnswerDuration() {
  854. const remainingTimeArr = this.remainingTime.split(":");
  855. const remainingSeconds = parseInt(remainingTimeArr[0]) * 60 + parseInt(remainingTimeArr[1]);
  856. return 30 - remainingSeconds;
  857. },
  858. // 提交当前答案
  859. async submitCurrentAnswer() {
  860. if (!this.currentAnswer || this.isSubmitting)
  861. return;
  862. try {
  863. this.isSubmitting = true;
  864. let answerContent = "";
  865. let answerOptions = [];
  866. let blankAnswers = [];
  867. if (this.currentAnswer.questionType === 0) {
  868. answerContent = this.currentAnswer.answer;
  869. answerOptions = [];
  870. } else if (this.currentAnswer.questionType === 1 || this.currentAnswer.questionType === 3 || this.currentAnswer.questionType === 4) {
  871. const selectedIndex = this.currentAnswer.answer;
  872. const selectedOption = this.currentQuestion.options[selectedIndex];
  873. console.log("selectedOption", selectedOption);
  874. const optionId = selectedOption.id ? selectedOption.id : selectedIndex;
  875. answerContent = optionId.toString();
  876. answerOptions = [optionId];
  877. } else if (this.currentAnswer.questionType === 2) {
  878. const selectedIndices = this.currentAnswer.answer;
  879. const selectedOptionIds = selectedIndices.map((index) => {
  880. const option = this.currentQuestion.options[index];
  881. return option.id ? option.id : index;
  882. });
  883. answerOptions = selectedOptionIds;
  884. } else if (this.currentAnswer.questionType === 6) {
  885. const blankAnswerObj = this.currentAnswer.answer;
  886. if (blankAnswerObj && typeof blankAnswerObj === "object") {
  887. blankAnswers = Object.entries(blankAnswerObj).map(([key, value]) => ({
  888. blank_index: parseInt(key),
  889. answer_text: value || ""
  890. }));
  891. }
  892. }
  893. const submitData = {
  894. position_id: JSON.parse(common_vendor.index.getStorageSync("selectedJob")).id,
  895. applicant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).id,
  896. job_position_question_id: this.currentAnswer.questionId,
  897. // answer_content: answerContent,
  898. answer_options: answerOptions,
  899. answer_duration: this.currentAnswer.answerDuration || 0,
  900. tenant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || 1
  901. };
  902. if (this.currentAnswer.questionType === 6 && blankAnswers.length > 0) {
  903. submitData.blank_answers = blankAnswers;
  904. }
  905. console.log("提交数据:", submitData);
  906. const res2 = await this.$http.post(`${common_config.apiBaseUrl}/api/job/submit_answer`, submitData);
  907. console.log("提交答案响应:", res2);
  908. return res2;
  909. } catch (error) {
  910. console.error("提交答案失败:", error);
  911. common_vendor.index.showToast({
  912. title: "提交答案失败,请重试",
  913. icon: "none"
  914. });
  915. throw error;
  916. } finally {
  917. common_vendor.index.hideLoading();
  918. this.isSubmitting = false;
  919. }
  920. },
  921. // 修改 goToNextQuestion 方法
  922. async goToNextQuestion() {
  923. if (this.currentQuestionIndex >= this.questions.length - 1) {
  924. this.showContinueButton = true;
  925. setTimeout(() => {
  926. this.scrollTop = 0;
  927. this.$nextTick(() => {
  928. setTimeout(() => {
  929. this.scrollTop = 1e4;
  930. }, 100);
  931. });
  932. }, 200);
  933. return;
  934. }
  935. this.currentQuestionIndex++;
  936. this.currentScrollId = "question-" + this.currentQuestionIndex;
  937. this.showResult = false;
  938. this.selectedOption = null;
  939. this.selectedOptions = [];
  940. this.openQuestionAnswer = "";
  941. if (this.questions[this.currentQuestionIndex]) {
  942. this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;
  943. this.resetTimer();
  944. this.playAiSpeaking();
  945. setTimeout(() => {
  946. this.pauseAiSpeaking();
  947. }, 2e3);
  948. }
  949. },
  950. // 填空题相关方法
  951. // 获取填空值
  952. getBlankValue(questionId, blankIndex) {
  953. if (this.currentBlankAnswers[questionId] && this.currentBlankAnswers[questionId][blankIndex]) {
  954. return this.currentBlankAnswers[questionId][blankIndex];
  955. }
  956. return "";
  957. },
  958. // 处理填空输入
  959. handleBlankInput(questionId, blankIndex, event) {
  960. const value = event.detail.value;
  961. if (!this.currentBlankAnswers[questionId]) {
  962. this.$set(this.currentBlankAnswers, questionId, {});
  963. }
  964. this.$set(this.currentBlankAnswers[questionId], blankIndex, value);
  965. const existingAnswerIndex = this.blankAnswers.findIndex((answer) => answer.questionId === questionId);
  966. if (existingAnswerIndex > -1) {
  967. this.blankAnswers[existingAnswerIndex].answer = this.currentBlankAnswers[questionId];
  968. } else {
  969. this.blankAnswers.push({
  970. questionId,
  971. answer: this.currentBlankAnswers[questionId]
  972. });
  973. }
  974. },
  975. // 检查是否有填空答案
  976. hasBlankAnswers(question) {
  977. const answers = this.currentBlankAnswers[question.id];
  978. if (!answers)
  979. return false;
  980. return Object.values(answers).some((value) => value && value.trim() !== "");
  981. },
  982. // 提交填空题
  983. submitBlankQuestion(qIndex) {
  984. this.questions[qIndex];
  985. this.checkAnswer();
  986. this.saveAnswer();
  987. this.submitCurrentAnswer().then(() => {
  988. setTimeout(() => {
  989. this.goToNextQuestion();
  990. }, 500);
  991. }).catch((error) => {
  992. console.error("提交答案失败:", error);
  993. setTimeout(() => {
  994. this.goToNextQuestion();
  995. }, 1e3);
  996. });
  997. }
  998. },
  999. // 添加生命周期钩子,确保在组件销毁时清除计时器
  1000. beforeDestroy() {
  1001. if (this.timerInterval) {
  1002. clearInterval(this.timerInterval);
  1003. }
  1004. }
  1005. };
  1006. if (!Array) {
  1007. const _component_uni_load_more = common_vendor.resolveComponent("uni-load-more");
  1008. _component_uni_load_more();
  1009. }
  1010. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  1011. return common_vendor.e({
  1012. a: !$data.digitalHumanUrl
  1013. }, !$data.digitalHumanUrl ? {
  1014. b: $data.mode,
  1015. c: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  1016. } : $data.digitalHumanUrl ? {
  1017. e: $data.digitalHumanUrl
  1018. } : {
  1019. f: common_assets._imports_0
  1020. }, {
  1021. d: $data.digitalHumanUrl,
  1022. g: $data.showPageWarning ? 1 : "",
  1023. h: common_vendor.f($data.questions, (question, qIndex, i0) => {
  1024. return common_vendor.e({
  1025. a: common_vendor.t(qIndex + 1),
  1026. b: common_vendor.t(question.questionTypeName),
  1027. c: common_vendor.t(question.text),
  1028. d: question.is_required_correct
  1029. }, question.is_required_correct ? {} : {}, {
  1030. e: question.questionType === 3 && question.imageUrl
  1031. }, question.questionType === 3 && question.imageUrl ? {
  1032. f: question.imageUrl
  1033. } : {}, {
  1034. g: question.questionType !== 0 && question.questionType !== 6
  1035. }, question.questionType !== 0 && question.questionType !== 6 ? {
  1036. h: common_vendor.f(question.options, (option, index, i1) => {
  1037. return {
  1038. a: common_vendor.t(option.option_text || (typeof option === "string" ? option : JSON.stringify(option))),
  1039. b: index,
  1040. c: $options.isOptionSelected(question, qIndex, index) ? 1 : "",
  1041. d: common_vendor.o(($event) => $options.handleOptionClick(qIndex, index), index)
  1042. };
  1043. }),
  1044. i: qIndex !== $data.currentQuestionIndex ? 1 : ""
  1045. } : {}, {
  1046. j: question.questionType === 6
  1047. }, question.questionType === 6 ? common_vendor.e({
  1048. k: question.blanks && question.blanks.length > 0
  1049. }, question.blanks && question.blanks.length > 0 ? {
  1050. l: common_vendor.f(question.blanks, (blank, blankIndex, i1) => {
  1051. return {
  1052. a: common_vendor.t(blankIndex + 1),
  1053. b: $options.getBlankValue(question.id, blankIndex + 1),
  1054. c: common_vendor.o(($event) => $options.handleBlankInput(question.id, blankIndex + 1, $event), blankIndex),
  1055. d: blankIndex
  1056. };
  1057. }),
  1058. m: qIndex !== $data.currentQuestionIndex
  1059. } : {}, {
  1060. n: qIndex === $data.currentQuestionIndex && $options.hasBlankAnswers(question)
  1061. }, qIndex === $data.currentQuestionIndex && $options.hasBlankAnswers(question) ? {
  1062. o: common_vendor.o(($event) => $options.submitBlankQuestion(qIndex), question.id)
  1063. } : {}) : {}, {
  1064. p: qIndex === $data.currentQuestionIndex && question.questionType === 2
  1065. }, qIndex === $data.currentQuestionIndex && question.questionType === 2 ? {
  1066. q: common_vendor.t("下一题"),
  1067. r: common_vendor.o(($event) => $options.nextQuestion(), question.id),
  1068. s: $data.selectedOptions.length === 0
  1069. } : {}, {
  1070. t: question.id,
  1071. v: "question-" + qIndex
  1072. });
  1073. }),
  1074. i: common_vendor.t($data.questions.length),
  1075. j: $data.showContinueButton
  1076. }, $data.showContinueButton ? {
  1077. k: common_vendor.t($data.currentGroupIndex < $data.questionGroups.length - 1 ? "继续下一部分" : "完成测试"),
  1078. l: common_vendor.o((...args) => $options.handleContinue && $options.handleContinue(...args))
  1079. } : {}, {
  1080. m: $data.currentScrollId,
  1081. n: $data.scrollTop,
  1082. o: $data.showEndModal
  1083. }, $data.showEndModal ? {
  1084. p: common_vendor.o((...args) => $options.navigateToInterview && $options.navigateToInterview(...args))
  1085. } : {}, {
  1086. q: $data.interviewCompleted
  1087. }, $data.interviewCompleted ? {
  1088. r: common_assets._imports_0,
  1089. s: common_vendor.o((...args) => $options.back && $options.back(...args))
  1090. } : {}, {
  1091. t: $data.loading
  1092. }, $data.loading ? {
  1093. v: common_vendor.p({
  1094. status: "loading",
  1095. contentText: {
  1096. contentdown: "加载中..."
  1097. }
  1098. })
  1099. } : {}, {
  1100. w: !$data.loading && $data.loadError
  1101. }, !$data.loading && $data.loadError ? {
  1102. x: common_vendor.o((...args) => $options.fetchInterviewData && $options.fetchInterviewData(...args))
  1103. } : {}, {
  1104. y: $data.showPromptModal
  1105. }, $data.showPromptModal ? {
  1106. z: common_vendor.o((...args) => $options.handlePromptConfirm && $options.handlePromptConfirm(...args))
  1107. } : {});
  1108. }
  1109. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  1110. wx.createPage(MiniProgramPage);