identity-verify.js 56 KB

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