identity-verify.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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: "http://121.36.251.245:9000/minlong/9c139c99-a613-49f2-986b-8a3ac20a9b1b.mp4",
  17. // 用于存储AI数字人视频URL
  18. showReplayButton: false,
  19. cameraStream: null,
  20. // 存储摄像头流
  21. cameraError: null,
  22. // 存储摄像头错误信息
  23. useMiniProgramCameraComponent: false,
  24. // 添加小程序相机组件标志
  25. cameraContext: null,
  26. // 添加相机上下文
  27. currentSubtitle: "",
  28. subtitles: [
  29. {
  30. startTime: 0,
  31. // 开始时间(秒)
  32. endTime: 5,
  33. // 结束时间(秒)
  34. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  35. },
  36. {
  37. startTime: 5,
  38. endTime: 13,
  39. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  40. },
  41. {
  42. startTime: 13,
  43. endTime: 20,
  44. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  45. }
  46. ],
  47. secondVideoSubtitles: [
  48. {
  49. startTime: 0,
  50. endTime: 10,
  51. text: "请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:"
  52. }
  53. ],
  54. thirdVideoSubtitles: [
  55. {
  56. startTime: 0,
  57. endTime: 3,
  58. text: "在工作中,你如何确保个人防护装备的正确使用?"
  59. }
  60. ],
  61. fourthVideoSubtitles: [
  62. {
  63. startTime: 0,
  64. endTime: 3,
  65. text: "描述一次你与团队合作改善生产流程的经历。"
  66. }
  67. ],
  68. fifthVideoSubtitles: [
  69. {
  70. startTime: 0,
  71. endTime: 6,
  72. text: "你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?"
  73. }
  74. ],
  75. sixthVideoSubtitles: [
  76. {
  77. startTime: 0,
  78. endTime: 5,
  79. text: "您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?"
  80. }
  81. ],
  82. showAnswerButton: false,
  83. // 控制答题按钮显示
  84. currentVideoIndex: 0,
  85. // 当前播放的视频索引
  86. videoList: [
  87. "http://121.36.251.245:9000/minlong/9c139c99-a613-49f2-986b-8a3ac20a9b1b.mp4",
  88. // 第一段视频
  89. "http://121.36.251.245:9000/minlong/15467aca-2c1e-4d40-bebc-11fc984f0a24.mp4",
  90. // 第二段视频
  91. "http://121.36.251.245:9000/minlong/a96f295c-ecba-4b08-b627-0ed0dc72d727.mp4",
  92. // 第三段视频
  93. "http://121.36.251.245:9000/minlong/e152eb38-7e03-4bb0-8b8d-73d51342d2aa.mp4",
  94. "http://121.36.251.245:9000/minlong/6381eec3-1373-400f-aa8c-e229b60522ea.mp4",
  95. "http://121.36.251.245:9000/minlong/2a11fe78-38d5-4af3-91c8-a080feb122e0.mp4"
  96. //结束
  97. ],
  98. isRecording: false,
  99. recordingTimer: null,
  100. showStopRecordingButton: false,
  101. mediaRecorder: null,
  102. recordedChunks: [],
  103. recorder: null,
  104. lastUploadedVideoUrl: "",
  105. showStartRecordingButton: false,
  106. showRetryButton: false,
  107. // 控制重试按钮显示
  108. lastVideoToRetry: null
  109. // 存储上次失败的视频URL,用于重试
  110. };
  111. },
  112. mounted() {
  113. this.playDigitalHumanVideo();
  114. this.checkAudioPermission();
  115. this.initCamera();
  116. setTimeout(() => {
  117. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  118. this.testAudioInput();
  119. }
  120. }, 3e3);
  121. },
  122. beforeDestroy() {
  123. this.stopUserCamera();
  124. },
  125. methods: {
  126. // 初始化相机
  127. async initCamera() {
  128. const systemInfo = common_vendor.index.getSystemInfoSync();
  129. const isMiniProgram = systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay" || systemInfo.uniPlatform === "mp-baidu";
  130. if (isMiniProgram) {
  131. this.useMiniProgramCameraComponent = true;
  132. this.cameraContext = common_vendor.index.createCameraContext();
  133. common_vendor.index.getSetting({
  134. success: (res) => {
  135. if (!res.authSetting["scope.record"]) {
  136. common_vendor.index.authorize({
  137. scope: "scope.record",
  138. success: () => {
  139. console.log("录音权限已获取");
  140. },
  141. fail: (err) => {
  142. console.error("录音权限获取失败:", err);
  143. this.showPermissionDialog("录音");
  144. }
  145. });
  146. }
  147. if (!res.authSetting["scope.camera"]) {
  148. common_vendor.index.authorize({
  149. scope: "scope.camera",
  150. success: () => {
  151. console.log("相机权限已获取");
  152. },
  153. fail: (err) => {
  154. console.error("相机权限获取失败:", err);
  155. this.showPermissionDialog("相机");
  156. }
  157. });
  158. }
  159. }
  160. });
  161. } else {
  162. try {
  163. const constraints = {
  164. audio: {
  165. echoCancellation: true,
  166. noiseSuppression: true,
  167. autoGainControl: true
  168. },
  169. video: {
  170. width: { ideal: 640, max: 1280 },
  171. // 控制视频宽度
  172. height: { ideal: 480, max: 720 },
  173. // 控制视频高度
  174. frameRate: { ideal: 15, max: 24 },
  175. // 控制帧率
  176. facingMode: "user"
  177. }
  178. };
  179. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  180. this.cameraStream = stream;
  181. const audioTracks = stream.getAudioTracks();
  182. console.log("音频轨道数量:", audioTracks.length);
  183. if (audioTracks.length > 0) {
  184. console.log("音频轨道已获取:", audioTracks[0].label);
  185. audioTracks[0].enabled = true;
  186. } else {
  187. console.warn("未检测到音频轨道,尝试单独获取音频");
  188. this.tryGetAudioOnly();
  189. }
  190. const videoElement = this.$refs.userCameraVideo;
  191. if (videoElement) {
  192. videoElement.srcObject = stream;
  193. videoElement.muted = true;
  194. }
  195. } catch (error) {
  196. console.error("获取摄像头失败:", error);
  197. this.cameraError = error.message || "无法访问摄像头";
  198. common_vendor.index.showToast({
  199. title: "无法访问摄像头,请检查权限设置",
  200. icon: "none"
  201. });
  202. }
  203. }
  204. },
  205. // 停止用户摄像头
  206. stopUserCamera() {
  207. if (this.cameraStream) {
  208. this.cameraStream.getTracks().forEach((track) => {
  209. track.stop();
  210. });
  211. this.cameraStream = null;
  212. }
  213. },
  214. async fetchData() {
  215. this.loading = true;
  216. this.assistantResponse = "";
  217. this.audioTranscript = "";
  218. this.processedResponses = [];
  219. try {
  220. const requestTask = common_vendor.index.request({
  221. url: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
  222. method: "POST",
  223. header: {
  224. "Content-Type": "application/json",
  225. "Authorization": "Bearer sk-9e1ec73a7d97493b8613c63f06b6110c"
  226. },
  227. data: {
  228. "model": "qwen-omni-turbo",
  229. "messages": [
  230. {
  231. "role": "user",
  232. "content": [
  233. {
  234. "type": "input_audio",
  235. "input_audio": {
  236. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  237. "format": "wav"
  238. }
  239. },
  240. {
  241. "type": "text",
  242. "text": "这段音频在说什么"
  243. }
  244. ]
  245. }
  246. ],
  247. "stream": true,
  248. "stream_options": {
  249. "include_usage": true
  250. },
  251. "modalities": ["text", "audio"],
  252. "audio": { "voice": "Cherry", "format": "wav" }
  253. },
  254. success: (res) => {
  255. console.log("请求成功,响应数据:", res.data);
  256. if (typeof res.data === "string" && res.data.includes("data: {")) {
  257. const chunks = res.data.split("data: ").filter((chunk) => chunk.trim() !== "");
  258. chunks.forEach((chunk) => {
  259. this.handleStreamResponse(chunk);
  260. });
  261. } else {
  262. this.handleStreamResponse(res.data);
  263. }
  264. this.playDigitalHumanVideo();
  265. },
  266. fail: (err) => {
  267. console.error("请求失败:", err);
  268. },
  269. complete: () => {
  270. this.loading = false;
  271. }
  272. });
  273. } catch (error) {
  274. console.error("获取数据失败:", error);
  275. this.loading = false;
  276. }
  277. },
  278. handleStreamResponse(data) {
  279. if (typeof data === "string") {
  280. if (data === "[DONE]")
  281. return;
  282. try {
  283. const cleanData = data.trim();
  284. if (cleanData.startsWith("{") && cleanData.endsWith("}")) {
  285. const jsonData = JSON.parse(cleanData);
  286. this.processStreamChunk(jsonData);
  287. }
  288. } catch (e) {
  289. console.error("解析JSON失败:", e, "原始数据:", data);
  290. }
  291. } else {
  292. this.processStreamChunk(data);
  293. }
  294. },
  295. processStreamChunk(chunk) {
  296. if (chunk.choices && chunk.choices.length > 0) {
  297. const choice = chunk.choices[0];
  298. if (choice.delta && choice.delta.content) {
  299. this.assistantResponse += choice.delta.content;
  300. }
  301. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  302. this.audioTranscript += choice.delta.audio.transcript;
  303. }
  304. if (choice.delta) {
  305. const result = {};
  306. if (choice.delta.role) {
  307. result.role = choice.delta.role;
  308. }
  309. if (choice.delta.audio && choice.delta.audio.transcript) {
  310. result.transcript = choice.delta.audio.transcript;
  311. }
  312. if (Object.keys(result).length > 0) {
  313. this.processedResponses.push(result);
  314. }
  315. }
  316. }
  317. },
  318. processResponseData() {
  319. this.processedResponses = this.responses.map((item) => {
  320. const result = {};
  321. if (item.delta && item.delta.role) {
  322. result.role = item.delta.role;
  323. }
  324. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  325. result.transcript = item.delta.audio.transcript;
  326. }
  327. return result;
  328. }).filter((item) => Object.keys(item).length > 0);
  329. },
  330. // 播放数字人视频
  331. playDigitalHumanVideo() {
  332. this.videoUrl = this.videoList[this.currentVideoIndex];
  333. this.videoPlaying = true;
  334. this.$nextTick(() => {
  335. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  336. if (videoContext) {
  337. videoContext.play();
  338. setTimeout(() => {
  339. if (this.videoPlaying && this.$refs.videoPlayer) {
  340. console.log("视频应该正在播放");
  341. } else {
  342. console.log("视频可能未成功播放,尝试替代方案");
  343. this.tryAlternativeVideoPath();
  344. }
  345. }, 1e3);
  346. } else {
  347. console.error("无法创建视频上下文");
  348. this.tryAlternativeVideoPath();
  349. }
  350. });
  351. },
  352. // 修改 tryAlternativeVideoPath 方法
  353. tryAlternativeVideoPath() {
  354. console.log("尝试使用替代路径");
  355. const alternativePaths = [
  356. "./static/demo.mp4",
  357. "../static/demo.mp4",
  358. "static/demo.mp4",
  359. "/static/demo.mp4",
  360. // 添加绝对路径
  361. `${window.location.origin}/static/demo.mp4`
  362. ];
  363. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  364. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  365. this.videoUrl = alternativePaths[nextPathIndex];
  366. console.log("尝试新路径:", this.videoUrl);
  367. this.$nextTick(() => {
  368. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  369. if (videoContext) {
  370. videoContext.stop();
  371. videoContext.play();
  372. setTimeout(() => {
  373. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  374. console.log("所有路径均失败,尝试使用uni.getVideoInfo检查视频");
  375. this.checkVideoWithAPI();
  376. }
  377. }, 1e3);
  378. }
  379. });
  380. },
  381. // 添加新方法:使用uni API检查视频
  382. checkVideoWithAPI() {
  383. common_vendor.index.getVideoInfo({
  384. src: "/static/demo.mp4",
  385. success: (res) => {
  386. console.log("视频信息获取成功:", res);
  387. this.videoUrl = "/static/demo.mp4";
  388. this.$nextTick(() => {
  389. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  390. if (videoContext) {
  391. videoContext.play();
  392. }
  393. });
  394. },
  395. fail: (err) => {
  396. console.error("视频信息获取失败:", err);
  397. this.fallbackToLocalVideo();
  398. }
  399. });
  400. },
  401. // 添加新方法:回退到本地视频
  402. fallbackToLocalVideo() {
  403. console.log("尝试使用本地视频资源");
  404. const platform = common_vendor.index.getSystemInfoSync().platform;
  405. if (platform === "android" || platform === "ios") {
  406. this.videoUrl = platform === "android" ? "android.resource://package_name/raw/demo" : "file:///assets/demo.mp4";
  407. this.$nextTick(() => {
  408. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  409. if (videoContext) {
  410. videoContext.play();
  411. }
  412. });
  413. } else {
  414. this.videoPlaying = false;
  415. common_vendor.index.showToast({
  416. title: "视频加载失败,显示静态图片",
  417. icon: "none"
  418. });
  419. }
  420. },
  421. // 修改 handleVideoError 方法
  422. handleVideoError(e) {
  423. console.error("视频加载错误:", e);
  424. if (e && e.detail) {
  425. console.error("详细错误信息:", e.detail);
  426. }
  427. common_vendor.index.getFileInfo({
  428. filePath: this.videoUrl.startsWith("/") ? this.videoUrl.substring(1) : this.videoUrl,
  429. success: (res) => {
  430. console.log("文件存在,大小:", res.size);
  431. this.tryDifferentFormat();
  432. },
  433. fail: (err) => {
  434. console.error("文件不存在或无法访问:", err);
  435. this.tryAlternativeVideoPath();
  436. }
  437. });
  438. common_vendor.index.showToast({
  439. title: "视频加载失败,请检查文件是否存在",
  440. icon: "none",
  441. duration: 2e3
  442. });
  443. },
  444. // 添加新方法:尝试不同格式
  445. tryDifferentFormat() {
  446. console.log("尝试不同的视频格式");
  447. const formats = [
  448. { ext: "mp4", mime: "video/mp4" },
  449. { ext: "webm", mime: "video/webm" },
  450. { ext: "ogg", mime: "video/ogg" },
  451. { ext: "mov", mime: "video/quicktime" }
  452. ];
  453. const currentPath = this.videoUrl;
  454. const basePath = currentPath.substring(0, currentPath.lastIndexOf(".")) || "/static/demo";
  455. let nextFormat = formats.find((f) => !currentPath.endsWith(f.ext));
  456. if (nextFormat) {
  457. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  458. console.log("尝试新格式:", this.videoUrl);
  459. this.$nextTick(() => {
  460. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  461. if (videoContext) {
  462. videoContext.stop();
  463. videoContext.play();
  464. }
  465. });
  466. } else {
  467. this.useBuiltInResource();
  468. }
  469. },
  470. // 添加新方法:使用内置资源
  471. useBuiltInResource() {
  472. console.log("尝试使用内置资源");
  473. const platform = common_vendor.index.getSystemInfoSync().platform;
  474. if (platform === "windows") {
  475. process.env.UNI_INPUT_DIR || "";
  476. this.videoUrl = `./static/demo.mp4`;
  477. console.log("Windows平台尝试路径:", this.videoUrl);
  478. } else if (platform === "android" || platform === "ios") {
  479. this.useNativeVideo();
  480. } else {
  481. const baseUrl = window.location.origin;
  482. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  483. console.log("Web平台尝试URL:", this.videoUrl);
  484. }
  485. this.$nextTick(() => {
  486. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  487. if (videoContext) {
  488. videoContext.play();
  489. }
  490. });
  491. },
  492. // 添加新方法:使用原生视频能力
  493. useNativeVideo() {
  494. console.log("尝试使用原生视频能力");
  495. common_vendor.index.chooseVideo({
  496. sourceType: ["album"],
  497. success: (res) => {
  498. this.videoUrl = res.tempFilePath;
  499. this.$nextTick(() => {
  500. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  501. if (videoContext) {
  502. videoContext.play();
  503. }
  504. });
  505. },
  506. fail: () => {
  507. this.videoPlaying = false;
  508. common_vendor.index.showToast({
  509. title: "无法加载视频,显示静态图片",
  510. icon: "none"
  511. });
  512. }
  513. });
  514. },
  515. // 处理视频结束事件
  516. handleVideoEnded() {
  517. console.log("视频播放结束");
  518. this.videoPlaying = false;
  519. if (this.currentVideoIndex >= 1) {
  520. this.showStartRecordingButton = true;
  521. } else {
  522. this.showAnswerButton = true;
  523. }
  524. },
  525. // 添加新方法:开始录制用户回答
  526. startRecordingAnswer() {
  527. console.log("开始录制用户回答");
  528. this.isRecording = true;
  529. const systemInfo = common_vendor.index.getSystemInfoSync();
  530. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  531. if (isMiniProgram) {
  532. this.startMiniProgramRecording();
  533. } else {
  534. this.startBrowserRecording();
  535. }
  536. this.showStopRecordingButton = true;
  537. },
  538. // 修改小程序环境下的录制方法
  539. startMiniProgramRecording() {
  540. if (!this.cameraContext) {
  541. this.cameraContext = common_vendor.index.createCameraContext();
  542. }
  543. common_vendor.index.getSetting({
  544. success: (res) => {
  545. const hasRecordAuth = res.authSetting["scope.record"];
  546. const hasCameraAuth = res.authSetting["scope.camera"];
  547. if (!hasRecordAuth || !hasCameraAuth) {
  548. console.warn("缺少必要权限,请求权限");
  549. this.requestMiniProgramPermissions();
  550. return;
  551. }
  552. const options = {
  553. timeout: 6e4,
  554. // 最长录制60秒
  555. quality: "medium",
  556. // 可选值:'low', 'medium', 'high'
  557. compressed: true,
  558. // 是否压缩视频
  559. success: () => {
  560. console.log("小程序录像开始成功");
  561. },
  562. fail: (err) => {
  563. console.error("小程序录像开始失败:", err);
  564. common_vendor.index.showToast({
  565. title: "录制失败,请检查相机权限",
  566. icon: "none"
  567. });
  568. this.proceedToNextQuestion();
  569. },
  570. timeoutCallback: () => {
  571. console.log("录制超时自动停止");
  572. this.stopRecordingAnswer();
  573. }
  574. };
  575. this.recorder = this.cameraContext.startRecord(options);
  576. }
  577. });
  578. },
  579. // 添加新方法:请求小程序权限
  580. requestMiniProgramPermissions() {
  581. common_vendor.index.authorize({
  582. scope: "scope.record",
  583. success: () => {
  584. console.log("录音权限已获取");
  585. common_vendor.index.authorize({
  586. scope: "scope.camera",
  587. success: () => {
  588. console.log("相机权限已获取");
  589. this.startMiniProgramRecording();
  590. },
  591. fail: (err) => {
  592. console.error("相机权限获取失败:", err);
  593. this.showPermissionDialog("相机");
  594. }
  595. });
  596. },
  597. fail: (err) => {
  598. console.error("录音权限获取失败:", err);
  599. this.showPermissionDialog("录音");
  600. }
  601. });
  602. },
  603. // 修改浏览器环境下的录制方法
  604. startBrowserRecording() {
  605. if (!this.cameraStream) {
  606. console.error("没有可用的摄像头流");
  607. common_vendor.index.showToast({
  608. title: "录制失败,摄像头未就绪",
  609. icon: "none"
  610. });
  611. this.proceedToNextQuestion();
  612. return;
  613. }
  614. try {
  615. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  616. if (!hasAudio) {
  617. console.warn("警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流");
  618. navigator.mediaDevices.getUserMedia({
  619. audio: {
  620. echoCancellation: true,
  621. noiseSuppression: true,
  622. autoGainControl: true
  623. },
  624. video: true
  625. }).then((newStream) => {
  626. const audioTracks = newStream.getAudioTracks();
  627. if (audioTracks.length > 0) {
  628. console.log("成功获取音频轨道:", audioTracks[0].label);
  629. const videoTrack = this.cameraStream.getVideoTracks()[0];
  630. const audioTrack = newStream.getAudioTracks()[0];
  631. const combinedStream = new MediaStream();
  632. if (videoTrack)
  633. combinedStream.addTrack(videoTrack);
  634. if (audioTrack)
  635. combinedStream.addTrack(audioTrack);
  636. this.cameraStream = combinedStream;
  637. const videoElement = this.$refs.userCameraVideo;
  638. if (videoElement) {
  639. videoElement.srcObject = combinedStream;
  640. videoElement.muted = true;
  641. }
  642. this.setupMediaRecorder(combinedStream);
  643. } else {
  644. console.warn("仍然无法获取音频轨道");
  645. this.setupMediaRecorder(this.cameraStream);
  646. }
  647. }).catch((err) => {
  648. console.error("获取音频失败:", err);
  649. this.setupMediaRecorder(this.cameraStream);
  650. });
  651. } else {
  652. console.log("检测到音频轨道,直接使用");
  653. this.setupMediaRecorder(this.cameraStream);
  654. }
  655. } catch (error) {
  656. console.error("浏览器录制失败:", error);
  657. common_vendor.index.showToast({
  658. title: "录制失败,浏览器可能不支持此功能",
  659. icon: "none"
  660. });
  661. this.proceedToNextQuestion();
  662. }
  663. },
  664. // 修改 setupMediaRecorder 方法
  665. setupMediaRecorder(stream) {
  666. const videoTracks = stream.getVideoTracks();
  667. const audioTracks = stream.getAudioTracks();
  668. console.log("设置MediaRecorder - 视频轨道:", videoTracks.length, "音频轨道:", audioTracks.length);
  669. let mimeType = "";
  670. const supportedTypes = [
  671. "video/webm;codecs=vp9,opus",
  672. "video/webm;codecs=vp8,opus",
  673. "video/webm;codecs=h264,opus",
  674. "video/mp4;codecs=h264,aac",
  675. "video/webm",
  676. "video/mp4"
  677. ];
  678. for (const type of supportedTypes) {
  679. if (MediaRecorder.isTypeSupported(type)) {
  680. mimeType = type;
  681. console.log("使用支持的MIME类型:", mimeType);
  682. break;
  683. }
  684. }
  685. const options = {
  686. mimeType: mimeType || "",
  687. audioBitsPerSecond: 64e3,
  688. // 降低音频比特率
  689. videoBitsPerSecond: 1e6
  690. // 降低视频比特率到1Mbps
  691. };
  692. try {
  693. this.mediaRecorder = new MediaRecorder(stream, options);
  694. console.log("MediaRecorder创建成功,使用选项:", options);
  695. } catch (e) {
  696. console.warn("使用指定选项创建MediaRecorder失败,尝试使用默认选项");
  697. this.mediaRecorder = new MediaRecorder(stream);
  698. }
  699. this.recordedChunks = [];
  700. this.mediaRecorder.ondataavailable = (event) => {
  701. if (event.data && event.data.size > 0) {
  702. this.recordedChunks.push(event.data);
  703. console.log(`收到数据块: ${event.data.size} 字节`);
  704. }
  705. };
  706. this.mediaRecorder.onstop = async () => {
  707. console.log("MediaRecorder停止,数据块数量:", this.recordedChunks.length);
  708. if (this.recordedChunks.length === 0) {
  709. console.error("没有录制到数据");
  710. common_vendor.index.showToast({
  711. title: "录制失败,未捕获到数据",
  712. icon: "none"
  713. });
  714. this.proceedToNextQuestion();
  715. return;
  716. }
  717. const mimeType2 = this.mediaRecorder.mimeType || "video/webm";
  718. const blob = new Blob(this.recordedChunks, { type: mimeType2 });
  719. console.log("创建Blob,原始大小:", blob.size, "类型:", mimeType2);
  720. common_vendor.index.showLoading({
  721. title: "正在处理视频...",
  722. mask: true
  723. });
  724. try {
  725. const compressedBlob = await this.compressVideo(blob);
  726. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  727. const file = new File([compressedBlob], fileName, { type: mimeType2 });
  728. common_vendor.index.hideLoading();
  729. this.uploadRecordedVideo(file);
  730. } catch (error) {
  731. console.error("视频处理失败:", error);
  732. common_vendor.index.hideLoading();
  733. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  734. const file = new File([blob], fileName, { type: mimeType2 });
  735. this.uploadRecordedVideo(file);
  736. }
  737. };
  738. this.mediaRecorder.onerror = (event) => {
  739. console.error("MediaRecorder错误:", event.error);
  740. };
  741. try {
  742. this.mediaRecorder.start(1e3);
  743. console.log("MediaRecorder开始录制");
  744. } catch (e) {
  745. console.error("开始录制失败:", e);
  746. }
  747. },
  748. // 添加新方法:停止录制用户回答
  749. stopRecordingAnswer() {
  750. console.log("停止录制用户回答");
  751. this.isRecording = false;
  752. if (this.recordingTimer) {
  753. clearTimeout(this.recordingTimer);
  754. this.recordingTimer = null;
  755. }
  756. common_vendor.index.hideLoading();
  757. this.showStopRecordingButton = false;
  758. const systemInfo = common_vendor.index.getSystemInfoSync();
  759. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  760. if (isMiniProgram) {
  761. this.stopMiniProgramRecording();
  762. } else {
  763. this.stopBrowserRecording();
  764. }
  765. },
  766. // 添加新方法:停止小程序录制
  767. stopMiniProgramRecording() {
  768. if (!this.cameraContext) {
  769. console.error("相机上下文不存在");
  770. this.proceedToNextQuestion();
  771. return;
  772. }
  773. this.cameraContext.stopRecord({
  774. success: (res) => {
  775. console.log("小程序录像停止成功:", res);
  776. const tempFilePath = res.tempVideoPath;
  777. this.uploadRecordedVideo(tempFilePath);
  778. },
  779. fail: (err) => {
  780. console.error("小程序录像停止失败:", err);
  781. common_vendor.index.showToast({
  782. title: "录制失败",
  783. icon: "none"
  784. });
  785. this.proceedToNextQuestion();
  786. }
  787. });
  788. },
  789. // 添加新方法:停止浏览器录制
  790. stopBrowserRecording() {
  791. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  792. this.mediaRecorder.stop();
  793. console.log("浏览器录制停止成功");
  794. } else {
  795. console.error("MediaRecorder不存在或已经停止");
  796. this.proceedToNextQuestion();
  797. }
  798. },
  799. // 修改上传录制的视频方法
  800. uploadRecordedVideo(fileOrPath) {
  801. console.log("准备上传视频:", typeof fileOrPath === "string" ? fileOrPath : fileOrPath.name);
  802. common_vendor.index.showLoading({
  803. title: "正在上传...",
  804. mask: true
  805. });
  806. const userInfo = common_vendor.index.getStorageSync("userInfo");
  807. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  808. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || "1";
  809. let questionId;
  810. switch (this.currentVideoIndex) {
  811. case 1:
  812. questionId = 10;
  813. break;
  814. case 2:
  815. questionId = 11;
  816. break;
  817. case 3:
  818. questionId = 12;
  819. break;
  820. case 4:
  821. questionId = 13;
  822. break;
  823. default:
  824. questionId = 10;
  825. }
  826. if (typeof fileOrPath !== "string") {
  827. const formData = new FormData();
  828. formData.append("file", fileOrPath);
  829. formData.append("openid", openid);
  830. formData.append("tenant_id", tenant_id);
  831. formData.append("application_id", common_vendor.index.getStorageSync("appId"));
  832. formData.append("question_id", questionId);
  833. formData.append("video_duration", 0);
  834. formData.append("has_audio", "true");
  835. const xhr = new XMLHttpRequest();
  836. xhr.open("POST", `${common_config.apiBaseUrl}/api/system/upload/`, true);
  837. xhr.timeout = 12e4;
  838. xhr.onload = () => {
  839. common_vendor.index.hideLoading();
  840. if (xhr.status === 200) {
  841. try {
  842. const res = JSON.parse(xhr.responseText);
  843. console.log("上传响应:", res);
  844. if (res.code === 2e3) {
  845. const videoUrl = res.data.url || res.data.photoUrl || "";
  846. if (videoUrl) {
  847. this.submitVideoToInterview(videoUrl);
  848. } else {
  849. common_vendor.index.showToast({
  850. title: "视频URL获取失败",
  851. icon: "none"
  852. });
  853. }
  854. } else {
  855. common_vendor.index.showToast({
  856. title: res.msg || "上传失败,请重试",
  857. icon: "none"
  858. });
  859. }
  860. } catch (e) {
  861. console.error("解析响应失败:", e);
  862. common_vendor.index.showToast({
  863. title: "解析响应失败",
  864. icon: "none"
  865. });
  866. }
  867. } else {
  868. common_vendor.index.showToast({
  869. title: "上传失败,HTTP状态: " + xhr.status,
  870. icon: "none"
  871. });
  872. }
  873. };
  874. xhr.onerror = () => {
  875. common_vendor.index.hideLoading();
  876. console.error("上传网络错误");
  877. common_vendor.index.showToast({
  878. title: "网络错误,请重试",
  879. icon: "none"
  880. });
  881. };
  882. xhr.upload.onprogress = (event) => {
  883. if (event.lengthComputable) {
  884. const percentComplete = Math.round(event.loaded / event.total * 100);
  885. console.log("上传进度:", percentComplete + "%");
  886. }
  887. };
  888. xhr.send(formData);
  889. return;
  890. }
  891. common_vendor.index.uploadFile({
  892. url: `${common_config.apiBaseUrl}/api/system/upload/`,
  893. filePath: fileOrPath,
  894. name: "file",
  895. formData: {
  896. openid,
  897. tenant_id,
  898. application_id: common_vendor.index.getStorageSync("appId"),
  899. question_id: questionId,
  900. video_duration: 0,
  901. has_audio: "true"
  902. // 明确标记视频包含音频
  903. },
  904. success: (uploadRes) => {
  905. try {
  906. const res = JSON.parse(uploadRes.data);
  907. console.log("上传响应:", res);
  908. if (res.code === 2e3) {
  909. const videoUrl = res.data.permanent_link || res.data.url || "";
  910. if (videoUrl) {
  911. this.submitVideoToInterview(videoUrl);
  912. } else {
  913. common_vendor.index.showToast({
  914. title: "视频URL获取失败",
  915. icon: "none"
  916. });
  917. }
  918. } else {
  919. common_vendor.index.showToast({
  920. title: res.msg || "上传失败,请重试",
  921. icon: "none"
  922. });
  923. }
  924. } catch (e) {
  925. console.error("解析响应失败:", e);
  926. common_vendor.index.showToast({
  927. title: "解析响应失败",
  928. icon: "none"
  929. });
  930. }
  931. },
  932. fail: (err) => {
  933. console.error("上传失败:", err);
  934. common_vendor.index.showToast({
  935. title: "上传失败,请重试",
  936. icon: "none"
  937. });
  938. },
  939. complete: () => {
  940. common_vendor.index.hideLoading();
  941. }
  942. });
  943. },
  944. // 修改 submitVideoToInterview 方法
  945. submitVideoToInterview(videoUrl) {
  946. console.log("提交视频URL到面试接口:", videoUrl);
  947. common_vendor.index.showLoading({
  948. title: "正在提交...",
  949. mask: true
  950. });
  951. let questionId;
  952. switch (this.currentVideoIndex) {
  953. case 1:
  954. questionId = 10;
  955. break;
  956. case 2:
  957. questionId = 11;
  958. break;
  959. case 3:
  960. questionId = 12;
  961. break;
  962. case 4:
  963. questionId = 13;
  964. break;
  965. default:
  966. questionId = 10;
  967. }
  968. const requestData = {
  969. application_id: common_vendor.index.getStorageSync("appId"),
  970. question_id: questionId,
  971. video_url: videoUrl,
  972. tenant_id: common_vendor.index.getStorageSync("tenant_id") || "1"
  973. };
  974. common_vendor.index.request({
  975. url: `${common_config.apiBaseUrl}/api/job/upload_question_video`,
  976. method: "POST",
  977. data: requestData,
  978. // 使用data而不是formData
  979. header: {
  980. "content-type": "application/x-www-form-urlencoded"
  981. // 修改为form-data格式的content-type
  982. },
  983. success: (res) => {
  984. console.log("面试接口提交成功:", res);
  985. common_vendor.index.hideLoading();
  986. if (res.data.code === 0 || res.data.code === 2e3) {
  987. common_vendor.index.showToast({
  988. title: "回答已提交",
  989. icon: "success"
  990. });
  991. this.lastUploadedVideoUrl = videoUrl;
  992. this.showRetryButton = false;
  993. this.lastVideoToRetry = null;
  994. setTimeout(() => {
  995. if (this.currentVideoIndex === 4) {
  996. common_vendor.index.navigateTo({
  997. url: "/pages/camera/camera"
  998. });
  999. } else {
  1000. this.proceedToNextQuestion();
  1001. }
  1002. }, 1500);
  1003. } else {
  1004. common_vendor.index.showToast({
  1005. title: res.data.msg || "提交失败,请重试",
  1006. icon: "none"
  1007. });
  1008. this.lastVideoToRetry = videoUrl;
  1009. this.showRetryButton = true;
  1010. }
  1011. },
  1012. fail: (err) => {
  1013. console.error("面试接口提交失败:", err);
  1014. common_vendor.index.hideLoading();
  1015. common_vendor.index.showToast({
  1016. title: "网络错误,请重试",
  1017. icon: "none"
  1018. });
  1019. this.lastVideoToRetry = videoUrl;
  1020. this.showRetryButton = true;
  1021. }
  1022. });
  1023. },
  1024. // 修改 proceedToNextQuestion 方法
  1025. proceedToNextQuestion() {
  1026. if (this.currentVideoIndex === 4) {
  1027. common_vendor.index.navigateTo({
  1028. url: "/pages/camera/camera"
  1029. });
  1030. return;
  1031. }
  1032. this.currentVideoIndex++;
  1033. if (this.currentVideoIndex < this.videoList.length) {
  1034. this.videoUrl = this.videoList[this.currentVideoIndex];
  1035. this.videoPlaying = true;
  1036. this.currentSubtitle = "";
  1037. this.$nextTick(() => {
  1038. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1039. if (videoContext) {
  1040. videoContext.play();
  1041. }
  1042. });
  1043. } else {
  1044. common_vendor.index.showToast({
  1045. title: "面试完成",
  1046. icon: "success",
  1047. duration: 2e3
  1048. });
  1049. setTimeout(() => {
  1050. common_vendor.index.navigateTo({
  1051. url: "/pages/interview-result/interview-result"
  1052. });
  1053. }, 2e3);
  1054. }
  1055. },
  1056. // 修改 handleAnswerButtonClick 方法
  1057. handleAnswerButtonClick() {
  1058. this.showAnswerButton = false;
  1059. this.proceedToNextQuestion();
  1060. },
  1061. // 处理相机错误
  1062. handleCameraError(e) {
  1063. console.error("相机错误:", e);
  1064. common_vendor.index.showToast({
  1065. title: "相机初始化失败,请检查权限设置",
  1066. icon: "none"
  1067. });
  1068. this.tryFallbackOptions();
  1069. },
  1070. // 添加新方法:尝试备用选项
  1071. tryFallbackOptions() {
  1072. const systemInfo = common_vendor.index.getSystemInfoSync();
  1073. if (systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay") {
  1074. this.useMiniProgramCamera();
  1075. } else {
  1076. this.showStaticCameraPlaceholder();
  1077. }
  1078. },
  1079. // 添加新方法:使用小程序相机API
  1080. useMiniProgramCamera() {
  1081. console.log("尝试使用小程序相机组件");
  1082. this.useMiniProgramCameraComponent = true;
  1083. },
  1084. // 添加新方法:显示静态图像
  1085. showStaticCameraPlaceholder() {
  1086. console.log("显示静态摄像头占位图");
  1087. const img = document.createElement("img");
  1088. img.src = "/static/images/camera-placeholder.png";
  1089. img.className = "static-camera-image";
  1090. img.style.width = "100%";
  1091. img.style.height = "100%";
  1092. img.style.objectFit = "cover";
  1093. const container = this.$refs.userCameraVideo.parentNode;
  1094. container.appendChild(img);
  1095. },
  1096. // 处理视频时间更新事件
  1097. handleTimeUpdate(e) {
  1098. const currentTime = e.target.currentTime;
  1099. let currentSubtitles;
  1100. if (this.currentVideoIndex === 0) {
  1101. currentSubtitles = this.subtitles;
  1102. } else if (this.currentVideoIndex === 1) {
  1103. currentSubtitles = this.secondVideoSubtitles;
  1104. } else if (this.currentVideoIndex === 2) {
  1105. currentSubtitles = this.thirdVideoSubtitles;
  1106. } else if (this.currentVideoIndex === 3) {
  1107. currentSubtitles = this.fourthVideoSubtitles;
  1108. } else if (this.currentVideoIndex === 4) {
  1109. currentSubtitles = this.fifthVideoSubtitles;
  1110. } else if (this.currentVideoIndex === 5) {
  1111. currentSubtitles = this.sixthVideoSubtitles;
  1112. } else {
  1113. currentSubtitles = [];
  1114. }
  1115. const subtitle = currentSubtitles.find(
  1116. (sub) => currentTime >= sub.startTime && currentTime < sub.endTime
  1117. );
  1118. this.currentSubtitle = subtitle ? subtitle.text : "";
  1119. },
  1120. // Add a new method to handle the "Start Recording" button click
  1121. handleStartRecordingClick() {
  1122. this.showStartRecordingButton = false;
  1123. this.startRecordingAnswer();
  1124. },
  1125. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1126. checkAudioPermission() {
  1127. const systemInfo = common_vendor.index.getSystemInfoSync();
  1128. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1129. if (isMiniProgram) {
  1130. common_vendor.index.getSetting({
  1131. success: (res) => {
  1132. if (!res.authSetting["scope.record"]) {
  1133. common_vendor.index.authorize({
  1134. scope: "scope.record",
  1135. success: () => {
  1136. console.log("录音权限已获取");
  1137. },
  1138. fail: (err) => {
  1139. console.error("录音权限获取失败:", err);
  1140. this.showPermissionDialog("录音");
  1141. }
  1142. });
  1143. }
  1144. if (!res.authSetting["scope.camera"]) {
  1145. common_vendor.index.authorize({
  1146. scope: "scope.camera",
  1147. success: () => {
  1148. console.log("相机权限已获取");
  1149. },
  1150. fail: (err) => {
  1151. console.error("相机权限获取失败:", err);
  1152. this.showPermissionDialog("相机");
  1153. }
  1154. });
  1155. }
  1156. }
  1157. });
  1158. }
  1159. },
  1160. // 添加新方法:测试音频输入
  1161. testAudioInput() {
  1162. if (!this.cameraStream) {
  1163. console.warn("没有可用的媒体流,无法测试音频");
  1164. return;
  1165. }
  1166. const audioTracks = this.cameraStream.getAudioTracks();
  1167. if (audioTracks.length === 0) {
  1168. console.warn("没有检测到音频轨道,尝试重新获取");
  1169. this.tryGetAudioOnly();
  1170. return;
  1171. }
  1172. console.log("音频轨道信息:", audioTracks[0].getSettings());
  1173. try {
  1174. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1175. if (!AudioContext) {
  1176. console.warn("浏览器不支持AudioContext");
  1177. return;
  1178. }
  1179. const audioContext = new AudioContext();
  1180. const analyser = audioContext.createAnalyser();
  1181. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1182. microphone.connect(analyser);
  1183. analyser.fftSize = 256;
  1184. const bufferLength = analyser.frequencyBinCount;
  1185. const dataArray = new Uint8Array(bufferLength);
  1186. let silenceCounter = 0;
  1187. const checkAudio = () => {
  1188. if (this.isRecording)
  1189. return;
  1190. analyser.getByteFrequencyData(dataArray);
  1191. let sum = 0;
  1192. for (let i = 0; i < bufferLength; i++) {
  1193. sum += dataArray[i];
  1194. }
  1195. const average = sum / bufferLength;
  1196. if (average > 10) {
  1197. console.log("检测到音频输入,音量:", average);
  1198. silenceCounter = 0;
  1199. } else {
  1200. silenceCounter++;
  1201. if (silenceCounter > 10) {
  1202. console.warn("持续检测不到音频输入,可能麦克风未正常工作");
  1203. silenceCounter = 0;
  1204. }
  1205. }
  1206. requestAnimationFrame(checkAudio);
  1207. };
  1208. checkAudio();
  1209. } catch (e) {
  1210. console.error("音频测试失败:", e);
  1211. }
  1212. },
  1213. // 添加新方法:尝试单独获取音频
  1214. tryGetAudioOnly() {
  1215. navigator.mediaDevices.getUserMedia({ audio: true }).then((audioStream) => {
  1216. if (this.cameraStream) {
  1217. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1218. const audioTrack = audioStream.getAudioTracks()[0];
  1219. const combinedStream = new MediaStream();
  1220. if (videoTrack)
  1221. combinedStream.addTrack(videoTrack);
  1222. if (audioTrack)
  1223. combinedStream.addTrack(audioTrack);
  1224. this.cameraStream = combinedStream;
  1225. const videoElement = this.$refs.userCameraVideo;
  1226. if (videoElement) {
  1227. videoElement.srcObject = combinedStream;
  1228. videoElement.muted = true;
  1229. }
  1230. console.log("成功合并音频和视频轨道");
  1231. } else {
  1232. console.warn("没有视频流可合并");
  1233. }
  1234. }).catch((err) => {
  1235. console.error("单独获取音频失败:", err);
  1236. });
  1237. },
  1238. // 添加新方法:显示权限对话框
  1239. showPermissionDialog(permissionType) {
  1240. common_vendor.index.showModal({
  1241. title: "需要权限",
  1242. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1243. confirmText: "去设置",
  1244. success: (res) => {
  1245. if (res.confirm) {
  1246. common_vendor.index.openSetting({
  1247. success: (settingRes) => {
  1248. console.log("设置页面打开成功", settingRes);
  1249. }
  1250. });
  1251. }
  1252. }
  1253. });
  1254. },
  1255. // 添加重试上传方法
  1256. retryVideoUpload() {
  1257. if (this.lastVideoToRetry) {
  1258. this.showRetryButton = false;
  1259. common_vendor.index.showLoading({
  1260. title: "正在重新提交...",
  1261. mask: true
  1262. });
  1263. this.submitVideoToInterview(this.lastVideoToRetry);
  1264. } else {
  1265. common_vendor.index.showToast({
  1266. title: "没有可重试的视频",
  1267. icon: "none"
  1268. });
  1269. }
  1270. },
  1271. // 添加一个新方法用于压缩视频
  1272. async compressVideo(videoBlob) {
  1273. if (videoBlob.size < 5 * 1024 * 1024) {
  1274. return videoBlob;
  1275. }
  1276. console.log("开始压缩视频,原始大小:", videoBlob.size);
  1277. const videoElement = document.createElement("video");
  1278. videoElement.muted = true;
  1279. videoElement.autoplay = false;
  1280. const canvas = document.createElement("canvas");
  1281. const ctx = canvas.getContext("2d");
  1282. videoElement.src = URL.createObjectURL(videoBlob);
  1283. return new Promise((resolve) => {
  1284. videoElement.onloadedmetadata = () => {
  1285. const width = Math.floor(videoElement.videoWidth / 2);
  1286. const height = Math.floor(videoElement.videoHeight / 2);
  1287. canvas.width = width;
  1288. canvas.height = height;
  1289. const stream = canvas.captureStream(15);
  1290. if (videoElement.captureStream) {
  1291. const originalStream = videoElement.captureStream();
  1292. const audioTracks = originalStream.getAudioTracks();
  1293. if (audioTracks.length > 0) {
  1294. stream.addTrack(audioTracks[0]);
  1295. }
  1296. }
  1297. const options = {
  1298. mimeType: "video/webm;codecs=vp8,opus",
  1299. audioBitsPerSecond: 64e3,
  1300. videoBitsPerSecond: 8e5
  1301. // 800kbps
  1302. };
  1303. const mediaRecorder = new MediaRecorder(stream, options);
  1304. const chunks = [];
  1305. mediaRecorder.ondataavailable = (e) => {
  1306. if (e.data.size > 0) {
  1307. chunks.push(e.data);
  1308. }
  1309. };
  1310. mediaRecorder.onstop = () => {
  1311. const compressedBlob = new Blob(chunks, { type: "video/webm" });
  1312. console.log("视频压缩完成,压缩后大小:", compressedBlob.size);
  1313. resolve(compressedBlob);
  1314. };
  1315. videoElement.onplay = () => {
  1316. mediaRecorder.start(100);
  1317. const drawFrame = () => {
  1318. if (videoElement.paused || videoElement.ended) {
  1319. mediaRecorder.stop();
  1320. return;
  1321. }
  1322. ctx.drawImage(videoElement, 0, 0, width, height);
  1323. requestAnimationFrame(drawFrame);
  1324. };
  1325. drawFrame();
  1326. };
  1327. videoElement.play();
  1328. };
  1329. });
  1330. }
  1331. }
  1332. };
  1333. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  1334. return common_vendor.e({
  1335. a: $data.videoUrl,
  1336. b: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  1337. c: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  1338. d: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  1339. e: $data.currentSubtitle
  1340. }, $data.currentSubtitle ? {
  1341. f: common_vendor.t($data.currentSubtitle)
  1342. } : {}, {
  1343. g: $data.showAnswerButton
  1344. }, $data.showAnswerButton ? {
  1345. h: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  1346. } : {}, {
  1347. i: $data.useMiniProgramCameraComponent
  1348. }, $data.useMiniProgramCameraComponent ? {
  1349. j: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  1350. } : {}, {
  1351. k: $data.loading
  1352. }, $data.loading ? {} : {}, {
  1353. l: $data.showDebugInfo
  1354. }, $data.showDebugInfo ? common_vendor.e({
  1355. m: $data.assistantResponse
  1356. }, $data.assistantResponse ? {
  1357. n: common_vendor.t($data.assistantResponse)
  1358. } : {}, {
  1359. o: $data.audioTranscript
  1360. }, $data.audioTranscript ? {
  1361. p: common_vendor.t($data.audioTranscript)
  1362. } : {}, {
  1363. q: common_vendor.f($data.processedResponses, (item, index, i0) => {
  1364. return common_vendor.e({
  1365. a: item.role
  1366. }, item.role ? {
  1367. b: common_vendor.t(item.role)
  1368. } : {}, {
  1369. c: item.transcript
  1370. }, item.transcript ? {
  1371. d: common_vendor.t(item.transcript)
  1372. } : {}, {
  1373. e: index
  1374. });
  1375. })
  1376. }) : {}, {
  1377. r: $data.showStopRecordingButton
  1378. }, $data.showStopRecordingButton ? {
  1379. s: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  1380. } : {}, {
  1381. t: $data.isRecording
  1382. }, $data.isRecording ? {} : {}, {
  1383. v: $data.showStartRecordingButton
  1384. }, $data.showStartRecordingButton ? {
  1385. w: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  1386. } : {}, {
  1387. x: $data.showRetryButton
  1388. }, $data.showRetryButton ? {
  1389. y: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  1390. } : {});
  1391. }
  1392. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-464e78c6"]]);
  1393. wx.createPage(MiniProgramPage);