interview-question.js 125 KB

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