identity-verify.js 64 KB

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