identity-verify.js 63 KB

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