identity-verify.js 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086
  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. followUpQuestion: "",
  9. // 追问问题
  10. followUpAudioUrl: "",
  11. // 追问音频URL
  12. audioContext: null,
  13. // 音频上下文
  14. followUpQuestions: [],
  15. // 追问问题列表
  16. isWaitingForAnswer: false,
  17. // 添加新的状态来控制是否在等待用户回答
  18. currentFollowUpIndex: -1,
  19. // 当前追问问题索引
  20. showSubtitleText: false,
  21. // 是否显示字幕
  22. subtitleText: "",
  23. // 字幕文本
  24. parentQuestion: "",
  25. // 父问题文本
  26. isFollowUpQuestion: false,
  27. // 是否是追问问题
  28. loading: false,
  29. responses: [],
  30. processedResponses: [],
  31. assistantResponse: "",
  32. audioTranscript: "",
  33. videoPlaying: false,
  34. showDebugInfo: false,
  35. // 设置为true可以显示调试信息
  36. videoUrl: "",
  37. // 修改为空字符串,将通过API获取
  38. showReplayButton: false,
  39. cameraStream: null,
  40. // 存储摄像头流
  41. cameraError: null,
  42. // 存储摄像头错误信息
  43. useMiniProgramCameraComponent: false,
  44. // 添加小程序相机组件标志
  45. cameraContext: null,
  46. // 添加相机上下文
  47. currentSubtitle: "",
  48. currentTranslation: "",
  49. // 添加翻译文本
  50. subtitles: [],
  51. // 修改为空数组,将通过API获取
  52. // 移除硬编码的字幕数组
  53. // secondVideoSubtitles: [...],
  54. // thirdVideoSubtitles: [...],
  55. // 等等...
  56. showAnswerButton: false,
  57. // 控制答题按钮显示
  58. currentVideoIndex: 0,
  59. // 当前播放的视频索引
  60. videoList: [],
  61. // 修改为空数组,将通过API获取
  62. // 其他属性保持不变...
  63. questions: [],
  64. // 添加新属性存储API返回的问题数据
  65. introVideoUrl: (() => {
  66. try {
  67. const configStr = common_vendor.index.getStorageSync("configData");
  68. if (configStr && configStr.trim()) {
  69. return JSON.parse(configStr).digital_human_opening_video_url || "https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4";
  70. }
  71. return "https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4";
  72. } catch (error) {
  73. console.warn("解析配置数据失败:", error);
  74. return {};
  75. }
  76. })(),
  77. //'https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4', // 保留介绍视频
  78. isRecording: false,
  79. recordingTimer: null,
  80. showStopRecordingButton: false,
  81. mediaRecorder: null,
  82. recordedChunks: [],
  83. recorder: null,
  84. lastUploadedVideoUrl: "",
  85. showStartRecordingButton: false,
  86. showRetryButton: false,
  87. // 控制重试按钮显示
  88. lastVideoToRetry: null,
  89. // 存储上次失败的视频URL,用于重试
  90. recordingStartTime: null,
  91. // 录制开始时间
  92. recordingTimerCount: 0,
  93. // 录制计时器计数
  94. recordingTimeDisplay: "00:00",
  95. // 格式化的录制时间显示
  96. // 添加上传队列相关数据
  97. uploadQueue: [],
  98. // 存储待上传的视频
  99. isUploading: false,
  100. // 标记是否正在上传
  101. uploadProgress: {},
  102. // 存储每个视频的上传进度
  103. uploadStatus: {},
  104. // 存储每个视频的上传状态
  105. showUploadStatus: false,
  106. // 是否显示上传状态指示器
  107. uploadStatusText: "",
  108. // 上传状态文本
  109. mediaRecorderTimeout: null,
  110. // 用于存储MediaRecorder的超时机制
  111. maxRecordingTime: 300,
  112. // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  113. remainingTime: 300,
  114. // 剩余录制时间(秒)- 从60秒改为300秒
  115. countdownValue: 10,
  116. // 倒计时数值
  117. showCountdown: false,
  118. // 是否显示倒计时蒙层
  119. countdownTimer: null,
  120. showGif: false,
  121. // 控制是否显示GIF
  122. gifUrl: "",
  123. // GIF图片的URL
  124. globalSocketTask: null,
  125. // 添加全局 WebSocket 连接对象
  126. lowScoreVideoUrl: "https://data.qicai321.com/minlong/latentsync/0530e7f5-1957-422d-8f34-ba4a92608081_result.mp4",
  127. // 低分提示视频URL
  128. showRerecordButton: false,
  129. // 控制重新录制按钮显示
  130. isPlayingLowScoreVideo: false,
  131. // 标记是否正在播放低分提示视频
  132. lowScoreVideoSubtitles: [
  133. {
  134. startTime: 0,
  135. endTime: 10,
  136. // 假设视频长度为10秒,可根据实际情况调整
  137. text: "我未听清楚您在说什么,请您再说一遍!"
  138. }
  139. ],
  140. // 用于存储低分提示视频的字幕
  141. retryCount: 0,
  142. // 添加重试次数计数器
  143. maxRetryAttempts: 2,
  144. // 最大重试次数限制
  145. needPlayLowScoreVideo: false,
  146. // 是否需要播放低分视频
  147. finalRecordingDuration: 0,
  148. // 最终录制时长
  149. historyTime: 0,
  150. // 添加历史时间记录
  151. followUpQuestions: [],
  152. // 存储追问问题数据
  153. currentParentQuestionId: null,
  154. // 当前父问题ID
  155. hasPlayedFollowUp: {},
  156. // 记录已播放的追问,格式: {questionId: true}
  157. isFollowUpQuestion: false,
  158. // 标记当前是否正在播放追问问题
  159. currentFollowUpQuestion: null,
  160. // 添加当前追问问题的完整信息
  161. lastQuestionWasFollowUp: false,
  162. lastFollowUpQuestionId: null,
  163. lastUpdateTime: Date.now(),
  164. // 添加最后更新时间戳
  165. subtitleMap: {},
  166. // 用于存储字幕和翻译的映射
  167. progressPercent: 0,
  168. // 录制进度百分比
  169. progressColor: "#05dc8b",
  170. // 进度条颜色
  171. progressBgColor: "rgba(0, 0, 0,0.3)",
  172. // 进度条背景色
  173. parentQuestion: "",
  174. // 添加父问题存储
  175. screenCaptureCount: 0,
  176. // 添加截屏次数记录
  177. personDetectionSocket: null,
  178. // WebSocket对象
  179. personDetectionInterval: null,
  180. // 定时器对象
  181. showCameraWarning: false,
  182. // 添加新的数据属性
  183. showPageWarning: false,
  184. // 添加新的数据属性
  185. followUpQuestion: "",
  186. // 追问问题
  187. followUpAudioUrl: "",
  188. // 追问音频URL
  189. audioContext: null,
  190. // 音频上下文
  191. followUpQuestions: [],
  192. // 追问问题列表
  193. currentFollowUpIndex: -1,
  194. // 当前追问问题索引
  195. showSubtitleText: false,
  196. // 是否显示字幕
  197. subtitleText: "",
  198. // 字幕文本
  199. isAudioPlaying: false,
  200. isWaitingForAnswer: false
  201. // 添加新的状态来控制是否在等待用户回答
  202. };
  203. },
  204. mounted() {
  205. this.fetchQuestions();
  206. this.fetchFollowUpQuestions();
  207. this.checkAudioPermission();
  208. this.initCamera();
  209. this.checkIOSCameraRecordPermission();
  210. this.checkAndFixRenderingIssues();
  211. setTimeout(() => {
  212. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  213. this.testAudioInput();
  214. }
  215. }, 3e3);
  216. this.historyTime = 0;
  217. common_vendor.index.setKeepScreenOn({
  218. keepScreenOn: true
  219. });
  220. common_vendor.index.onUserCaptureScreen(() => {
  221. console.log("User captured screen");
  222. this.screenCaptureCount++;
  223. if (this.screenCaptureCount === 1) {
  224. common_vendor.index.showModal({
  225. title: "Warning",
  226. content: "Screen capture detected. If you capture the screen again, your interview will be invalid.",
  227. showCancel: false,
  228. confirmText: "OK"
  229. });
  230. } else if (this.screenCaptureCount >= 2) {
  231. common_vendor.index.showModal({
  232. title: "Interview Invalid",
  233. content: "Your interview has been invalidated due to multiple screen captures. Please contact the recruiter for assistance.",
  234. showCancel: false,
  235. confirmText: "OK",
  236. success: () => {
  237. this.invalidateInterview();
  238. }
  239. });
  240. }
  241. });
  242. const systemInfo = common_vendor.index.getSystemInfoSync();
  243. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  244. if (isMiniProgram) {
  245. this.initPersonDetectionWebSocket();
  246. }
  247. },
  248. beforeDestroy() {
  249. common_vendor.index.offUserCaptureScreen();
  250. this.cleanupPersonDetectionWebSocket();
  251. },
  252. methods: {
  253. // 处理音频播放完成
  254. handleAudioEnd() {
  255. console.log("音频播放完成");
  256. this.isWaitingForAnswer = true;
  257. this.showStartRecordingButton = true;
  258. setTimeout(() => {
  259. this.startCountdown();
  260. }, 100);
  261. },
  262. // 开始倒计时
  263. startCountdown() {
  264. this.showCountdown = true;
  265. this.countdownValue = 5;
  266. this.countdownTimer = setInterval(() => {
  267. this.countdownValue--;
  268. if (this.countdownValue <= 0) {
  269. this.clearCountdown();
  270. this.showStartRecordingButton = false;
  271. this.startRecordingAnswer();
  272. }
  273. }, 1e3);
  274. },
  275. // 清除倒计时
  276. clearCountdown() {
  277. if (this.countdownTimer) {
  278. clearInterval(this.countdownTimer);
  279. this.countdownTimer = null;
  280. }
  281. this.showCountdown = false;
  282. },
  283. // 处理追问问题
  284. async handleFollowUpQuestion(questionData) {
  285. console.log("处理追问问题数据:", questionData);
  286. if (!questionData || !questionData.follow_up_voice_url || !questionData.follow_up_question) {
  287. console.error("追问数据不完整:", questionData);
  288. common_vendor.index.showToast({
  289. title: "获取追问数据失败",
  290. icon: "none"
  291. });
  292. return;
  293. }
  294. this.parentQuestion = questionData.original_question || "";
  295. let audioUrl = "";
  296. if (questionData.follow_up_voice_url.direct_url) {
  297. audioUrl = questionData.follow_up_voice_url.direct_url;
  298. } else if (questionData.follow_up_voice_url.file_url) {
  299. audioUrl = questionData.follow_up_voice_url.file_url;
  300. }
  301. if (!audioUrl.startsWith("http")) {
  302. audioUrl = common_config.apiBaseUrl + audioUrl;
  303. }
  304. console.log("处理后的音频URL:", audioUrl);
  305. const followUpQuestionObj = {
  306. id: Date.now(),
  307. question: questionData.follow_up_question.trim(),
  308. // 去除可能的空白字符
  309. audioUrl,
  310. originalQuestion: questionData.original_question,
  311. sessionId: questionData.session_id
  312. };
  313. console.log("创建的追问问题对象:", followUpQuestionObj);
  314. this.followUpQuestions.push(followUpQuestionObj);
  315. this.currentFollowUpIndex = this.followUpQuestions.length - 1;
  316. this.followUpQuestion = followUpQuestionObj.question;
  317. this.followUpAudioUrl = followUpQuestionObj.audioUrl;
  318. this.isFollowUpQuestion = true;
  319. this.currentFollowUpQuestion = followUpQuestionObj;
  320. this.showSubtitle(followUpQuestionObj.question, true);
  321. try {
  322. await this.playFollowUpAudio();
  323. console.log("音频播放完成,准备录制回答");
  324. this.prepareToAnswer(true, followUpQuestionObj);
  325. } catch (error) {
  326. console.error("音频播放失败:", error);
  327. this.prepareToAnswer(true, followUpQuestionObj);
  328. }
  329. },
  330. // 准备回答问题
  331. prepareToAnswer(isFollowUp = false, questionData = null) {
  332. console.log("准备回答问题:", { isFollowUp, questionData });
  333. this.isRecording = false;
  334. this.recordedTime = 0;
  335. this.showStopRecordingButton = false;
  336. if (isFollowUp && questionData) {
  337. this.currentFollowUpQuestion = questionData;
  338. this.isFollowUpQuestion = true;
  339. }
  340. this.isWaitingForAnswer = true;
  341. this.handleAudioEnd();
  342. },
  343. // 调用面试互动接口
  344. async callInterviewInteraction(questionId) {
  345. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  346. const appId = common_vendor.index.getStorageSync("appId");
  347. try {
  348. console.log("开始调用面试互动接口", { questionId, appId });
  349. const res = await common_vendor.index.request({
  350. url: `${common_config.apiBaseUrl}/api/voice_interview_interaction/`,
  351. method: "POST",
  352. data: {
  353. tenant_id: userInfo.tenant_id || 1,
  354. question_id: questionId,
  355. position_config_id: 6,
  356. application_id: appId
  357. },
  358. header: {
  359. "content-type": "application/json"
  360. }
  361. });
  362. console.log("面试互动接口返回数据:", res);
  363. if (res.data.success) {
  364. await this.handleFollowUpQuestion(res.data);
  365. } else {
  366. console.error("面试互动接口返回错误:", res.data);
  367. common_vendor.index.showToast({
  368. title: "获取追问失败",
  369. icon: "none"
  370. });
  371. }
  372. } catch (error) {
  373. console.error("调用面试互动接口失败:", error);
  374. }
  375. },
  376. // 播放追问音频
  377. async playFollowUpAudio() {
  378. return new Promise((resolve, reject) => {
  379. console.log("开始播放追问音频, URL:", this.followUpAudioUrl);
  380. if (!this.followUpAudioUrl) {
  381. console.error("没有音频URL");
  382. common_vendor.index.showToast({
  383. title: "音频URL无效",
  384. icon: "none"
  385. });
  386. reject(new Error("没有音频URL"));
  387. return;
  388. }
  389. common_vendor.index.showLoading({
  390. title: "加载音频中..."
  391. });
  392. this.stopAndDestroyAudio();
  393. try {
  394. const innerAudioContext = common_vendor.index.createInnerAudioContext();
  395. this.audioContext = innerAudioContext;
  396. innerAudioContext.autoplay = true;
  397. innerAudioContext.obeyMuteSwitch = false;
  398. innerAudioContext.volume = 1;
  399. innerAudioContext.onCanplay(() => {
  400. console.log("音频可以播放");
  401. common_vendor.index.hideLoading();
  402. this.isAudioPlaying = true;
  403. common_vendor.index.showToast({
  404. title: "正在播放追问",
  405. icon: "none",
  406. duration: 2e3
  407. });
  408. });
  409. innerAudioContext.onEnded(() => {
  410. console.log("追问音频播放完成");
  411. this.isAudioPlaying = false;
  412. resolve();
  413. setTimeout(() => {
  414. this.stopAndDestroyAudio();
  415. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  416. }, 100);
  417. });
  418. innerAudioContext.onError((res) => {
  419. console.error("音频播放错误:", res);
  420. this.isAudioPlaying = false;
  421. common_vendor.index.hideLoading();
  422. common_vendor.index.showToast({
  423. title: "音频播放失败",
  424. icon: "none"
  425. });
  426. reject(res);
  427. this.stopAndDestroyAudio();
  428. });
  429. console.log("设置音频源:", this.followUpAudioUrl);
  430. innerAudioContext.src = this.followUpAudioUrl;
  431. } catch (error) {
  432. console.error("创建或播放音频失败:", error);
  433. this.isAudioPlaying = false;
  434. common_vendor.index.hideLoading();
  435. common_vendor.index.showToast({
  436. title: "音频播放失败",
  437. icon: "none"
  438. });
  439. this.stopAndDestroyAudio();
  440. reject(error);
  441. }
  442. });
  443. },
  444. // 停止并销毁音频
  445. stopAndDestroyAudio() {
  446. if (!this.audioContext) {
  447. return;
  448. }
  449. const ctx = this.audioContext;
  450. this.audioContext = null;
  451. try {
  452. if (this.isAudioPlaying) {
  453. ctx.stop();
  454. }
  455. } catch (error) {
  456. console.error("停止音频播放失败:", error);
  457. }
  458. try {
  459. ctx.destroy();
  460. } catch (error) {
  461. console.error("销毁音频实例失败:", error);
  462. }
  463. this.isAudioPlaying = false;
  464. },
  465. // 绑定音频事件
  466. bindAudioEvents(audioContext, resolve, reject) {
  467. audioContext.onCanplay(() => {
  468. console.log("音频可以播放");
  469. common_vendor.index.showToast({
  470. title: "正在播放追问",
  471. icon: "none",
  472. duration: 2e3
  473. });
  474. });
  475. audioContext.onTimeUpdate(() => {
  476. console.log("音频播放进度:", audioContext.currentTime);
  477. });
  478. audioContext.onEnded(() => {
  479. console.log("追问音频播放完成");
  480. this.cleanupAudioContext();
  481. resolve();
  482. });
  483. audioContext.onError((res) => {
  484. console.error("音频播放错误:", res);
  485. common_vendor.index.showToast({
  486. title: "音频播放失败",
  487. icon: "none"
  488. });
  489. this.cleanupAudioContext();
  490. reject(res);
  491. });
  492. },
  493. // 修改准备回答方法
  494. prepareToAnswer(isFollowUp = false, questionData = null) {
  495. this.isRecording = false;
  496. this.recordedTime = 0;
  497. this.showStopRecordingButton = false;
  498. if (isFollowUp && questionData) {
  499. this.currentFollowUpQuestion = questionData;
  500. this.isFollowUpQuestion = true;
  501. }
  502. this.isWaitingForAnswer = true;
  503. this.handleAudioEnd();
  504. },
  505. // 初始化相机
  506. async initCamera() {
  507. const systemInfo = common_vendor.index.getSystemInfoSync();
  508. const isMiniProgram = systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay" || systemInfo.uniPlatform === "mp-baidu";
  509. if (isMiniProgram) {
  510. this.useMiniProgramCameraComponent = true;
  511. this.cameraContext = common_vendor.index.createCameraContext();
  512. common_vendor.index.getSetting({
  513. success: (res) => {
  514. if (!res.authSetting["scope.record"]) {
  515. common_vendor.index.authorize({
  516. scope: "scope.record",
  517. success: () => {
  518. console.log("录音权限已获取");
  519. },
  520. fail: (err) => {
  521. console.error("录音权限获取失败:", err);
  522. this.showPermissionDialog("录音");
  523. }
  524. });
  525. }
  526. if (!res.authSetting["scope.camera"]) {
  527. common_vendor.index.authorize({
  528. scope: "scope.camera",
  529. success: () => {
  530. console.log("相机权限已获取");
  531. },
  532. fail: (err) => {
  533. console.error("相机权限获取失败:", err);
  534. this.showPermissionDialog("相机");
  535. }
  536. });
  537. }
  538. const systemInfo2 = common_vendor.index.getSystemInfoSync();
  539. if (systemInfo2.platform === "ios") {
  540. if (!res.authSetting["scope.camera"] || !res.authSetting["scope.record"]) {
  541. console.log("iOS需要同时获取相机和录音权限");
  542. }
  543. }
  544. }
  545. });
  546. } else {
  547. try {
  548. const constraints = {
  549. audio: {
  550. echoCancellation: true,
  551. noiseSuppression: true,
  552. autoGainControl: true
  553. },
  554. video: {
  555. width: { ideal: 640, max: 1280 },
  556. // 控制视频宽度
  557. height: { ideal: 480, max: 720 },
  558. // 控制视频高度
  559. frameRate: { ideal: 15, max: 24 },
  560. // 控制帧率
  561. facingMode: "user"
  562. }
  563. };
  564. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  565. this.cameraStream = stream;
  566. const audioTracks = stream.getAudioTracks();
  567. console.log("音频轨道数量:", audioTracks.length);
  568. if (audioTracks.length > 0) {
  569. console.log("音频轨道已获取:", audioTracks[0].label);
  570. audioTracks[0].enabled = true;
  571. } else {
  572. console.warn("未检测到音频轨道,尝试单独获取音频");
  573. this.tryGetAudioOnly();
  574. }
  575. const videoElement = this.$refs.userCameraVideo;
  576. if (videoElement) {
  577. videoElement.srcObject = stream;
  578. videoElement.muted = true;
  579. }
  580. } catch (error) {
  581. console.error("获取摄像头失败:", error);
  582. this.cameraError = error.message || "无法访问摄像头";
  583. common_vendor.index.showToast({
  584. title: "无法访问摄像头,请检查权限设置",
  585. icon: "none"
  586. });
  587. }
  588. }
  589. },
  590. // 停止用户摄像头
  591. stopUserCamera() {
  592. if (this.cameraStream) {
  593. this.cameraStream.getTracks().forEach((track) => {
  594. track.stop();
  595. });
  596. this.cameraStream = null;
  597. }
  598. },
  599. async fetchData() {
  600. this.loading = true;
  601. this.assistantResponse = "";
  602. this.audioTranscript = "";
  603. this.processedResponses = [];
  604. try {
  605. const requestTask = common_vendor.index.request({
  606. url: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
  607. method: "POST",
  608. header: {
  609. "Content-Type": "application/json",
  610. "Authorization": "Bearer sk-9e1ec73a7d97493b8613c63f06b6110c"
  611. },
  612. data: {
  613. "model": "qwen-omni-turbo",
  614. "messages": [
  615. {
  616. "role": "user",
  617. "content": [
  618. {
  619. "type": "input_audio",
  620. "input_audio": {
  621. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  622. "format": "wav"
  623. }
  624. },
  625. {
  626. "type": "text",
  627. "text": "这段音频在说什么"
  628. }
  629. ]
  630. }
  631. ],
  632. "stream": true,
  633. "stream_options": {
  634. "include_usage": true
  635. },
  636. "modalities": ["text", "audio"],
  637. "audio": { "voice": "Cherry", "format": "wav" }
  638. },
  639. success: (res) => {
  640. console.log("请求成功,响应数据:", res.data);
  641. if (typeof res.data === "string" && res.data.includes("data: {")) {
  642. const chunks = res.data.split("data: ").filter((chunk) => chunk.trim() !== "");
  643. chunks.forEach((chunk) => {
  644. this.handleStreamResponse(chunk);
  645. });
  646. } else {
  647. this.handleStreamResponse(res.data);
  648. }
  649. this.playDigitalHumanVideo();
  650. },
  651. fail: (err) => {
  652. console.error("请求失败:", err);
  653. },
  654. complete: () => {
  655. this.loading = false;
  656. }
  657. });
  658. } catch (error) {
  659. console.error("获取数据失败:", error);
  660. this.loading = false;
  661. }
  662. },
  663. handleStreamResponse(data) {
  664. if (typeof data === "string") {
  665. if (data === "[DONE]")
  666. return;
  667. try {
  668. const cleanData = data.trim();
  669. if (cleanData.startsWith("{") && cleanData.endsWith("}")) {
  670. const jsonData = JSON.parse(cleanData);
  671. this.processStreamChunk(jsonData);
  672. }
  673. } catch (e) {
  674. console.error("解析JSON失败:", e, "原始数据:", data);
  675. }
  676. } else {
  677. this.processStreamChunk(data);
  678. }
  679. },
  680. processStreamChunk(chunk) {
  681. if (chunk.choices && chunk.choices.length > 0) {
  682. const choice = chunk.choices[0];
  683. if (choice.delta && choice.delta.content) {
  684. this.assistantResponse += choice.delta.content;
  685. }
  686. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  687. this.audioTranscript += choice.delta.audio.transcript;
  688. }
  689. if (choice.delta) {
  690. const result = {};
  691. if (choice.delta.role) {
  692. result.role = choice.delta.role;
  693. }
  694. if (choice.delta.audio && choice.delta.audio.transcript) {
  695. result.transcript = choice.delta.audio.transcript;
  696. }
  697. if (Object.keys(result).length > 0) {
  698. this.processedResponses.push(result);
  699. }
  700. }
  701. }
  702. },
  703. processResponseData() {
  704. this.processedResponses = this.responses.map((item) => {
  705. const result = {};
  706. if (item.delta && item.delta.role) {
  707. result.role = item.delta.role;
  708. }
  709. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  710. result.transcript = item.delta.audio.transcript;
  711. }
  712. return result;
  713. }).filter((item) => Object.keys(item).length > 0);
  714. },
  715. // 播放数字人视频
  716. playDigitalHumanVideo() {
  717. if (this.videoList.length === 0) {
  718. this.fetchQuestions();
  719. return;
  720. }
  721. this.videoUrl = this.videoList[this.currentVideoIndex];
  722. this.videoPlaying = true;
  723. this.$nextTick(() => {
  724. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  725. if (videoContext) {
  726. videoContext.play();
  727. setTimeout(() => {
  728. if (this.videoPlaying && this.$refs.videoPlayer) {
  729. console.log("视频应该正在播放");
  730. } else {
  731. console.log("视频可能未成功播放,尝试替代方案");
  732. this.tryAlternativeVideoPath();
  733. }
  734. }, 1e3);
  735. } else {
  736. console.error("无法创建视频上下文");
  737. this.tryAlternativeVideoPath();
  738. }
  739. });
  740. },
  741. // 修改 tryAlternativeVideoPath 方法
  742. tryAlternativeVideoPath() {
  743. console.log("尝试使用替代路径");
  744. const alternativePaths = [
  745. "./static/demo.mp4",
  746. "../static/demo.mp4",
  747. "static/demo.mp4",
  748. "/static/demo.mp4",
  749. // 添加绝对路径
  750. `${window.location.origin}/static/demo.mp4`
  751. ];
  752. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  753. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  754. this.videoUrl = alternativePaths[nextPathIndex];
  755. console.log("尝试新路径:", this.videoUrl);
  756. this.$nextTick(() => {
  757. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  758. if (videoContext) {
  759. videoContext.stop();
  760. videoContext.play();
  761. setTimeout(() => {
  762. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  763. console.log("所有路径均失败,尝试使用uni.getVideoInfo检查视频");
  764. this.checkVideoWithAPI();
  765. }
  766. }, 1e3);
  767. }
  768. });
  769. },
  770. // 添加新方法:使用uni API检查视频
  771. checkVideoWithAPI() {
  772. common_vendor.index.getVideoInfo({
  773. src: "/static/demo.mp4",
  774. success: (res) => {
  775. console.log("视频信息获取成功:", res);
  776. this.videoUrl = "/static/demo.mp4";
  777. this.$nextTick(() => {
  778. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  779. if (videoContext) {
  780. videoContext.play();
  781. }
  782. });
  783. },
  784. fail: (err) => {
  785. console.error("视频信息获取失败:", err);
  786. this.fallbackToLocalVideo();
  787. }
  788. });
  789. },
  790. // 添加新方法:回退到本地视频
  791. fallbackToLocalVideo() {
  792. console.log("尝试使用本地视频资源");
  793. const platform = common_vendor.index.getSystemInfoSync().platform;
  794. if (platform === "android" || platform === "ios") {
  795. this.videoUrl = platform === "android" ? "android.resource://package_name/raw/demo" : "file:///assets/demo.mp4";
  796. this.$nextTick(() => {
  797. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  798. if (videoContext) {
  799. videoContext.play();
  800. }
  801. });
  802. } else {
  803. this.videoPlaying = false;
  804. common_vendor.index.showToast({
  805. title: "视频加载失败,显示静态图片",
  806. icon: "none"
  807. });
  808. }
  809. },
  810. // 修改 handleVideoError 方法
  811. handleVideoError(e) {
  812. console.error("视频加载错误:", e);
  813. if (e && e.detail) {
  814. console.error("详细错误信息:", e.detail);
  815. }
  816. common_vendor.index.getFileInfo({
  817. filePath: this.videoUrl.startsWith("/") ? this.videoUrl.substring(1) : this.videoUrl,
  818. success: (res) => {
  819. console.log("文件存在,大小:", res.size);
  820. this.tryDifferentFormat();
  821. },
  822. fail: (err) => {
  823. console.error("文件不存在或无法访问:", err);
  824. this.tryAlternativeVideoPath();
  825. }
  826. });
  827. common_vendor.index.showToast({
  828. title: "视频加载失败,请检查文件是否存在",
  829. icon: "none",
  830. duration: 2e3
  831. });
  832. },
  833. // 添加新方法:尝试不同格式
  834. tryDifferentFormat() {
  835. console.log("尝试不同的视频格式");
  836. const formats = [
  837. { ext: "mp4", mime: "video/mp4" },
  838. { ext: "webm", mime: "video/webm" },
  839. { ext: "ogg", mime: "video/ogg" },
  840. { ext: "mov", mime: "video/quicktime" }
  841. ];
  842. const currentPath = this.videoUrl;
  843. const basePath = currentPath.substring(0, currentPath.lastIndexOf(".")) || "/static/demo";
  844. let nextFormat = formats.find((f) => !currentPath.endsWith(f.ext));
  845. if (nextFormat) {
  846. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  847. console.log("尝试新格式:", this.videoUrl);
  848. this.$nextTick(() => {
  849. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  850. if (videoContext) {
  851. videoContext.stop();
  852. videoContext.play();
  853. }
  854. });
  855. } else {
  856. this.useBuiltInResource();
  857. }
  858. },
  859. // 添加新方法:使用内置资源
  860. useBuiltInResource() {
  861. console.log("尝试使用内置资源");
  862. const platform = common_vendor.index.getSystemInfoSync().platform;
  863. if (platform === "windows") {
  864. process.env.UNI_INPUT_DIR || "";
  865. this.videoUrl = `./static/demo.mp4`;
  866. console.log("Windows平台尝试路径:", this.videoUrl);
  867. } else if (platform === "android" || platform === "ios") {
  868. this.useNativeVideo();
  869. } else {
  870. const baseUrl = window.location.origin;
  871. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  872. console.log("Web平台尝试URL:", this.videoUrl);
  873. }
  874. this.$nextTick(() => {
  875. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  876. if (videoContext) {
  877. videoContext.play();
  878. }
  879. });
  880. },
  881. // 添加新方法:使用原生视频能力
  882. useNativeVideo() {
  883. console.log("尝试使用原生视频能力");
  884. common_vendor.index.chooseVideo({
  885. sourceType: ["album"],
  886. success: (res) => {
  887. this.videoUrl = res.tempFilePath;
  888. this.$nextTick(() => {
  889. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  890. if (videoContext) {
  891. videoContext.play();
  892. }
  893. });
  894. },
  895. fail: () => {
  896. this.videoPlaying = false;
  897. common_vendor.index.showToast({
  898. title: "无法加载视频,显示静态图片",
  899. icon: "none"
  900. });
  901. }
  902. });
  903. },
  904. // 处理视频结束事件
  905. handleVideoEnded() {
  906. console.log("视频播放结束事件触发");
  907. const videoElement = this.$refs.videoPlayer;
  908. if (videoElement) {
  909. const currentTime = videoElement.currentTime || 0;
  910. const duration = videoElement.duration || 0;
  911. if (duration > 0 && duration - currentTime > 0.5) {
  912. console.log("检测到可能的误触发结束事件,继续播放", currentTime, duration);
  913. setTimeout(() => {
  914. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  915. if (videoContext) {
  916. if (duration - currentTime > 2) {
  917. videoContext.seek(this.historyTime || 0);
  918. } else {
  919. videoContext.play();
  920. }
  921. }
  922. }, 100);
  923. return;
  924. }
  925. console.log("视频确认结束,当前时间:", currentTime, "总时长:", duration);
  926. }
  927. this.videoPlaying = false;
  928. if (this.isPlayingLowScoreVideo) {
  929. console.log("低分提示视频播放完成");
  930. if (this.retryCount < this.maxRetryAttempts) {
  931. console.log(`显示重新录制按钮 (重试次数: ${this.retryCount + 1}/${this.maxRetryAttempts})`);
  932. this.showRerecordButton = true;
  933. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  934. this.currentSubtitle = this.currentFollowUpQuestion.digital_human_video_subtitle || this.currentFollowUpQuestion.question;
  935. }
  936. } else {
  937. console.log("已达到最大重试次数,继续下一题");
  938. this.retryCount = 0;
  939. this.proceedToNextQuestion();
  940. }
  941. this.isPlayingLowScoreVideo = false;
  942. return;
  943. }
  944. if (this.isFollowUpQuestion) {
  945. console.log("追问问题视频播放完成");
  946. this.showStartRecordingButton = true;
  947. this.startCountdown();
  948. return;
  949. }
  950. if (this.currentVideoIndex >= 1) {
  951. this.showStartRecordingButton = true;
  952. this.startCountdown();
  953. } else {
  954. this.showAnswerButton = true;
  955. }
  956. if (this.isFollowUpQuestion) {
  957. this.isFollowUpQuestion = false;
  958. this.currentFollowUpQuestion = null;
  959. }
  960. },
  961. // 添加新方法:开始倒计时
  962. startCountdown() {
  963. this.clearCountdown();
  964. this.showCountdown = true;
  965. this.countdownValue = 10;
  966. this.countdownTimer = setInterval(() => {
  967. if (!this.showStartRecordingButton) {
  968. this.clearCountdown();
  969. return;
  970. }
  971. this.countdownValue--;
  972. if (this.countdownValue <= 0) {
  973. this.clearCountdown();
  974. this.showStartRecordingButton = false;
  975. this.startRecordingAnswer();
  976. }
  977. }, 1e3);
  978. },
  979. // 添加新方法:清除倒计时
  980. clearCountdown() {
  981. if (this.countdownTimer) {
  982. clearInterval(this.countdownTimer);
  983. this.countdownTimer = null;
  984. }
  985. this.showCountdown = false;
  986. },
  987. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  988. handleStartRecordingClick() {
  989. this.showStartRecordingButton = false;
  990. this.clearCountdown();
  991. this.isWaitingForAnswer = false;
  992. this.startRecordingAnswer();
  993. },
  994. // 修改 stopRecordingAnswer 方法,添加录制时长检查
  995. stopRecordingAnswer() {
  996. this.clearCountdown();
  997. this.isWaitingForAnswer = false;
  998. this.isWaitingForAnswer = false;
  999. const recordingDuration = this.getRecordingDuration();
  1000. const minimumDuration = 3;
  1001. const lowScoreDuration = 7;
  1002. if (recordingDuration < minimumDuration) {
  1003. common_vendor.index.showToast({
  1004. title: "录制时间过短,请至少录制3秒",
  1005. icon: "none",
  1006. duration: 2e3
  1007. });
  1008. return;
  1009. }
  1010. this.completeRecordingStop();
  1011. if (recordingDuration < lowScoreDuration) {
  1012. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  1013. this.needPlayLowScoreVideo = true;
  1014. } else {
  1015. this.needPlayLowScoreVideo = false;
  1016. }
  1017. },
  1018. // 添加新方法:开始录制用户回答
  1019. startRecordingAnswer() {
  1020. console.log("开始录制用户回答");
  1021. this.isRecording = true;
  1022. this.recordingStartTime = Date.now();
  1023. this.recordingTimerCount = 0;
  1024. this.remainingTime = this.maxRecordingTime;
  1025. this.recordingTimer = setInterval(() => {
  1026. this.recordingTimerCount++;
  1027. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  1028. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1029. this.progressPercent = this.recordingTimerCount / this.maxRecordingTime * 100;
  1030. if (this.recordingTimerCount >= this.maxRecordingTime) {
  1031. console.log("已达到最大录制时间(5分钟),自动停止录制");
  1032. this.stopRecordingAnswer();
  1033. }
  1034. }, 1e3);
  1035. const systemInfo = common_vendor.index.getSystemInfoSync();
  1036. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1037. if (isMiniProgram) {
  1038. this.startMiniProgramRecording();
  1039. } else {
  1040. this.startBrowserRecording();
  1041. }
  1042. this.showStopRecordingButton = true;
  1043. },
  1044. // 添加一个新方法:重置相机组件
  1045. resetCamera() {
  1046. console.log("重置相机组件");
  1047. this.useMiniProgramCameraComponent = false;
  1048. if (this.cameraContext) {
  1049. this.cameraContext = null;
  1050. }
  1051. setTimeout(() => {
  1052. this.useMiniProgramCameraComponent = true;
  1053. setTimeout(() => {
  1054. this.cameraContext = common_vendor.index.createCameraContext();
  1055. console.log("相机组件已重置");
  1056. }, 500);
  1057. }, 500);
  1058. },
  1059. // 修改 startMiniProgramRecording 方法
  1060. startMiniProgramRecording() {
  1061. console.log("开始小程序录制方法");
  1062. const systemInfo = common_vendor.index.getSystemInfoSync();
  1063. const isIOS = systemInfo.platform === "ios";
  1064. if (isIOS) {
  1065. this.resetCamera();
  1066. setTimeout(() => {
  1067. this.actualStartRecording(isIOS);
  1068. }, 1e3);
  1069. } else {
  1070. this.actualStartRecording(isIOS);
  1071. }
  1072. },
  1073. // 添加新方法:实际开始录制
  1074. actualStartRecording(isIOS) {
  1075. if (!this.cameraContext) {
  1076. this.cameraContext = common_vendor.index.createCameraContext();
  1077. console.log("创建新的相机上下文");
  1078. }
  1079. common_vendor.index.getSetting({
  1080. success: (res) => {
  1081. const hasRecordAuth = res.authSetting["scope.record"];
  1082. const hasCameraAuth = res.authSetting["scope.camera"];
  1083. if (!hasRecordAuth || !hasCameraAuth) {
  1084. console.warn("缺少必要权限,请求权限");
  1085. this.requestMiniProgramPermissions();
  1086. return;
  1087. }
  1088. if (isIOS) {
  1089. console.log("iOS: 检查相机状态");
  1090. const options = {
  1091. timeout: 3e5,
  1092. // 300秒超时 (5分钟)
  1093. quality: "low",
  1094. // 降低质量
  1095. compressed: true,
  1096. success: () => {
  1097. console.log("iOS录制开始成功");
  1098. },
  1099. fail: (err) => {
  1100. console.error("iOS录制失败:", err);
  1101. this.useAlternativeRecordingMethod();
  1102. }
  1103. };
  1104. try {
  1105. console.log("尝试开始录制");
  1106. this.recorder = this.cameraContext.startRecord(options);
  1107. } catch (e) {
  1108. console.error("开始录制异常:", e);
  1109. this.useAlternativeRecordingMethod();
  1110. }
  1111. } else {
  1112. const options = {
  1113. timeout: 3e5,
  1114. // 300秒超时 (5分钟)
  1115. quality: "medium",
  1116. compressed: true,
  1117. success: () => {
  1118. console.log("Android录制开始成功");
  1119. },
  1120. fail: (err) => {
  1121. console.error("Android录制失败:", err);
  1122. common_vendor.index.showToast({
  1123. title: "录制失败,请检查相机权限",
  1124. icon: "none"
  1125. });
  1126. this.proceedToNextQuestion();
  1127. }
  1128. };
  1129. this.recorder = this.cameraContext.startRecord(options);
  1130. }
  1131. }
  1132. });
  1133. },
  1134. // 添加新方法:使用替代录制方法
  1135. useAlternativeRecordingMethod() {
  1136. console.log("使用替代录制方法");
  1137. common_vendor.index.showActionSheet({
  1138. itemList: ["使用相册中的视频", "跳过此问题"],
  1139. success: (res) => {
  1140. if (res.tapIndex === 0) {
  1141. common_vendor.index.chooseVideo({
  1142. sourceType: ["album"],
  1143. maxDuration: 300,
  1144. // 从60秒改为300秒
  1145. camera: "front",
  1146. success: (res2) => {
  1147. console.log("选择视频成功:", res2.tempFilePath);
  1148. this.isRecording = false;
  1149. this.showStopRecordingButton = false;
  1150. this.uploadRecordedVideo(res2.tempFilePath);
  1151. },
  1152. fail: () => {
  1153. console.log("用户取消选择视频");
  1154. this.proceedToNextQuestion();
  1155. }
  1156. });
  1157. } else {
  1158. console.log("用户选择跳过问题");
  1159. this.proceedToNextQuestion();
  1160. }
  1161. },
  1162. fail: () => {
  1163. console.log("操作取消");
  1164. this.proceedToNextQuestion();
  1165. }
  1166. });
  1167. },
  1168. // 添加新方法:请求小程序权限
  1169. requestMiniProgramPermissions() {
  1170. common_vendor.index.authorize({
  1171. scope: "scope.record",
  1172. success: () => {
  1173. console.log("录音权限已获取");
  1174. common_vendor.index.authorize({
  1175. scope: "scope.camera",
  1176. success: () => {
  1177. console.log("相机权限已获取");
  1178. this.startMiniProgramRecording();
  1179. },
  1180. fail: (err) => {
  1181. console.error("相机权限获取失败:", err);
  1182. this.showPermissionDialog("相机");
  1183. }
  1184. });
  1185. },
  1186. fail: (err) => {
  1187. console.error("录音权限获取失败:", err);
  1188. this.showPermissionDialog("录音");
  1189. }
  1190. });
  1191. },
  1192. // 修改浏览器环境下的录制方法
  1193. startBrowserRecording() {
  1194. if (!this.cameraStream) {
  1195. console.error("没有可用的摄像头流");
  1196. common_vendor.index.showToast({
  1197. title: "录制失败,摄像头未就绪",
  1198. icon: "none"
  1199. });
  1200. this.proceedToNextQuestion();
  1201. return;
  1202. }
  1203. try {
  1204. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  1205. if (!hasAudio) {
  1206. console.warn("警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流");
  1207. navigator.mediaDevices.getUserMedia({
  1208. audio: {
  1209. echoCancellation: true,
  1210. noiseSuppression: true,
  1211. autoGainControl: true
  1212. },
  1213. video: true
  1214. }).then((newStream) => {
  1215. const audioTracks = newStream.getAudioTracks();
  1216. if (audioTracks.length > 0) {
  1217. console.log("成功获取音频轨道:", audioTracks[0].label);
  1218. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1219. const audioTrack = newStream.getAudioTracks()[0];
  1220. const combinedStream = new MediaStream();
  1221. if (videoTrack)
  1222. combinedStream.addTrack(videoTrack);
  1223. if (audioTrack)
  1224. combinedStream.addTrack(audioTrack);
  1225. this.cameraStream = combinedStream;
  1226. const videoElement = this.$refs.userCameraVideo;
  1227. if (videoElement) {
  1228. videoElement.srcObject = combinedStream;
  1229. videoElement.muted = true;
  1230. }
  1231. this.setupMediaRecorder(combinedStream);
  1232. } else {
  1233. console.warn("仍然无法获取音频轨道");
  1234. this.setupMediaRecorder(this.cameraStream);
  1235. }
  1236. }).catch((err) => {
  1237. console.error("获取音频失败:", err);
  1238. this.setupMediaRecorder(this.cameraStream);
  1239. });
  1240. } else {
  1241. console.log("检测到音频轨道,直接使用");
  1242. this.setupMediaRecorder(this.cameraStream);
  1243. }
  1244. } catch (error) {
  1245. console.error("浏览器录制失败:", error);
  1246. common_vendor.index.showToast({
  1247. title: "录制失败,浏览器可能不支持此功能",
  1248. icon: "none"
  1249. });
  1250. this.proceedToNextQuestion();
  1251. }
  1252. },
  1253. // 修改 setupMediaRecorder 方法
  1254. setupMediaRecorder(stream) {
  1255. const videoTracks = stream.getVideoTracks();
  1256. const audioTracks = stream.getAudioTracks();
  1257. console.log("设置MediaRecorder - 视频轨道:", videoTracks.length, "音频轨道:", audioTracks.length);
  1258. let mimeType = "";
  1259. const supportedTypes = [
  1260. "video/webm;codecs=vp9,opus",
  1261. "video/webm;codecs=vp8,opus",
  1262. "video/webm;codecs=h264,opus",
  1263. "video/mp4;codecs=h264,aac",
  1264. "video/webm",
  1265. "video/mp4"
  1266. ];
  1267. for (const type of supportedTypes) {
  1268. if (MediaRecorder.isTypeSupported(type)) {
  1269. mimeType = type;
  1270. console.log("使用支持的MIME类型:", mimeType);
  1271. break;
  1272. }
  1273. }
  1274. const options = {
  1275. mimeType: mimeType || "",
  1276. audioBitsPerSecond: 64e3,
  1277. // 降低音频比特率
  1278. videoBitsPerSecond: 1e6
  1279. // 降低视频比特率到1Mbps
  1280. };
  1281. try {
  1282. this.mediaRecorder = new MediaRecorder(stream, options);
  1283. console.log("MediaRecorder创建成功,使用选项:", options);
  1284. } catch (e) {
  1285. console.warn("使用指定选项创建MediaRecorder失败,尝试使用默认选项");
  1286. this.mediaRecorder = new MediaRecorder(stream);
  1287. }
  1288. this.recordedChunks = [];
  1289. this.mediaRecorder.ondataavailable = (event) => {
  1290. if (event.data && event.data.size > 0) {
  1291. this.recordedChunks.push(event.data);
  1292. console.log(`收到数据块: ${event.data.size} 字节`);
  1293. }
  1294. };
  1295. this.mediaRecorder.onstop = async () => {
  1296. console.log("MediaRecorder停止,数据块数量:", this.recordedChunks.length);
  1297. if (this.recordedChunks.length === 0) {
  1298. console.error("没有录制到数据");
  1299. common_vendor.index.showToast({
  1300. title: "录制失败,未捕获到数据",
  1301. icon: "none"
  1302. });
  1303. this.proceedToNextQuestion();
  1304. return;
  1305. }
  1306. const mimeType2 = this.mediaRecorder.mimeType || "video/webm";
  1307. const blob = new Blob(this.recordedChunks, { type: mimeType2 });
  1308. console.log("创建Blob,原始大小:", blob.size, "类型:", mimeType2);
  1309. common_vendor.index.showLoading({
  1310. title: "正在处理视频...",
  1311. mask: true
  1312. });
  1313. try {
  1314. const compressedBlob = await this.compressVideo(blob);
  1315. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1316. const file = new File([compressedBlob], fileName, { type: mimeType2 });
  1317. common_vendor.index.hideLoading();
  1318. this.uploadRecordedVideo(file);
  1319. } catch (error) {
  1320. console.error("视频处理失败:", error);
  1321. common_vendor.index.hideLoading();
  1322. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1323. const file = new File([blob], fileName, { type: mimeType2 });
  1324. this.uploadRecordedVideo(file);
  1325. }
  1326. };
  1327. this.mediaRecorder.onerror = (event) => {
  1328. console.error("MediaRecorder错误:", event.error);
  1329. };
  1330. try {
  1331. this.mediaRecorder.start(1e3);
  1332. console.log("MediaRecorder开始录制");
  1333. this.mediaRecorderTimeout = setTimeout(() => {
  1334. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  1335. console.log("MediaRecorder备份超时机制触发,停止录制");
  1336. this.mediaRecorder.stop();
  1337. }
  1338. }, 3e5);
  1339. } catch (e) {
  1340. console.error("开始录制失败:", e);
  1341. }
  1342. },
  1343. // 添加新方法:停止录制用户回答
  1344. stopRecordingAnswer() {
  1345. console.log("停止录制用户回答");
  1346. if (this.countdownTimer) {
  1347. clearInterval(this.countdownTimer);
  1348. this.countdownTimer = null;
  1349. this.showCountdown = false;
  1350. }
  1351. this.isWaitingForAnswer = false;
  1352. const recordingDuration = this.getRecordingDuration();
  1353. const minimumDuration = 3;
  1354. const lowScoreDuration = 7;
  1355. if (recordingDuration < minimumDuration) {
  1356. common_vendor.index.showToast({
  1357. title: "录制时间过短,请至少录制3秒",
  1358. icon: "none",
  1359. duration: 2e3
  1360. });
  1361. return;
  1362. }
  1363. this.completeRecordingStop();
  1364. if (recordingDuration < lowScoreDuration) {
  1365. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  1366. this.needPlayLowScoreVideo = true;
  1367. } else {
  1368. this.needPlayLowScoreVideo = false;
  1369. }
  1370. },
  1371. // 添加新方法:获取录制时长
  1372. getRecordingDuration() {
  1373. if (this.recordingStartTime) {
  1374. return (Date.now() - this.recordingStartTime) / 1e3;
  1375. }
  1376. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1377. return this.$refs.userCameraVideo.currentTime || 0;
  1378. }
  1379. if (this.recordingTimerCount) {
  1380. return this.recordingTimerCount;
  1381. }
  1382. return 0;
  1383. },
  1384. // 添加新方法:重置录制
  1385. resetRecording() {
  1386. if (this.recordingTimer) {
  1387. clearTimeout(this.recordingTimer);
  1388. }
  1389. this.recordingStartTime = Date.now();
  1390. this.recordingTimerCount = 0;
  1391. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1392. this.mediaRecorder.stop();
  1393. this.recordedChunks = [];
  1394. setTimeout(() => {
  1395. this.startBrowserRecording();
  1396. }, 500);
  1397. } else if (this.cameraContext) {
  1398. this.cameraContext.stopRecord({
  1399. success: () => {
  1400. console.log("相机录制已停止");
  1401. },
  1402. fail: (err) => {
  1403. console.error("停止相机录制失败:", err);
  1404. }
  1405. });
  1406. }
  1407. common_vendor.index.showToast({
  1408. title: "请重新开始回答",
  1409. icon: "none",
  1410. duration: 2e3
  1411. });
  1412. },
  1413. // 修改 completeRecordingStop 方法,确保正确处理追问问题ID
  1414. completeRecordingStop() {
  1415. console.log("完成录制停止");
  1416. if (this.recordingTimer) {
  1417. clearInterval(this.recordingTimer);
  1418. this.recordingTimer = null;
  1419. }
  1420. this.finalRecordingDuration = this.recordingTimerCount;
  1421. this.showStopRecordingButton = false;
  1422. this.isRecording = false;
  1423. const systemInfo = common_vendor.index.getSystemInfoSync();
  1424. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1425. if (isMiniProgram) {
  1426. this.stopMiniProgramRecording();
  1427. } else {
  1428. this.stopBrowserRecording();
  1429. }
  1430. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1431. console.log("当前是追问问题,记录追问问题ID:", this.currentFollowUpQuestion.id);
  1432. this.lastQuestionWasFollowUp = true;
  1433. this.lastFollowUpQuestionId = this.currentFollowUpQuestion.id;
  1434. } else {
  1435. this.lastQuestionWasFollowUp = false;
  1436. this.lastFollowUpQuestionId = null;
  1437. }
  1438. },
  1439. // 修改 stopMiniProgramRecording 方法,确保正确传递追问问题ID
  1440. stopMiniProgramRecording() {
  1441. console.log("停止小程序录制");
  1442. if (!this.cameraContext) {
  1443. console.error("相机上下文不存在");
  1444. this.proceedToNextQuestion();
  1445. return;
  1446. }
  1447. this.cameraContext.stopRecord({
  1448. success: async (res) => {
  1449. console.log("小程序录制停止成功:", res);
  1450. const videoPath = res.tempVideoPath;
  1451. if (videoPath) {
  1452. await this.uploadRecordedVideo(videoPath);
  1453. } else {
  1454. console.error("未获取到录制视频路径");
  1455. this.proceedToNextQuestion();
  1456. }
  1457. },
  1458. fail: (err) => {
  1459. console.error("小程序录制停止失败:", err);
  1460. this.proceedToNextQuestion();
  1461. }
  1462. });
  1463. },
  1464. // 添加新方法:停止浏览器录制
  1465. stopBrowserRecording() {
  1466. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1467. this.mediaRecorder.stop();
  1468. console.log("浏览器录制停止成功");
  1469. } else {
  1470. console.error("MediaRecorder不存在或已经停止");
  1471. this.proceedToNextQuestion();
  1472. }
  1473. },
  1474. // 修改 uploadRecordedVideo 方法,添加记录父问题ID的逻辑
  1475. uploadRecordedVideo(fileOrPath) {
  1476. console.log("准备上传视频:", typeof fileOrPath === "string" ? fileOrPath : fileOrPath.name);
  1477. console.log("当前问题ID:", this.currentParentQuestionId);
  1478. console.log("当前问题类型:", this.isFollowUpQuestion);
  1479. console.log("当前追问问题:", this.currentFollowUpQuestion);
  1480. let questionId;
  1481. let isFollowUpQuestionUpload = false;
  1482. let questionText = "";
  1483. let questionForm = 0;
  1484. if (this.currentFollowUpQuestion && (this.currentFollowUpQuestion.question_form === 5 || this.isFollowUpQuestion)) {
  1485. questionId = this.currentFollowUpQuestion.id;
  1486. isFollowUpQuestionUpload = true;
  1487. questionText = this.currentFollowUpQuestion.question || "";
  1488. questionForm = 5;
  1489. console.log("正在上传追问问题的回答:", {
  1490. questionId,
  1491. questionText,
  1492. questionForm,
  1493. isFollowUp: true
  1494. });
  1495. } else {
  1496. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  1497. if (currentQuestion && currentQuestion.id) {
  1498. questionId = currentQuestion.id;
  1499. questionText = currentQuestion.question || "";
  1500. console.log("正在上传常规问题的回答:", {
  1501. questionId,
  1502. questionText,
  1503. questionForm: 0
  1504. });
  1505. } else {
  1506. questionId = this.getDefaultQuestionId(this.currentVideoIndex);
  1507. console.log("使用默认问题ID:", questionId);
  1508. }
  1509. }
  1510. const uploadTask = {
  1511. id: Date.now().toString(),
  1512. file: fileOrPath,
  1513. questionId,
  1514. isFollowUp: isFollowUpQuestionUpload,
  1515. questionText,
  1516. questionForm,
  1517. attempts: 0,
  1518. maxAttempts: 3,
  1519. parentQuestionId: this.currentParentQuestionId
  1520. };
  1521. if (!isFollowUpQuestionUpload) {
  1522. this.currentParentQuestionId = questionId;
  1523. }
  1524. this.uploadQueue.push(uploadTask);
  1525. this.uploadProgress[uploadTask.id] = 0;
  1526. this.uploadStatus[uploadTask.id] = "pending";
  1527. common_vendor.index.showToast({
  1528. title: "正在上传回答...",
  1529. icon: "loading",
  1530. duration: 1500
  1531. });
  1532. this.updateUploadStatusText();
  1533. return new Promise((resolve) => {
  1534. const checkUploadStatus = () => {
  1535. if (!this.isUploading && this.uploadQueue.length === 0) {
  1536. this.handlePostUploadActions(uploadTask);
  1537. resolve();
  1538. } else {
  1539. setTimeout(checkUploadStatus, 500);
  1540. }
  1541. };
  1542. if (!this.isUploading) {
  1543. this.processUploadQueue();
  1544. }
  1545. checkUploadStatus();
  1546. });
  1547. },
  1548. // 添加新方法:处理上传后的逻辑
  1549. handlePostUploadActions(task) {
  1550. if (this.needPlayLowScoreVideo && this.retryCount < 1) {
  1551. this.playLowScoreVideo();
  1552. this.needPlayLowScoreVideo = false;
  1553. } else {
  1554. if (task.isFollowUp) {
  1555. console.log("追问问题回答完成,进入下一个问题");
  1556. this.retryCount = 0;
  1557. this.isFollowUpQuestion = false;
  1558. this.currentFollowUpQuestion = null;
  1559. this.proceedToNextQuestion();
  1560. } else {
  1561. this.currentParentQuestionId = task.questionId;
  1562. console.log("检查常规问题的追问,父问题ID:", this.currentParentQuestionId);
  1563. this.checkAndPlayFollowUpQuestion(this.currentParentQuestionId);
  1564. }
  1565. }
  1566. },
  1567. // 修改 processUploadQueue 方法
  1568. processUploadQueue() {
  1569. if (this.uploadQueue.length === 0) {
  1570. this.isUploading = false;
  1571. this.showUploadStatus = false;
  1572. return;
  1573. }
  1574. this.isUploading = true;
  1575. this.showUploadStatus = true;
  1576. const task = this.uploadQueue[0];
  1577. this.uploadStatus[task.id] = "uploading";
  1578. this.updateUploadStatusText();
  1579. task.attempts++;
  1580. if (typeof task.file === "string") {
  1581. this.uploadFileWithUni(task);
  1582. } else {
  1583. this.uploadFileWithXHR(task);
  1584. }
  1585. },
  1586. // 添加新方法:使用XMLHttpRequest上传文件
  1587. uploadFileWithXHR(task) {
  1588. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1589. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1590. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1591. const formData = new FormData();
  1592. formData.append("file", task.file);
  1593. formData.append("openid", openid);
  1594. formData.append("tenant_id", tenant_id);
  1595. formData.append("application_id", common_vendor.index.getStorageSync("appId"));
  1596. formData.append("question_id", task.questionId);
  1597. formData.append("video_duration", 0);
  1598. formData.append("has_audio", "true");
  1599. const xhr = new XMLHttpRequest();
  1600. xhr.open("POST", `${common_config.apiBaseUrl}/api/upload/`, true);
  1601. xhr.timeout = 12e4;
  1602. xhr.upload.onprogress = (event) => {
  1603. if (event.lengthComputable) {
  1604. const percentComplete = Math.round(event.loaded / event.total * 100);
  1605. this.uploadProgress[task.id] = percentComplete;
  1606. this.updateUploadStatusText();
  1607. }
  1608. };
  1609. xhr.onload = () => {
  1610. if (xhr.status === 200) {
  1611. try {
  1612. const res = JSON.parse(xhr.responseText);
  1613. console.log("上传响应:", res);
  1614. if (res.code === 2e3) {
  1615. const videoUrl = res.data.url || res.data.photoUrl || "";
  1616. if (videoUrl) {
  1617. this.uploadStatus[task.id] = "success";
  1618. this.updateUploadStatusText();
  1619. this.submitVideoToInterview(videoUrl, task);
  1620. } else {
  1621. this.handleUploadFailure(task, "视频URL获取失败");
  1622. }
  1623. } else {
  1624. this.handleUploadFailure(task, res.msg || "上传失败");
  1625. }
  1626. } catch (e) {
  1627. this.handleUploadFailure(task, "解析响应失败");
  1628. }
  1629. } else {
  1630. this.handleUploadFailure(task, "HTTP状态: " + xhr.status);
  1631. }
  1632. };
  1633. xhr.onerror = () => {
  1634. this.handleUploadFailure(task, "网络错误");
  1635. };
  1636. xhr.ontimeout = () => {
  1637. this.handleUploadFailure(task, "上传超时");
  1638. };
  1639. xhr.send(formData);
  1640. },
  1641. // 添加新方法:使用uni.uploadFile上传文件
  1642. uploadFileWithUni(task) {
  1643. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1644. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1645. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1646. const uploadTask = common_vendor.index.uploadFile({
  1647. url: `${common_config.apiBaseUrl}/api/upload/`,
  1648. filePath: task.file,
  1649. name: "file",
  1650. formData: {
  1651. openid,
  1652. tenant_id,
  1653. application_id: common_vendor.index.getStorageSync("appId"),
  1654. question_id: task.questionId,
  1655. video_duration: 0,
  1656. has_audio: "true"
  1657. },
  1658. success: (uploadRes) => {
  1659. try {
  1660. const res = JSON.parse(uploadRes.data);
  1661. console.log("上传响应:", res);
  1662. if (res.code === 2e3) {
  1663. const videoUrl = res.data.permanent_link || res.data.url || "";
  1664. if (videoUrl) {
  1665. this.uploadStatus[task.id] = "success";
  1666. this.updateUploadStatusText();
  1667. this.submitVideoToInterview(videoUrl, task);
  1668. } else {
  1669. this.handleUploadFailure(task, "视频URL获取失败");
  1670. }
  1671. } else {
  1672. this.handleUploadFailure(task, res.msg || "上传失败");
  1673. }
  1674. } catch (e) {
  1675. this.handleUploadFailure(task, "解析响应失败");
  1676. }
  1677. },
  1678. fail: (err) => {
  1679. this.handleUploadFailure(task, err.errMsg || "上传失败");
  1680. }
  1681. });
  1682. uploadTask.onProgressUpdate((res) => {
  1683. this.uploadProgress[task.id] = res.progress;
  1684. this.updateUploadStatusText();
  1685. });
  1686. },
  1687. // 添加新方法:处理上传失败
  1688. handleUploadFailure(task, errorMsg) {
  1689. console.error("上传失败:", errorMsg);
  1690. this.uploadStatus[task.id] = "failed";
  1691. this.updateUploadStatusText();
  1692. if (task.attempts < task.maxAttempts) {
  1693. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1694. setTimeout(() => {
  1695. this.uploadProgress[task.id] = 0;
  1696. if (typeof task.file !== "string") {
  1697. this.uploadFileWithXHR(task);
  1698. } else {
  1699. this.uploadFileWithUni(task);
  1700. }
  1701. }, 5e3);
  1702. } else {
  1703. console.log("超过最大重试次数,放弃上传");
  1704. common_vendor.index.showToast({
  1705. title: "视频上传失败,请稍后重试",
  1706. icon: "none",
  1707. duration: 2e3
  1708. });
  1709. this.uploadQueue.shift();
  1710. this.processUploadQueue();
  1711. }
  1712. },
  1713. // 修改 submitVideoToInterview 方法
  1714. submitVideoToInterview(videoUrl, task) {
  1715. const requestData = {
  1716. application_id: common_vendor.index.getStorageSync("appId"),
  1717. question_id: task.questionId,
  1718. video_url: videoUrl,
  1719. tenant_id: JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1",
  1720. is_follow_up: task.isFollowUp ? 1 : 0,
  1721. question_form: task.questionForm,
  1722. question_text: task.questionText
  1723. };
  1724. console.log("提交视频到面试接口", requestData);
  1725. common_vendor.index.request({
  1726. url: `${common_config.apiBaseUrl}/api/job/upload_video`,
  1727. method: "POST",
  1728. data: requestData,
  1729. header: {
  1730. "content-type": "application/x-www-form-urlencoded"
  1731. },
  1732. success: async (res) => {
  1733. if (res.data.code === 200 || res.data.code === 2e3) {
  1734. console.log("视频提交成功,准备调用面试互动接口");
  1735. if (!task.isFollowUp) {
  1736. try {
  1737. await this.callInterviewInteraction(res.data.data.job_position_question_id);
  1738. console.log("面试互动接口调用成功");
  1739. } catch (error) {
  1740. console.error("面试互动接口调用失败:", error);
  1741. this.proceedToNextQuestion();
  1742. }
  1743. } else {
  1744. console.log("追问回答上传完成,继续下一个问题");
  1745. this.proceedToNextQuestion();
  1746. }
  1747. common_vendor.index.showToast({
  1748. title: "回答已提交",
  1749. icon: "success",
  1750. duration: 1500
  1751. });
  1752. this.uploadQueue.shift();
  1753. this.processUploadQueue();
  1754. } else {
  1755. this.handleUploadError(task, "提交失败: " + (res.data.msg || "未知错误"));
  1756. }
  1757. },
  1758. fail: (err) => {
  1759. console.error("提交视频失败:", err);
  1760. this.handleUploadError(task, "提交失败: " + err.errMsg);
  1761. }
  1762. });
  1763. },
  1764. // 添加新方法:处理提交失败
  1765. handleSubmitFailure(task, errorMsg) {
  1766. console.error("提交失败:", errorMsg);
  1767. this.uploadStatus[task.id] = "failed";
  1768. this.updateUploadStatusText();
  1769. if (task.attempts < task.maxAttempts) {
  1770. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1771. setTimeout(() => {
  1772. this.submitVideoToInterview(task.videoUrl, task);
  1773. }, 5e3);
  1774. } else {
  1775. console.log("超过最大重试次数,放弃提交");
  1776. common_vendor.index.showToast({
  1777. title: "视频提交失败,请稍后重试",
  1778. icon: "none",
  1779. duration: 2e3
  1780. });
  1781. this.uploadQueue.shift();
  1782. this.processUploadQueue();
  1783. }
  1784. },
  1785. // 添加新方法:更新上传状态文本
  1786. updateUploadStatusText() {
  1787. if (this.uploadQueue.length === 0) {
  1788. this.uploadStatusText = "";
  1789. return;
  1790. }
  1791. const currentTask = this.uploadQueue[0];
  1792. const progress = this.uploadProgress[currentTask.id] || 0;
  1793. const status = this.uploadStatus[currentTask.id] || "pending";
  1794. let statusText = "";
  1795. switch (status) {
  1796. case "pending":
  1797. statusText = "等待上传";
  1798. break;
  1799. case "uploading":
  1800. statusText = `上传中 ${progress}%`;
  1801. break;
  1802. case "success":
  1803. statusText = "上传成功,提交中...";
  1804. break;
  1805. case "failed":
  1806. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? "即将重试" : "已放弃"}`;
  1807. break;
  1808. }
  1809. const questionTypeText = currentTask.isFollowUp ? "追问" : "问题";
  1810. const questionShortText = currentTask.questionText ? currentTask.questionText.length > 10 ? currentTask.questionText.substring(0, 10) + "..." : currentTask.questionText : `${questionTypeText}${currentTask.questionId}`;
  1811. this.uploadStatusText = `${questionTypeText}「${questionShortText}」:${statusText}`;
  1812. if (this.uploadQueue.length > 1) {
  1813. this.uploadStatusText += ` (${this.uploadQueue.length}个视频待处理)`;
  1814. }
  1815. },
  1816. // 修改 proceedToNextQuestion 方法,确保在切换视频时重置历史时间
  1817. proceedToNextQuestion() {
  1818. console.log("继续下一个问题");
  1819. this.currentVideoIndex++;
  1820. this.historyTime = 0;
  1821. if (this.currentVideoIndex < this.videoList.length) {
  1822. this.videoUrl = this.videoList[this.currentVideoIndex];
  1823. this.videoPlaying = true;
  1824. this.$nextTick(() => {
  1825. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1826. if (videoContext) {
  1827. videoContext.play();
  1828. }
  1829. });
  1830. } else {
  1831. console.log("所有视频已播放完毕");
  1832. this.stopUserCamera();
  1833. this.clearCountdown();
  1834. const currentJobDetail = JSON.parse(common_vendor.index.getStorageSync("selectedJob"));
  1835. console.log("当前职位ID:", currentJobDetail);
  1836. const jobId = currentJobDetail ? currentJobDetail.id : null;
  1837. console.log("当前职位ID:", jobId);
  1838. setTimeout(() => {
  1839. if (jobId === 9) {
  1840. common_vendor.index.navigateTo({
  1841. url: "/pages/interview-question/interview-question",
  1842. success: () => {
  1843. console.log("成功跳转到interview-question页面");
  1844. },
  1845. fail: (err) => {
  1846. console.error("跳转到interview-question页面失败:", err);
  1847. common_vendor.index.redirectTo({
  1848. url: "/pages/interview-question/interview-question",
  1849. fail: (redirectErr) => {
  1850. console.error("重定向到interview-question页面也失败:", redirectErr);
  1851. common_vendor.index.navigateBack({
  1852. delta: 1
  1853. });
  1854. }
  1855. });
  1856. }
  1857. });
  1858. } else {
  1859. common_vendor.index.navigateTo({
  1860. url: "/pages/camera/camera",
  1861. success: () => {
  1862. console.log("成功跳转到camera页面");
  1863. },
  1864. fail: (err) => {
  1865. console.error("跳转到camera页面失败:", err);
  1866. common_vendor.index.redirectTo({
  1867. url: "/pages/camera/camera",
  1868. fail: (redirectErr) => {
  1869. console.error("重定向到camera页面也失败:", redirectErr);
  1870. common_vendor.index.switchTab({
  1871. url: "/pages/camera/camera",
  1872. fail: (switchErr) => {
  1873. console.error("所有跳转方式都失败:", switchErr);
  1874. common_vendor.index.navigateBack({
  1875. delta: 1
  1876. });
  1877. }
  1878. });
  1879. }
  1880. });
  1881. }
  1882. });
  1883. }
  1884. }, 2e3);
  1885. }
  1886. },
  1887. // 修改 handleAnswerButtonClick 方法,确保在切换视频时重置历史时间
  1888. handleAnswerButtonClick() {
  1889. console.log("点击开始回答按钮");
  1890. this.showAnswerButton = false;
  1891. this.currentVideoIndex++;
  1892. this.historyTime = 0;
  1893. if (this.currentVideoIndex < this.videoList.length) {
  1894. this.videoUrl = this.videoList[this.currentVideoIndex];
  1895. this.videoPlaying = true;
  1896. this.$nextTick(() => {
  1897. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1898. if (videoContext) {
  1899. videoContext.play();
  1900. }
  1901. });
  1902. }
  1903. },
  1904. // 处理相机错误
  1905. handleCameraError(e) {
  1906. console.error("相机错误:", e);
  1907. const systemInfo = common_vendor.index.getSystemInfoSync();
  1908. const isIOS = systemInfo.platform === "ios";
  1909. if (isIOS) {
  1910. console.log("iOS相机错误,尝试重新初始化");
  1911. common_vendor.index.showToast({
  1912. title: "相机初始化中...",
  1913. icon: "loading",
  1914. duration: 2e3
  1915. });
  1916. this.resetCamera();
  1917. if (this.isRecording) {
  1918. this.isRecording = false;
  1919. this.showStopRecordingButton = false;
  1920. setTimeout(() => {
  1921. this.useAlternativeRecordingMethod();
  1922. }, 1e3);
  1923. }
  1924. } else {
  1925. common_vendor.index.showToast({
  1926. title: "相机初始化失败,请检查权限设置",
  1927. icon: "none"
  1928. });
  1929. this.tryFallbackOptions();
  1930. }
  1931. },
  1932. // 添加新方法:尝试备用选项
  1933. tryFallbackOptions() {
  1934. const systemInfo = common_vendor.index.getSystemInfoSync();
  1935. if (systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay") {
  1936. this.useMiniProgramCamera();
  1937. } else {
  1938. this.showStaticCameraPlaceholder();
  1939. }
  1940. },
  1941. // 添加新方法:使用小程序相机API
  1942. useMiniProgramCamera() {
  1943. console.log("尝试使用小程序相机组件");
  1944. this.useMiniProgramCameraComponent = true;
  1945. },
  1946. // 添加新方法:显示静态图像
  1947. showStaticCameraPlaceholder() {
  1948. console.log("显示静态摄像头占位图");
  1949. const img = document.createElement("img");
  1950. img.src = "/static/images/camera-placeholder.png";
  1951. img.className = "static-camera-image";
  1952. img.style.width = "100%";
  1953. img.style.height = "100%";
  1954. img.style.objectFit = "cover";
  1955. const container = this.$refs.userCameraVideo.parentNode;
  1956. container.appendChild(img);
  1957. },
  1958. // 修改 handleTimeUpdate 方法
  1959. handleTimeUpdate(e) {
  1960. const currentTime = e.detail.currentTime || e.target.currentTime;
  1961. if (this.isRecording && this.recordingTimerCount) {
  1962. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1963. }
  1964. if (this.isPlayingLowScoreVideo) {
  1965. if (this.lowScoreVideoSubtitles && this.lowScoreVideoSubtitles.length > 0) {
  1966. const subtitle = this.lowScoreVideoSubtitles[0];
  1967. this.currentSubtitle = subtitle.text;
  1968. }
  1969. return;
  1970. }
  1971. let currentSubtitles = null;
  1972. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1973. const subtitleKey = `followUpSubtitles_${this.currentFollowUpQuestion.id}`;
  1974. currentSubtitles = this[subtitleKey] || [{
  1975. startTime: 0,
  1976. endTime: 30,
  1977. text: this.currentFollowUpQuestion.digital_human_video_subtitle || this.currentFollowUpQuestion.question
  1978. }];
  1979. } else {
  1980. if (this.currentVideoIndex === 0) {
  1981. currentSubtitles = this.subtitles;
  1982. } else {
  1983. const subtitleArrayName = `question${this.currentVideoIndex}Subtitles`;
  1984. currentSubtitles = this[subtitleArrayName];
  1985. }
  1986. }
  1987. if (currentSubtitles && currentSubtitles.length > 0) {
  1988. const subtitle = currentSubtitles.find(
  1989. (sub) => currentTime >= sub.startTime && currentTime <= sub.endTime
  1990. ) || currentSubtitles[0];
  1991. if (subtitle) {
  1992. this.currentSubtitle = subtitle.text;
  1993. }
  1994. }
  1995. this.historyTime = currentTime;
  1996. this.lastUpdateTime = Date.now();
  1997. },
  1998. // Add a new method to handle the "Start Recording" button click
  1999. handleStartRecordingClick() {
  2000. this.showStartRecordingButton = false;
  2001. this.clearCountdown();
  2002. this.isWaitingForAnswer = false;
  2003. this.startRecordingAnswer();
  2004. },
  2005. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  2006. checkAudioPermission() {
  2007. const systemInfo = common_vendor.index.getSystemInfoSync();
  2008. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2009. if (isMiniProgram) {
  2010. common_vendor.index.getSetting({
  2011. success: (res) => {
  2012. if (!res.authSetting["scope.record"]) {
  2013. common_vendor.index.authorize({
  2014. scope: "scope.record",
  2015. success: () => {
  2016. console.log("录音权限已获取");
  2017. },
  2018. fail: (err) => {
  2019. console.error("录音权限获取失败:", err);
  2020. this.showPermissionDialog("录音");
  2021. }
  2022. });
  2023. }
  2024. if (!res.authSetting["scope.camera"]) {
  2025. common_vendor.index.authorize({
  2026. scope: "scope.camera",
  2027. success: () => {
  2028. console.log("相机权限已获取");
  2029. },
  2030. fail: (err) => {
  2031. console.error("相机权限获取失败:", err);
  2032. this.showPermissionDialog("相机");
  2033. }
  2034. });
  2035. }
  2036. }
  2037. });
  2038. }
  2039. },
  2040. // 添加新方法:测试音频输入
  2041. testAudioInput() {
  2042. if (!this.cameraStream) {
  2043. console.warn("没有可用的媒体流,无法测试音频");
  2044. return;
  2045. }
  2046. const audioTracks = this.cameraStream.getAudioTracks();
  2047. if (audioTracks.length === 0) {
  2048. console.warn("没有检测到音频轨道,尝试重新获取");
  2049. this.tryGetAudioOnly();
  2050. return;
  2051. }
  2052. console.log("音频轨道信息:", audioTracks[0].getSettings());
  2053. try {
  2054. const AudioContext = window.AudioContext || window.webkitAudioContext;
  2055. if (!AudioContext) {
  2056. console.warn("浏览器不支持AudioContext");
  2057. return;
  2058. }
  2059. const audioContext = new AudioContext();
  2060. const analyser = audioContext.createAnalyser();
  2061. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  2062. microphone.connect(analyser);
  2063. analyser.fftSize = 256;
  2064. const bufferLength = analyser.frequencyBinCount;
  2065. const dataArray = new Uint8Array(bufferLength);
  2066. let silenceCounter = 0;
  2067. const checkAudio = () => {
  2068. if (this.isRecording)
  2069. return;
  2070. analyser.getByteFrequencyData(dataArray);
  2071. let sum = 0;
  2072. for (let i = 0; i < bufferLength; i++) {
  2073. sum += dataArray[i];
  2074. }
  2075. const average = sum / bufferLength;
  2076. if (average > 10) {
  2077. console.log("检测到音频输入,音量:", average);
  2078. silenceCounter = 0;
  2079. } else {
  2080. silenceCounter++;
  2081. if (silenceCounter > 10) {
  2082. console.warn("持续检测不到音频输入,可能麦克风未正常工作");
  2083. silenceCounter = 0;
  2084. }
  2085. }
  2086. requestAnimationFrame(checkAudio);
  2087. };
  2088. checkAudio();
  2089. } catch (e) {
  2090. console.error("音频测试失败:", e);
  2091. }
  2092. },
  2093. // 添加新方法:尝试单独获取音频
  2094. tryGetAudioOnly() {
  2095. navigator.mediaDevices.getUserMedia({ audio: true }).then((audioStream) => {
  2096. if (this.cameraStream) {
  2097. const videoTrack = this.cameraStream.getVideoTracks()[0];
  2098. const audioTrack = audioStream.getAudioTracks()[0];
  2099. const combinedStream = new MediaStream();
  2100. if (videoTrack)
  2101. combinedStream.addTrack(videoTrack);
  2102. if (audioTrack)
  2103. combinedStream.addTrack(audioTrack);
  2104. this.cameraStream = combinedStream;
  2105. const videoElement = this.$refs.userCameraVideo;
  2106. if (videoElement) {
  2107. videoElement.srcObject = combinedStream;
  2108. videoElement.muted = true;
  2109. }
  2110. console.log("成功合并音频和视频轨道");
  2111. } else {
  2112. console.warn("没有视频流可合并");
  2113. }
  2114. }).catch((err) => {
  2115. console.error("单独获取音频失败:", err);
  2116. });
  2117. },
  2118. // 添加新方法:显示权限对话框
  2119. showPermissionDialog(permissionType) {
  2120. common_vendor.index.showModal({
  2121. title: "需要权限",
  2122. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  2123. confirmText: "去设置",
  2124. success: (res) => {
  2125. if (res.confirm) {
  2126. common_vendor.index.openSetting({
  2127. success: (settingRes) => {
  2128. console.log("设置页面打开成功", settingRes);
  2129. }
  2130. });
  2131. }
  2132. }
  2133. });
  2134. },
  2135. // 添加重试上传方法
  2136. retryVideoUpload() {
  2137. if (this.lastVideoToRetry) {
  2138. this.showRetryButton = false;
  2139. common_vendor.index.showLoading({
  2140. title: "正在重新提交...",
  2141. mask: true
  2142. });
  2143. this.submitVideoToInterview(this.lastVideoToRetry);
  2144. } else {
  2145. common_vendor.index.showToast({
  2146. title: "没有可重试的视频",
  2147. icon: "none"
  2148. });
  2149. }
  2150. },
  2151. // 添加一个新方法用于压缩视频
  2152. async compressVideo(videoBlob) {
  2153. if (videoBlob.size < 5 * 1024 * 1024) {
  2154. return videoBlob;
  2155. }
  2156. console.log("开始压缩视频,原始大小:", videoBlob.size);
  2157. const videoElement = document.createElement("video");
  2158. videoElement.muted = true;
  2159. videoElement.autoplay = false;
  2160. const canvas = document.createElement("canvas");
  2161. const ctx = canvas.getContext("2d");
  2162. videoElement.src = URL.createObjectURL(videoBlob);
  2163. return new Promise((resolve) => {
  2164. videoElement.onloadedmetadata = () => {
  2165. const width = Math.floor(videoElement.videoWidth / 2);
  2166. const height = Math.floor(videoElement.videoHeight / 2);
  2167. canvas.width = width;
  2168. canvas.height = height;
  2169. const stream = canvas.captureStream(15);
  2170. if (videoElement.captureStream) {
  2171. const originalStream = videoElement.captureStream();
  2172. const audioTracks = originalStream.getAudioTracks();
  2173. if (audioTracks.length > 0) {
  2174. stream.addTrack(audioTracks[0]);
  2175. }
  2176. }
  2177. const options = {
  2178. mimeType: "video/webm;codecs=vp8,opus",
  2179. audioBitsPerSecond: 64e3,
  2180. videoBitsPerSecond: 8e5
  2181. // 800kbps
  2182. };
  2183. const mediaRecorder = new MediaRecorder(stream, options);
  2184. const chunks = [];
  2185. mediaRecorder.ondataavailable = (e) => {
  2186. if (e.data.size > 0) {
  2187. chunks.push(e.data);
  2188. }
  2189. };
  2190. mediaRecorder.onstop = () => {
  2191. const compressedBlob = new Blob(chunks, { type: "video/webm" });
  2192. console.log("视频压缩完成,压缩后大小:", compressedBlob.size);
  2193. resolve(compressedBlob);
  2194. };
  2195. videoElement.onplay = () => {
  2196. mediaRecorder.start(10);
  2197. const drawFrame = () => {
  2198. if (videoElement.paused || videoElement.ended) {
  2199. mediaRecorder.stop();
  2200. return;
  2201. }
  2202. ctx.drawImage(videoElement, 0, 0, width, height);
  2203. requestAnimationFrame(drawFrame);
  2204. };
  2205. drawFrame();
  2206. };
  2207. videoElement.play();
  2208. };
  2209. });
  2210. },
  2211. // 修改 checkIOSCameraRecordPermission 方法
  2212. checkIOSCameraRecordPermission() {
  2213. const systemInfo = common_vendor.index.getSystemInfoSync();
  2214. if (systemInfo.platform !== "ios")
  2215. return;
  2216. common_vendor.index.getSetting({
  2217. success: (res) => {
  2218. if (!res.authSetting["scope.camera"]) {
  2219. common_vendor.index.authorize({
  2220. scope: "scope.camera",
  2221. success: () => {
  2222. console.log("iOS相机权限已获取");
  2223. },
  2224. fail: (err) => {
  2225. console.error("iOS相机权限获取失败:", err);
  2226. this.showPermissionDialog("相机");
  2227. }
  2228. });
  2229. }
  2230. if (!res.authSetting["scope.record"]) {
  2231. common_vendor.index.authorize({
  2232. scope: "scope.record",
  2233. success: () => {
  2234. console.log("iOS录音权限已获取");
  2235. },
  2236. fail: (err) => {
  2237. console.error("iOS录音权限获取失败:", err);
  2238. this.showPermissionDialog("录音");
  2239. }
  2240. });
  2241. }
  2242. }
  2243. });
  2244. },
  2245. // 添加新方法:检查并修复渲染问题
  2246. checkAndFixRenderingIssues() {
  2247. try {
  2248. if (typeof u !== "undefined" && u) {
  2249. if (!u.currentQuestion) {
  2250. console.log("修复: 创建缺失的currentQuestion对象");
  2251. u.currentQuestion = {};
  2252. }
  2253. if (u.currentQuestion && typeof u.currentQuestion.isImportant === "undefined") {
  2254. console.log("修复: 设置缺失的isImportant属性");
  2255. u.currentQuestion.isImportant = false;
  2256. }
  2257. }
  2258. } catch (e) {
  2259. console.log("防御性检查异常:", e);
  2260. }
  2261. },
  2262. // 添加格式化时间的辅助方法
  2263. formatTime(seconds) {
  2264. if (!seconds && seconds !== 0)
  2265. return "03:30";
  2266. const minutes = Math.floor(seconds / 60);
  2267. const remainingSeconds = seconds % 60;
  2268. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  2269. },
  2270. // 添加获取问题数据的方法
  2271. fetchQuestions() {
  2272. this.loading = true;
  2273. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  2274. let position_id = 1;
  2275. try {
  2276. const selectedJob = JSON.parse(common_vendor.index.getStorageSync("selectedJob") || "{}");
  2277. if (selectedJob && selectedJob.id) {
  2278. position_id = selectedJob.id;
  2279. console.log("使用选择的职位ID获取问题:", position_id);
  2280. } else {
  2281. console.warn("未找到选择的职位信息,使用默认职位ID:", position_id);
  2282. }
  2283. } catch (e) {
  2284. console.error("解析职位信息失败:", e);
  2285. }
  2286. const timestamp = Date.now();
  2287. const requestUrl = `${common_config.apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=0&_t=${timestamp}`;
  2288. console.log("完整的请求URL:", requestUrl);
  2289. common_vendor.index.request({
  2290. url: requestUrl,
  2291. method: "GET",
  2292. success: (res) => {
  2293. if (res.data && res.data.code === 2e3 && res.data.data && res.data.data.items) {
  2294. console.log("获取问题数据成功:", res.data.data.items);
  2295. this.questions = res.data.data.items;
  2296. this.processQuestionData();
  2297. this.playIntroVideo();
  2298. } else {
  2299. console.error("获取问题数据失败:", res.data);
  2300. common_vendor.index.showToast({
  2301. title: "获取问题数据失败",
  2302. icon: "none"
  2303. });
  2304. }
  2305. },
  2306. fail: (err) => {
  2307. console.error("请求问题数据失败:", err);
  2308. common_vendor.index.showToast({
  2309. title: "网络错误,请检查网络连接",
  2310. icon: "none"
  2311. });
  2312. },
  2313. complete: () => {
  2314. this.loading = false;
  2315. }
  2316. });
  2317. },
  2318. // 处理问题数据,提取视频URL和字幕
  2319. processQuestionData() {
  2320. this.videoList = [];
  2321. this.videoList.push(this.introVideoUrl);
  2322. this.subtitles = [
  2323. {
  2324. startTime: 0,
  2325. endTime: 5,
  2326. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  2327. },
  2328. {
  2329. startTime: 5,
  2330. endTime: 13,
  2331. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  2332. },
  2333. {
  2334. startTime: 13,
  2335. endTime: 20,
  2336. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  2337. }
  2338. ];
  2339. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  2340. const questionsToUse = sortedQuestions.slice(0, 5);
  2341. questionsToUse.forEach((question, index) => {
  2342. if (question.digital_human_video_url) {
  2343. this.videoList.push(question.digital_human_video_url);
  2344. const subtitleArray = [{
  2345. startTime: 0,
  2346. endTime: 30,
  2347. // 延长字幕显示时间到30秒
  2348. text: question.digital_human_video_subtitle || question.question
  2349. }];
  2350. const videoIndex = this.videoList.length - 1;
  2351. this[`question${videoIndex}Subtitles`] = subtitleArray;
  2352. this.subtitleMap[question.digital_human_video_url] = subtitleArray;
  2353. }
  2354. });
  2355. if (this.videoList.length <= 1)
  2356. ;
  2357. else {
  2358. console.log("处理后的视频列表:", this.videoList);
  2359. console.log("处理后的字幕:", this.subtitles);
  2360. }
  2361. },
  2362. // 使用默认视频和字幕(作为备用)
  2363. useDefaultVideosAndSubtitles() {
  2364. console.log("使用默认视频和字幕");
  2365. this.subtitles = [
  2366. {
  2367. startTime: 0,
  2368. endTime: 5,
  2369. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  2370. },
  2371. {
  2372. startTime: 5,
  2373. endTime: 13,
  2374. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  2375. },
  2376. {
  2377. startTime: 13,
  2378. endTime: 20,
  2379. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  2380. }
  2381. ];
  2382. this.question1Subtitles = [{
  2383. startTime: 0,
  2384. endTime: 10,
  2385. text: "请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:"
  2386. }];
  2387. this.question2Subtitles = [{
  2388. startTime: 0,
  2389. endTime: 4,
  2390. text: "在工作中,你如何确保个人防护装备的正确使用?"
  2391. }];
  2392. this.question3Subtitles = [{
  2393. startTime: 0,
  2394. endTime: 4,
  2395. text: "描述一次你与团队合作改善生产流程的经历。"
  2396. }];
  2397. this.question4Subtitles = [{
  2398. startTime: 0,
  2399. endTime: 6,
  2400. text: "你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?"
  2401. }];
  2402. this.question5Subtitles = [{
  2403. startTime: 0,
  2404. endTime: 5,
  2405. text: "您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?"
  2406. }];
  2407. },
  2408. // 播放介绍视频
  2409. playIntroVideo() {
  2410. this.videoUrl = this.videoList[0];
  2411. this.videoPlaying = true;
  2412. this.$nextTick(() => {
  2413. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2414. if (videoContext) {
  2415. videoContext.play();
  2416. }
  2417. });
  2418. },
  2419. // 添加新方法:获取默认问题ID
  2420. getDefaultQuestionId(videoIndex) {
  2421. switch (videoIndex) {
  2422. case 1:
  2423. return 10;
  2424. case 2:
  2425. return 11;
  2426. case 3:
  2427. return 12;
  2428. case 4:
  2429. return 13;
  2430. default:
  2431. return 10;
  2432. }
  2433. },
  2434. // 添加新方法:根据索引获取当前问题
  2435. getCurrentQuestionByIndex(videoIndex) {
  2436. const questionIndex = videoIndex - 1;
  2437. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  2438. if (questionIndex >= 0 && questionIndex < sortedQuestions.length) {
  2439. return sortedQuestions[questionIndex];
  2440. }
  2441. return null;
  2442. },
  2443. // 添加新方法:重置录制状态,准备重新回答
  2444. resetForRerecording() {
  2445. console.log("重置录制状态,准备重新回答");
  2446. this.isRecording = false;
  2447. this.showStopRecordingButton = false;
  2448. this.showStartRecordingButton = true;
  2449. this.clearCountdown();
  2450. if (this.recordingTimer) {
  2451. clearInterval(this.recordingTimer);
  2452. this.recordingTimer = null;
  2453. }
  2454. this.recordingTimerCount = 0;
  2455. this.recordingTimeDisplay = "00:00 ";
  2456. this.remainingTime = this.maxRecordingTime;
  2457. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  2458. this.mediaRecorder.stop();
  2459. this.recordedChunks = [];
  2460. }
  2461. if (this.cameraContext) {
  2462. this.cameraContext.stopRecord({
  2463. success: () => {
  2464. console.log("相机录制已停止");
  2465. },
  2466. fail: (err) => {
  2467. console.error("停止相机录制失败:", err);
  2468. }
  2469. });
  2470. }
  2471. },
  2472. // 修改 playLowScoreVideo 方法,优化字幕显示
  2473. playLowScoreVideo() {
  2474. console.log("播放低分提示视频");
  2475. this.isPlayingLowScoreVideo = true;
  2476. this.showStartRecordingButton = false;
  2477. this.showStopRecordingButton = false;
  2478. this.showRerecordButton = false;
  2479. this.videoUrl = this.lowScoreVideoUrl;
  2480. this.videoPlaying = true;
  2481. const originalSubtitle = this.currentSubtitle;
  2482. this.currentSubtitle = "";
  2483. this.$nextTick(() => {
  2484. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2485. if (videoContext) {
  2486. videoContext.play();
  2487. setTimeout(() => {
  2488. this.currentSubtitle = "我未听清楚您在说什么,请您再说一遍!";
  2489. this.lowScoreVideoSubtitles = [
  2490. {
  2491. startTime: 0,
  2492. endTime: 30,
  2493. text: "我未听清楚您在说什么,请您再说一遍!",
  2494. translation: "I didn't quite catch what you said, could you please repeat it?"
  2495. }
  2496. ];
  2497. setTimeout(() => {
  2498. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  2499. this.currentSubtitle = this.currentFollowUpQuestion.digital_human_video_subtitle || this.currentFollowUpQuestion.question;
  2500. } else {
  2501. this.currentSubtitle = originalSubtitle;
  2502. }
  2503. }, 3e3);
  2504. }, 500);
  2505. }
  2506. });
  2507. },
  2508. // 添加新方法:处理重新录制按钮点击
  2509. handleRerecordButtonClick() {
  2510. console.log("点击重新录制按钮");
  2511. this.retryCount++;
  2512. console.log(`当前重试次数: ${this.retryCount}/${this.maxRetryAttempts}`);
  2513. this.showRerecordButton = false;
  2514. this.handleStartRecordingClick();
  2515. },
  2516. // 页面卸载时关闭WebSocket连接
  2517. onUnload() {
  2518. console.log("页面卸载,关闭WebSocket连接");
  2519. this.cleanupPersonDetectionWebSocket();
  2520. this.stopUserCamera();
  2521. this.clearCountdown();
  2522. if (this.recordingTimer) {
  2523. clearInterval(this.recordingTimer);
  2524. this.recordingTimer = null;
  2525. }
  2526. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  2527. this.mediaRecorder.stop();
  2528. }
  2529. common_vendor.index.setKeepScreenOn({
  2530. keepScreenOn: false
  2531. });
  2532. },
  2533. // 页面隐藏时关闭WebSocket连接
  2534. onHide() {
  2535. console.log("页面隐藏,关闭WebSocket连接");
  2536. this.cleanupPersonDetectionWebSocket();
  2537. if (this.isRecording) {
  2538. console.log("页面隐藏时正在录制,录制将继续");
  2539. common_vendor.index.showToast({
  2540. title: "请不要离开面试页面",
  2541. icon: "none",
  2542. duration: 2e3
  2543. });
  2544. }
  2545. },
  2546. // 页面显示时重新初始化WebSocket连接
  2547. onShow() {
  2548. console.log("identity-verify页面onShow");
  2549. console.log("当前本地存储中的职位信息:", common_vendor.index.getStorageSync("selectedJob"));
  2550. this.fetchQuestions();
  2551. this.fetchFollowUpQuestions();
  2552. const systemInfo = common_vendor.index.getSystemInfoSync();
  2553. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2554. if (isMiniProgram) {
  2555. if (!this.personDetectionSocket) {
  2556. console.log("重新初始化WebSocket连接");
  2557. this.initPersonDetectionWebSocket();
  2558. } else if (!this.personDetectionInterval) {
  2559. console.log("重新启动人脸检测定时器");
  2560. this.startPersonDetectionInterval();
  2561. }
  2562. }
  2563. },
  2564. // 添加新方法:阻止视频控制
  2565. preventVideoControl(e) {
  2566. e.preventDefault();
  2567. e.stopPropagation();
  2568. console.log("阻止视频控制操作");
  2569. return false;
  2570. },
  2571. // 添加获取追问问题的方法
  2572. fetchFollowUpQuestions() {
  2573. const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  2574. let position_id = 1;
  2575. try {
  2576. const selectedJob = JSON.parse(common_vendor.index.getStorageSync("selectedJob") || "{}");
  2577. if (selectedJob && selectedJob.id) {
  2578. position_id = selectedJob.id;
  2579. console.log("使用选择的职位ID获取追问问题:", position_id);
  2580. } else {
  2581. console.warn("未找到选择的职位信息,使用默认职位ID:", position_id);
  2582. }
  2583. } catch (e) {
  2584. console.error("解析职位信息失败:", e);
  2585. }
  2586. const timestamp = Date.now();
  2587. const requestUrl = `${common_config.apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=5&_t=${timestamp}`;
  2588. console.log("获取追问问题的请求URL:", requestUrl);
  2589. common_vendor.index.request({
  2590. url: requestUrl,
  2591. method: "GET",
  2592. success: (res) => {
  2593. if (res.data && res.data.code === 2e3 && res.data.data && res.data.data.items) {
  2594. console.log("获取追问问题数据成功:", res.data.data.items);
  2595. this.followUpQuestions = res.data.data.items.map((item) => ({
  2596. ...item,
  2597. question_form: 5
  2598. // 确保每个追问问题都有正确的question_form值
  2599. }));
  2600. this.processFollowUpQuestions();
  2601. } else {
  2602. console.error("获取追问问题数据失败:", res.data);
  2603. this.followUpQuestions = [];
  2604. }
  2605. },
  2606. fail: (err) => {
  2607. console.error("请求追问问题数据失败:", err);
  2608. this.followUpQuestions = [];
  2609. }
  2610. });
  2611. },
  2612. // 改进处理追问问题数据的方法
  2613. processFollowUpQuestions() {
  2614. if (!this.followUpQuestions || this.followUpQuestions.length === 0) {
  2615. console.log("没有追问问题数据");
  2616. this.followUpQuestions = [];
  2617. return;
  2618. }
  2619. console.log("处理追问问题数据,总数:", this.followUpQuestions.length);
  2620. this.followUpQuestions = this.followUpQuestions.filter((q) => q.parent_question_id);
  2621. const parentQuestionIds = /* @__PURE__ */ new Set();
  2622. this.followUpQuestions.forEach((question) => {
  2623. if (question.parent_question_id) {
  2624. parentQuestionIds.add(question.parent_question_id);
  2625. }
  2626. });
  2627. console.log("有追问的父问题ID:", Array.from(parentQuestionIds));
  2628. this.hasPlayedFollowUp = {};
  2629. },
  2630. // 修改检查并播放追问问题的方法
  2631. checkAndPlayFollowUpQuestion(parentQuestionId) {
  2632. console.log("检查是否有追问问题,父问题ID:", parentQuestionId);
  2633. if (this.isWaitingForAnswer) {
  2634. console.log("正在等待用户回答,暂不处理追问");
  2635. return;
  2636. }
  2637. if (!parentQuestionId) {
  2638. console.warn("没有父问题ID,无法检查追问问题");
  2639. this.proceedToNextQuestion();
  2640. return;
  2641. }
  2642. if (this.hasPlayedFollowUp[parentQuestionId]) {
  2643. console.log("已经播放过此问题的追问,直接进入下一个问题");
  2644. this.retryCount = 0;
  2645. this.proceedToNextQuestion();
  2646. return;
  2647. }
  2648. const followUpQuestion = this.followUpQuestions.find(
  2649. (q) => q.parent_question_id === parentQuestionId && q.digital_human_video_url && q.question_form === 5
  2650. );
  2651. if (followUpQuestion) {
  2652. console.log("找到追问问题:", followUpQuestion);
  2653. const recordingDuration = this.getRecordingDuration();
  2654. const lowScoreDuration = 7;
  2655. if (recordingDuration < lowScoreDuration) {
  2656. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  2657. this.retryCount++;
  2658. this.needPlayLowScoreVideo = true;
  2659. this.playLowScoreVideo();
  2660. return;
  2661. }
  2662. this.retryCount = 0;
  2663. this.hasPlayedFollowUp[parentQuestionId] = true;
  2664. this.isFollowUpQuestion = true;
  2665. this.currentParentQuestionId = parentQuestionId;
  2666. this.currentFollowUpQuestion = followUpQuestion;
  2667. this.playFollowUpQuestionVideo(followUpQuestion);
  2668. } else {
  2669. console.log("没有找到对应的追问问题,继续下一个问题");
  2670. this.retryCount = 0;
  2671. this.proceedToNextQuestion();
  2672. }
  2673. },
  2674. // 修改播放追问问题视频的方法
  2675. playFollowUpQuestionVideo(followUpQuestion) {
  2676. console.log("播放追问问题视频:", followUpQuestion.digital_human_video_url);
  2677. this.parentQuestion = this.currentSubtitle;
  2678. this.videoUrl = followUpQuestion.digital_human_video_url;
  2679. this.videoPlaying = true;
  2680. this.isFollowUpQuestion = true;
  2681. this.currentFollowUpQuestion = followUpQuestion;
  2682. const followUpSubtitles = [{
  2683. startTime: 0,
  2684. endTime: 60,
  2685. // 延长显示时间到60秒
  2686. text: followUpQuestion.digital_human_video_subtitle || followUpQuestion.question
  2687. }];
  2688. const subtitleKey = `followUpSubtitles_${followUpQuestion.id}`;
  2689. this[subtitleKey] = followUpSubtitles;
  2690. this.$nextTick(() => {
  2691. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2692. if (videoContext) {
  2693. videoContext.play();
  2694. this.currentSubtitle = followUpSubtitles[0].text;
  2695. }
  2696. });
  2697. },
  2698. // 添加作废面试的方法
  2699. invalidateInterview() {
  2700. if (this.isRecording) {
  2701. this.stopRecordingAnswer();
  2702. }
  2703. if (this.recordingTimer) {
  2704. clearInterval(this.recordingTimer);
  2705. }
  2706. this.isRecording = false;
  2707. this.showStopRecordingButton = false;
  2708. this.showStartRecordingButton = false;
  2709. setTimeout(() => {
  2710. common_vendor.index.switchTab({
  2711. url: "/pages/index/index"
  2712. });
  2713. }, 500);
  2714. },
  2715. initPersonDetectionWebSocket() {
  2716. if (this.personDetectionSocket) {
  2717. this.cleanupPersonDetectionWebSocket();
  2718. }
  2719. try {
  2720. this.personDetectionSocket = common_vendor.index.connectSocket({
  2721. url: `${common_config.personDetectionWsUrl}/ws/interview-room/room_${common_vendor.index.getStorageSync("appId")}/${common_vendor.index.getStorageSync("appId")}/`,
  2722. success: () => {
  2723. console.log("WebSocket connection initiated");
  2724. },
  2725. fail: (error) => {
  2726. console.error("WebSocket connection failed:", error);
  2727. }
  2728. });
  2729. this.personDetectionSocket.onOpen(() => {
  2730. console.log("WebSocket connection opened");
  2731. this.startPersonDetectionInterval();
  2732. });
  2733. this.personDetectionSocket.onError((error) => {
  2734. console.error("WebSocket error:", error);
  2735. this.cleanupPersonDetectionWebSocket();
  2736. });
  2737. this.personDetectionSocket.onClose(() => {
  2738. console.log("WebSocket connection closed");
  2739. this.cleanupPersonDetectionWebSocket();
  2740. });
  2741. this.personDetectionSocket.onMessage((res) => {
  2742. try {
  2743. const data = JSON.parse(res.data);
  2744. if (data.type === "person_detection_result") {
  2745. this.handlePersonDetectionResult(data);
  2746. }
  2747. } catch (error) {
  2748. console.error("Error parsing WebSocket message:", error);
  2749. }
  2750. });
  2751. } catch (error) {
  2752. console.error("Error initializing WebSocket:", error);
  2753. this.cleanupPersonDetectionWebSocket();
  2754. }
  2755. },
  2756. startPersonDetectionInterval() {
  2757. if (this.personDetectionInterval) {
  2758. clearInterval(this.personDetectionInterval);
  2759. }
  2760. this.personDetectionInterval = setInterval(() => {
  2761. if (this.personDetectionSocket && this.cameraContext) {
  2762. this.cameraContext.takePhoto({
  2763. quality: "low",
  2764. success: (res) => {
  2765. const tempFilePath = res.tempImagePath;
  2766. common_vendor.index.getFileSystemManager().readFile({
  2767. filePath: tempFilePath,
  2768. encoding: "base64",
  2769. success: (res2) => {
  2770. const base64Image = res2.data;
  2771. this.personDetectionSocket.send({
  2772. data: JSON.stringify({
  2773. type: "person_detection",
  2774. image_data: base64Image
  2775. })
  2776. });
  2777. },
  2778. fail: (error) => {
  2779. console.error("Error reading image file:", error);
  2780. }
  2781. });
  2782. },
  2783. fail: (error) => {
  2784. console.error("Error taking photo:", error);
  2785. }
  2786. });
  2787. }
  2788. }, 3e3);
  2789. },
  2790. cleanupPersonDetectionWebSocket() {
  2791. if (this.personDetectionInterval) {
  2792. clearInterval(this.personDetectionInterval);
  2793. this.personDetectionInterval = null;
  2794. }
  2795. if (this.personDetectionSocket) {
  2796. try {
  2797. this.personDetectionSocket.close();
  2798. } catch (error) {
  2799. console.error("Error closing WebSocket:", error);
  2800. }
  2801. this.personDetectionSocket = null;
  2802. }
  2803. },
  2804. handlePersonDetectionResult(data) {
  2805. if (data.data.identity.status !== "identity_verified") {
  2806. setTimeout(() => {
  2807. this.showPageWarning = false;
  2808. }, 3e3);
  2809. }
  2810. },
  2811. // 添加新方法:处理追问问题
  2812. async handleFollowUpQuestion(questionData) {
  2813. console.log("处理追问问题数据:", questionData);
  2814. if (!questionData || !questionData.follow_up_voice_url || !questionData.follow_up_question) {
  2815. console.error("追问数据不完整:", questionData);
  2816. common_vendor.index.showToast({
  2817. title: "获取追问数据失败",
  2818. icon: "none"
  2819. });
  2820. return;
  2821. }
  2822. this.parentQuestion = questionData.original_question || "";
  2823. let audioUrl = "";
  2824. if (questionData.follow_up_voice_url.direct_url) {
  2825. audioUrl = questionData.follow_up_voice_url.direct_url;
  2826. } else if (questionData.follow_up_voice_url.file_url) {
  2827. audioUrl = questionData.follow_up_voice_url.file_url;
  2828. }
  2829. if (!audioUrl.startsWith("http")) {
  2830. audioUrl = common_config.apiBaseUrl + audioUrl;
  2831. }
  2832. console.log("处理后的音频URL:", audioUrl);
  2833. const followUpQuestionObj = {
  2834. id: Date.now(),
  2835. question: questionData.follow_up_question.trim(),
  2836. // 去除可能的空白字符
  2837. audioUrl,
  2838. originalQuestion: questionData.original_question,
  2839. sessionId: questionData.session_id
  2840. };
  2841. console.log("创建的追问问题对象:", followUpQuestionObj);
  2842. this.followUpQuestions.push(followUpQuestionObj);
  2843. this.currentFollowUpIndex = this.followUpQuestions.length - 1;
  2844. this.followUpQuestion = followUpQuestionObj.question;
  2845. this.followUpAudioUrl = followUpQuestionObj.audioUrl;
  2846. this.isFollowUpQuestion = true;
  2847. this.currentFollowUpQuestion = followUpQuestionObj;
  2848. this.showSubtitle(followUpQuestionObj.question, true);
  2849. try {
  2850. await this.playFollowUpAudio();
  2851. console.log("音频播放完成,准备录制回答");
  2852. this.prepareToAnswer(true, followUpQuestionObj);
  2853. } catch (error) {
  2854. console.error("音频播放失败:", error);
  2855. this.prepareToAnswer(true, followUpQuestionObj);
  2856. }
  2857. },
  2858. // 播放追问音频
  2859. async playFollowUpAudio() {
  2860. return new Promise((resolve, reject) => {
  2861. console.log("开始播放追问音频, URL:", this.followUpAudioUrl);
  2862. if (!this.followUpAudioUrl) {
  2863. console.error("没有音频URL");
  2864. reject(new Error("没有音频URL"));
  2865. return;
  2866. }
  2867. common_vendor.index.showLoading({
  2868. title: "加载音频中..."
  2869. });
  2870. this.stopAndDestroyAudio();
  2871. try {
  2872. const innerAudioContext = common_vendor.index.createInnerAudioContext();
  2873. this.audioContext = innerAudioContext;
  2874. innerAudioContext.autoplay = true;
  2875. innerAudioContext.obeyMuteSwitch = false;
  2876. innerAudioContext.volume = 1;
  2877. innerAudioContext.onCanplay(() => {
  2878. console.log("音频可以播放");
  2879. common_vendor.index.hideLoading();
  2880. this.isAudioPlaying = true;
  2881. common_vendor.index.showToast({
  2882. title: "正在播放追问",
  2883. icon: "none",
  2884. duration: 2e3
  2885. });
  2886. });
  2887. innerAudioContext.onEnded(() => {
  2888. console.log("追问音频播放完成");
  2889. this.isAudioPlaying = false;
  2890. resolve();
  2891. setTimeout(() => {
  2892. this.stopAndDestroyAudio();
  2893. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  2894. }, 100);
  2895. });
  2896. innerAudioContext.onError((res) => {
  2897. console.error("音频播放错误:", res);
  2898. this.isAudioPlaying = false;
  2899. common_vendor.index.hideLoading();
  2900. common_vendor.index.showToast({
  2901. title: "音频播放失败",
  2902. icon: "none"
  2903. });
  2904. reject(res);
  2905. this.stopAndDestroyAudio();
  2906. });
  2907. console.log("设置音频源:", this.followUpAudioUrl);
  2908. innerAudioContext.src = this.followUpAudioUrl;
  2909. } catch (error) {
  2910. console.error("创建或播放音频失败:", error);
  2911. this.isAudioPlaying = false;
  2912. common_vendor.index.hideLoading();
  2913. common_vendor.index.showToast({
  2914. title: "音频播放失败",
  2915. icon: "none"
  2916. });
  2917. this.stopAndDestroyAudio();
  2918. reject(error);
  2919. }
  2920. });
  2921. },
  2922. // 停止并销毁音频
  2923. stopAndDestroyAudio() {
  2924. if (!this.audioContext) {
  2925. return;
  2926. }
  2927. const ctx = this.audioContext;
  2928. this.audioContext = null;
  2929. try {
  2930. if (this.isAudioPlaying) {
  2931. ctx.stop();
  2932. }
  2933. } catch (error) {
  2934. console.error("停止音频播放失败:", error);
  2935. }
  2936. try {
  2937. ctx.destroy();
  2938. } catch (error) {
  2939. console.error("销毁音频实例失败:", error);
  2940. }
  2941. this.isAudioPlaying = false;
  2942. },
  2943. // 显示字幕
  2944. showSubtitle(text, isFollowUp = true) {
  2945. console.log("显示字幕:", text);
  2946. this.subtitleText = text;
  2947. this.showSubtitleText = true;
  2948. this.isFollowUpQuestion = isFollowUp;
  2949. this.$nextTick(() => {
  2950. setTimeout(() => {
  2951. this.showSubtitleText = false;
  2952. }, 8e3);
  2953. });
  2954. },
  2955. // 显示问题对话框
  2956. showQuestionDialog() {
  2957. if (this.followUpQuestion) {
  2958. common_vendor.index.showModal({
  2959. title: "追问问题",
  2960. content: this.followUpQuestion,
  2961. showCancel: false,
  2962. success: () => {
  2963. this.prepareToAnswer(true, this.followUpQuestions[this.currentFollowUpIndex]);
  2964. }
  2965. });
  2966. }
  2967. }
  2968. },
  2969. computed: {
  2970. // 计算进度比例
  2971. progressRatio() {
  2972. return this.recordingTimerCount / this.maxRecordingTime;
  2973. },
  2974. // 右半圆旋转角度
  2975. half1Turn() {
  2976. if (this.progressRatio <= 0.5) {
  2977. return this.progressRatio * 2 * 180;
  2978. } else {
  2979. return 180;
  2980. }
  2981. },
  2982. // 左半圆旋转角度
  2983. half2Turn() {
  2984. if (this.progressRatio <= 0.5) {
  2985. return 0;
  2986. } else {
  2987. return (this.progressRatio - 0.5) * 2 * 180;
  2988. }
  2989. },
  2990. // 修改进度百分比的计算方式
  2991. progressPercent() {
  2992. return Math.min(Math.round(this.recordingTimerCount / this.maxRecordingTime * 100), 100);
  2993. },
  2994. // 添加总题数计算属性
  2995. totalQuestions() {
  2996. return this.questions.length || 5;
  2997. }
  2998. }
  2999. };
  3000. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  3001. return common_vendor.e({
  3002. a: $data.videoUrl,
  3003. b: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  3004. c: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  3005. d: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  3006. e: common_vendor.o((...args) => $options.preventVideoControl && $options.preventVideoControl(...args)),
  3007. f: common_vendor.o((...args) => $options.preventVideoControl && $options.preventVideoControl(...args)),
  3008. g: $data.showAnswerButton
  3009. }, $data.showAnswerButton ? {
  3010. h: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  3011. } : {}, {
  3012. i: $data.currentSubtitle || $data.showSubtitleText && $data.subtitleText
  3013. }, $data.currentSubtitle || $data.showSubtitleText && $data.subtitleText ? common_vendor.e({
  3014. j: $data.currentVideoIndex > 0
  3015. }, $data.currentVideoIndex > 0 ? {
  3016. k: common_vendor.t($data.currentVideoIndex),
  3017. l: common_vendor.t($options.totalQuestions)
  3018. } : {}, {
  3019. m: !$data.isFollowUpQuestion
  3020. }, !$data.isFollowUpQuestion ? {
  3021. n: common_vendor.t($data.currentSubtitle)
  3022. } : {
  3023. o: common_vendor.t($data.parentQuestion),
  3024. p: common_vendor.t($data.subtitleText || $data.currentSubtitle)
  3025. }) : {}, {
  3026. q: $data.useMiniProgramCameraComponent
  3027. }, $data.useMiniProgramCameraComponent ? {
  3028. r: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  3029. } : {}, {
  3030. s: $data.showPageWarning ? 1 : "",
  3031. t: $data.loading
  3032. }, $data.loading ? {} : {}, {
  3033. v: $data.showDebugInfo
  3034. }, $data.showDebugInfo ? common_vendor.e({
  3035. w: $data.assistantResponse
  3036. }, $data.assistantResponse ? {
  3037. x: common_vendor.t($data.assistantResponse)
  3038. } : {}, {
  3039. y: $data.audioTranscript
  3040. }, $data.audioTranscript ? {
  3041. z: common_vendor.t($data.audioTranscript)
  3042. } : {}, {
  3043. A: common_vendor.f($data.processedResponses, (item, index, i0) => {
  3044. return common_vendor.e({
  3045. a: item.role
  3046. }, item.role ? {
  3047. b: common_vendor.t(item.role)
  3048. } : {}, {
  3049. c: item.transcript
  3050. }, item.transcript ? {
  3051. d: common_vendor.t(item.transcript)
  3052. } : {}, {
  3053. e: index
  3054. });
  3055. })
  3056. }) : {}, {
  3057. B: $data.showStopRecordingButton
  3058. }, $data.showStopRecordingButton ? {
  3059. C: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  3060. } : {}, {
  3061. D: $data.isRecording
  3062. }, $data.isRecording ? {
  3063. E: common_vendor.t($data.recordingTimeDisplay),
  3064. F: `conic-gradient(${$data.progressColor} ${$options.progressPercent}%, ${$data.progressBgColor} 0%)`
  3065. } : {}, {
  3066. G: $data.showStartRecordingButton
  3067. }, $data.showStartRecordingButton ? {
  3068. H: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  3069. } : {}, {
  3070. I: $data.showRetryButton
  3071. }, $data.showRetryButton ? {
  3072. J: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  3073. } : {}, {
  3074. K: $data.showCountdown
  3075. }, $data.showCountdown ? {
  3076. L: common_vendor.t($data.countdownValue)
  3077. } : {}, {
  3078. M: $data.showRerecordButton
  3079. }, $data.showRerecordButton ? {
  3080. N: common_vendor.o((...args) => $options.handleRerecordButtonClick && $options.handleRerecordButtonClick(...args))
  3081. } : {}, {
  3082. O: $data.showPageWarning ? 1 : ""
  3083. });
  3084. }
  3085. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-464e78c6"]]);
  3086. wx.createPage(MiniProgramPage);