identity-verify.js 55 KB

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