interview-question.js 77 KB

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