identity-verify.js 58 KB

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