identity-verify.js 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_config = require("../../common/config.js");
  4. const _sfc_main = {
  5. name: "IdentityVerify",
  6. data() {
  7. return {
  8. loading: false,
  9. responses: [],
  10. processedResponses: [],
  11. assistantResponse: "",
  12. audioTranscript: "",
  13. videoPlaying: false,
  14. showDebugInfo: false,
  15. // 设置为true可以显示调试信息
  16. videoUrl: "",
  17. // 修改为空字符串,将通过API获取
  18. showReplayButton: false,
  19. cameraStream: null,
  20. // 存储摄像头流
  21. cameraError: null,
  22. // 存储摄像头错误信息
  23. useMiniProgramCameraComponent: false,
  24. // 添加小程序相机组件标志
  25. cameraContext: null,
  26. // 添加相机上下文
  27. currentSubtitle: "",
  28. currentTranslation: "",
  29. // 添加翻译文本
  30. subtitles: [],
  31. // 修改为空数组,将通过API获取
  32. // 移除硬编码的字幕数组
  33. // secondVideoSubtitles: [...],
  34. // thirdVideoSubtitles: [...],
  35. // 等等...
  36. showAnswerButton: false,
  37. // 控制答题按钮显示
  38. currentVideoIndex: 0,
  39. // 当前播放的视频索引
  40. videoList: [],
  41. // 修改为空数组,将通过API获取
  42. // 其他属性保持不变...
  43. questions: [],
  44. // 添加新属性存储API返回的问题数据
  45. introVideoUrl: (() => {
  46. try {
  47. const configStr = common_vendor.index.getStorageSync("configData");
  48. if (configStr && configStr.trim()) {
  49. return JSON.parse(configStr).digital_human_opening_video_url || "https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4";
  50. }
  51. return "https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4";
  52. } catch (error) {
  53. console.warn("解析配置数据失败:", error);
  54. return {};
  55. }
  56. })(),
  57. //'https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4', // 保留介绍视频
  58. isRecording: false,
  59. recordingTimer: null,
  60. showStopRecordingButton: false,
  61. mediaRecorder: null,
  62. recordedChunks: [],
  63. recorder: null,
  64. lastUploadedVideoUrl: "",
  65. showStartRecordingButton: false,
  66. showRetryButton: false,
  67. // 控制重试按钮显示
  68. lastVideoToRetry: null,
  69. // 存储上次失败的视频URL,用于重试
  70. recordingStartTime: null,
  71. // 录制开始时间
  72. recordingTimerCount: 0,
  73. // 录制计时器计数
  74. recordingTimeDisplay: "00:00",
  75. // 格式化的录制时间显示
  76. // 添加上传队列相关数据
  77. uploadQueue: [],
  78. // 存储待上传的视频
  79. isUploading: false,
  80. // 标记是否正在上传
  81. uploadProgress: {},
  82. // 存储每个视频的上传进度
  83. uploadStatus: {},
  84. // 存储每个视频的上传状态
  85. showUploadStatus: false,
  86. // 是否显示上传状态指示器
  87. uploadStatusText: "",
  88. // 上传状态文本
  89. mediaRecorderTimeout: null,
  90. // 用于存储MediaRecorder的超时机制
  91. maxRecordingTime: 300,
  92. // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  93. remainingTime: 300,
  94. // 剩余录制时间(秒)- 从60秒改为300秒
  95. countdownValue: 10,
  96. // 倒计时数值
  97. showCountdown: false,
  98. // 是否显示倒计时蒙层
  99. countdownTimer: null,
  100. showGif: false,
  101. // 控制是否显示GIF
  102. gifUrl: "",
  103. // GIF图片的URL
  104. globalSocketTask: null,
  105. // 添加全局 WebSocket 连接对象
  106. lowScoreVideoUrl: "https://data.qicai321.com/minlong/latentsync/0530e7f5-1957-422d-8f34-ba4a92608081_result.mp4",
  107. // 低分提示视频URL
  108. showRerecordButton: false,
  109. // 控制重新录制按钮显示
  110. isPlayingLowScoreVideo: false,
  111. // 标记是否正在播放低分提示视频
  112. lowScoreVideoSubtitles: [
  113. {
  114. startTime: 0,
  115. endTime: 10,
  116. // 假设视频长度为10秒,可根据实际情况调整
  117. text: "我未听清楚您在说什么,请您再说一遍!"
  118. }
  119. ],
  120. // 用于存储低分提示视频的字幕
  121. retryCount: 0,
  122. // 添加重试次数计数器
  123. maxRetryAttempts: 2,
  124. // 最大重试次数限制
  125. needPlayLowScoreVideo: false,
  126. // 是否需要播放低分视频
  127. finalRecordingDuration: 0,
  128. // 最终录制时长
  129. historyTime: 0,
  130. // 添加历史时间记录
  131. followUpQuestions: [],
  132. // 存储追问问题数据
  133. currentParentQuestionId: null,
  134. // 当前父问题ID
  135. hasPlayedFollowUp: {},
  136. // 记录已播放的追问,格式: {questionId: true}
  137. isFollowUpQuestion: false,
  138. // 标记当前是否正在播放追问问题
  139. currentFollowUpQuestion: null,
  140. // 添加当前追问问题的完整信息
  141. lastQuestionWasFollowUp: false,
  142. lastFollowUpQuestionId: null,
  143. lastUpdateTime: Date.now(),
  144. // 添加最后更新时间戳
  145. subtitleMap: {},
  146. // 用于存储字幕和翻译的映射
  147. progressPercent: 0,
  148. // 录制进度百分比
  149. progressColor: "#05dc8b",
  150. // 进度条颜色
  151. progressBgColor: "rgba(0, 0, 0,0.3)",
  152. // 进度条背景色
  153. parentQuestion: "",
  154. // 添加父问题存储
  155. screenCaptureCount: 0,
  156. // 添加截屏次数记录
  157. personDetectionSocket: null,
  158. // WebSocket对象
  159. personDetectionInterval: null,
  160. // 定时器对象
  161. showCameraWarning: false,
  162. // 添加新的数据属性
  163. showPageWarning: false
  164. // 添加新的数据属性
  165. };
  166. },
  167. mounted() {
  168. this.fetchQuestions();
  169. this.fetchFollowUpQuestions();
  170. this.checkAudioPermission();
  171. this.initCamera();
  172. this.checkIOSCameraRecordPermission();
  173. this.checkAndFixRenderingIssues();
  174. setTimeout(() => {
  175. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  176. this.testAudioInput();
  177. }
  178. }, 3e3);
  179. this.historyTime = 0;
  180. common_vendor.index.setKeepScreenOn({
  181. keepScreenOn: true
  182. });
  183. common_vendor.index.onUserCaptureScreen(() => {
  184. console.log("User captured screen");
  185. this.screenCaptureCount++;
  186. if (this.screenCaptureCount === 1) {
  187. common_vendor.index.showModal({
  188. title: "Warning",
  189. content: "Screen capture detected. If you capture the screen again, your interview will be invalid.",
  190. showCancel: false,
  191. confirmText: "OK"
  192. });
  193. } else if (this.screenCaptureCount >= 2) {
  194. common_vendor.index.showModal({
  195. title: "Interview Invalid",
  196. content: "Your interview has been invalidated due to multiple screen captures. Please contact the recruiter for assistance.",
  197. showCancel: false,
  198. confirmText: "OK",
  199. success: () => {
  200. this.invalidateInterview();
  201. }
  202. });
  203. }
  204. });
  205. const systemInfo = common_vendor.index.getSystemInfoSync();
  206. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  207. if (isMiniProgram) {
  208. this.initPersonDetectionWebSocket();
  209. }
  210. },
  211. beforeDestroy() {
  212. common_vendor.index.offUserCaptureScreen();
  213. this.cleanupPersonDetectionWebSocket();
  214. },
  215. methods: {
  216. // 初始化相机
  217. async initCamera() {
  218. const systemInfo = common_vendor.index.getSystemInfoSync();
  219. const isMiniProgram = systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay" || systemInfo.uniPlatform === "mp-baidu";
  220. if (isMiniProgram) {
  221. this.useMiniProgramCameraComponent = true;
  222. this.cameraContext = common_vendor.index.createCameraContext();
  223. common_vendor.index.getSetting({
  224. success: (res) => {
  225. if (!res.authSetting["scope.record"]) {
  226. common_vendor.index.authorize({
  227. scope: "scope.record",
  228. success: () => {
  229. console.log("录音权限已获取");
  230. },
  231. fail: (err) => {
  232. console.error("录音权限获取失败:", err);
  233. this.showPermissionDialog("录音");
  234. }
  235. });
  236. }
  237. if (!res.authSetting["scope.camera"]) {
  238. common_vendor.index.authorize({
  239. scope: "scope.camera",
  240. success: () => {
  241. console.log("相机权限已获取");
  242. },
  243. fail: (err) => {
  244. console.error("相机权限获取失败:", err);
  245. this.showPermissionDialog("相机");
  246. }
  247. });
  248. }
  249. const systemInfo2 = common_vendor.index.getSystemInfoSync();
  250. if (systemInfo2.platform === "ios") {
  251. if (!res.authSetting["scope.camera"] || !res.authSetting["scope.record"]) {
  252. console.log("iOS需要同时获取相机和录音权限");
  253. }
  254. }
  255. }
  256. });
  257. } else {
  258. try {
  259. const constraints = {
  260. audio: {
  261. echoCancellation: true,
  262. noiseSuppression: true,
  263. autoGainControl: true
  264. },
  265. video: {
  266. width: { ideal: 640, max: 1280 },
  267. // 控制视频宽度
  268. height: { ideal: 480, max: 720 },
  269. // 控制视频高度
  270. frameRate: { ideal: 15, max: 24 },
  271. // 控制帧率
  272. facingMode: "user"
  273. }
  274. };
  275. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  276. this.cameraStream = stream;
  277. const audioTracks = stream.getAudioTracks();
  278. console.log("音频轨道数量:", audioTracks.length);
  279. if (audioTracks.length > 0) {
  280. console.log("音频轨道已获取:", audioTracks[0].label);
  281. audioTracks[0].enabled = true;
  282. } else {
  283. console.warn("未检测到音频轨道,尝试单独获取音频");
  284. this.tryGetAudioOnly();
  285. }
  286. const videoElement = this.$refs.userCameraVideo;
  287. if (videoElement) {
  288. videoElement.srcObject = stream;
  289. videoElement.muted = true;
  290. }
  291. } catch (error) {
  292. console.error("获取摄像头失败:", error);
  293. this.cameraError = error.message || "无法访问摄像头";
  294. common_vendor.index.showToast({
  295. title: "无法访问摄像头,请检查权限设置",
  296. icon: "none"
  297. });
  298. }
  299. }
  300. },
  301. // 停止用户摄像头
  302. stopUserCamera() {
  303. if (this.cameraStream) {
  304. this.cameraStream.getTracks().forEach((track) => {
  305. track.stop();
  306. });
  307. this.cameraStream = null;
  308. }
  309. },
  310. async fetchData() {
  311. this.loading = true;
  312. this.assistantResponse = "";
  313. this.audioTranscript = "";
  314. this.processedResponses = [];
  315. try {
  316. const requestTask = common_vendor.index.request({
  317. url: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
  318. method: "POST",
  319. header: {
  320. "Content-Type": "application/json",
  321. "Authorization": "Bearer sk-9e1ec73a7d97493b8613c63f06b6110c"
  322. },
  323. data: {
  324. "model": "qwen-omni-turbo",
  325. "messages": [
  326. {
  327. "role": "user",
  328. "content": [
  329. {
  330. "type": "input_audio",
  331. "input_audio": {
  332. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  333. "format": "wav"
  334. }
  335. },
  336. {
  337. "type": "text",
  338. "text": "这段音频在说什么"
  339. }
  340. ]
  341. }
  342. ],
  343. "stream": true,
  344. "stream_options": {
  345. "include_usage": true
  346. },
  347. "modalities": ["text", "audio"],
  348. "audio": { "voice": "Cherry", "format": "wav" }
  349. },
  350. success: (res) => {
  351. console.log("请求成功,响应数据:", res.data);
  352. if (typeof res.data === "string" && res.data.includes("data: {")) {
  353. const chunks = res.data.split("data: ").filter((chunk) => chunk.trim() !== "");
  354. chunks.forEach((chunk) => {
  355. this.handleStreamResponse(chunk);
  356. });
  357. } else {
  358. this.handleStreamResponse(res.data);
  359. }
  360. this.playDigitalHumanVideo();
  361. },
  362. fail: (err) => {
  363. console.error("请求失败:", err);
  364. },
  365. complete: () => {
  366. this.loading = false;
  367. }
  368. });
  369. } catch (error) {
  370. console.error("获取数据失败:", error);
  371. this.loading = false;
  372. }
  373. },
  374. handleStreamResponse(data) {
  375. if (typeof data === "string") {
  376. if (data === "[DONE]")
  377. return;
  378. try {
  379. const cleanData = data.trim();
  380. if (cleanData.startsWith("{") && cleanData.endsWith("}")) {
  381. const jsonData = JSON.parse(cleanData);
  382. this.processStreamChunk(jsonData);
  383. }
  384. } catch (e) {
  385. console.error("解析JSON失败:", e, "原始数据:", data);
  386. }
  387. } else {
  388. this.processStreamChunk(data);
  389. }
  390. },
  391. processStreamChunk(chunk) {
  392. if (chunk.choices && chunk.choices.length > 0) {
  393. const choice = chunk.choices[0];
  394. if (choice.delta && choice.delta.content) {
  395. this.assistantResponse += choice.delta.content;
  396. }
  397. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  398. this.audioTranscript += choice.delta.audio.transcript;
  399. }
  400. if (choice.delta) {
  401. const result = {};
  402. if (choice.delta.role) {
  403. result.role = choice.delta.role;
  404. }
  405. if (choice.delta.audio && choice.delta.audio.transcript) {
  406. result.transcript = choice.delta.audio.transcript;
  407. }
  408. if (Object.keys(result).length > 0) {
  409. this.processedResponses.push(result);
  410. }
  411. }
  412. }
  413. },
  414. processResponseData() {
  415. this.processedResponses = this.responses.map((item) => {
  416. const result = {};
  417. if (item.delta && item.delta.role) {
  418. result.role = item.delta.role;
  419. }
  420. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  421. result.transcript = item.delta.audio.transcript;
  422. }
  423. return result;
  424. }).filter((item) => Object.keys(item).length > 0);
  425. },
  426. // 播放数字人视频
  427. playDigitalHumanVideo() {
  428. if (this.videoList.length === 0) {
  429. this.fetchQuestions();
  430. return;
  431. }
  432. this.videoUrl = this.videoList[this.currentVideoIndex];
  433. this.videoPlaying = true;
  434. this.$nextTick(() => {
  435. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  436. if (videoContext) {
  437. videoContext.play();
  438. setTimeout(() => {
  439. if (this.videoPlaying && this.$refs.videoPlayer) {
  440. console.log("视频应该正在播放");
  441. } else {
  442. console.log("视频可能未成功播放,尝试替代方案");
  443. this.tryAlternativeVideoPath();
  444. }
  445. }, 1e3);
  446. } else {
  447. console.error("无法创建视频上下文");
  448. this.tryAlternativeVideoPath();
  449. }
  450. });
  451. },
  452. // 修改 tryAlternativeVideoPath 方法
  453. tryAlternativeVideoPath() {
  454. console.log("尝试使用替代路径");
  455. const alternativePaths = [
  456. "./static/demo.mp4",
  457. "../static/demo.mp4",
  458. "static/demo.mp4",
  459. "/static/demo.mp4",
  460. // 添加绝对路径
  461. `${window.location.origin}/static/demo.mp4`
  462. ];
  463. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  464. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  465. this.videoUrl = alternativePaths[nextPathIndex];
  466. console.log("尝试新路径:", this.videoUrl);
  467. this.$nextTick(() => {
  468. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  469. if (videoContext) {
  470. videoContext.stop();
  471. videoContext.play();
  472. setTimeout(() => {
  473. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  474. console.log("所有路径均失败,尝试使用uni.getVideoInfo检查视频");
  475. this.checkVideoWithAPI();
  476. }
  477. }, 1e3);
  478. }
  479. });
  480. },
  481. // 添加新方法:使用uni API检查视频
  482. checkVideoWithAPI() {
  483. common_vendor.index.getVideoInfo({
  484. src: "/static/demo.mp4",
  485. success: (res) => {
  486. console.log("视频信息获取成功:", res);
  487. this.videoUrl = "/static/demo.mp4";
  488. this.$nextTick(() => {
  489. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  490. if (videoContext) {
  491. videoContext.play();
  492. }
  493. });
  494. },
  495. fail: (err) => {
  496. console.error("视频信息获取失败:", err);
  497. this.fallbackToLocalVideo();
  498. }
  499. });
  500. },
  501. // 添加新方法:回退到本地视频
  502. fallbackToLocalVideo() {
  503. console.log("尝试使用本地视频资源");
  504. const platform = common_vendor.index.getSystemInfoSync().platform;
  505. if (platform === "android" || platform === "ios") {
  506. this.videoUrl = platform === "android" ? "android.resource://package_name/raw/demo" : "file:///assets/demo.mp4";
  507. this.$nextTick(() => {
  508. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  509. if (videoContext) {
  510. videoContext.play();
  511. }
  512. });
  513. } else {
  514. this.videoPlaying = false;
  515. common_vendor.index.showToast({
  516. title: "视频加载失败,显示静态图片",
  517. icon: "none"
  518. });
  519. }
  520. },
  521. // 修改 handleVideoError 方法
  522. handleVideoError(e) {
  523. console.error("视频加载错误:", e);
  524. if (e && e.detail) {
  525. console.error("详细错误信息:", e.detail);
  526. }
  527. common_vendor.index.getFileInfo({
  528. filePath: this.videoUrl.startsWith("/") ? this.videoUrl.substring(1) : this.videoUrl,
  529. success: (res) => {
  530. console.log("文件存在,大小:", res.size);
  531. this.tryDifferentFormat();
  532. },
  533. fail: (err) => {
  534. console.error("文件不存在或无法访问:", err);
  535. this.tryAlternativeVideoPath();
  536. }
  537. });
  538. common_vendor.index.showToast({
  539. title: "视频加载失败,请检查文件是否存在",
  540. icon: "none",
  541. duration: 2e3
  542. });
  543. },
  544. // 添加新方法:尝试不同格式
  545. tryDifferentFormat() {
  546. console.log("尝试不同的视频格式");
  547. const formats = [
  548. { ext: "mp4", mime: "video/mp4" },
  549. { ext: "webm", mime: "video/webm" },
  550. { ext: "ogg", mime: "video/ogg" },
  551. { ext: "mov", mime: "video/quicktime" }
  552. ];
  553. const currentPath = this.videoUrl;
  554. const basePath = currentPath.substring(0, currentPath.lastIndexOf(".")) || "/static/demo";
  555. let nextFormat = formats.find((f) => !currentPath.endsWith(f.ext));
  556. if (nextFormat) {
  557. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  558. console.log("尝试新格式:", this.videoUrl);
  559. this.$nextTick(() => {
  560. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  561. if (videoContext) {
  562. videoContext.stop();
  563. videoContext.play();
  564. }
  565. });
  566. } else {
  567. this.useBuiltInResource();
  568. }
  569. },
  570. // 添加新方法:使用内置资源
  571. useBuiltInResource() {
  572. console.log("尝试使用内置资源");
  573. const platform = common_vendor.index.getSystemInfoSync().platform;
  574. if (platform === "windows") {
  575. process.env.UNI_INPUT_DIR || "";
  576. this.videoUrl = `./static/demo.mp4`;
  577. console.log("Windows平台尝试路径:", this.videoUrl);
  578. } else if (platform === "android" || platform === "ios") {
  579. this.useNativeVideo();
  580. } else {
  581. const baseUrl = window.location.origin;
  582. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  583. console.log("Web平台尝试URL:", this.videoUrl);
  584. }
  585. this.$nextTick(() => {
  586. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  587. if (videoContext) {
  588. videoContext.play();
  589. }
  590. });
  591. },
  592. // 添加新方法:使用原生视频能力
  593. useNativeVideo() {
  594. console.log("尝试使用原生视频能力");
  595. common_vendor.index.chooseVideo({
  596. sourceType: ["album"],
  597. success: (res) => {
  598. this.videoUrl = res.tempFilePath;
  599. this.$nextTick(() => {
  600. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  601. if (videoContext) {
  602. videoContext.play();
  603. }
  604. });
  605. },
  606. fail: () => {
  607. this.videoPlaying = false;
  608. common_vendor.index.showToast({
  609. title: "无法加载视频,显示静态图片",
  610. icon: "none"
  611. });
  612. }
  613. });
  614. },
  615. // 处理视频结束事件
  616. handleVideoEnded() {
  617. console.log("视频播放结束事件触发");
  618. const videoElement = this.$refs.videoPlayer;
  619. if (videoElement) {
  620. const currentTime = videoElement.currentTime || 0;
  621. const duration = videoElement.duration || 0;
  622. if (duration > 0 && duration - currentTime > 0.5) {
  623. console.log("检测到可能的误触发结束事件,继续播放", currentTime, duration);
  624. setTimeout(() => {
  625. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  626. if (videoContext) {
  627. if (duration - currentTime > 2) {
  628. videoContext.seek(this.historyTime || 0);
  629. } else {
  630. videoContext.play();
  631. }
  632. }
  633. }, 100);
  634. return;
  635. }
  636. console.log("视频确认结束,当前时间:", currentTime, "总时长:", duration);
  637. }
  638. this.videoPlaying = false;
  639. if (this.isPlayingLowScoreVideo) {
  640. console.log("低分提示视频播放完成");
  641. if (this.retryCount < this.maxRetryAttempts) {
  642. console.log(`显示重新录制按钮 (重试次数: ${this.retryCount + 1}/${this.maxRetryAttempts})`);
  643. this.showRerecordButton = true;
  644. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  645. this.currentSubtitle = this.currentFollowUpQuestion.digital_human_video_subtitle || this.currentFollowUpQuestion.question;
  646. }
  647. } else {
  648. console.log("已达到最大重试次数,继续下一题");
  649. this.retryCount = 0;
  650. this.proceedToNextQuestion();
  651. }
  652. this.isPlayingLowScoreVideo = false;
  653. return;
  654. }
  655. if (this.isFollowUpQuestion) {
  656. console.log("追问问题视频播放完成");
  657. this.showStartRecordingButton = true;
  658. this.startCountdown();
  659. return;
  660. }
  661. if (this.currentVideoIndex >= 1) {
  662. this.showStartRecordingButton = true;
  663. this.startCountdown();
  664. } else {
  665. this.showAnswerButton = true;
  666. }
  667. if (this.isFollowUpQuestion) {
  668. this.isFollowUpQuestion = false;
  669. this.currentFollowUpQuestion = null;
  670. }
  671. },
  672. // 添加新方法:开始倒计时
  673. startCountdown() {
  674. this.showCountdown = true;
  675. this.countdownValue = 10;
  676. this.countdownTimer = setInterval(() => {
  677. this.countdownValue--;
  678. if (this.countdownValue <= 0) {
  679. this.clearCountdown();
  680. this.showStartRecordingButton = false;
  681. this.startRecordingAnswer();
  682. }
  683. }, 1e3);
  684. },
  685. // 添加新方法:清除倒计时
  686. clearCountdown() {
  687. if (this.countdownTimer) {
  688. clearInterval(this.countdownTimer);
  689. this.countdownTimer = null;
  690. }
  691. this.showCountdown = false;
  692. },
  693. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  694. handleStartRecordingClick() {
  695. this.showStartRecordingButton = false;
  696. this.clearCountdown();
  697. this.startRecordingAnswer();
  698. },
  699. // 修改 stopRecordingAnswer 方法,添加录制时长检查
  700. stopRecordingAnswer() {
  701. this.clearCountdown();
  702. const recordingDuration = this.getRecordingDuration();
  703. const minimumDuration = 3;
  704. const lowScoreDuration = 7;
  705. if (recordingDuration < minimumDuration) {
  706. common_vendor.index.showToast({
  707. title: "录制时间过短,请至少录制3秒",
  708. icon: "none",
  709. duration: 2e3
  710. });
  711. return;
  712. }
  713. this.completeRecordingStop();
  714. if (recordingDuration < lowScoreDuration) {
  715. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  716. this.needPlayLowScoreVideo = true;
  717. } else {
  718. this.needPlayLowScoreVideo = false;
  719. }
  720. },
  721. // 添加新方法:开始录制用户回答
  722. startRecordingAnswer() {
  723. console.log("开始录制用户回答");
  724. this.isRecording = true;
  725. this.recordingStartTime = Date.now();
  726. this.recordingTimerCount = 0;
  727. this.remainingTime = this.maxRecordingTime;
  728. this.recordingTimer = setInterval(() => {
  729. this.recordingTimerCount++;
  730. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  731. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  732. this.progressPercent = this.recordingTimerCount / this.maxRecordingTime * 100;
  733. if (this.recordingTimerCount >= this.maxRecordingTime) {
  734. console.log("已达到最大录制时间(5分钟),自动停止录制");
  735. this.stopRecordingAnswer();
  736. }
  737. }, 1e3);
  738. const systemInfo = common_vendor.index.getSystemInfoSync();
  739. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  740. if (isMiniProgram) {
  741. this.startMiniProgramRecording();
  742. } else {
  743. this.startBrowserRecording();
  744. }
  745. this.showStopRecordingButton = true;
  746. },
  747. // 添加一个新方法:重置相机组件
  748. resetCamera() {
  749. console.log("重置相机组件");
  750. this.useMiniProgramCameraComponent = false;
  751. if (this.cameraContext) {
  752. this.cameraContext = null;
  753. }
  754. setTimeout(() => {
  755. this.useMiniProgramCameraComponent = true;
  756. setTimeout(() => {
  757. this.cameraContext = common_vendor.index.createCameraContext();
  758. console.log("相机组件已重置");
  759. }, 500);
  760. }, 500);
  761. },
  762. // 修改 startMiniProgramRecording 方法
  763. startMiniProgramRecording() {
  764. console.log("开始小程序录制方法");
  765. const systemInfo = common_vendor.index.getSystemInfoSync();
  766. const isIOS = systemInfo.platform === "ios";
  767. if (isIOS) {
  768. this.resetCamera();
  769. setTimeout(() => {
  770. this.actualStartRecording(isIOS);
  771. }, 1e3);
  772. } else {
  773. this.actualStartRecording(isIOS);
  774. }
  775. },
  776. // 添加新方法:实际开始录制
  777. actualStartRecording(isIOS) {
  778. if (!this.cameraContext) {
  779. this.cameraContext = common_vendor.index.createCameraContext();
  780. console.log("创建新的相机上下文");
  781. }
  782. common_vendor.index.getSetting({
  783. success: (res) => {
  784. const hasRecordAuth = res.authSetting["scope.record"];
  785. const hasCameraAuth = res.authSetting["scope.camera"];
  786. if (!hasRecordAuth || !hasCameraAuth) {
  787. console.warn("缺少必要权限,请求权限");
  788. this.requestMiniProgramPermissions();
  789. return;
  790. }
  791. if (isIOS) {
  792. console.log("iOS: 检查相机状态");
  793. const options = {
  794. timeout: 3e5,
  795. // 300秒超时 (5分钟)
  796. quality: "low",
  797. // 降低质量
  798. compressed: true,
  799. success: () => {
  800. console.log("iOS录制开始成功");
  801. },
  802. fail: (err) => {
  803. console.error("iOS录制失败:", err);
  804. this.useAlternativeRecordingMethod();
  805. }
  806. };
  807. try {
  808. console.log("尝试开始录制");
  809. this.recorder = this.cameraContext.startRecord(options);
  810. } catch (e) {
  811. console.error("开始录制异常:", e);
  812. this.useAlternativeRecordingMethod();
  813. }
  814. } else {
  815. const options = {
  816. timeout: 3e5,
  817. // 300秒超时 (5分钟)
  818. quality: "medium",
  819. compressed: true,
  820. success: () => {
  821. console.log("Android录制开始成功");
  822. },
  823. fail: (err) => {
  824. console.error("Android录制失败:", err);
  825. common_vendor.index.showToast({
  826. title: "录制失败,请检查相机权限",
  827. icon: "none"
  828. });
  829. this.proceedToNextQuestion();
  830. }
  831. };
  832. this.recorder = this.cameraContext.startRecord(options);
  833. }
  834. }
  835. });
  836. },
  837. // 添加新方法:使用替代录制方法
  838. useAlternativeRecordingMethod() {
  839. console.log("使用替代录制方法");
  840. common_vendor.index.showActionSheet({
  841. itemList: ["使用相册中的视频", "跳过此问题"],
  842. success: (res) => {
  843. if (res.tapIndex === 0) {
  844. common_vendor.index.chooseVideo({
  845. sourceType: ["album"],
  846. maxDuration: 300,
  847. // 从60秒改为300秒
  848. camera: "front",
  849. success: (res2) => {
  850. console.log("选择视频成功:", res2.tempFilePath);
  851. this.isRecording = false;
  852. this.showStopRecordingButton = false;
  853. this.uploadRecordedVideo(res2.tempFilePath);
  854. },
  855. fail: () => {
  856. console.log("用户取消选择视频");
  857. this.proceedToNextQuestion();
  858. }
  859. });
  860. } else {
  861. console.log("用户选择跳过问题");
  862. this.proceedToNextQuestion();
  863. }
  864. },
  865. fail: () => {
  866. console.log("操作取消");
  867. this.proceedToNextQuestion();
  868. }
  869. });
  870. },
  871. // 添加新方法:请求小程序权限
  872. requestMiniProgramPermissions() {
  873. common_vendor.index.authorize({
  874. scope: "scope.record",
  875. success: () => {
  876. console.log("录音权限已获取");
  877. common_vendor.index.authorize({
  878. scope: "scope.camera",
  879. success: () => {
  880. console.log("相机权限已获取");
  881. this.startMiniProgramRecording();
  882. },
  883. fail: (err) => {
  884. console.error("相机权限获取失败:", err);
  885. this.showPermissionDialog("相机");
  886. }
  887. });
  888. },
  889. fail: (err) => {
  890. console.error("录音权限获取失败:", err);
  891. this.showPermissionDialog("录音");
  892. }
  893. });
  894. },
  895. // 修改浏览器环境下的录制方法
  896. startBrowserRecording() {
  897. if (!this.cameraStream) {
  898. console.error("没有可用的摄像头流");
  899. common_vendor.index.showToast({
  900. title: "录制失败,摄像头未就绪",
  901. icon: "none"
  902. });
  903. this.proceedToNextQuestion();
  904. return;
  905. }
  906. try {
  907. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  908. if (!hasAudio) {
  909. console.warn("警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流");
  910. navigator.mediaDevices.getUserMedia({
  911. audio: {
  912. echoCancellation: true,
  913. noiseSuppression: true,
  914. autoGainControl: true
  915. },
  916. video: true
  917. }).then((newStream) => {
  918. const audioTracks = newStream.getAudioTracks();
  919. if (audioTracks.length > 0) {
  920. console.log("成功获取音频轨道:", audioTracks[0].label);
  921. const videoTrack = this.cameraStream.getVideoTracks()[0];
  922. const audioTrack = newStream.getAudioTracks()[0];
  923. const combinedStream = new MediaStream();
  924. if (videoTrack)
  925. combinedStream.addTrack(videoTrack);
  926. if (audioTrack)
  927. combinedStream.addTrack(audioTrack);
  928. this.cameraStream = combinedStream;
  929. const videoElement = this.$refs.userCameraVideo;
  930. if (videoElement) {
  931. videoElement.srcObject = combinedStream;
  932. videoElement.muted = true;
  933. }
  934. this.setupMediaRecorder(combinedStream);
  935. } else {
  936. console.warn("仍然无法获取音频轨道");
  937. this.setupMediaRecorder(this.cameraStream);
  938. }
  939. }).catch((err) => {
  940. console.error("获取音频失败:", err);
  941. this.setupMediaRecorder(this.cameraStream);
  942. });
  943. } else {
  944. console.log("检测到音频轨道,直接使用");
  945. this.setupMediaRecorder(this.cameraStream);
  946. }
  947. } catch (error) {
  948. console.error("浏览器录制失败:", error);
  949. common_vendor.index.showToast({
  950. title: "录制失败,浏览器可能不支持此功能",
  951. icon: "none"
  952. });
  953. this.proceedToNextQuestion();
  954. }
  955. },
  956. // 修改 setupMediaRecorder 方法
  957. setupMediaRecorder(stream) {
  958. const videoTracks = stream.getVideoTracks();
  959. const audioTracks = stream.getAudioTracks();
  960. console.log("设置MediaRecorder - 视频轨道:", videoTracks.length, "音频轨道:", audioTracks.length);
  961. let mimeType = "";
  962. const supportedTypes = [
  963. "video/webm;codecs=vp9,opus",
  964. "video/webm;codecs=vp8,opus",
  965. "video/webm;codecs=h264,opus",
  966. "video/mp4;codecs=h264,aac",
  967. "video/webm",
  968. "video/mp4"
  969. ];
  970. for (const type of supportedTypes) {
  971. if (MediaRecorder.isTypeSupported(type)) {
  972. mimeType = type;
  973. console.log("使用支持的MIME类型:", mimeType);
  974. break;
  975. }
  976. }
  977. const options = {
  978. mimeType: mimeType || "",
  979. audioBitsPerSecond: 64e3,
  980. // 降低音频比特率
  981. videoBitsPerSecond: 1e6
  982. // 降低视频比特率到1Mbps
  983. };
  984. try {
  985. this.mediaRecorder = new MediaRecorder(stream, options);
  986. console.log("MediaRecorder创建成功,使用选项:", options);
  987. } catch (e) {
  988. console.warn("使用指定选项创建MediaRecorder失败,尝试使用默认选项");
  989. this.mediaRecorder = new MediaRecorder(stream);
  990. }
  991. this.recordedChunks = [];
  992. this.mediaRecorder.ondataavailable = (event) => {
  993. if (event.data && event.data.size > 0) {
  994. this.recordedChunks.push(event.data);
  995. console.log(`收到数据块: ${event.data.size} 字节`);
  996. }
  997. };
  998. this.mediaRecorder.onstop = async () => {
  999. console.log("MediaRecorder停止,数据块数量:", this.recordedChunks.length);
  1000. if (this.recordedChunks.length === 0) {
  1001. console.error("没有录制到数据");
  1002. common_vendor.index.showToast({
  1003. title: "录制失败,未捕获到数据",
  1004. icon: "none"
  1005. });
  1006. this.proceedToNextQuestion();
  1007. return;
  1008. }
  1009. const mimeType2 = this.mediaRecorder.mimeType || "video/webm";
  1010. const blob = new Blob(this.recordedChunks, { type: mimeType2 });
  1011. console.log("创建Blob,原始大小:", blob.size, "类型:", mimeType2);
  1012. common_vendor.index.showLoading({
  1013. title: "正在处理视频...",
  1014. mask: true
  1015. });
  1016. try {
  1017. const compressedBlob = await this.compressVideo(blob);
  1018. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1019. const file = new File([compressedBlob], fileName, { type: mimeType2 });
  1020. common_vendor.index.hideLoading();
  1021. this.uploadRecordedVideo(file);
  1022. } catch (error) {
  1023. console.error("视频处理失败:", error);
  1024. common_vendor.index.hideLoading();
  1025. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1026. const file = new File([blob], fileName, { type: mimeType2 });
  1027. this.uploadRecordedVideo(file);
  1028. }
  1029. };
  1030. this.mediaRecorder.onerror = (event) => {
  1031. console.error("MediaRecorder错误:", event.error);
  1032. };
  1033. try {
  1034. this.mediaRecorder.start(1e3);
  1035. console.log("MediaRecorder开始录制");
  1036. this.mediaRecorderTimeout = setTimeout(() => {
  1037. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  1038. console.log("MediaRecorder备份超时机制触发,停止录制");
  1039. this.mediaRecorder.stop();
  1040. }
  1041. }, 3e5);
  1042. } catch (e) {
  1043. console.error("开始录制失败:", e);
  1044. }
  1045. },
  1046. // 添加新方法:停止录制用户回答
  1047. stopRecordingAnswer() {
  1048. console.log("停止录制用户回答");
  1049. if (this.countdownTimer) {
  1050. clearInterval(this.countdownTimer);
  1051. this.countdownTimer = null;
  1052. this.showCountdown = false;
  1053. }
  1054. const recordingDuration = this.getRecordingDuration();
  1055. const minimumDuration = 3;
  1056. const lowScoreDuration = 7;
  1057. if (recordingDuration < minimumDuration) {
  1058. common_vendor.index.showToast({
  1059. title: "录制时间过短,请至少录制3秒",
  1060. icon: "none",
  1061. duration: 2e3
  1062. });
  1063. return;
  1064. }
  1065. this.completeRecordingStop();
  1066. if (recordingDuration < lowScoreDuration) {
  1067. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  1068. this.needPlayLowScoreVideo = true;
  1069. } else {
  1070. this.needPlayLowScoreVideo = false;
  1071. }
  1072. },
  1073. // 添加新方法:获取录制时长
  1074. getRecordingDuration() {
  1075. if (this.recordingStartTime) {
  1076. return (Date.now() - this.recordingStartTime) / 1e3;
  1077. }
  1078. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1079. return this.$refs.userCameraVideo.currentTime || 0;
  1080. }
  1081. if (this.recordingTimerCount) {
  1082. return this.recordingTimerCount;
  1083. }
  1084. return 0;
  1085. },
  1086. // 添加新方法:重置录制
  1087. resetRecording() {
  1088. if (this.recordingTimer) {
  1089. clearTimeout(this.recordingTimer);
  1090. }
  1091. this.recordingStartTime = Date.now();
  1092. this.recordingTimerCount = 0;
  1093. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1094. this.mediaRecorder.stop();
  1095. this.recordedChunks = [];
  1096. setTimeout(() => {
  1097. this.startBrowserRecording();
  1098. }, 500);
  1099. } else if (this.cameraContext) {
  1100. this.cameraContext.stopRecord({
  1101. success: () => {
  1102. console.log("相机录制已停止");
  1103. },
  1104. fail: (err) => {
  1105. console.error("停止相机录制失败:", err);
  1106. }
  1107. });
  1108. }
  1109. common_vendor.index.showToast({
  1110. title: "请重新开始回答",
  1111. icon: "none",
  1112. duration: 2e3
  1113. });
  1114. },
  1115. // 修改 completeRecordingStop 方法,确保正确处理追问问题ID
  1116. completeRecordingStop() {
  1117. console.log("完成录制停止");
  1118. if (this.recordingTimer) {
  1119. clearInterval(this.recordingTimer);
  1120. this.recordingTimer = null;
  1121. }
  1122. this.finalRecordingDuration = this.recordingTimerCount;
  1123. this.showStopRecordingButton = false;
  1124. this.isRecording = false;
  1125. const systemInfo = common_vendor.index.getSystemInfoSync();
  1126. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1127. if (isMiniProgram) {
  1128. this.stopMiniProgramRecording();
  1129. } else {
  1130. this.stopBrowserRecording();
  1131. }
  1132. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1133. console.log("当前是追问问题,记录追问问题ID:", this.currentFollowUpQuestion.id);
  1134. this.lastQuestionWasFollowUp = true;
  1135. this.lastFollowUpQuestionId = this.currentFollowUpQuestion.id;
  1136. } else {
  1137. this.lastQuestionWasFollowUp = false;
  1138. this.lastFollowUpQuestionId = null;
  1139. }
  1140. },
  1141. // 修改 stopMiniProgramRecording 方法,确保正确传递追问问题ID
  1142. stopMiniProgramRecording() {
  1143. console.log("停止小程序录制");
  1144. if (!this.cameraContext) {
  1145. console.error("相机上下文不存在");
  1146. this.proceedToNextQuestion();
  1147. return;
  1148. }
  1149. this.cameraContext.stopRecord({
  1150. success: (res) => {
  1151. console.log("小程序录制停止成功:", res);
  1152. const videoPath = res.tempVideoPath;
  1153. if (videoPath) {
  1154. this.uploadRecordedVideo(videoPath);
  1155. } else {
  1156. console.error("未获取到录制视频路径");
  1157. this.proceedToNextQuestion();
  1158. }
  1159. },
  1160. fail: (err) => {
  1161. console.error("小程序录制停止失败:", err);
  1162. this.proceedToNextQuestion();
  1163. }
  1164. });
  1165. },
  1166. // 添加新方法:停止浏览器录制
  1167. stopBrowserRecording() {
  1168. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1169. this.mediaRecorder.stop();
  1170. console.log("浏览器录制停止成功");
  1171. } else {
  1172. console.error("MediaRecorder不存在或已经停止");
  1173. this.proceedToNextQuestion();
  1174. }
  1175. },
  1176. // 修改 uploadRecordedVideo 方法,添加记录父问题ID的逻辑
  1177. uploadRecordedVideo(fileOrPath) {
  1178. console.log("准备上传视频:", typeof fileOrPath === "string" ? fileOrPath : fileOrPath.name);
  1179. console.log("当前问题ID:", this.currentParentQuestionId);
  1180. console.log("当前问题类型:", this.isFollowUpQuestion);
  1181. console.log("当前追问问题:", this.currentFollowUpQuestion);
  1182. let questionId;
  1183. let isFollowUpQuestionUpload = false;
  1184. let questionText = "";
  1185. let questionForm = 0;
  1186. if (this.currentFollowUpQuestion && (this.currentFollowUpQuestion.question_form === 5 || this.isFollowUpQuestion)) {
  1187. questionId = this.currentFollowUpQuestion.id;
  1188. isFollowUpQuestionUpload = true;
  1189. questionText = this.currentFollowUpQuestion.question || "";
  1190. questionForm = 5;
  1191. console.log("正在上传追问问题的回答:", {
  1192. questionId,
  1193. questionText,
  1194. questionForm,
  1195. isFollowUp: true
  1196. });
  1197. } else {
  1198. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  1199. if (currentQuestion && currentQuestion.id) {
  1200. questionId = currentQuestion.id;
  1201. questionText = currentQuestion.question || "";
  1202. console.log("正在上传常规问题的回答:", {
  1203. questionId,
  1204. questionText,
  1205. questionForm: 0
  1206. });
  1207. } else {
  1208. questionId = this.getDefaultQuestionId(this.currentVideoIndex);
  1209. console.log("使用默认问题ID:", questionId);
  1210. }
  1211. }
  1212. const uploadTask = {
  1213. id: Date.now().toString(),
  1214. file: fileOrPath,
  1215. questionId,
  1216. isFollowUp: isFollowUpQuestionUpload,
  1217. questionText,
  1218. questionForm,
  1219. attempts: 0,
  1220. maxAttempts: 3,
  1221. parentQuestionId: this.currentParentQuestionId
  1222. // 添加父问题ID
  1223. };
  1224. if (!isFollowUpQuestionUpload) {
  1225. this.currentParentQuestionId = questionId;
  1226. }
  1227. this.uploadQueue.push(uploadTask);
  1228. this.uploadProgress[uploadTask.id] = 0;
  1229. this.uploadStatus[uploadTask.id] = "pending";
  1230. common_vendor.index.showToast({
  1231. title: "已完成回答",
  1232. icon: "none",
  1233. duration: 1500
  1234. });
  1235. this.updateUploadStatusText();
  1236. if (!this.isUploading) {
  1237. this.processUploadQueue();
  1238. }
  1239. this.handlePostUploadActions(uploadTask);
  1240. },
  1241. // 添加新方法:处理上传后的逻辑
  1242. handlePostUploadActions(task) {
  1243. if (this.needPlayLowScoreVideo && this.retryCount < 1) {
  1244. this.playLowScoreVideo();
  1245. this.needPlayLowScoreVideo = false;
  1246. } else {
  1247. if (task.isFollowUp) {
  1248. console.log("追问问题回答完成,进入下一个问题");
  1249. this.retryCount = 0;
  1250. this.isFollowUpQuestion = false;
  1251. this.currentFollowUpQuestion = null;
  1252. this.proceedToNextQuestion();
  1253. } else {
  1254. this.currentParentQuestionId = task.questionId;
  1255. console.log("检查常规问题的追问,父问题ID:", this.currentParentQuestionId);
  1256. this.checkAndPlayFollowUpQuestion(this.currentParentQuestionId);
  1257. }
  1258. }
  1259. },
  1260. // 修改 processUploadQueue 方法
  1261. processUploadQueue() {
  1262. if (this.uploadQueue.length === 0) {
  1263. this.isUploading = false;
  1264. this.showUploadStatus = false;
  1265. return;
  1266. }
  1267. this.isUploading = true;
  1268. this.showUploadStatus = true;
  1269. const task = this.uploadQueue[0];
  1270. this.uploadStatus[task.id] = "uploading";
  1271. this.updateUploadStatusText();
  1272. task.attempts++;
  1273. if (typeof task.file === "string") {
  1274. this.uploadFileWithUni(task);
  1275. } else {
  1276. this.uploadFileWithXHR(task);
  1277. }
  1278. },
  1279. // 添加新方法:使用XMLHttpRequest上传文件
  1280. uploadFileWithXHR(task) {
  1281. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1282. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1283. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1284. const formData = new FormData();
  1285. formData.append("file", task.file);
  1286. formData.append("openid", openid);
  1287. formData.append("tenant_id", tenant_id);
  1288. formData.append("application_id", common_vendor.index.getStorageSync("appId"));
  1289. formData.append("question_id", task.questionId);
  1290. formData.append("video_duration", 0);
  1291. formData.append("has_audio", "true");
  1292. const xhr = new XMLHttpRequest();
  1293. xhr.open("POST", `${common_config.apiBaseUrl}/api/upload/`, true);
  1294. xhr.timeout = 12e4;
  1295. xhr.upload.onprogress = (event) => {
  1296. if (event.lengthComputable) {
  1297. const percentComplete = Math.round(event.loaded / event.total * 100);
  1298. this.uploadProgress[task.id] = percentComplete;
  1299. this.updateUploadStatusText();
  1300. }
  1301. };
  1302. xhr.onload = () => {
  1303. if (xhr.status === 200) {
  1304. try {
  1305. const res = JSON.parse(xhr.responseText);
  1306. console.log("上传响应:", res);
  1307. if (res.code === 2e3) {
  1308. const videoUrl = res.data.url || res.data.photoUrl || "";
  1309. if (videoUrl) {
  1310. this.uploadStatus[task.id] = "success";
  1311. this.updateUploadStatusText();
  1312. this.submitVideoToInterview(videoUrl, task);
  1313. } else {
  1314. this.handleUploadFailure(task, "视频URL获取失败");
  1315. }
  1316. } else {
  1317. this.handleUploadFailure(task, res.msg || "上传失败");
  1318. }
  1319. } catch (e) {
  1320. this.handleUploadFailure(task, "解析响应失败");
  1321. }
  1322. } else {
  1323. this.handleUploadFailure(task, "HTTP状态: " + xhr.status);
  1324. }
  1325. };
  1326. xhr.onerror = () => {
  1327. this.handleUploadFailure(task, "网络错误");
  1328. };
  1329. xhr.ontimeout = () => {
  1330. this.handleUploadFailure(task, "上传超时");
  1331. };
  1332. xhr.send(formData);
  1333. },
  1334. // 添加新方法:使用uni.uploadFile上传文件
  1335. uploadFileWithUni(task) {
  1336. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1337. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1338. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1339. const uploadTask = common_vendor.index.uploadFile({
  1340. url: `${common_config.apiBaseUrl}/api/upload/`,
  1341. filePath: task.file,
  1342. name: "file",
  1343. formData: {
  1344. openid,
  1345. tenant_id,
  1346. application_id: common_vendor.index.getStorageSync("appId"),
  1347. question_id: task.questionId,
  1348. video_duration: 0,
  1349. has_audio: "true"
  1350. },
  1351. success: (uploadRes) => {
  1352. try {
  1353. const res = JSON.parse(uploadRes.data);
  1354. console.log("上传响应:", res);
  1355. if (res.code === 2e3) {
  1356. const videoUrl = res.data.permanent_link || res.data.url || "";
  1357. if (videoUrl) {
  1358. this.uploadStatus[task.id] = "success";
  1359. this.updateUploadStatusText();
  1360. this.submitVideoToInterview(videoUrl, task);
  1361. } else {
  1362. this.handleUploadFailure(task, "视频URL获取失败");
  1363. }
  1364. } else {
  1365. this.handleUploadFailure(task, res.msg || "上传失败");
  1366. }
  1367. } catch (e) {
  1368. this.handleUploadFailure(task, "解析响应失败");
  1369. }
  1370. },
  1371. fail: (err) => {
  1372. this.handleUploadFailure(task, err.errMsg || "上传失败");
  1373. }
  1374. });
  1375. uploadTask.onProgressUpdate((res) => {
  1376. this.uploadProgress[task.id] = res.progress;
  1377. this.updateUploadStatusText();
  1378. });
  1379. },
  1380. // 添加新方法:处理上传失败
  1381. handleUploadFailure(task, errorMsg) {
  1382. console.error("上传失败:", errorMsg);
  1383. this.uploadStatus[task.id] = "failed";
  1384. this.updateUploadStatusText();
  1385. if (task.attempts < task.maxAttempts) {
  1386. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1387. setTimeout(() => {
  1388. this.uploadProgress[task.id] = 0;
  1389. if (typeof task.file !== "string") {
  1390. this.uploadFileWithXHR(task);
  1391. } else {
  1392. this.uploadFileWithUni(task);
  1393. }
  1394. }, 5e3);
  1395. } else {
  1396. console.log("超过最大重试次数,放弃上传");
  1397. common_vendor.index.showToast({
  1398. title: "视频上传失败,请稍后重试",
  1399. icon: "none",
  1400. duration: 2e3
  1401. });
  1402. this.uploadQueue.shift();
  1403. this.processUploadQueue();
  1404. }
  1405. },
  1406. // 修改 submitVideoToInterview 方法
  1407. submitVideoToInterview(videoUrl, task) {
  1408. const requestData = {
  1409. application_id: common_vendor.index.getStorageSync("appId"),
  1410. question_id: task.questionId,
  1411. video_url: videoUrl,
  1412. tenant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1",
  1413. is_follow_up: task.isFollowUp ? 1 : 0,
  1414. question_form: task.questionForm,
  1415. question_text: task.questionText
  1416. };
  1417. common_vendor.index.request({
  1418. url: `${common_config.apiBaseUrl}/api/job/upload_video`,
  1419. method: "POST",
  1420. data: requestData,
  1421. header: {
  1422. "content-type": "application/x-www-form-urlencoded"
  1423. },
  1424. success: (res) => {
  1425. if (res.data.code === 200 || res.data.code === 2e3) {
  1426. this.uploadQueue.shift();
  1427. this.processUploadQueue();
  1428. } else {
  1429. this.handleUploadError(task, "提交失败: " + (res.data.msg || "未知错误"));
  1430. }
  1431. },
  1432. fail: (err) => {
  1433. this.handleUploadError(task, "网络错误: " + err.errMsg);
  1434. }
  1435. });
  1436. },
  1437. // 添加新方法:处理提交失败
  1438. handleSubmitFailure(task, errorMsg) {
  1439. console.error("提交失败:", errorMsg);
  1440. this.uploadStatus[task.id] = "failed";
  1441. this.updateUploadStatusText();
  1442. if (task.attempts < task.maxAttempts) {
  1443. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1444. setTimeout(() => {
  1445. this.submitVideoToInterview(task.videoUrl, task);
  1446. }, 5e3);
  1447. } else {
  1448. console.log("超过最大重试次数,放弃提交");
  1449. common_vendor.index.showToast({
  1450. title: "视频提交失败,请稍后重试",
  1451. icon: "none",
  1452. duration: 2e3
  1453. });
  1454. this.uploadQueue.shift();
  1455. this.processUploadQueue();
  1456. }
  1457. },
  1458. // 添加新方法:更新上传状态文本
  1459. updateUploadStatusText() {
  1460. if (this.uploadQueue.length === 0) {
  1461. this.uploadStatusText = "";
  1462. return;
  1463. }
  1464. const currentTask = this.uploadQueue[0];
  1465. const progress = this.uploadProgress[currentTask.id] || 0;
  1466. const status = this.uploadStatus[currentTask.id] || "pending";
  1467. let statusText = "";
  1468. switch (status) {
  1469. case "pending":
  1470. statusText = "等待上传";
  1471. break;
  1472. case "uploading":
  1473. statusText = `上传中 ${progress}%`;
  1474. break;
  1475. case "success":
  1476. statusText = "上传成功,提交中...";
  1477. break;
  1478. case "failed":
  1479. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? "即将重试" : "已放弃"}`;
  1480. break;
  1481. }
  1482. const questionTypeText = currentTask.isFollowUp ? "追问" : "问题";
  1483. const questionShortText = currentTask.questionText ? currentTask.questionText.length > 10 ? currentTask.questionText.substring(0, 10) + "..." : currentTask.questionText : `${questionTypeText}${currentTask.questionId}`;
  1484. this.uploadStatusText = `${questionTypeText}「${questionShortText}」:${statusText}`;
  1485. if (this.uploadQueue.length > 1) {
  1486. this.uploadStatusText += ` (${this.uploadQueue.length}个视频待处理)`;
  1487. }
  1488. },
  1489. // 修改 proceedToNextQuestion 方法,确保在切换视频时重置历史时间
  1490. proceedToNextQuestion() {
  1491. console.log("继续下一个问题");
  1492. this.currentVideoIndex++;
  1493. this.historyTime = 0;
  1494. if (this.currentVideoIndex < this.videoList.length) {
  1495. this.videoUrl = this.videoList[this.currentVideoIndex];
  1496. this.videoPlaying = true;
  1497. this.$nextTick(() => {
  1498. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1499. if (videoContext) {
  1500. videoContext.play();
  1501. }
  1502. });
  1503. } else {
  1504. console.log("所有视频已播放完毕");
  1505. this.stopUserCamera();
  1506. this.clearCountdown();
  1507. const currentJobDetail = JSON.parse(common_vendor.index.getStorageSync("selectedJob"));
  1508. console.log("当前职位ID:", currentJobDetail);
  1509. const jobId = currentJobDetail ? currentJobDetail.id : null;
  1510. console.log("当前职位ID:", jobId);
  1511. setTimeout(() => {
  1512. if (jobId === 9) {
  1513. common_vendor.index.navigateTo({
  1514. url: "/pages/interview-question/interview-question",
  1515. success: () => {
  1516. console.log("成功跳转到interview-question页面");
  1517. },
  1518. fail: (err) => {
  1519. console.error("跳转到interview-question页面失败:", err);
  1520. common_vendor.index.redirectTo({
  1521. url: "/pages/interview-question/interview-question",
  1522. fail: (redirectErr) => {
  1523. console.error("重定向到interview-question页面也失败:", redirectErr);
  1524. common_vendor.index.navigateBack({
  1525. delta: 1
  1526. });
  1527. }
  1528. });
  1529. }
  1530. });
  1531. } else {
  1532. common_vendor.index.navigateTo({
  1533. url: "/pages/camera/camera",
  1534. success: () => {
  1535. console.log("成功跳转到camera页面");
  1536. },
  1537. fail: (err) => {
  1538. console.error("跳转到camera页面失败:", err);
  1539. common_vendor.index.redirectTo({
  1540. url: "/pages/camera/camera",
  1541. fail: (redirectErr) => {
  1542. console.error("重定向到camera页面也失败:", redirectErr);
  1543. common_vendor.index.switchTab({
  1544. url: "/pages/camera/camera",
  1545. fail: (switchErr) => {
  1546. console.error("所有跳转方式都失败:", switchErr);
  1547. common_vendor.index.navigateBack({
  1548. delta: 1
  1549. });
  1550. }
  1551. });
  1552. }
  1553. });
  1554. }
  1555. });
  1556. }
  1557. }, 2e3);
  1558. }
  1559. },
  1560. // 修改 handleAnswerButtonClick 方法,确保在切换视频时重置历史时间
  1561. handleAnswerButtonClick() {
  1562. console.log("点击开始回答按钮");
  1563. this.showAnswerButton = false;
  1564. this.currentVideoIndex++;
  1565. this.historyTime = 0;
  1566. if (this.currentVideoIndex < this.videoList.length) {
  1567. this.videoUrl = this.videoList[this.currentVideoIndex];
  1568. this.videoPlaying = true;
  1569. this.$nextTick(() => {
  1570. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1571. if (videoContext) {
  1572. videoContext.play();
  1573. }
  1574. });
  1575. }
  1576. },
  1577. // 处理相机错误
  1578. handleCameraError(e) {
  1579. console.error("相机错误:", e);
  1580. const systemInfo = common_vendor.index.getSystemInfoSync();
  1581. const isIOS = systemInfo.platform === "ios";
  1582. if (isIOS) {
  1583. console.log("iOS相机错误,尝试重新初始化");
  1584. common_vendor.index.showToast({
  1585. title: "相机初始化中...",
  1586. icon: "loading",
  1587. duration: 2e3
  1588. });
  1589. this.resetCamera();
  1590. if (this.isRecording) {
  1591. this.isRecording = false;
  1592. this.showStopRecordingButton = false;
  1593. setTimeout(() => {
  1594. this.useAlternativeRecordingMethod();
  1595. }, 1e3);
  1596. }
  1597. } else {
  1598. common_vendor.index.showToast({
  1599. title: "相机初始化失败,请检查权限设置",
  1600. icon: "none"
  1601. });
  1602. this.tryFallbackOptions();
  1603. }
  1604. },
  1605. // 添加新方法:尝试备用选项
  1606. tryFallbackOptions() {
  1607. const systemInfo = common_vendor.index.getSystemInfoSync();
  1608. if (systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay") {
  1609. this.useMiniProgramCamera();
  1610. } else {
  1611. this.showStaticCameraPlaceholder();
  1612. }
  1613. },
  1614. // 添加新方法:使用小程序相机API
  1615. useMiniProgramCamera() {
  1616. console.log("尝试使用小程序相机组件");
  1617. this.useMiniProgramCameraComponent = true;
  1618. },
  1619. // 添加新方法:显示静态图像
  1620. showStaticCameraPlaceholder() {
  1621. console.log("显示静态摄像头占位图");
  1622. const img = document.createElement("img");
  1623. img.src = "/static/images/camera-placeholder.png";
  1624. img.className = "static-camera-image";
  1625. img.style.width = "100%";
  1626. img.style.height = "100%";
  1627. img.style.objectFit = "cover";
  1628. const container = this.$refs.userCameraVideo.parentNode;
  1629. container.appendChild(img);
  1630. },
  1631. // 修改 handleTimeUpdate 方法
  1632. handleTimeUpdate(e) {
  1633. const currentTime = e.detail.currentTime || e.target.currentTime;
  1634. if (this.isRecording && this.recordingTimerCount) {
  1635. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1636. }
  1637. if (this.isPlayingLowScoreVideo) {
  1638. if (this.lowScoreVideoSubtitles && this.lowScoreVideoSubtitles.length > 0) {
  1639. const subtitle = this.lowScoreVideoSubtitles[0];
  1640. this.currentSubtitle = subtitle.text;
  1641. }
  1642. return;
  1643. }
  1644. let currentSubtitles = null;
  1645. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1646. const subtitleKey = `followUpSubtitles_${this.currentFollowUpQuestion.id}`;
  1647. currentSubtitles = this[subtitleKey] || [{
  1648. startTime: 0,
  1649. endTime: 30,
  1650. text: this.currentFollowUpQuestion.digital_human_video_subtitle || this.currentFollowUpQuestion.question
  1651. }];
  1652. } else {
  1653. if (this.currentVideoIndex === 0) {
  1654. currentSubtitles = this.subtitles;
  1655. } else {
  1656. const subtitleArrayName = `question${this.currentVideoIndex}Subtitles`;
  1657. currentSubtitles = this[subtitleArrayName];
  1658. }
  1659. }
  1660. if (currentSubtitles && currentSubtitles.length > 0) {
  1661. const subtitle = currentSubtitles.find(
  1662. (sub) => currentTime >= sub.startTime && currentTime <= sub.endTime
  1663. ) || currentSubtitles[0];
  1664. if (subtitle) {
  1665. this.currentSubtitle = subtitle.text;
  1666. }
  1667. }
  1668. this.historyTime = currentTime;
  1669. this.lastUpdateTime = Date.now();
  1670. },
  1671. // Add a new method to handle the "Start Recording" button click
  1672. handleStartRecordingClick() {
  1673. this.showStartRecordingButton = false;
  1674. this.clearCountdown();
  1675. this.startRecordingAnswer();
  1676. },
  1677. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1678. checkAudioPermission() {
  1679. const systemInfo = common_vendor.index.getSystemInfoSync();
  1680. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1681. if (isMiniProgram) {
  1682. common_vendor.index.getSetting({
  1683. success: (res) => {
  1684. if (!res.authSetting["scope.record"]) {
  1685. common_vendor.index.authorize({
  1686. scope: "scope.record",
  1687. success: () => {
  1688. console.log("录音权限已获取");
  1689. },
  1690. fail: (err) => {
  1691. console.error("录音权限获取失败:", err);
  1692. this.showPermissionDialog("录音");
  1693. }
  1694. });
  1695. }
  1696. if (!res.authSetting["scope.camera"]) {
  1697. common_vendor.index.authorize({
  1698. scope: "scope.camera",
  1699. success: () => {
  1700. console.log("相机权限已获取");
  1701. },
  1702. fail: (err) => {
  1703. console.error("相机权限获取失败:", err);
  1704. this.showPermissionDialog("相机");
  1705. }
  1706. });
  1707. }
  1708. }
  1709. });
  1710. }
  1711. },
  1712. // 添加新方法:测试音频输入
  1713. testAudioInput() {
  1714. if (!this.cameraStream) {
  1715. console.warn("没有可用的媒体流,无法测试音频");
  1716. return;
  1717. }
  1718. const audioTracks = this.cameraStream.getAudioTracks();
  1719. if (audioTracks.length === 0) {
  1720. console.warn("没有检测到音频轨道,尝试重新获取");
  1721. this.tryGetAudioOnly();
  1722. return;
  1723. }
  1724. console.log("音频轨道信息:", audioTracks[0].getSettings());
  1725. try {
  1726. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1727. if (!AudioContext) {
  1728. console.warn("浏览器不支持AudioContext");
  1729. return;
  1730. }
  1731. const audioContext = new AudioContext();
  1732. const analyser = audioContext.createAnalyser();
  1733. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1734. microphone.connect(analyser);
  1735. analyser.fftSize = 256;
  1736. const bufferLength = analyser.frequencyBinCount;
  1737. const dataArray = new Uint8Array(bufferLength);
  1738. let silenceCounter = 0;
  1739. const checkAudio = () => {
  1740. if (this.isRecording)
  1741. return;
  1742. analyser.getByteFrequencyData(dataArray);
  1743. let sum = 0;
  1744. for (let i = 0; i < bufferLength; i++) {
  1745. sum += dataArray[i];
  1746. }
  1747. const average = sum / bufferLength;
  1748. if (average > 10) {
  1749. console.log("检测到音频输入,音量:", average);
  1750. silenceCounter = 0;
  1751. } else {
  1752. silenceCounter++;
  1753. if (silenceCounter > 10) {
  1754. console.warn("持续检测不到音频输入,可能麦克风未正常工作");
  1755. silenceCounter = 0;
  1756. }
  1757. }
  1758. requestAnimationFrame(checkAudio);
  1759. };
  1760. checkAudio();
  1761. } catch (e) {
  1762. console.error("音频测试失败:", e);
  1763. }
  1764. },
  1765. // 添加新方法:尝试单独获取音频
  1766. tryGetAudioOnly() {
  1767. navigator.mediaDevices.getUserMedia({ audio: true }).then((audioStream) => {
  1768. if (this.cameraStream) {
  1769. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1770. const audioTrack = audioStream.getAudioTracks()[0];
  1771. const combinedStream = new MediaStream();
  1772. if (videoTrack)
  1773. combinedStream.addTrack(videoTrack);
  1774. if (audioTrack)
  1775. combinedStream.addTrack(audioTrack);
  1776. this.cameraStream = combinedStream;
  1777. const videoElement = this.$refs.userCameraVideo;
  1778. if (videoElement) {
  1779. videoElement.srcObject = combinedStream;
  1780. videoElement.muted = true;
  1781. }
  1782. console.log("成功合并音频和视频轨道");
  1783. } else {
  1784. console.warn("没有视频流可合并");
  1785. }
  1786. }).catch((err) => {
  1787. console.error("单独获取音频失败:", err);
  1788. });
  1789. },
  1790. // 添加新方法:显示权限对话框
  1791. showPermissionDialog(permissionType) {
  1792. common_vendor.index.showModal({
  1793. title: "需要权限",
  1794. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1795. confirmText: "去设置",
  1796. success: (res) => {
  1797. if (res.confirm) {
  1798. common_vendor.index.openSetting({
  1799. success: (settingRes) => {
  1800. console.log("设置页面打开成功", settingRes);
  1801. }
  1802. });
  1803. }
  1804. }
  1805. });
  1806. },
  1807. // 添加重试上传方法
  1808. retryVideoUpload() {
  1809. if (this.lastVideoToRetry) {
  1810. this.showRetryButton = false;
  1811. common_vendor.index.showLoading({
  1812. title: "正在重新提交...",
  1813. mask: true
  1814. });
  1815. this.submitVideoToInterview(this.lastVideoToRetry);
  1816. } else {
  1817. common_vendor.index.showToast({
  1818. title: "没有可重试的视频",
  1819. icon: "none"
  1820. });
  1821. }
  1822. },
  1823. // 添加一个新方法用于压缩视频
  1824. async compressVideo(videoBlob) {
  1825. if (videoBlob.size < 5 * 1024 * 1024) {
  1826. return videoBlob;
  1827. }
  1828. console.log("开始压缩视频,原始大小:", videoBlob.size);
  1829. const videoElement = document.createElement("video");
  1830. videoElement.muted = true;
  1831. videoElement.autoplay = false;
  1832. const canvas = document.createElement("canvas");
  1833. const ctx = canvas.getContext("2d");
  1834. videoElement.src = URL.createObjectURL(videoBlob);
  1835. return new Promise((resolve) => {
  1836. videoElement.onloadedmetadata = () => {
  1837. const width = Math.floor(videoElement.videoWidth / 2);
  1838. const height = Math.floor(videoElement.videoHeight / 2);
  1839. canvas.width = width;
  1840. canvas.height = height;
  1841. const stream = canvas.captureStream(15);
  1842. if (videoElement.captureStream) {
  1843. const originalStream = videoElement.captureStream();
  1844. const audioTracks = originalStream.getAudioTracks();
  1845. if (audioTracks.length > 0) {
  1846. stream.addTrack(audioTracks[0]);
  1847. }
  1848. }
  1849. const options = {
  1850. mimeType: "video/webm;codecs=vp8,opus",
  1851. audioBitsPerSecond: 64e3,
  1852. videoBitsPerSecond: 8e5
  1853. // 800kbps
  1854. };
  1855. const mediaRecorder = new MediaRecorder(stream, options);
  1856. const chunks = [];
  1857. mediaRecorder.ondataavailable = (e) => {
  1858. if (e.data.size > 0) {
  1859. chunks.push(e.data);
  1860. }
  1861. };
  1862. mediaRecorder.onstop = () => {
  1863. const compressedBlob = new Blob(chunks, { type: "video/webm" });
  1864. console.log("视频压缩完成,压缩后大小:", compressedBlob.size);
  1865. resolve(compressedBlob);
  1866. };
  1867. videoElement.onplay = () => {
  1868. mediaRecorder.start(10);
  1869. const drawFrame = () => {
  1870. if (videoElement.paused || videoElement.ended) {
  1871. mediaRecorder.stop();
  1872. return;
  1873. }
  1874. ctx.drawImage(videoElement, 0, 0, width, height);
  1875. requestAnimationFrame(drawFrame);
  1876. };
  1877. drawFrame();
  1878. };
  1879. videoElement.play();
  1880. };
  1881. });
  1882. },
  1883. // 修改 checkIOSCameraRecordPermission 方法
  1884. checkIOSCameraRecordPermission() {
  1885. const systemInfo = common_vendor.index.getSystemInfoSync();
  1886. if (systemInfo.platform !== "ios")
  1887. return;
  1888. common_vendor.index.getSetting({
  1889. success: (res) => {
  1890. if (!res.authSetting["scope.camera"]) {
  1891. common_vendor.index.authorize({
  1892. scope: "scope.camera",
  1893. success: () => {
  1894. console.log("iOS相机权限已获取");
  1895. },
  1896. fail: (err) => {
  1897. console.error("iOS相机权限获取失败:", err);
  1898. this.showPermissionDialog("相机");
  1899. }
  1900. });
  1901. }
  1902. if (!res.authSetting["scope.record"]) {
  1903. common_vendor.index.authorize({
  1904. scope: "scope.record",
  1905. success: () => {
  1906. console.log("iOS录音权限已获取");
  1907. },
  1908. fail: (err) => {
  1909. console.error("iOS录音权限获取失败:", err);
  1910. this.showPermissionDialog("录音");
  1911. }
  1912. });
  1913. }
  1914. }
  1915. });
  1916. },
  1917. // 添加新方法:检查并修复渲染问题
  1918. checkAndFixRenderingIssues() {
  1919. try {
  1920. if (typeof u !== "undefined" && u) {
  1921. if (!u.currentQuestion) {
  1922. console.log("修复: 创建缺失的currentQuestion对象");
  1923. u.currentQuestion = {};
  1924. }
  1925. if (u.currentQuestion && typeof u.currentQuestion.isImportant === "undefined") {
  1926. console.log("修复: 设置缺失的isImportant属性");
  1927. u.currentQuestion.isImportant = false;
  1928. }
  1929. }
  1930. } catch (e) {
  1931. console.log("防御性检查异常:", e);
  1932. }
  1933. },
  1934. // 添加格式化时间的辅助方法
  1935. formatTime(seconds) {
  1936. if (!seconds && seconds !== 0)
  1937. return "03:30";
  1938. const minutes = Math.floor(seconds / 60);
  1939. const remainingSeconds = seconds % 60;
  1940. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  1941. },
  1942. // 添加获取问题数据的方法
  1943. fetchQuestions() {
  1944. this.loading = true;
  1945. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1946. let position_id = 1;
  1947. try {
  1948. const selectedJob = JSON.parse(common_vendor.index.getStorageSync("selectedJob") || "{}");
  1949. if (selectedJob && selectedJob.id) {
  1950. position_id = selectedJob.id;
  1951. console.log("使用选择的职位ID获取问题:", position_id);
  1952. } else {
  1953. console.warn("未找到选择的职位信息,使用默认职位ID:", position_id);
  1954. }
  1955. } catch (e) {
  1956. console.error("解析职位信息失败:", e);
  1957. }
  1958. const timestamp = Date.now();
  1959. const requestUrl = `${common_config.apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=0&_t=${timestamp}`;
  1960. console.log("完整的请求URL:", requestUrl);
  1961. common_vendor.index.request({
  1962. url: requestUrl,
  1963. method: "GET",
  1964. success: (res) => {
  1965. if (res.data && res.data.code === 2e3 && res.data.data && res.data.data.items) {
  1966. console.log("获取问题数据成功:", res.data.data.items);
  1967. this.questions = res.data.data.items;
  1968. this.processQuestionData();
  1969. this.playIntroVideo();
  1970. } else {
  1971. console.error("获取问题数据失败:", res.data);
  1972. common_vendor.index.showToast({
  1973. title: "获取问题数据失败",
  1974. icon: "none"
  1975. });
  1976. }
  1977. },
  1978. fail: (err) => {
  1979. console.error("请求问题数据失败:", err);
  1980. common_vendor.index.showToast({
  1981. title: "网络错误,请检查网络连接",
  1982. icon: "none"
  1983. });
  1984. },
  1985. complete: () => {
  1986. this.loading = false;
  1987. }
  1988. });
  1989. },
  1990. // 处理问题数据,提取视频URL和字幕
  1991. processQuestionData() {
  1992. this.videoList = [];
  1993. this.videoList.push(this.introVideoUrl);
  1994. this.subtitles = [
  1995. {
  1996. startTime: 0,
  1997. endTime: 5,
  1998. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  1999. },
  2000. {
  2001. startTime: 5,
  2002. endTime: 13,
  2003. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  2004. },
  2005. {
  2006. startTime: 13,
  2007. endTime: 20,
  2008. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  2009. }
  2010. ];
  2011. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  2012. const questionsToUse = sortedQuestions.slice(0, 5);
  2013. questionsToUse.forEach((question, index) => {
  2014. if (question.digital_human_video_url) {
  2015. this.videoList.push(question.digital_human_video_url);
  2016. const subtitleArray = [{
  2017. startTime: 0,
  2018. endTime: 30,
  2019. // 延长字幕显示时间到30秒
  2020. text: question.digital_human_video_subtitle || question.question
  2021. }];
  2022. const videoIndex = this.videoList.length - 1;
  2023. this[`question${videoIndex}Subtitles`] = subtitleArray;
  2024. this.subtitleMap[question.digital_human_video_url] = subtitleArray;
  2025. }
  2026. });
  2027. if (this.videoList.length <= 1)
  2028. ;
  2029. else {
  2030. console.log("处理后的视频列表:", this.videoList);
  2031. console.log("处理后的字幕:", this.subtitles);
  2032. }
  2033. },
  2034. // 使用默认视频和字幕(作为备用)
  2035. useDefaultVideosAndSubtitles() {
  2036. console.log("使用默认视频和字幕");
  2037. this.subtitles = [
  2038. {
  2039. startTime: 0,
  2040. endTime: 5,
  2041. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  2042. },
  2043. {
  2044. startTime: 5,
  2045. endTime: 13,
  2046. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  2047. },
  2048. {
  2049. startTime: 13,
  2050. endTime: 20,
  2051. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  2052. }
  2053. ];
  2054. this.question1Subtitles = [{
  2055. startTime: 0,
  2056. endTime: 10,
  2057. text: "请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:"
  2058. }];
  2059. this.question2Subtitles = [{
  2060. startTime: 0,
  2061. endTime: 4,
  2062. text: "在工作中,你如何确保个人防护装备的正确使用?"
  2063. }];
  2064. this.question3Subtitles = [{
  2065. startTime: 0,
  2066. endTime: 4,
  2067. text: "描述一次你与团队合作改善生产流程的经历。"
  2068. }];
  2069. this.question4Subtitles = [{
  2070. startTime: 0,
  2071. endTime: 6,
  2072. text: "你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?"
  2073. }];
  2074. this.question5Subtitles = [{
  2075. startTime: 0,
  2076. endTime: 5,
  2077. text: "您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?"
  2078. }];
  2079. },
  2080. // 播放介绍视频
  2081. playIntroVideo() {
  2082. this.videoUrl = this.videoList[0];
  2083. this.videoPlaying = true;
  2084. this.$nextTick(() => {
  2085. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2086. if (videoContext) {
  2087. videoContext.play();
  2088. }
  2089. });
  2090. },
  2091. // 添加新方法:获取默认问题ID
  2092. getDefaultQuestionId(videoIndex) {
  2093. switch (videoIndex) {
  2094. case 1:
  2095. return 10;
  2096. case 2:
  2097. return 11;
  2098. case 3:
  2099. return 12;
  2100. case 4:
  2101. return 13;
  2102. default:
  2103. return 10;
  2104. }
  2105. },
  2106. // 添加新方法:根据索引获取当前问题
  2107. getCurrentQuestionByIndex(videoIndex) {
  2108. const questionIndex = videoIndex - 1;
  2109. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  2110. if (questionIndex >= 0 && questionIndex < sortedQuestions.length) {
  2111. return sortedQuestions[questionIndex];
  2112. }
  2113. return null;
  2114. },
  2115. // 添加新方法:重置录制状态,准备重新回答
  2116. resetForRerecording() {
  2117. console.log("重置录制状态,准备重新回答");
  2118. this.isRecording = false;
  2119. this.showStopRecordingButton = false;
  2120. this.showStartRecordingButton = true;
  2121. this.clearCountdown();
  2122. if (this.recordingTimer) {
  2123. clearInterval(this.recordingTimer);
  2124. this.recordingTimer = null;
  2125. }
  2126. this.recordingTimerCount = 0;
  2127. this.recordingTimeDisplay = "00:00 ";
  2128. this.remainingTime = this.maxRecordingTime;
  2129. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  2130. this.mediaRecorder.stop();
  2131. this.recordedChunks = [];
  2132. }
  2133. if (this.cameraContext) {
  2134. this.cameraContext.stopRecord({
  2135. success: () => {
  2136. console.log("相机录制已停止");
  2137. },
  2138. fail: (err) => {
  2139. console.error("停止相机录制失败:", err);
  2140. }
  2141. });
  2142. }
  2143. },
  2144. // 修改 playLowScoreVideo 方法,优化字幕显示
  2145. playLowScoreVideo() {
  2146. console.log("播放低分提示视频");
  2147. this.isPlayingLowScoreVideo = true;
  2148. this.showStartRecordingButton = false;
  2149. this.showStopRecordingButton = false;
  2150. this.showRerecordButton = false;
  2151. this.videoUrl = this.lowScoreVideoUrl;
  2152. this.videoPlaying = true;
  2153. const originalSubtitle = this.currentSubtitle;
  2154. this.currentSubtitle = "";
  2155. this.$nextTick(() => {
  2156. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2157. if (videoContext) {
  2158. videoContext.play();
  2159. setTimeout(() => {
  2160. this.currentSubtitle = "我未听清楚您在说什么,请您再说一遍!";
  2161. this.lowScoreVideoSubtitles = [
  2162. {
  2163. startTime: 0,
  2164. endTime: 30,
  2165. text: "我未听清楚您在说什么,请您再说一遍!",
  2166. translation: "I didn't quite catch what you said, could you please repeat it?"
  2167. }
  2168. ];
  2169. setTimeout(() => {
  2170. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  2171. this.currentSubtitle = this.currentFollowUpQuestion.digital_human_video_subtitle || this.currentFollowUpQuestion.question;
  2172. } else {
  2173. this.currentSubtitle = originalSubtitle;
  2174. }
  2175. }, 3e3);
  2176. }, 500);
  2177. }
  2178. });
  2179. },
  2180. // 添加新方法:处理重新录制按钮点击
  2181. handleRerecordButtonClick() {
  2182. console.log("点击重新录制按钮");
  2183. this.retryCount++;
  2184. console.log(`当前重试次数: ${this.retryCount}/${this.maxRetryAttempts}`);
  2185. this.showRerecordButton = false;
  2186. this.handleStartRecordingClick();
  2187. },
  2188. // 页面卸载时关闭WebSocket连接
  2189. onUnload() {
  2190. console.log("页面卸载,关闭WebSocket连接");
  2191. this.cleanupPersonDetectionWebSocket();
  2192. this.stopUserCamera();
  2193. this.clearCountdown();
  2194. if (this.recordingTimer) {
  2195. clearInterval(this.recordingTimer);
  2196. this.recordingTimer = null;
  2197. }
  2198. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  2199. this.mediaRecorder.stop();
  2200. }
  2201. common_vendor.index.setKeepScreenOn({
  2202. keepScreenOn: false
  2203. });
  2204. },
  2205. // 页面隐藏时关闭WebSocket连接
  2206. onHide() {
  2207. console.log("页面隐藏,关闭WebSocket连接");
  2208. this.cleanupPersonDetectionWebSocket();
  2209. if (this.isRecording) {
  2210. console.log("页面隐藏时正在录制,录制将继续");
  2211. common_vendor.index.showToast({
  2212. title: "请不要离开面试页面",
  2213. icon: "none",
  2214. duration: 2e3
  2215. });
  2216. }
  2217. },
  2218. // 页面显示时重新初始化WebSocket连接
  2219. onShow() {
  2220. console.log("identity-verify页面onShow");
  2221. console.log("当前本地存储中的职位信息:", common_vendor.index.getStorageSync("selectedJob"));
  2222. this.fetchQuestions();
  2223. this.fetchFollowUpQuestions();
  2224. const systemInfo = common_vendor.index.getSystemInfoSync();
  2225. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2226. if (isMiniProgram) {
  2227. if (!this.personDetectionSocket) {
  2228. console.log("重新初始化WebSocket连接");
  2229. this.initPersonDetectionWebSocket();
  2230. } else if (!this.personDetectionInterval) {
  2231. console.log("重新启动人脸检测定时器");
  2232. this.startPersonDetectionInterval();
  2233. }
  2234. }
  2235. },
  2236. // 添加新方法:阻止视频控制
  2237. preventVideoControl(e) {
  2238. e.preventDefault();
  2239. e.stopPropagation();
  2240. console.log("阻止视频控制操作");
  2241. return false;
  2242. },
  2243. // 添加获取追问问题的方法
  2244. fetchFollowUpQuestions() {
  2245. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  2246. let position_id = 1;
  2247. try {
  2248. const selectedJob = JSON.parse(common_vendor.index.getStorageSync("selectedJob") || "{}");
  2249. if (selectedJob && selectedJob.id) {
  2250. position_id = selectedJob.id;
  2251. console.log("使用选择的职位ID获取追问问题:", position_id);
  2252. } else {
  2253. console.warn("未找到选择的职位信息,使用默认职位ID:", position_id);
  2254. }
  2255. } catch (e) {
  2256. console.error("解析职位信息失败:", e);
  2257. }
  2258. const timestamp = Date.now();
  2259. const requestUrl = `${common_config.apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=5&_t=${timestamp}`;
  2260. console.log("获取追问问题的请求URL:", requestUrl);
  2261. common_vendor.index.request({
  2262. url: requestUrl,
  2263. method: "GET",
  2264. success: (res) => {
  2265. if (res.data && res.data.code === 2e3 && res.data.data && res.data.data.items) {
  2266. console.log("获取追问问题数据成功:", res.data.data.items);
  2267. this.followUpQuestions = res.data.data.items.map((item) => ({
  2268. ...item,
  2269. question_form: 5
  2270. // 确保每个追问问题都有正确的question_form值
  2271. }));
  2272. this.processFollowUpQuestions();
  2273. } else {
  2274. console.error("获取追问问题数据失败:", res.data);
  2275. this.followUpQuestions = [];
  2276. }
  2277. },
  2278. fail: (err) => {
  2279. console.error("请求追问问题数据失败:", err);
  2280. this.followUpQuestions = [];
  2281. }
  2282. });
  2283. },
  2284. // 改进处理追问问题数据的方法
  2285. processFollowUpQuestions() {
  2286. if (!this.followUpQuestions || this.followUpQuestions.length === 0) {
  2287. console.log("没有追问问题数据");
  2288. this.followUpQuestions = [];
  2289. return;
  2290. }
  2291. console.log("处理追问问题数据,总数:", this.followUpQuestions.length);
  2292. this.followUpQuestions = this.followUpQuestions.filter((q) => q.parent_question_id);
  2293. const parentQuestionIds = /* @__PURE__ */ new Set();
  2294. this.followUpQuestions.forEach((question) => {
  2295. if (question.parent_question_id) {
  2296. parentQuestionIds.add(question.parent_question_id);
  2297. }
  2298. });
  2299. console.log("有追问的父问题ID:", Array.from(parentQuestionIds));
  2300. this.hasPlayedFollowUp = {};
  2301. },
  2302. // 修改检查并播放追问问题的方法
  2303. checkAndPlayFollowUpQuestion(parentQuestionId) {
  2304. console.log("检查是否有追问问题,父问题ID:", parentQuestionId);
  2305. if (!parentQuestionId) {
  2306. console.warn("没有父问题ID,无法检查追问问题");
  2307. this.proceedToNextQuestion();
  2308. return;
  2309. }
  2310. if (this.hasPlayedFollowUp[parentQuestionId]) {
  2311. console.log("已经播放过此问题的追问,直接进入下一个问题");
  2312. this.retryCount = 0;
  2313. this.proceedToNextQuestion();
  2314. return;
  2315. }
  2316. const followUpQuestion = this.followUpQuestions.find(
  2317. (q) => q.parent_question_id === parentQuestionId && q.digital_human_video_url && q.question_form === 5
  2318. );
  2319. if (followUpQuestion) {
  2320. console.log("找到追问问题:", followUpQuestion);
  2321. const recordingDuration = this.getRecordingDuration();
  2322. const lowScoreDuration = 7;
  2323. if (recordingDuration < lowScoreDuration) {
  2324. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  2325. this.retryCount++;
  2326. this.needPlayLowScoreVideo = true;
  2327. this.playLowScoreVideo();
  2328. return;
  2329. }
  2330. this.retryCount = 0;
  2331. this.hasPlayedFollowUp[parentQuestionId] = true;
  2332. this.isFollowUpQuestion = true;
  2333. this.currentParentQuestionId = parentQuestionId;
  2334. this.currentFollowUpQuestion = followUpQuestion;
  2335. this.playFollowUpQuestionVideo(followUpQuestion);
  2336. } else {
  2337. console.log("没有找到对应的追问问题,继续下一个问题");
  2338. this.retryCount = 0;
  2339. this.proceedToNextQuestion();
  2340. }
  2341. },
  2342. // 修改播放追问问题视频的方法
  2343. playFollowUpQuestionVideo(followUpQuestion) {
  2344. console.log("播放追问问题视频:", followUpQuestion.digital_human_video_url);
  2345. this.parentQuestion = this.currentSubtitle;
  2346. this.videoUrl = followUpQuestion.digital_human_video_url;
  2347. this.videoPlaying = true;
  2348. this.isFollowUpQuestion = true;
  2349. this.currentFollowUpQuestion = followUpQuestion;
  2350. const followUpSubtitles = [{
  2351. startTime: 0,
  2352. endTime: 60,
  2353. // 延长显示时间到60秒
  2354. text: followUpQuestion.digital_human_video_subtitle || followUpQuestion.question
  2355. }];
  2356. const subtitleKey = `followUpSubtitles_${followUpQuestion.id}`;
  2357. this[subtitleKey] = followUpSubtitles;
  2358. this.$nextTick(() => {
  2359. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2360. if (videoContext) {
  2361. videoContext.play();
  2362. this.currentSubtitle = followUpSubtitles[0].text;
  2363. }
  2364. });
  2365. },
  2366. // 添加作废面试的方法
  2367. invalidateInterview() {
  2368. if (this.isRecording) {
  2369. this.stopRecordingAnswer();
  2370. }
  2371. if (this.recordingTimer) {
  2372. clearInterval(this.recordingTimer);
  2373. }
  2374. this.isRecording = false;
  2375. this.showStopRecordingButton = false;
  2376. this.showStartRecordingButton = false;
  2377. setTimeout(() => {
  2378. common_vendor.index.switchTab({
  2379. url: "/pages/index/index"
  2380. });
  2381. }, 500);
  2382. },
  2383. initPersonDetectionWebSocket() {
  2384. if (this.personDetectionSocket) {
  2385. this.cleanupPersonDetectionWebSocket();
  2386. }
  2387. try {
  2388. this.personDetectionSocket = common_vendor.index.connectSocket({
  2389. url: `${common_config.personDetectionWsUrl}/ws/interview-room/room_${common_vendor.index.getStorageSync("appId")}/${common_vendor.index.getStorageSync("appId")}/`,
  2390. success: () => {
  2391. console.log("WebSocket connection initiated");
  2392. },
  2393. fail: (error) => {
  2394. console.error("WebSocket connection failed:", error);
  2395. }
  2396. });
  2397. this.personDetectionSocket.onOpen(() => {
  2398. console.log("WebSocket connection opened");
  2399. this.startPersonDetectionInterval();
  2400. });
  2401. this.personDetectionSocket.onError((error) => {
  2402. console.error("WebSocket error:", error);
  2403. this.cleanupPersonDetectionWebSocket();
  2404. });
  2405. this.personDetectionSocket.onClose(() => {
  2406. console.log("WebSocket connection closed");
  2407. this.cleanupPersonDetectionWebSocket();
  2408. });
  2409. this.personDetectionSocket.onMessage((res) => {
  2410. try {
  2411. const data = JSON.parse(res.data);
  2412. console.log(data);
  2413. if (data.type === "person_detection_result") {
  2414. this.handlePersonDetectionResult(data);
  2415. }
  2416. } catch (error) {
  2417. console.error("Error parsing WebSocket message:", error);
  2418. }
  2419. });
  2420. } catch (error) {
  2421. console.error("Error initializing WebSocket:", error);
  2422. this.cleanupPersonDetectionWebSocket();
  2423. }
  2424. },
  2425. startPersonDetectionInterval() {
  2426. if (this.personDetectionInterval) {
  2427. clearInterval(this.personDetectionInterval);
  2428. }
  2429. this.personDetectionInterval = setInterval(() => {
  2430. if (this.personDetectionSocket && this.cameraContext) {
  2431. this.cameraContext.takePhoto({
  2432. quality: "low",
  2433. success: (res) => {
  2434. const tempFilePath = res.tempImagePath;
  2435. common_vendor.index.getFileSystemManager().readFile({
  2436. filePath: tempFilePath,
  2437. encoding: "base64",
  2438. success: (res2) => {
  2439. const base64Image = res2.data;
  2440. this.personDetectionSocket.send({
  2441. data: JSON.stringify({
  2442. type: "person_detection",
  2443. image_data: base64Image
  2444. })
  2445. });
  2446. },
  2447. fail: (error) => {
  2448. console.error("Error reading image file:", error);
  2449. }
  2450. });
  2451. },
  2452. fail: (error) => {
  2453. console.error("Error taking photo:", error);
  2454. }
  2455. });
  2456. }
  2457. }, 3e3);
  2458. },
  2459. cleanupPersonDetectionWebSocket() {
  2460. if (this.personDetectionInterval) {
  2461. clearInterval(this.personDetectionInterval);
  2462. this.personDetectionInterval = null;
  2463. }
  2464. if (this.personDetectionSocket) {
  2465. try {
  2466. this.personDetectionSocket.close();
  2467. } catch (error) {
  2468. console.error("Error closing WebSocket:", error);
  2469. }
  2470. this.personDetectionSocket = null;
  2471. }
  2472. },
  2473. handlePersonDetectionResult(data) {
  2474. console.log(data.data.detection.has_person);
  2475. console.log(data.data.identity.status);
  2476. if (data.data.identity.status !== "identity_verified") {
  2477. this.showPageWarning = true;
  2478. common_vendor.index.showToast({
  2479. title: data.data.identity.message,
  2480. icon: "none",
  2481. duration: 3e3
  2482. });
  2483. common_vendor.index.vibrateLong({
  2484. success: function() {
  2485. console.log("Vibration successful");
  2486. },
  2487. fail: function(err) {
  2488. console.error("Vibration failed:", err);
  2489. }
  2490. });
  2491. setTimeout(() => {
  2492. this.showPageWarning = false;
  2493. }, 3e3);
  2494. }
  2495. }
  2496. },
  2497. computed: {
  2498. // 计算进度比例
  2499. progressRatio() {
  2500. return this.recordingTimerCount / this.maxRecordingTime;
  2501. },
  2502. // 右半圆旋转角度
  2503. half1Turn() {
  2504. if (this.progressRatio <= 0.5) {
  2505. return this.progressRatio * 2 * 180;
  2506. } else {
  2507. return 180;
  2508. }
  2509. },
  2510. // 左半圆旋转角度
  2511. half2Turn() {
  2512. if (this.progressRatio <= 0.5) {
  2513. return 0;
  2514. } else {
  2515. return (this.progressRatio - 0.5) * 2 * 180;
  2516. }
  2517. },
  2518. // 修改进度百分比的计算方式
  2519. progressPercent() {
  2520. return Math.min(Math.round(this.recordingTimerCount / this.maxRecordingTime * 100), 100);
  2521. },
  2522. // 添加总题数计算属性
  2523. totalQuestions() {
  2524. return this.questions.length || 5;
  2525. }
  2526. }
  2527. };
  2528. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  2529. return common_vendor.e({
  2530. a: $data.videoUrl,
  2531. b: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  2532. c: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  2533. d: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  2534. e: common_vendor.o((...args) => $options.preventVideoControl && $options.preventVideoControl(...args)),
  2535. f: common_vendor.o((...args) => $options.preventVideoControl && $options.preventVideoControl(...args)),
  2536. g: $data.showAnswerButton
  2537. }, $data.showAnswerButton ? {
  2538. h: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  2539. } : {}, {
  2540. i: $data.currentSubtitle
  2541. }, $data.currentSubtitle ? common_vendor.e({
  2542. j: $data.currentVideoIndex > 0
  2543. }, $data.currentVideoIndex > 0 ? {
  2544. k: common_vendor.t($data.currentVideoIndex),
  2545. l: common_vendor.t($options.totalQuestions)
  2546. } : {}, {
  2547. m: !$data.isFollowUpQuestion
  2548. }, !$data.isFollowUpQuestion ? {
  2549. n: common_vendor.t($data.currentSubtitle)
  2550. } : {
  2551. o: common_vendor.t($data.parentQuestion),
  2552. p: common_vendor.t($data.currentSubtitle)
  2553. }) : {}, {
  2554. q: $data.useMiniProgramCameraComponent
  2555. }, $data.useMiniProgramCameraComponent ? {
  2556. r: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  2557. } : {}, {
  2558. s: $data.showPageWarning ? 1 : "",
  2559. t: $data.loading
  2560. }, $data.loading ? {} : {}, {
  2561. v: $data.showDebugInfo
  2562. }, $data.showDebugInfo ? common_vendor.e({
  2563. w: $data.assistantResponse
  2564. }, $data.assistantResponse ? {
  2565. x: common_vendor.t($data.assistantResponse)
  2566. } : {}, {
  2567. y: $data.audioTranscript
  2568. }, $data.audioTranscript ? {
  2569. z: common_vendor.t($data.audioTranscript)
  2570. } : {}, {
  2571. A: common_vendor.f($data.processedResponses, (item, index, i0) => {
  2572. return common_vendor.e({
  2573. a: item.role
  2574. }, item.role ? {
  2575. b: common_vendor.t(item.role)
  2576. } : {}, {
  2577. c: item.transcript
  2578. }, item.transcript ? {
  2579. d: common_vendor.t(item.transcript)
  2580. } : {}, {
  2581. e: index
  2582. });
  2583. })
  2584. }) : {}, {
  2585. B: $data.showStopRecordingButton
  2586. }, $data.showStopRecordingButton ? {
  2587. C: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  2588. } : {}, {
  2589. D: $data.isRecording
  2590. }, $data.isRecording ? {
  2591. E: common_vendor.t($data.recordingTimeDisplay),
  2592. F: `conic-gradient(${$data.progressColor} ${$options.progressPercent}%, ${$data.progressBgColor} 0%)`
  2593. } : {}, {
  2594. G: $data.showStartRecordingButton
  2595. }, $data.showStartRecordingButton ? {
  2596. H: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  2597. } : {}, {
  2598. I: $data.showRetryButton
  2599. }, $data.showRetryButton ? {
  2600. J: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  2601. } : {}, {
  2602. K: $data.showCountdown
  2603. }, $data.showCountdown ? {
  2604. L: common_vendor.t($data.countdownValue)
  2605. } : {}, {
  2606. M: $data.showRerecordButton
  2607. }, $data.showRerecordButton ? {
  2608. N: common_vendor.o((...args) => $options.handleRerecordButtonClick && $options.handleRerecordButtonClick(...args))
  2609. } : {}, {
  2610. O: $data.showPageWarning ? 1 : ""
  2611. });
  2612. }
  2613. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-464e78c6"]]);
  2614. wx.createPage(MiniProgramPage);