interview-question.js 72 KB

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