identity-verify.js 84 KB

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