identity-verify.js 64 KB

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