interview-question.vue 160 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083
  1. <template>
  2. <div class="identity-verify-container">
  3. <div class="digital-human-container">
  4. <!-- AI数字人视频/图像显示区域 -->
  5. <div class="digital-human-video">
  6. <!-- 添加GIF图片显示区域 -->
  7. <!-- <img v-if="showGif && gifUrl" :src="gifUrl" class="gif-player" alt="面试官GIF" /> -->
  8. <!-- <video
  9. v-if="showGif && gifUrl"
  10. :src="gifUrl"
  11. id="mpVideo"
  12. autoplay
  13. class="video-player"
  14. :controls="false"
  15. loop
  16. muted
  17. object-fit="contain"
  18. ></video> v-if="!showGif" -->
  19. <!-- 视频播放区域,当showGif为false时显示 -->
  20. <video
  21. :src="videoUrl"
  22. id="myVideo"
  23. ref="videoPlayer"
  24. autoplay
  25. playsinline
  26. disablePictureInPicture
  27. controlsList="nodownload nofullscreen noremoteplayback"
  28. class="video-player"
  29. :controls="false"
  30. @error="handleVideoError"
  31. @ended="handleVideoEnded"
  32. @timeupdate="handleTimeUpdate">
  33. </video>
  34. <!-- 添加透明蒙层 -->
  35. <div class="video-overlay"></div>
  36. <div class="answer-button-container" v-if="showAnswerButton">
  37. <button class="answer-button" @click="handleAnswerButtonClick">
  38. 开始提问
  39. </button>
  40. </div>
  41. <!-- 添加字幕覆盖层 :data-time="formatTime(recordingTimerCount) || '03:30'"-->
  42. <div class="subtitle-overlay" v-if="currentSubtitle" >
  43. {{ currentSubtitle }}
  44. </div>
  45. <!-- 添加答题按钮 -->
  46. </div>
  47. <!-- 用户摄像头视频显示区域 -->
  48. <div class="user-camera-container">
  49. <!-- 在小程序环境中使用camera组件 -->
  50. <camera v-if="useMiniProgramCameraComponent"
  51. device-position="front"
  52. flash="off"
  53. class="user-camera-video"
  54. @error="handleCameraError"
  55. :record-audio="true"
  56. frame-size="small"
  57. resolution="low"
  58. :disable-zoom="true"
  59. :enable-camera="true"
  60. :enable-record="true"
  61. mode="normal">
  62. </camera>
  63. <!-- 在H5/App环境中使用video元素 -->
  64. <video v-else
  65. id="userCamera"
  66. ref="userCameraVideo"
  67. autoplay
  68. playsinline
  69. muted
  70. class="user-camera-video"
  71. :controls="false">
  72. </video>
  73. </div>
  74. <!-- 字幕/文本覆盖区域 -->
  75. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  76. {{ assistantResponse }}
  77. </div> -->
  78. </div>
  79. <!-- 加载状态 -->
  80. <div v-if="loading" class="loading">加载中...</div>
  81. <!-- 控制面板(可选,可以隐藏) -->
  82. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  83. <div v-if="showDebugInfo" class="response-container">
  84. <div v-if="assistantResponse" class="response-item">
  85. <div class="response-content">
  86. <span>助手回复: {{ assistantResponse }}</span>
  87. </div>
  88. </div>
  89. <div v-if="audioTranscript" class="response-item">
  90. <div class="response-content">
  91. <span>音频转写: {{ audioTranscript }}</span>
  92. </div>
  93. </div>
  94. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  95. <div class="response-content">
  96. <span v-if="item.role">角色: {{ item.role }}</span>
  97. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  98. </div>
  99. </div>
  100. </div>
  101. <!-- 添加停止录制按钮 -->
  102. <div class="stop-recording-button-container" v-if="showStopRecordingButton">
  103. <button class="stop-recording-button" @click="stopRecordingAnswer">
  104. 完成提问
  105. </button>
  106. </div>
  107. <!-- 添加录制指示器 -->
  108. <div class="recording-indicator" v-if="isRecording">
  109. <div class="recording-dot"></div>
  110. <span class="recording-text">正在录制...</span>
  111. <span class="timer-text">{{ recordingTimeDisplay || '00:00 / 05:00' }}</span>
  112. </div>
  113. <!-- 添加开始回答按钮 -->
  114. <div class="start-recording-button-container" v-if="showStartRecordingButton">
  115. <button class="start-recording-button" @click="handleStartRecordingClick">
  116. 开始提问
  117. </button>
  118. </div>
  119. <!-- 添加重试上传按钮 -->
  120. <div class="retry-button-container" v-if="showRetryButton">
  121. <button class="retry-button" @click="retryVideoUpload">
  122. 重新上传
  123. </button>
  124. </div>
  125. <!-- 在模板部分添加录制时长显示 -->
  126. <!-- <div class="recording-timer" v-if="isRecording">
  127. <span class="timer-text">{{ recordingTimeDisplay || '00:00' }}</span>
  128. </div> -->
  129. <!-- 添加上传状态指示器 -->
  130. <!-- <div class="upload-status-indicator" v-if="showUploadStatus && uploadStatusText">
  131. <div class="upload-status-content">
  132. <div class="upload-status-icon" :class="{'uploading': isUploading}"></div>
  133. <span class="upload-status-text">{{ uploadStatusText }}</span>
  134. </div>
  135. </div> -->
  136. <!-- 修改录制时长显示 -->
  137. <!-- <div class="recording-timer" v-if="isRecording">
  138. <span class="timer-text">{{ recordingTimeDisplay || '00:00 / 05:00' }}</span>
  139. <span class="remaining-time" :class="{'warning': remainingTime <= 10}">
  140. 剩余: {{ formatTime(remainingTime) }}
  141. </span>
  142. </div> -->
  143. <!-- 在模板部分添加倒计时蒙层 -->
  144. <div class="countdown-overlay" v-if="showCountdown">
  145. <div class="countdown-content">
  146. <div class="countdown-number">{{ countdownValue }}</div>
  147. <div class="countdown-text">准备开始回答</div>
  148. </div>
  149. </div>
  150. <!-- 在template中添加蒙层 -->
  151. <div class="ai-voice-overlay" v-if="isPlayingAiVoice">
  152. <div class="ai-voice-content">
  153. <div class="ai-voice-icon"></div>
  154. <div class="ai-voice-text">面试官正在回复...</div>
  155. </div>
  156. </div>
  157. <!-- 在模板部分添加继续提问和结束面试按钮 -->
  158. <div class="continue-question-container" v-if="showContinueQuestionButton">
  159. <button class="continue-question-button" @click="handleContinueQuestionClick">
  160. 继续提问
  161. </button>
  162. </div>
  163. <!-- <div class="end-interview-container" v-if="showEndInterviewButton">
  164. <button class="end-interview-button" @click="handleEndInterviewClick">
  165. 结束提问
  166. </button>
  167. </div> -->
  168. <!-- 添加提问轮次显示 -->
  169. <!-- <div class="question-round-indicator" v-if="questionRound > 0">
  170. <span class="round-text">第 {{ questionRound }}/{{ maxQuestionRounds }} 轮提问</span>
  171. </div> -->
  172. </div>
  173. </template>
  174. <script>
  175. import { apiBaseUrl } from '@/common/config.js';
  176. export default {
  177. name: 'IdentityVerify',
  178. data() {
  179. return {
  180. loading: false,
  181. responses: [],
  182. processedResponses: [],
  183. assistantResponse: '',
  184. audioTranscript: '',
  185. videoPlaying: false,
  186. showDebugInfo: false, // 设置为true可以显示调试信息
  187. videoUrl: 'https://data.qicai321.com/minlong/1809457d-30c5-40bb-b6d8-a8c2e8e1e9e2.mp4', //'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/abdaa6fda8494e3a8613304743ed0433.mp4', // 用于存储AI数字人视频URL
  188. showReplayButton: false,
  189. cameraStream: null, // 存储摄像头流
  190. cameraError: null, // 存储摄像头错误信息
  191. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  192. cameraContext: null, // 添加相机上下文
  193. currentSubtitle: '',
  194. subtitles: [
  195. {
  196. startTime: 0, // 开始时间(秒)
  197. endTime: 6, // 结束时间(秒)
  198. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  199. },
  200. ],
  201. // secondVideoSubtitles: [
  202. // {
  203. // startTime: 0,
  204. // endTime: 10,
  205. // text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  206. // }
  207. // ],
  208. // thirdVideoSubtitles: [
  209. // {
  210. // startTime: 0,
  211. // endTime: 4,
  212. // text: '在工作中,你如何确保个人防护装备的正确使用?'
  213. // }
  214. // ],
  215. // fourthVideoSubtitles: [
  216. // {
  217. // startTime: 0,
  218. // endTime: 4,
  219. // text: '描述一次你与团队合作改善生产流程的经历。'
  220. // }
  221. // ],
  222. // fifthVideoSubtitles: [
  223. // {
  224. // startTime: 0,
  225. // endTime: 6,
  226. // text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  227. // }
  228. // ],
  229. // sixthVideoSubtitles: [
  230. // {
  231. // startTime: 0,
  232. // endTime: 5,
  233. // text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  234. // }
  235. // ],
  236. showAnswerButton: false, // 控制答题按钮显示
  237. currentVideoIndex: 0, // 当前播放的视频索引
  238. videoList: [
  239. 'https://data.qicai321.com/minlong/1809457d-30c5-40bb-b6d8-a8c2e8e1e9e2.mp4'//结束
  240. ],
  241. isRecording: false,
  242. recordingTimer: null,
  243. showStopRecordingButton: false,
  244. mediaRecorder: null,
  245. recordedChunks: [],
  246. recorder: null,
  247. lastUploadedVideoUrl: '',
  248. showStartRecordingButton: false,
  249. showRetryButton: false, // 控制重试按钮显示
  250. lastVideoToRetry: null, // 存储上次失败的视频URL,用于重试
  251. recordingStartTime: null, // 录制开始时间
  252. recordingTimerCount: 0, // 录制计时器计数
  253. recordingTimeDisplay: '00:00', // 格式化的录制时间显示
  254. // 添加上传队列相关数据
  255. uploadQueue: [], // 存储待上传的视频
  256. isUploading: false, // 标记是否正在上传
  257. uploadProgress: {}, // 存储每个视频的上传进度
  258. uploadStatus: {}, // 存储每个视频的上传状态
  259. showUploadStatus: false, // 是否显示上传状态指示器
  260. uploadStatusText: '', // 上传状态文本
  261. mediaRecorderTimeout: null, // 用于存储MediaRecorder的超时机制
  262. maxRecordingTime: 300, // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  263. remainingTime: 300, // 剩余录制时间(秒)- 从60秒改为300秒
  264. // 修改初始视频索引,添加参数控制
  265. startFromLastVideo: false, // 是否从最后一个视频开始播放
  266. countdownValue: 10, // 倒计时数值
  267. showCountdown: false, // 是否显示倒计时蒙层
  268. countdownTimer: null,
  269. showGif: false, // 控制是否显示GIF
  270. gifUrl: '', // GIF图片的URL
  271. // 添加AI语音交互相关属性
  272. aiVoiceUrl: '', // AI语音URL
  273. aiText: '', // AI回复文本
  274. transcribedText: '', // 转写文本
  275. isPlayingAiVoice: false, // 是否正在播放AI语音
  276. aiAudioPlayer: null, // AI语音播放器
  277. // 添加新的状态变量
  278. showContinueQuestionButton: false, // 控制"继续提问"按钮显示
  279. showEndInterviewButton: false, // 控制"结束面试"按钮显示
  280. questionRound: 0, // 当前提问轮次
  281. maxQuestionRounds: 1, // 最大提问轮次(可配置)
  282. conversationHistory: [], // 对话历史记录
  283. pendingAutoNavigation: false, // 标记是否有待处理的自动跳转
  284. hasTriedFallbackMethod: false, // 标记是否尝试过备选方法播放音频
  285. hasShownTextFallbackToast: false, // 标记是否已显示文本回退提示
  286. }
  287. },
  288. onLoad(options) {
  289. // 检查是否有参数指定从最后一个视频开始
  290. if (options && options.startFromLast === 'true') {
  291. this.startFromLastVideo = true;
  292. // 设置为最后一个视频的索引
  293. this.currentVideoIndex = this.videoList.length - 1;
  294. }
  295. },
  296. mounted() {
  297. /* this.fetchData() */
  298. // 如果需要从最后一个视频开始,直接设置
  299. if (this.startFromLastVideo) {
  300. console.log('从最后一个视频开始播放');
  301. this.currentVideoIndex = this.videoList.length - 1;
  302. this.videoUrl = this.videoList[this.currentVideoIndex];
  303. }
  304. this.playDigitalHumanVideo();
  305. this.checkAudioPermission();
  306. this.initCamera();
  307. this.checkIOSCameraRecordPermission();
  308. // 添加防御性检查,避免渲染错误
  309. this.checkAndFixRenderingIssues();
  310. // 添加音频测试
  311. setTimeout(() => {
  312. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  313. this.testAudioInput();
  314. }
  315. }, 3000);
  316. uni.setKeepScreenOn({
  317. keepScreenOn: true
  318. });
  319. },
  320. beforeDestroy() {
  321. // 组件销毁前停止摄像头
  322. this.stopUserCamera();
  323. // 销毁AI音频播放器
  324. if (this.aiAudioPlayer) {
  325. try {
  326. this.aiAudioPlayer.destroy();
  327. } catch (e) {
  328. console.error('销毁音频播放器失败:', e);
  329. }
  330. this.aiAudioPlayer = null;
  331. }
  332. // 清除所有计时器
  333. if (this.recordingTimer) {
  334. clearInterval(this.recordingTimer);
  335. this.recordingTimer = null;
  336. }
  337. if (this.countdownTimer) {
  338. clearInterval(this.countdownTimer);
  339. this.countdownTimer = null;
  340. }
  341. if (this.mediaRecorderTimeout) {
  342. clearTimeout(this.mediaRecorderTimeout);
  343. this.mediaRecorderTimeout = null;
  344. }
  345. // 如果有正在进行的录制,停止录制
  346. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  347. try {
  348. this.mediaRecorder.stop();
  349. } catch (e) {
  350. console.error('停止MediaRecorder失败:', e);
  351. }
  352. }
  353. if (this.recorder) {
  354. try {
  355. this.cameraContext.stopRecord();
  356. } catch (e) {
  357. console.error('停止小程序录制失败:', e);
  358. }
  359. }
  360. // 关闭屏幕常亮
  361. uni.setKeepScreenOn({
  362. keepScreenOn: false
  363. });
  364. },
  365. methods: {
  366. // 初始化相机
  367. async initCamera() {
  368. // 检查平台
  369. const systemInfo = uni.getSystemInfoSync();
  370. // 判断是否在小程序环境中
  371. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  372. systemInfo.uniPlatform === 'mp-alipay' ||
  373. systemInfo.uniPlatform === 'mp-baidu';
  374. if (isMiniProgram) {
  375. // 小程序环境使用小程序的相机API
  376. this.useMiniProgramCameraComponent = true;
  377. // 创建相机上下文
  378. this.cameraContext = uni.createCameraContext();
  379. // 确保已获取录音和相机权限
  380. uni.getSetting({
  381. success: (res) => {
  382. // 检查录音权限
  383. if (!res.authSetting['scope.record']) {
  384. uni.authorize({
  385. scope: 'scope.record',
  386. success: () => {
  387. console.log('录音权限已获取');
  388. },
  389. fail: (err) => {
  390. console.error('录音权限获取失败:', err);
  391. this.showPermissionDialog('录音');
  392. }
  393. });
  394. }
  395. // 检查相机权限
  396. if (!res.authSetting['scope.camera']) {
  397. uni.authorize({
  398. scope: 'scope.camera',
  399. success: () => {
  400. console.log('相机权限已获取');
  401. },
  402. fail: (err) => {
  403. console.error('相机权限获取失败:', err);
  404. this.showPermissionDialog('相机');
  405. }
  406. });
  407. }
  408. // 在iOS上,可能需要额外的权限检查
  409. const systemInfo = uni.getSystemInfoSync();
  410. if (systemInfo.platform === 'ios') {
  411. // 在iOS上,不要使用无效的权限范围
  412. // 移除以下代码块
  413. /*
  414. if (!res.authSetting['scope.camera.record']) {
  415. uni.authorize({
  416. scope: 'scope.camera.record',
  417. success: () => {
  418. console.log('相机录制权限已获取');
  419. },
  420. fail: (err) => {
  421. console.error('相机录制权限获取失败:', err);
  422. this.showPermissionDialog('相机录制');
  423. }
  424. });
  425. }
  426. */
  427. // 确保同时获取了相机和录音权限
  428. if (!res.authSetting['scope.camera'] || !res.authSetting['scope.record']) {
  429. console.log('iOS需要同时获取相机和录音权限');
  430. }
  431. }
  432. }
  433. });
  434. } else {
  435. // H5/App环境使用浏览器的API
  436. try {
  437. // 请求摄像头和麦克风权限并获取媒体流,添加更多约束
  438. const constraints = {
  439. audio: {
  440. echoCancellation: true,
  441. noiseSuppression: true,
  442. autoGainControl: true
  443. },
  444. video: {
  445. width: { ideal: 640, max: 1280 }, // 控制视频宽度
  446. height: { ideal: 480, max: 720 }, // 控制视频高度
  447. frameRate: { ideal: 15, max: 24 }, // 控制帧率
  448. facingMode: 'user'
  449. }
  450. };
  451. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  452. // 保存流以便后续使用
  453. this.cameraStream = stream;
  454. // 检查音频轨道
  455. const audioTracks = stream.getAudioTracks();
  456. console.log('音频轨道数量:', audioTracks.length);
  457. if (audioTracks.length > 0) {
  458. console.log('音频轨道已获取:', audioTracks[0].label);
  459. // 确保音频轨道已启用
  460. audioTracks[0].enabled = true;
  461. } else {
  462. console.warn('未检测到音频轨道,尝试单独获取音频');
  463. this.tryGetAudioOnly();
  464. }
  465. // 将流设置到视频元素
  466. const videoElement = this.$refs.userCameraVideo;
  467. if (videoElement) {
  468. videoElement.srcObject = stream;
  469. videoElement.muted = true; // 避免回声,但仍然录制声音
  470. }
  471. } catch (error) {
  472. console.error('获取摄像头失败:', error);
  473. this.cameraError = error.message || '无法访问摄像头';
  474. // 显示错误提示
  475. uni.showToast({
  476. title: '无法访问摄像头,请检查权限设置',
  477. icon: 'none'
  478. });
  479. }
  480. }
  481. },
  482. // 停止用户摄像头
  483. stopUserCamera() {
  484. if (this.cameraStream) {
  485. // 停止所有轨道
  486. this.cameraStream.getTracks().forEach(track => {
  487. track.stop();
  488. });
  489. this.cameraStream = null;
  490. }
  491. },
  492. async fetchData() {
  493. this.loading = true
  494. this.assistantResponse = ''
  495. this.audioTranscript = ''
  496. this.processedResponses = []
  497. try {
  498. // 使用uni.request代替fetch
  499. const requestTask = uni.request({
  500. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  501. method: 'POST',
  502. header: {
  503. 'Content-Type': 'application/json',
  504. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  505. },
  506. data: {
  507. "model": "qwen-omni-turbo",
  508. "messages": [
  509. {
  510. "role": "user",
  511. "content": [
  512. {
  513. "type": "input_audio",
  514. "input_audio": {
  515. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  516. "format": "wav"
  517. }
  518. },
  519. {
  520. "type": "text",
  521. "text": "这段音频在说什么"
  522. }
  523. ]
  524. }
  525. ],
  526. "stream":true,
  527. "stream_options":{
  528. "include_usage":true
  529. },
  530. "modalities":["text","audio"],
  531. "audio":{"voice":"Cherry","format":"wav"}
  532. },
  533. success: (res) => {
  534. console.log('请求成功,响应数据:', res.data);
  535. // 检查响应数据是否包含多个JSON对象
  536. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  537. // 处理包含多个JSON对象的情况
  538. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  539. chunks.forEach(chunk => {
  540. this.handleStreamResponse(chunk);
  541. });
  542. } else {
  543. // 处理单个响应对象的情况
  544. this.handleStreamResponse(res.data);
  545. }
  546. // 模拟获取到数字人视频并播放
  547. this.playDigitalHumanVideo();
  548. },
  549. fail: (err) => {
  550. console.error('请求失败:', err);
  551. },
  552. complete: () => {
  553. this.loading = false;
  554. }
  555. });
  556. } catch (error) {
  557. console.error('获取数据失败:', error);
  558. this.loading = false;
  559. }
  560. },
  561. handleStreamResponse(data) {
  562. // 处理流式响应数据
  563. if (typeof data === 'string') {
  564. // 处理字符串格式的响应
  565. if (data === '[DONE]') return;
  566. try {
  567. // 移除可能存在的换行符和多余空格
  568. const cleanData = data.trim();
  569. // 检查是否是有效的JSON字符串
  570. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  571. const jsonData = JSON.parse(cleanData);
  572. this.processStreamChunk(jsonData);
  573. }
  574. } catch (e) {
  575. console.error('解析JSON失败:', e, '原始数据:', data);
  576. }
  577. } else {
  578. // 处理对象格式的响应
  579. this.processStreamChunk(data);
  580. }
  581. },
  582. processStreamChunk(chunk) {
  583. if (chunk.choices && chunk.choices.length > 0) {
  584. const choice = chunk.choices[0];
  585. // 处理助手回复内容
  586. if (choice.delta && choice.delta.content) {
  587. this.assistantResponse += choice.delta.content;
  588. }
  589. // 处理音频转写内容
  590. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  591. this.audioTranscript += choice.delta.audio.transcript;
  592. }
  593. // 处理角色和音频转写
  594. if (choice.delta) {
  595. const result = {};
  596. if (choice.delta.role) {
  597. result.role = choice.delta.role;
  598. }
  599. if (choice.delta.audio && choice.delta.audio.transcript) {
  600. result.transcript = choice.delta.audio.transcript;
  601. }
  602. if (Object.keys(result).length > 0) {
  603. this.processedResponses.push(result);
  604. }
  605. }
  606. }
  607. },
  608. processResponseData() {
  609. // 处理返回的数据
  610. this.processedResponses = this.responses.map(item => {
  611. const result = {}
  612. // 处理角色信息
  613. if (item.delta && item.delta.role) {
  614. result.role = item.delta.role
  615. }
  616. // 处理音频转写文本
  617. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  618. result.transcript = item.delta.audio.transcript
  619. }
  620. return result
  621. }).filter(item => Object.keys(item).length > 0)
  622. },
  623. // 播放数字人视频
  624. playDigitalHumanVideo() {
  625. // 设置当前视频
  626. if(JSON.parse(uni.getStorageSync('configData'))==null){
  627. this.videoUrl = this.videoList[this.currentVideoIndex];
  628. }else{
  629. this.videoUrl =JSON.parse(uni.getStorageSync('configData')).digital_human?.ending_video_url || this.videoList[this.currentVideoIndex];
  630. }
  631. this.videoPlaying = true;
  632. console.log(`播放视频 ${this.currentVideoIndex + 1}/${this.videoList.length}: ${this.videoUrl}`);
  633. // 使用 uni.createVideoContext 来控制视频
  634. this.$nextTick(() => {
  635. const videoContext = uni.createVideoContext('myVideo', this);
  636. if (videoContext) {
  637. videoContext.play();
  638. // 设置超时检查,确认视频是否真的在播放
  639. setTimeout(() => {
  640. if (this.videoPlaying && this.$refs.videoPlayer) {
  641. console.log('视频应该正在播放');
  642. } else {
  643. console.log('视频可能未成功播放,尝试替代方案');
  644. this.tryAlternativeVideoPath();
  645. }
  646. }, 1000);
  647. } else {
  648. console.error('无法创建视频上下文');
  649. this.tryAlternativeVideoPath();
  650. }
  651. });
  652. },
  653. // 修改 tryAlternativeVideoPath 方法
  654. tryAlternativeVideoPath() {
  655. console.log('尝试使用替代路径');
  656. // 尝试不同的路径格式
  657. const alternativePaths = [
  658. './static/demo.mp4',
  659. '../static/demo.mp4',
  660. 'static/demo.mp4',
  661. '/static/demo.mp4',
  662. // 添加绝对路径
  663. `${window.location.origin}/static/demo.mp4`
  664. ];
  665. // 获取当前路径索引
  666. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  667. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  668. // 设置下一个路径
  669. this.videoUrl = alternativePaths[nextPathIndex];
  670. console.log('尝试新路径:', this.videoUrl);
  671. this.$nextTick(() => {
  672. const videoContext = uni.createVideoContext('myVideo', this);
  673. if (videoContext) {
  674. videoContext.stop();
  675. videoContext.play();
  676. // 检查是否成功播放
  677. setTimeout(() => {
  678. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  679. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  680. this.checkVideoWithAPI();
  681. }
  682. }, 1000);
  683. }
  684. });
  685. },
  686. // 添加新方法:使用uni API检查视频
  687. checkVideoWithAPI() {
  688. // 尝试使用uni.getVideoInfo API检查视频是否可用
  689. uni.getVideoInfo({
  690. src: '/static/demo.mp4',
  691. success: (res) => {
  692. console.log('视频信息获取成功:', res);
  693. // 如果能获取到视频信息,再次尝试播放
  694. this.videoUrl = '/static/demo.mp4';
  695. this.$nextTick(() => {
  696. const videoContext = uni.createVideoContext('myVideo', this);
  697. if (videoContext) {
  698. videoContext.play();
  699. }
  700. });
  701. },
  702. fail: (err) => {
  703. console.error('视频信息获取失败:', err);
  704. // 最后尝试使用uni.chooseVideo API
  705. this.fallbackToLocalVideo();
  706. }
  707. });
  708. },
  709. // 添加新方法:回退到本地视频
  710. fallbackToLocalVideo() {
  711. console.log('尝试使用本地视频资源');
  712. // 检查平台
  713. const platform = uni.getSystemInfoSync().platform;
  714. if (platform === 'android' || platform === 'ios') {
  715. // 移动端可以尝试使用本地资源
  716. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  717. this.$nextTick(() => {
  718. const videoContext = uni.createVideoContext('myVideo', this);
  719. if (videoContext) {
  720. videoContext.play();
  721. }
  722. });
  723. } else {
  724. // 最终回退到静态图片
  725. this.videoPlaying = false;
  726. uni.showToast({
  727. title: '视频加载失败,显示静态图片',
  728. icon: 'none'
  729. });
  730. }
  731. },
  732. // 修改 handleVideoError 方法
  733. handleVideoError(e) {
  734. console.error('视频加载错误:', e);
  735. // 记录更详细的错误信息
  736. if (e && e.detail) {
  737. console.error('详细错误信息:', e.detail);
  738. }
  739. // 检查视频文件是否存在
  740. uni.getFileInfo({
  741. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  742. success: (res) => {
  743. console.log('文件存在,大小:', res.size);
  744. // 文件存在但播放失败,可能是格式问题
  745. this.tryDifferentFormat();
  746. },
  747. fail: (err) => {
  748. console.error('文件不存在或无法访问:', err);
  749. // 尝试不同路径
  750. this.tryAlternativeVideoPath();
  751. }
  752. });
  753. // 如果多次尝试后仍然失败,显示错误信息
  754. uni.showToast({
  755. title: '视频加载失败,请检查文件是否存在',
  756. icon: 'none',
  757. duration: 2000
  758. });
  759. },
  760. // 添加新方法:尝试不同格式
  761. tryDifferentFormat() {
  762. console.log('尝试不同的视频格式');
  763. // 尝试不同的视频格式
  764. const formats = [
  765. { ext: 'mp4', mime: 'video/mp4' },
  766. { ext: 'webm', mime: 'video/webm' },
  767. { ext: 'ogg', mime: 'video/ogg' },
  768. { ext: 'mov', mime: 'video/quicktime' }
  769. ];
  770. // 获取当前文件名(不含扩展名)
  771. const currentPath = this.videoUrl;
  772. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  773. // 尝试下一个格式
  774. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  775. if (nextFormat) {
  776. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  777. console.log('尝试新格式:', this.videoUrl);
  778. this.$nextTick(() => {
  779. const videoContext = uni.createVideoContext('myVideo', this);
  780. if (videoContext) {
  781. videoContext.stop();
  782. videoContext.play();
  783. }
  784. });
  785. } else {
  786. // 所有格式都尝试过了,使用内置资源
  787. this.useBuiltInResource();
  788. }
  789. },
  790. // 添加新方法:使用内置资源
  791. useBuiltInResource() {
  792. console.log('尝试使用内置资源');
  793. // 检查平台
  794. const platform = uni.getSystemInfoSync().platform;
  795. // 根据平台选择合适的视频源
  796. if (platform === 'windows') {
  797. // Windows平台特定处理
  798. // 尝试使用相对于应用根目录的路径
  799. const appRoot = process.env.UNI_INPUT_DIR || '';
  800. this.videoUrl = `./static/demo.mp4`;
  801. // 或者尝试使用file://协议
  802. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  803. console.log('Windows平台尝试路径:', this.videoUrl);
  804. } else if (platform === 'android' || platform === 'ios') {
  805. // 移动端
  806. this.useNativeVideo();
  807. } else {
  808. // Web平台
  809. // 尝试使用完整URL
  810. const baseUrl = window.location.origin;
  811. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  812. console.log('Web平台尝试URL:', this.videoUrl);
  813. }
  814. this.$nextTick(() => {
  815. const videoContext = uni.createVideoContext('myVideo', this);
  816. if (videoContext) {
  817. videoContext.play();
  818. }
  819. });
  820. },
  821. // 添加新方法:使用原生视频能力
  822. useNativeVideo() {
  823. console.log('尝试使用原生视频能力');
  824. // 在移动端,可以尝试使用原生视频播放器
  825. uni.chooseVideo({
  826. sourceType: ['album'],
  827. success: (res) => {
  828. this.videoUrl = res.tempFilePath;
  829. this.$nextTick(() => {
  830. const videoContext = uni.createVideoContext('myVideo', this);
  831. if (videoContext) {
  832. videoContext.play();
  833. }
  834. });
  835. },
  836. fail: () => {
  837. // 如果用户取消选择,回退到静态图片
  838. this.videoPlaying = false;
  839. uni.showToast({
  840. title: '无法加载视频,显示静态图片',
  841. icon: 'none'
  842. });
  843. }
  844. });
  845. },
  846. // 处理视频结束事件
  847. handleVideoEnded() {
  848. console.log('视频播放结束,当前视频索引:', this.currentVideoIndex);
  849. this.videoPlaying = false;
  850. /* // 设置对应问题的GIF图片URL
  851. // if (this.currentVideoIndex === 0) {
  852. this.gifUrl = 'http://121.36.251.245:9000/minlong/cae2da8f92ce4af278d3929ab8c5973f.mp4';
  853. // } else if (this.currentVideoIndex === 1) {
  854. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  855. // } else if (this.currentVideoIndex === 2) {
  856. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  857. // } else if (this.currentVideoIndex === 3) {
  858. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  859. // } else if (this.currentVideoIndex === 4) {
  860. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  861. // } else if (this.currentVideoIndex === 5) {
  862. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  863. // }
  864. // 显示GIF图片
  865. this.showGif = true; */
  866. // 视频结束后直接显示"开始作答"按钮
  867. this.showStartRecordingButton = true;
  868. this.startCountdown();
  869. this.showAnswerButton = false;
  870. },
  871. // 添加新方法:开始倒计时
  872. startCountdown() {
  873. // 显示倒计时蒙层
  874. this.showCountdown = true;
  875. this.countdownValue = 10;
  876. // 开始倒计时
  877. this.countdownTimer = setInterval(() => {
  878. this.countdownValue--;
  879. if (this.countdownValue <= 0) {
  880. // 倒计时结束,清除定时器
  881. this.clearCountdown();
  882. // 隐藏"开始回答"按钮
  883. this.showStartRecordingButton = false;
  884. // 开始录制
  885. this.startRecordingAnswer();
  886. }
  887. }, 1000);
  888. },
  889. // 添加新方法:清除倒计时
  890. clearCountdown() {
  891. if (this.countdownTimer) {
  892. clearInterval(this.countdownTimer);
  893. this.countdownTimer = null;
  894. }
  895. this.showCountdown = false;
  896. },
  897. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  898. handleStartRecordingClick() {
  899. // 隐藏"开始回答"按钮
  900. this.showStartRecordingButton = false;
  901. // 如果倒计时正在进行,清除倒计时
  902. this.clearCountdown();
  903. // 直接开始录制
  904. this.startRecordingAnswer();
  905. },
  906. // 修复 stopRecordingAnswer 方法,确保录制正确停止并上传
  907. stopRecordingAnswer() {
  908. // 如果倒计时正在进行,先清除倒计时
  909. this.clearCountdown();
  910. // 检查录制时长
  911. const recordingDuration = this.getRecordingDuration();
  912. const minimumDuration = 5; // 最小录制时长(秒)
  913. if (recordingDuration < minimumDuration) {
  914. // 录制时间过短,显示提示
  915. uni.showToast({
  916. title: '录制时间过短,请至少录制5秒',
  917. icon: 'none',
  918. duration: 2000
  919. });
  920. // 不执行停止录制逻辑,继续录制
  921. return;
  922. }
  923. // 隐藏停止录制按钮
  924. this.showStopRecordingButton = false;
  925. // 停止录制计时器
  926. if (this.recordingTimer) {
  927. clearInterval(this.recordingTimer);
  928. this.recordingTimer = null;
  929. }
  930. // 根据平台选择不同的停止录制方法
  931. const systemInfo = uni.getSystemInfoSync();
  932. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  933. if (isMiniProgram) {
  934. // 小程序环境使用小程序的停止录制API
  935. this.stopMiniProgramRecording();
  936. } else {
  937. // H5/App环境使用MediaRecorder API
  938. this.stopBrowserRecording();
  939. }
  940. // 标记录制已停止
  941. this.isRecording = false;
  942. },
  943. // 添加新方法:开始录制用户回答
  944. startRecordingAnswer() {
  945. console.log('开始录制用户回答');
  946. this.isRecording = true;
  947. // 记录录制开始时间
  948. this.recordingStartTime = Date.now();
  949. this.recordingTimerCount = 0;
  950. this.remainingTime = this.maxRecordingTime;
  951. // 启动计时器,每秒更新计时
  952. this.recordingTimer = setInterval(() => {
  953. this.recordingTimerCount++;
  954. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  955. // 更新UI显示录制时长和剩余时间
  956. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount) +
  957. ' / ' + this.formatTime(this.maxRecordingTime);
  958. // 检查是否达到最大录制时间
  959. if (this.recordingTimerCount >= this.maxRecordingTime) {
  960. console.log('已达到最大录制时间(60秒),自动停止录制');
  961. this.stopRecordingAnswer();
  962. }
  963. }, 1000);
  964. // 显示录制中的提示
  965. // uni.showLoading({
  966. // title: '正在录制...',
  967. // mask: true
  968. // });
  969. // 根据平台选择不同的录制方法
  970. const systemInfo = uni.getSystemInfoSync();
  971. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  972. if (isMiniProgram) {
  973. // 小程序环境使用小程序的录制API
  974. this.startMiniProgramRecording();
  975. } else {
  976. // H5/App环境使用MediaRecorder API
  977. this.startBrowserRecording();
  978. }
  979. // 显示停止录制按钮
  980. this.showStopRecordingButton = true;
  981. },
  982. // 添加一个新方法:重置相机组件
  983. resetCamera() {
  984. console.log('重置相机组件');
  985. // 先完全移除相机组件
  986. this.useMiniProgramCameraComponent = false;
  987. // 释放相机上下文
  988. if (this.cameraContext) {
  989. this.cameraContext = null;
  990. }
  991. // 延迟后重新创建相机组件
  992. setTimeout(() => {
  993. this.useMiniProgramCameraComponent = true;
  994. // 再次延迟创建相机上下文,确保组件已完全渲染
  995. setTimeout(() => {
  996. this.cameraContext = uni.createCameraContext();
  997. console.log('相机组件已重置');
  998. }, 500);
  999. }, 500);
  1000. },
  1001. // 修改 startMiniProgramRecording 方法
  1002. startMiniProgramRecording() {
  1003. console.log('开始小程序录制方法');
  1004. // 获取平台信息
  1005. const systemInfo = uni.getSystemInfoSync();
  1006. const isIOS = systemInfo.platform === 'ios';
  1007. // 在iOS上,先重置相机
  1008. if (isIOS) {
  1009. // 先重置相机组件
  1010. this.resetCamera();
  1011. // 延迟执行录制,给相机组件足够的初始化时间
  1012. setTimeout(() => {
  1013. this.actualStartRecording(isIOS);
  1014. }, 1000);
  1015. } else {
  1016. // Android直接开始
  1017. this.actualStartRecording(isIOS);
  1018. }
  1019. },
  1020. // 添加新方法:实际开始录制
  1021. actualStartRecording(isIOS) {
  1022. if (!this.cameraContext) {
  1023. this.cameraContext = uni.createCameraContext();
  1024. console.log('创建新的相机上下文');
  1025. }
  1026. // 确保有录音权限
  1027. uni.getSetting({
  1028. success: (res) => {
  1029. const hasRecordAuth = res.authSetting['scope.record'];
  1030. const hasCameraAuth = res.authSetting['scope.camera'];
  1031. if (!hasRecordAuth || !hasCameraAuth) {
  1032. console.warn('缺少必要权限,请求权限');
  1033. this.requestMiniProgramPermissions();
  1034. return;
  1035. }
  1036. // 在iOS上,先检查相机状态
  1037. if (isIOS) {
  1038. console.log('iOS: 检查相机状态');
  1039. // 使用最简单的选项,设置最大录制时间为5分钟
  1040. const options = {
  1041. timeout: 300000, // 300秒超时 (5分钟)
  1042. quality: 'low', // 降低质量
  1043. compressed: true,
  1044. success: () => {
  1045. console.log('iOS录制开始成功');
  1046. },
  1047. fail: (err) => {
  1048. console.error('iOS录制失败:', err);
  1049. // 如果失败,尝试使用不同的方法
  1050. this.useAlternativeRecordingMethod();
  1051. }
  1052. };
  1053. try {
  1054. console.log('尝试开始录制');
  1055. this.recorder = this.cameraContext.startRecord(options);
  1056. } catch (e) {
  1057. console.error('开始录制异常:', e);
  1058. this.useAlternativeRecordingMethod();
  1059. }
  1060. } else {
  1061. // Android使用标准选项,设置最大录制时间为5分钟
  1062. const options = {
  1063. timeout: 300000, // 300秒超时 (5分钟)
  1064. quality: 'medium',
  1065. compressed: true,
  1066. success: () => {
  1067. console.log('Android录制开始成功');
  1068. },
  1069. fail: (err) => {
  1070. console.error('Android录制失败:', err);
  1071. uni.showToast({
  1072. title: '录制失败,请检查相机权限',
  1073. icon: 'none'
  1074. });
  1075. this.proceedToNextQuestion();
  1076. }
  1077. };
  1078. this.recorder = this.cameraContext.startRecord(options);
  1079. }
  1080. }
  1081. });
  1082. },
  1083. // 添加新方法:使用替代录制方法
  1084. useAlternativeRecordingMethod() {
  1085. console.log('使用替代录制方法');
  1086. // 在iOS上,可以尝试使用chooseVideo API作为备选
  1087. uni.showActionSheet({
  1088. itemList: ['使用相册中的视频', '跳过此问题'],
  1089. success: (res) => {
  1090. if (res.tapIndex === 0) {
  1091. // 选择相册中的视频
  1092. uni.chooseVideo({
  1093. sourceType: ['album'],
  1094. maxDuration: 300, // 从60秒改为300秒
  1095. camera: 'front',
  1096. success: (res) => {
  1097. console.log('选择视频成功:', res.tempFilePath);
  1098. // 停止录制状态
  1099. this.isRecording = false;
  1100. this.showStopRecordingButton = false;
  1101. // 上传选择的视频
  1102. this.uploadRecordedVideo(res.tempFilePath);
  1103. },
  1104. fail: () => {
  1105. console.log('用户取消选择视频');
  1106. this.proceedToNextQuestion();
  1107. }
  1108. });
  1109. } else {
  1110. // 跳过此问题
  1111. console.log('用户选择跳过问题');
  1112. this.proceedToNextQuestion();
  1113. }
  1114. },
  1115. fail: () => {
  1116. console.log('操作取消');
  1117. this.proceedToNextQuestion();
  1118. }
  1119. });
  1120. },
  1121. // 添加新方法:请求小程序权限
  1122. requestMiniProgramPermissions() {
  1123. // 请求录音权限
  1124. uni.authorize({
  1125. scope: 'scope.record',
  1126. success: () => {
  1127. console.log('录音权限已获取');
  1128. // 请求相机权限
  1129. uni.authorize({
  1130. scope: 'scope.camera',
  1131. success: () => {
  1132. console.log('相机权限已获取');
  1133. // 权限都获取到后,开始录制
  1134. this.startMiniProgramRecording();
  1135. },
  1136. fail: (err) => {
  1137. console.error('相机权限获取失败:', err);
  1138. this.showPermissionDialog('相机');
  1139. }
  1140. });
  1141. },
  1142. fail: (err) => {
  1143. console.error('录音权限获取失败:', err);
  1144. this.showPermissionDialog('录音');
  1145. }
  1146. });
  1147. },
  1148. // 修改浏览器环境下的录制方法
  1149. startBrowserRecording() {
  1150. if (!this.cameraStream) {
  1151. console.error('没有可用的摄像头流');
  1152. uni.showToast({
  1153. title: '录制失败,摄像头未就绪',
  1154. icon: 'none'
  1155. });
  1156. this.proceedToNextQuestion();
  1157. return;
  1158. }
  1159. try {
  1160. // 检查流中是否包含音频轨道
  1161. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  1162. if (!hasAudio) {
  1163. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  1164. // 尝试重新获取带音频的媒体流
  1165. navigator.mediaDevices.getUserMedia({
  1166. audio: {
  1167. echoCancellation: true,
  1168. noiseSuppression: true,
  1169. autoGainControl: true
  1170. },
  1171. video: true
  1172. })
  1173. .then(newStream => {
  1174. // 检查新流是否包含音频轨道
  1175. const audioTracks = newStream.getAudioTracks();
  1176. if (audioTracks.length > 0) {
  1177. console.log('成功获取音频轨道:', audioTracks[0].label);
  1178. // 合并视频和音频轨道
  1179. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1180. const audioTrack = newStream.getAudioTracks()[0];
  1181. // 创建新的媒体流,包含视频和音频轨道
  1182. const combinedStream = new MediaStream();
  1183. if (videoTrack) combinedStream.addTrack(videoTrack);
  1184. if (audioTrack) combinedStream.addTrack(audioTrack);
  1185. this.cameraStream = combinedStream;
  1186. // 更新视频元素的源
  1187. const videoElement = this.$refs.userCameraVideo;
  1188. if (videoElement) {
  1189. videoElement.srcObject = combinedStream;
  1190. videoElement.muted = true; // 避免回声,但仍然录制声音
  1191. }
  1192. // 继续录制过程
  1193. this.setupMediaRecorder(combinedStream);
  1194. } else {
  1195. console.warn('仍然无法获取音频轨道');
  1196. // 使用现有流继续
  1197. this.setupMediaRecorder(this.cameraStream);
  1198. }
  1199. })
  1200. .catch(err => {
  1201. console.error('获取音频失败:', err);
  1202. // 使用现有流继续
  1203. this.setupMediaRecorder(this.cameraStream);
  1204. });
  1205. } else {
  1206. console.log('检测到音频轨道,直接使用');
  1207. // 已有音频轨道,直接使用
  1208. this.setupMediaRecorder(this.cameraStream);
  1209. }
  1210. } catch (error) {
  1211. console.error('浏览器录制失败:', error);
  1212. uni.showToast({
  1213. title: '录制失败,浏览器可能不支持此功能',
  1214. icon: 'none'
  1215. });
  1216. this.proceedToNextQuestion();
  1217. }
  1218. },
  1219. // 修改 setupMediaRecorder 方法
  1220. setupMediaRecorder(stream) {
  1221. // 检查流中的轨道
  1222. const videoTracks = stream.getVideoTracks();
  1223. const audioTracks = stream.getAudioTracks();
  1224. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  1225. // 尝试使用支持度更广的编码格式
  1226. let mimeType = '';
  1227. const supportedTypes = [
  1228. 'video/webm;codecs=vp9,opus',
  1229. 'video/webm;codecs=vp8,opus',
  1230. 'video/webm;codecs=h264,opus',
  1231. 'video/mp4;codecs=h264,aac',
  1232. 'video/webm',
  1233. 'video/mp4'
  1234. ];
  1235. for (const type of supportedTypes) {
  1236. if (MediaRecorder.isTypeSupported(type)) {
  1237. mimeType = type;
  1238. console.log('使用支持的MIME类型:', mimeType);
  1239. break;
  1240. }
  1241. }
  1242. // 创建MediaRecorder实例,设置较低的比特率以减小文件大小
  1243. const options = {
  1244. mimeType: mimeType || '',
  1245. audioBitsPerSecond: 64000, // 降低音频比特率
  1246. videoBitsPerSecond: 1000000, // 降低视频比特率到1Mbps
  1247. };
  1248. try {
  1249. this.mediaRecorder = new MediaRecorder(stream, options);
  1250. console.log('MediaRecorder创建成功,使用选项:', options);
  1251. } catch (e) {
  1252. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  1253. this.mediaRecorder = new MediaRecorder(stream);
  1254. }
  1255. // 存储录制的数据块
  1256. this.recordedChunks = [];
  1257. // 监听数据可用事件
  1258. this.mediaRecorder.ondataavailable = (event) => {
  1259. if (event.data && event.data.size > 0) {
  1260. this.recordedChunks.push(event.data);
  1261. console.log(`收到数据块: ${event.data.size} 字节`);
  1262. }
  1263. };
  1264. // 监听录制停止事件
  1265. this.mediaRecorder.onstop = async () => {
  1266. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  1267. if (this.recordedChunks.length === 0) {
  1268. console.error('没有录制到数据');
  1269. uni.showToast({
  1270. title: '录制失败,未捕获到数据',
  1271. icon: 'none'
  1272. });
  1273. this.proceedToNextQuestion();
  1274. return;
  1275. }
  1276. // 创建Blob对象
  1277. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  1278. const blob = new Blob(this.recordedChunks, { type: mimeType });
  1279. console.log('创建Blob,原始大小:', blob.size, '类型:', mimeType);
  1280. // 显示压缩中提示
  1281. uni.showLoading({
  1282. title: '正在处理视频...',
  1283. mask: true
  1284. });
  1285. try {
  1286. // 压缩视频
  1287. const compressedBlob = await this.compressVideo(blob);
  1288. // 将Blob转换为File对象
  1289. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1290. const file = new File([compressedBlob], fileName, { type: mimeType });
  1291. // 隐藏压缩提示
  1292. uni.hideLoading();
  1293. // 上传文件
  1294. this.uploadRecordedVideo(file);
  1295. } catch (error) {
  1296. console.error('视频处理失败:', error);
  1297. uni.hideLoading();
  1298. // 如果压缩失败,使用原始视频
  1299. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1300. const file = new File([blob], fileName, { type: mimeType });
  1301. this.uploadRecordedVideo(file);
  1302. }
  1303. };
  1304. // 监听错误
  1305. this.mediaRecorder.onerror = (event) => {
  1306. console.error('MediaRecorder错误:', event.error);
  1307. };
  1308. // 开始录制
  1309. try {
  1310. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  1311. console.log('MediaRecorder开始录制');
  1312. // 设置最大录制时间为60秒
  1313. // 注意:我们已经在startRecordingAnswer方法中设置了全局计时器
  1314. // 这里作为备份机制,确保即使全局计时器失效,也能停止录制
  1315. this.mediaRecorderTimeout = setTimeout(() => {
  1316. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  1317. console.log('MediaRecorder备份超时机制触发,停止录制');
  1318. this.mediaRecorder.stop();
  1319. }
  1320. }, 300000); // 300秒 (5分钟)
  1321. } catch (e) {
  1322. console.error('开始录制失败:', e);
  1323. }
  1324. },
  1325. // 添加新方法:停止录制用户回答
  1326. stopRecordingAnswer() {
  1327. console.log('停止录制用户回答');
  1328. if (this.countdownTimer) {
  1329. clearInterval(this.countdownTimer);
  1330. this.countdownTimer = null;
  1331. this.showCountdown = false;
  1332. }
  1333. // 检查录制时长
  1334. const recordingDuration = this.getRecordingDuration();
  1335. const minimumDuration = 5; // 最小录制时长(秒),从3秒改为5秒
  1336. if (recordingDuration < minimumDuration) {
  1337. // 录制时间过短,显示提示
  1338. uni.showToast({
  1339. title: '录制时间过短,请至少录制5秒',
  1340. icon: 'none',
  1341. duration: 2000
  1342. });
  1343. // 不执行停止录制逻辑,继续录制
  1344. return;
  1345. }
  1346. // 隐藏停止录制按钮
  1347. this.showStopRecordingButton = false;
  1348. // 停止录制计时器
  1349. if (this.recordingTimer) {
  1350. clearInterval(this.recordingTimer);
  1351. this.recordingTimer = null;
  1352. }
  1353. // 根据平台选择不同的停止录制方法
  1354. const systemInfo = uni.getSystemInfoSync();
  1355. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1356. if (isMiniProgram) {
  1357. // 小程序环境使用小程序的停止录制API
  1358. this.stopMiniProgramRecording();
  1359. } else {
  1360. // H5/App环境使用MediaRecorder API
  1361. this.stopBrowserRecording();
  1362. }
  1363. // 标记录制已停止
  1364. this.isRecording = false;
  1365. },
  1366. // 添加新方法:获取录制时长
  1367. getRecordingDuration() {
  1368. // 如果有明确的录制开始时间,计算实际录制时长
  1369. if (this.recordingStartTime) {
  1370. return (Date.now() - this.recordingStartTime) / 1000;
  1371. }
  1372. // 如果没有明确记录开始时间,尝试从视频元素获取
  1373. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1374. return this.$refs.userCameraVideo.currentTime || 0;
  1375. }
  1376. // 如果无法获取准确时长,返回估计值(如果有计时器)
  1377. if (this.recordingTimerCount) {
  1378. return this.recordingTimerCount;
  1379. }
  1380. // 默认返回0
  1381. return 0;
  1382. },
  1383. // 添加新方法:重置录制
  1384. resetRecording() {
  1385. // 保持录制状态,但重置计时器
  1386. if (this.recordingTimer) {
  1387. clearTimeout(this.recordingTimer);
  1388. }
  1389. // 重置录制开始时间
  1390. this.recordingStartTime = Date.now();
  1391. this.recordingTimerCount = 0;
  1392. // 如果是浏览器环境,需要重置MediaRecorder
  1393. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1394. // 停止当前录制
  1395. this.mediaRecorder.stop();
  1396. // 清空已录制的数据块
  1397. this.recordedChunks = [];
  1398. // 短暂延迟后重新开始录制
  1399. setTimeout(() => {
  1400. this.startBrowserRecording();
  1401. }, 500);
  1402. }
  1403. // 如果是小程序环境,需要重置相机录制
  1404. else if (this.cameraContext) {
  1405. // 停止当前录制
  1406. this.cameraContext.stopRecord({
  1407. success: () => {
  1408. console.log('重置录制:停止当前录制成功');
  1409. // 短暂延迟后重新开始录制
  1410. setTimeout(() => {
  1411. this.startMiniProgramRecording();
  1412. }, 500);
  1413. },
  1414. fail: (err) => {
  1415. console.error('重置录制:停止当前录制失败', err);
  1416. // 尝试直接重新开始录制
  1417. this.startMiniProgramRecording();
  1418. }
  1419. });
  1420. }
  1421. // 显示提示
  1422. uni.showToast({
  1423. title: '请重新开始回答',
  1424. icon: 'none',
  1425. duration: 2000
  1426. });
  1427. },
  1428. // 修复 completeRecordingStop 方法,确保视频正确上传和处理
  1429. completeRecordingStop() {
  1430. console.log('完成录制停止');
  1431. // 隐藏停止录制按钮
  1432. this.showStopRecordingButton = false;
  1433. // 停止录制计时器
  1434. if (this.recordingTimer) {
  1435. clearInterval(this.recordingTimer);
  1436. this.recordingTimer = null;
  1437. }
  1438. // 标记录制已停止
  1439. this.isRecording = false;
  1440. // 显示上传中状态
  1441. this.showUploadStatus = true;
  1442. this.uploadStatusText = '正在处理视频...';
  1443. this.isUploading = true;
  1444. // 根据平台选择不同的停止录制方法
  1445. const systemInfo = uni.getSystemInfoSync();
  1446. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1447. if (isMiniProgram) {
  1448. // 小程序环境使用小程序的停止录制API
  1449. this.stopMiniProgramRecording();
  1450. } else {
  1451. // H5/App环境使用MediaRecorder API
  1452. this.stopBrowserRecording();
  1453. }
  1454. },
  1455. // 修复 stopMiniProgramRecording 方法,确保小程序环境下正确停止录制
  1456. stopMiniProgramRecording() {
  1457. console.log('停止小程序录制');
  1458. if (!this.cameraContext) {
  1459. console.error('相机上下文不存在');
  1460. this.handleRecordingError(new Error('相机上下文不存在'));
  1461. return;
  1462. }
  1463. try {
  1464. // 显示加载提示
  1465. uni.showLoading({
  1466. title: '正在处理视频...',
  1467. mask: true
  1468. });
  1469. // 停止录制
  1470. this.cameraContext.stopRecord({
  1471. success: (res) => {
  1472. console.log('停止录制成功:', res);
  1473. // 隐藏加载提示
  1474. uni.hideLoading();
  1475. // 获取视频临时路径
  1476. const tempVideoPath = res.tempVideoPath || res.tempFilePath;
  1477. if (tempVideoPath) {
  1478. console.log('获取到视频临时路径:', tempVideoPath);
  1479. // 上传视频
  1480. this.uploadRecordedVideo(tempVideoPath);
  1481. } else {
  1482. console.error('未获取到视频临时路径');
  1483. this.handleRecordingError(new Error('未获取到视频临时路径'));
  1484. }
  1485. },
  1486. fail: (err) => {
  1487. console.error('停止录制失败:', err);
  1488. uni.hideLoading();
  1489. this.handleRecordingError(err);
  1490. }
  1491. });
  1492. } catch (e) {
  1493. console.error('停止录制异常:', e);
  1494. uni.hideLoading();
  1495. this.handleRecordingError(e);
  1496. }
  1497. },
  1498. // 添加新方法:停止浏览器录制
  1499. stopBrowserRecording() {
  1500. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1501. this.mediaRecorder.stop();
  1502. console.log('浏览器录制停止成功');
  1503. } else {
  1504. console.error('MediaRecorder不存在或已经停止');
  1505. this.proceedToNextQuestion();
  1506. }
  1507. },
  1508. // 修复 uploadRecordedVideo 方法,确保视频正确上传到服务器
  1509. uploadRecordedVideo(videoPath) {
  1510. console.log('开始上传录制的视频:', videoPath);
  1511. // 保存最后上传的视频路径,用于可能的重试
  1512. this.lastVideoToRetry = videoPath;
  1513. // 显示上传状态
  1514. this.showUploadStatus = true;
  1515. this.uploadStatusText = '正在上传视频...';
  1516. this.isUploading = true;
  1517. // 保存上传状态到本地存储
  1518. this.saveUploadStatus();
  1519. // 获取用户信息和租户ID
  1520. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1521. const openid = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).openid : '';
  1522. const application_id = uni.getStorageSync('appId') || '';
  1523. // 使用uni.uploadFile上传视频
  1524. const uploadTask = uni.uploadFile({
  1525. url: `${apiBaseUrl}/api/upload/`,
  1526. filePath: videoPath,
  1527. name: 'file',
  1528. formData: {
  1529. tenant_id: tenant_id,
  1530. openid: openid,
  1531. application_id: application_id,
  1532. biz_type: 'interview_video',
  1533. video_duration: this.recordingTimerCount || 0,
  1534. has_audio: 'true' // 明确标记包含音频
  1535. },
  1536. success: (uploadRes) => {
  1537. console.log('视频上传成功:', uploadRes);
  1538. try {
  1539. // 解析响应数据
  1540. const result = typeof uploadRes.data === 'string' ? JSON.parse(uploadRes.data) : uploadRes.data;
  1541. if ((result.success || result.code === 2000) && (result.data?.url || result.data?.permanent_link)) {
  1542. // 上传成功,获取视频URL
  1543. const videoUrl = result.data.url || result.data.permanent_link || '';
  1544. console.log('视频URL:', videoUrl);
  1545. // 更新上传状态
  1546. this.uploadStatusText = '视频处理完成,正在分析...';
  1547. // 保存上传状态
  1548. this.saveUploadStatus();
  1549. // 提交视频到AI语音交互接口
  1550. this.submitToAiVoiceInteraction(videoUrl);
  1551. } else {
  1552. console.error('上传响应格式错误:', result);
  1553. const errorMsg = result.msg || result.message || '上传响应格式错误';
  1554. this.handleUploadError(new Error(errorMsg));
  1555. }
  1556. } catch (e) {
  1557. console.error('解析上传响应失败:', e);
  1558. this.handleUploadError(e);
  1559. }
  1560. },
  1561. fail: (err) => {
  1562. console.error('视频上传失败:', err);
  1563. // 提供更友好的错误信息
  1564. let errorMessage = '网络错误,请检查网络连接';
  1565. if (err.errMsg) {
  1566. if (err.errMsg.includes('timeout')) {
  1567. errorMessage = '上传超时,请检查网络连接';
  1568. } else if (err.errMsg.includes('fail')) {
  1569. errorMessage = '上传失败: ' + err.errMsg;
  1570. }
  1571. }
  1572. this.handleUploadError(new Error(errorMessage));
  1573. },
  1574. complete: () => {
  1575. // 隐藏上传状态
  1576. this.showUploadStatus = false;
  1577. this.isUploading = false;
  1578. // 更新上传状态
  1579. this.saveUploadStatus();
  1580. }
  1581. });
  1582. // 监听上传进度
  1583. uploadTask.onProgressUpdate((res) => {
  1584. console.log('上传进度:', res.progress);
  1585. this.uploadStatusText = `正在上传视频... ${res.progress}%`;
  1586. // 如果上传速度过慢,提供提示
  1587. if (res.progress < 10 && res.totalTimeMs > 10000) {
  1588. this.uploadStatusText = `上传速度较慢,请耐心等待... ${res.progress}%`;
  1589. }
  1590. });
  1591. },
  1592. // 修改 processUploadQueue 方法,添加后台上传支持
  1593. processUploadQueue() {
  1594. // 如果队列为空,结束处理
  1595. if (this.uploadQueue.length === 0) {
  1596. this.isUploading = false;
  1597. this.showUploadStatus = false;
  1598. // 清除上传状态
  1599. try {
  1600. uni.removeStorageSync('videoUploadStatus');
  1601. } catch (e) {
  1602. console.error('清除上传状态失败:', e);
  1603. }
  1604. // 发送上传完成通知
  1605. this.notifyUploadComplete();
  1606. return;
  1607. }
  1608. // 标记为正在上传
  1609. this.isUploading = true;
  1610. this.showUploadStatus = true;
  1611. // 获取队列中的第一个任务
  1612. const task = this.uploadQueue[0];
  1613. // 更新任务状态
  1614. this.uploadStatus[task.id] = 'uploading';
  1615. this.updateUploadStatusText();
  1616. // 增加尝试次数
  1617. task.attempts++;
  1618. // 根据文件类型选择上传方法
  1619. if (typeof task.file !== 'string') {
  1620. // 浏览器环境,使用XMLHttpRequest上传
  1621. this.uploadFileWithXHR(task);
  1622. } else {
  1623. // 小程序环境,使用uni.uploadFile上传
  1624. this.uploadFileWithUni(task);
  1625. }
  1626. // 更新上传状态到本地存储
  1627. this.saveUploadStatus();
  1628. },
  1629. // 添加新方法:发送上传完成通知
  1630. notifyUploadComplete() {
  1631. // 使用uni.postMessage在页面间通信(小程序环境)
  1632. try {
  1633. uni.$emit('videoUploadComplete', {
  1634. timestamp: Date.now()
  1635. });
  1636. console.log('发送上传完成通知');
  1637. } catch (e) {
  1638. console.error('发送上传完成通知失败:', e);
  1639. }
  1640. // 如果在App环境,可以使用本地通知
  1641. const systemInfo = uni.getSystemInfoSync();
  1642. if (systemInfo.platform === 'android' || systemInfo.platform === 'ios') {
  1643. // 检查是否支持本地通知
  1644. if (plus && plus.push) {
  1645. plus.push.createMessage('视频上传完成', '您的面试视频已成功上传', {});
  1646. }
  1647. }
  1648. },
  1649. // 添加新方法:使用XMLHttpRequest上传文件
  1650. uploadFileWithXHR(task) {
  1651. // 获取用户信息
  1652. const userInfo = uni.getStorageSync('userInfo');
  1653. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1654. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1655. // 创建FormData
  1656. const formData = new FormData();
  1657. formData.append('file', task.file);
  1658. formData.append('openid', openid);
  1659. formData.append('tenant_id', tenant_id);
  1660. formData.append('application_id', uni.getStorageSync('appId'));
  1661. formData.append('question_id', task.questionId);
  1662. formData.append('video_duration', this.recordingTimerCount || 0);
  1663. formData.append('has_audio', 'true'); // 明确标记包含音频
  1664. // 创建XMLHttpRequest
  1665. const xhr = new XMLHttpRequest();
  1666. xhr.open('POST', `${apiBaseUrl}/api/upload/`, true);
  1667. xhr.timeout = 120000; // 2分钟超时
  1668. // 监听上传进度
  1669. xhr.upload.onprogress = (event) => {
  1670. if (event.lengthComputable) {
  1671. const percentComplete = Math.round((event.loaded / event.total) * 100);
  1672. this.uploadProgress[task.id] = percentComplete;
  1673. this.updateUploadStatusText();
  1674. }
  1675. };
  1676. // 监听请求完成
  1677. xhr.onload = () => {
  1678. if (xhr.status === 200) {
  1679. try {
  1680. const res = JSON.parse(xhr.responseText);
  1681. console.log('上传响应:', res);
  1682. if (res.code === 2000) {
  1683. // 获取上传后的视频URL
  1684. const videoUrl = res.data.url || res.data.photoUrl || '';
  1685. if (videoUrl) {
  1686. // 上传成功,更新状态
  1687. this.uploadStatus[task.id] = 'success';
  1688. this.updateUploadStatusText();
  1689. // 提交到面试接口
  1690. this.submitVideoToInterview(videoUrl, task);
  1691. } else {
  1692. this.handleUploadFailure(task, '视频URL获取失败');
  1693. }
  1694. } else {
  1695. this.handleUploadFailure(task, res.msg || '上传失败');
  1696. }
  1697. } catch (e) {
  1698. this.handleUploadFailure(task, '解析响应失败');
  1699. }
  1700. } else {
  1701. this.handleUploadFailure(task, 'HTTP状态: ' + xhr.status);
  1702. }
  1703. };
  1704. // 监听错误
  1705. xhr.onerror = () => {
  1706. this.handleUploadFailure(task, '网络错误');
  1707. };
  1708. // 监听超时
  1709. xhr.ontimeout = () => {
  1710. this.handleUploadFailure(task, '上传超时');
  1711. };
  1712. // 发送请求
  1713. xhr.send(formData);
  1714. },
  1715. // 添加新方法:使用uni.uploadFile上传文件
  1716. uploadFileWithUni(task) {
  1717. // 获取用户信息
  1718. const userInfo = uni.getStorageSync('userInfo');
  1719. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1720. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1721. // 创建上传任务
  1722. const uploadTask = uni.uploadFile({
  1723. url: `${apiBaseUrl}/api/upload/`,
  1724. filePath: task.file,
  1725. name: 'file',
  1726. formData: {
  1727. openid: openid,
  1728. tenant_id: tenant_id,
  1729. application_id: uni.getStorageSync('appId'),
  1730. question_id: task.questionId,
  1731. video_duration: this.recordingTimerCount || 0,
  1732. has_audio: 'true' // 明确标记包含音频
  1733. },
  1734. success: (uploadRes) => {
  1735. try {
  1736. const res = JSON.parse(uploadRes.data);
  1737. console.log('上传响应:', res);
  1738. if (res.code === 2000) {
  1739. // 获取上传后的视频URL
  1740. const videoUrl = res.data.permanent_link || res.data.url || '';
  1741. if (videoUrl) {
  1742. // 上传成功,更新状态
  1743. this.uploadStatus[task.id] = 'success';
  1744. this.updateUploadStatusText();
  1745. // 提交到面试接口
  1746. this.submitVideoToInterview(videoUrl, task);
  1747. } else {
  1748. this.handleUploadFailure(task, '视频URL获取失败');
  1749. }
  1750. } else {
  1751. this.handleUploadFailure(task, res.msg || '上传失败');
  1752. }
  1753. } catch (e) {
  1754. this.handleUploadFailure(task, '解析响应失败');
  1755. }
  1756. },
  1757. fail: (err) => {
  1758. this.handleUploadFailure(task, err.errMsg || '上传失败');
  1759. }
  1760. });
  1761. // 监听上传进度
  1762. uploadTask.onProgressUpdate((res) => {
  1763. this.uploadProgress[task.id] = res.progress;
  1764. this.updateUploadStatusText();
  1765. });
  1766. },
  1767. // 添加新方法:处理上传失败
  1768. handleUploadFailure(task, errorMsg) {
  1769. console.error('上传失败:', errorMsg);
  1770. // 更新任务状态
  1771. this.uploadStatus[task.id] = 'failed';
  1772. this.updateUploadStatusText();
  1773. // 检查是否还有重试机会
  1774. if (task.attempts < task.maxAttempts) {
  1775. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1776. // 5秒后重试
  1777. setTimeout(() => {
  1778. // 重置进度
  1779. this.uploadProgress[task.id] = 0;
  1780. // 重新开始上传
  1781. if (typeof task.file !== 'string') {
  1782. this.uploadFileWithXHR(task);
  1783. } else {
  1784. this.uploadFileWithUni(task);
  1785. }
  1786. }, 5000);
  1787. } else {
  1788. // 超过最大重试次数,移除任务并继续处理队列
  1789. console.log('超过最大重试次数,放弃上传');
  1790. // 显示错误提示
  1791. uni.showToast({
  1792. title: '视频上传失败,请稍后重试',
  1793. icon: 'none',
  1794. duration: 2000
  1795. });
  1796. // 从队列中移除当前任务
  1797. this.uploadQueue.shift();
  1798. // 继续处理队列中的下一个任务
  1799. this.processUploadQueue();
  1800. }
  1801. },
  1802. // 修改 submitVideoToInterview 方法
  1803. submitVideoToInterview(videoUrl, task = null) {
  1804. console.log('提交视频URL到面试接口:', videoUrl);
  1805. // 确定问题ID
  1806. let questionId;
  1807. if (task) {
  1808. questionId = task.questionId;
  1809. } else {
  1810. // 根据当前视频索引映射到正确的问题ID(兼容旧代码)
  1811. switch(this.currentVideoIndex) {
  1812. case 1:
  1813. questionId = 36;
  1814. break;
  1815. case 2:
  1816. questionId = 11;
  1817. break;
  1818. case 3:
  1819. questionId = 12;
  1820. break;
  1821. case 4:
  1822. questionId = 13;
  1823. break;
  1824. default:
  1825. questionId = 10;
  1826. }
  1827. }
  1828. // 准备请求参数
  1829. const requestData = {
  1830. application_id: uni.getStorageSync('appId'),
  1831. question_id: questionId,
  1832. video_url: videoUrl,
  1833. tenant_id: uni.getStorageSync('tenant_id') || '1',
  1834. has_audio: true // 明确标记包含音频
  1835. };
  1836. // 首先提交到面试接口
  1837. /* uni.request({
  1838. url: `${apiBaseUrl}/api/job/upload_video`,
  1839. method: 'POST',
  1840. data: requestData,
  1841. header: {
  1842. 'content-type': 'application/x-www-form-urlencoded'
  1843. },
  1844. success: (res) => {
  1845. console.log('面试接口提交成功:', res);
  1846. if (res.data.code === 0 || res.data.code === 2000) {
  1847. // 提交成功
  1848. if (task) {
  1849. // 从队列中移除当前任务
  1850. this.uploadQueue.shift();
  1851. // 继续处理队列中的下一个任务
  1852. this.processUploadQueue();
  1853. } else {
  1854. // 兼容旧代码的处理逻辑
  1855. uni.showToast({
  1856. title: '回答已提交',
  1857. icon: 'success'
  1858. });
  1859. // 保存最后上传的视频URL
  1860. this.lastUploadedVideoUrl = videoUrl;
  1861. // 隐藏重试按钮(如果之前显示了)
  1862. this.showRetryButton = false;
  1863. this.lastVideoToRetry = null;
  1864. }
  1865. // 提交到AI语音交互接口
  1866. } else {
  1867. // 提交失败
  1868. if (task) {
  1869. this.handleSubmitFailure(task, res.data.msg || '提交失败');
  1870. } else {
  1871. // 兼容旧代码的处理逻辑
  1872. uni.showToast({
  1873. title: res.data.msg || '提交失败,请重试',
  1874. icon: 'none'
  1875. });
  1876. // 保存失败的视频URL,用于重试
  1877. this.lastVideoToRetry = videoUrl;
  1878. // 显示重试按钮
  1879. this.showRetryButton = true;
  1880. }
  1881. }
  1882. },
  1883. fail: (err) => {
  1884. console.error('面试接口提交失败:', err);
  1885. if (task) {
  1886. this.handleSubmitFailure(task, err.errMsg || '网络错误');
  1887. } else {
  1888. // 兼容旧代码的处理逻辑
  1889. uni.hideLoading();
  1890. uni.showToast({
  1891. title: '网络错误,请重试',
  1892. icon: 'none'
  1893. });
  1894. // 保存失败的视频URL,用于重试
  1895. this.lastVideoToRetry = videoUrl;
  1896. // 显示重试按钮
  1897. this.showRetryButton = true;
  1898. }
  1899. }
  1900. }); */
  1901. this.submitToAiVoiceInteraction(videoUrl);
  1902. },
  1903. // 添加新方法:处理提交失败
  1904. handleSubmitFailure(task, errorMsg) {
  1905. console.error('提交失败:', errorMsg);
  1906. // 更新任务状态
  1907. this.uploadStatus[task.id] = 'failed';
  1908. this.updateUploadStatusText();
  1909. // 检查是否还有重试机会
  1910. if (task.attempts < task.maxAttempts) {
  1911. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1912. // 5秒后重试
  1913. setTimeout(() => {
  1914. // 重新提交
  1915. this.submitVideoToInterview(task.videoUrl, task);
  1916. }, 5000);
  1917. } else {
  1918. // 超过最大重试次数,移除任务并继续处理队列
  1919. console.log('超过最大重试次数,放弃提交');
  1920. // 显示错误提示
  1921. uni.showToast({
  1922. title: '视频提交失败,请稍后重试',
  1923. icon: 'none',
  1924. duration: 2000
  1925. });
  1926. // 从队列中移除当前任务
  1927. this.uploadQueue.shift();
  1928. // 继续处理队列中的下一个任务
  1929. this.processUploadQueue();
  1930. }
  1931. },
  1932. // 添加新方法:更新上传状态文本
  1933. updateUploadStatusText() {
  1934. if (this.uploadQueue.length === 0) {
  1935. this.uploadStatusText = '';
  1936. return;
  1937. }
  1938. const currentTask = this.uploadQueue[0];
  1939. const progress = this.uploadProgress[currentTask.id] || 0;
  1940. const status = this.uploadStatus[currentTask.id] || 'pending';
  1941. let statusText = '';
  1942. switch (status) {
  1943. case 'pending':
  1944. statusText = '等待上传';
  1945. break;
  1946. case 'uploading':
  1947. statusText = `上传中 ${progress}%`;
  1948. break;
  1949. case 'success':
  1950. statusText = '上传成功,提交中...';
  1951. break;
  1952. case 'failed':
  1953. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? '即将重试' : '已放弃'}`;
  1954. break;
  1955. }
  1956. this.uploadStatusText = `问题${currentTask.questionId - 9}:${statusText}`;
  1957. // 如果队列中有多个任务,显示总数
  1958. if (this.uploadQueue.length > 1) {
  1959. this.uploadStatusText += ` (${this.uploadQueue.length}个视频待处理)`;
  1960. }
  1961. },
  1962. // 修改 proceedToNextQuestion 方法
  1963. proceedToNextQuestion() {
  1964. console.log('准备跳转到下一页面');
  1965. // 如果正在播放AI语音,不立即跳转
  1966. if (this.isPlayingAiVoice) {
  1967. console.log('AI语音正在播放,等待播放完成后再跳转');
  1968. return;
  1969. }
  1970. // 不再等待上传队列完成,直接跳转
  1971. // this.navigateToNextPage();
  1972. // 如果有未完成的上传任务,在后台继续处理
  1973. if (this.uploadQueue.length > 0) {
  1974. console.log('上传队列将在后台继续处理...');
  1975. // 保存上传状态到本地存储,以便在其他页面可以查看
  1976. this.saveUploadStatus();
  1977. }
  1978. },
  1979. // 修改 navigateToNextPage 方法
  1980. navigateToNextPage() {
  1981. console.log('准备导航到下一个页面');
  1982. // 如果正在播放AI语音,不立即跳转
  1983. if (this.isPlayingAiVoice) {
  1984. console.log('AI语音正在播放,等待播放完成后再跳转');
  1985. return;
  1986. }
  1987. console.log('导航到下一个页面');
  1988. // 检查 selectedJob 的 id
  1989. try {
  1990. const currentJobDetail = uni.getStorageSync('selectedJob');
  1991. const jobId = currentJobDetail ? JSON.parse(currentJobDetail).id : null;
  1992. // 如果 id 为 9 或 10,直接跳转到 success 页面
  1993. if (jobId === 9 || jobId === 10) {
  1994. uni.navigateTo({
  1995. url: '/pages/success/success',
  1996. success: () => {
  1997. console.log('成功跳转到成功页面');
  1998. },
  1999. fail: (err) => {
  2000. console.error('跳转失败:', err);
  2001. // 如果跳转失败,尝试其他页面
  2002. this.handleNavigationFailure();
  2003. }
  2004. });
  2005. return; // 提前返回,不执行后续代码
  2006. }
  2007. } catch (e) {
  2008. console.error('解析 selectedJob 失败:', e);
  2009. // 解析失败时继续默认跳转逻辑
  2010. }
  2011. // 原有的跳转逻辑
  2012. uni.navigateTo({
  2013. url: '/pages/success/success',//'/pages/posture-guide/posture-guide?uploading=' + (this.uploadQueue.length > 0 ? 'true' : 'false'),
  2014. success: () => {
  2015. console.log('成功跳转到结果页面');
  2016. },
  2017. fail: (err) => {
  2018. console.error('跳转失败:', err);
  2019. this.handleNavigationFailure();
  2020. }
  2021. });
  2022. },
  2023. // 添加处理导航失败的方法
  2024. handleNavigationFailure() {
  2025. // 尝试跳转到其他页面
  2026. uni.navigateTo({
  2027. url: '/pages/success/success',//'/pages/posture-guide/posture-guide',
  2028. fail: (err2) => {
  2029. console.error('备用跳转也失败:', err2);
  2030. // 最后尝试返回上一页
  2031. uni.navigateBack({
  2032. delta: 1
  2033. });
  2034. }
  2035. });
  2036. },
  2037. // 修改 handleAnswerButtonClick 方法
  2038. handleAnswerButtonClick() {
  2039. // 隐藏答题按钮
  2040. this.showAnswerButton = false;
  2041. // 显示简短提示
  2042. uni.showToast({
  2043. title: '面试完成',
  2044. icon: 'success',
  2045. duration: 500 // 只显示0.5秒
  2046. });
  2047. // 如果有最后上传的视频URL,提交到AI语音交互接口
  2048. if (this.lastUploadedVideoUrl) {
  2049. this.submitToAiVoiceInteraction(this.lastUploadedVideoUrl);
  2050. } else {
  2051. // 没有视频URL,直接跳转
  2052. this.navigateToNextPage();
  2053. }
  2054. },
  2055. // 处理相机错误
  2056. handleCameraError(e) {
  2057. console.error('相机错误:', e);
  2058. // 获取平台信息
  2059. const systemInfo = uni.getSystemInfoSync();
  2060. const isIOS = systemInfo.platform === 'ios';
  2061. if (isIOS) {
  2062. console.log('iOS相机错误,尝试重新初始化');
  2063. // 显示提示
  2064. uni.showToast({
  2065. title: '相机初始化中...',
  2066. icon: 'loading',
  2067. duration: 2000
  2068. });
  2069. // 重置相机
  2070. this.resetCamera();
  2071. // 如果正在录制,停止录制
  2072. if (this.isRecording) {
  2073. this.isRecording = false;
  2074. this.showStopRecordingButton = false;
  2075. // 提供替代选项
  2076. setTimeout(() => {
  2077. this.useAlternativeRecordingMethod();
  2078. }, 1000);
  2079. }
  2080. } else {
  2081. // 显示错误提示
  2082. uni.showToast({
  2083. title: '相机初始化失败,请检查权限设置',
  2084. icon: 'none'
  2085. });
  2086. // 尝试备用选项
  2087. this.tryFallbackOptions();
  2088. }
  2089. },
  2090. // 添加新方法:尝试备用选项
  2091. tryFallbackOptions() {
  2092. // 检查环境
  2093. const systemInfo = uni.getSystemInfoSync();
  2094. // 在小程序环境中使用小程序API
  2095. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  2096. this.useMiniProgramCamera();
  2097. }
  2098. // 在H5环境中显示静态图像
  2099. else {
  2100. this.showStaticCameraPlaceholder();
  2101. }
  2102. },
  2103. // 添加新方法:使用小程序相机API
  2104. useMiniProgramCamera() {
  2105. console.log('尝试使用小程序相机组件');
  2106. // 这里需要在模板中添加小程序相机组件
  2107. // 并设置一个标志来控制显示
  2108. this.useMiniProgramCameraComponent = true;
  2109. },
  2110. // 添加新方法:显示静态图像
  2111. showStaticCameraPlaceholder() {
  2112. console.log('显示静态摄像头占位图');
  2113. // 创建一个图像元素
  2114. const img = document.createElement('img');
  2115. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  2116. img.className = 'static-camera-image';
  2117. img.style.width = '100%';
  2118. img.style.height = '100%';
  2119. img.style.objectFit = 'cover';
  2120. // 获取容器并添加图像
  2121. const container = this.$refs.userCameraVideo.parentNode;
  2122. container.appendChild(img);
  2123. },
  2124. // 处理视频时间更新事件
  2125. handleTimeUpdate(e) {
  2126. // 获取当前视频播放时间
  2127. const currentTime = e.target.currentTime;
  2128. // 如果正在录制,更新录制时间显示
  2129. if (this.isRecording && this.recordingTimerCount) {
  2130. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  2131. }
  2132. // 根据当前播放的视频索引选择对应的字幕数组
  2133. let currentSubtitles;
  2134. if (this.currentVideoIndex === 0) {
  2135. currentSubtitles = this.subtitles;
  2136. } else if (this.currentVideoIndex === 1) {
  2137. currentSubtitles = this.secondVideoSubtitles;
  2138. } else if (this.currentVideoIndex === 2) {
  2139. currentSubtitles = this.thirdVideoSubtitles;
  2140. } else if (this.currentVideoIndex === 3) {
  2141. currentSubtitles = this.fourthVideoSubtitles;
  2142. } else if (this.currentVideoIndex === 4) {
  2143. currentSubtitles = this.fifthVideoSubtitles;
  2144. } else if (this.currentVideoIndex === 5) {
  2145. currentSubtitles = this.sixthVideoSubtitles;
  2146. } else {
  2147. // 如果有更多视频,可以继续添加条件
  2148. currentSubtitles = [];
  2149. }
  2150. // 查找当前时间应该显示的字幕
  2151. const subtitle = currentSubtitles.find(
  2152. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  2153. );
  2154. // 更新当前字幕
  2155. this.currentSubtitle = subtitle ? subtitle.text : '';
  2156. },
  2157. // Add a new method to handle the "Start Recording" button click
  2158. handleStartRecordingClick() {
  2159. // 隐藏"开始回答"按钮
  2160. this.showStartRecordingButton = false;
  2161. // 如果倒计时正在进行,清除倒计时
  2162. this.clearCountdown();
  2163. // 开始录制
  2164. this.startRecordingAnswer();
  2165. },
  2166. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  2167. checkAudioPermission() {
  2168. // 在小程序环境中
  2169. const systemInfo = uni.getSystemInfoSync();
  2170. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2171. if (isMiniProgram) {
  2172. // 小程序环境下的权限请求
  2173. uni.getSetting({
  2174. success: (res) => {
  2175. if (!res.authSetting['scope.record']) {
  2176. uni.authorize({
  2177. scope: 'scope.record',
  2178. success: () => {
  2179. console.log('录音权限已获取');
  2180. },
  2181. fail: (err) => {
  2182. console.error('录音权限获取失败:', err);
  2183. this.showPermissionDialog('录音');
  2184. }
  2185. });
  2186. }
  2187. if (!res.authSetting['scope.camera']) {
  2188. uni.authorize({
  2189. scope: 'scope.camera',
  2190. success: () => {
  2191. console.log('相机权限已获取');
  2192. },
  2193. fail: (err) => {
  2194. console.error('相机权限获取失败:', err);
  2195. this.showPermissionDialog('相机');
  2196. }
  2197. });
  2198. }
  2199. }
  2200. });
  2201. } else {
  2202. // 浏览器环境下的权限请求
  2203. // ... 保持原有代码不变
  2204. }
  2205. },
  2206. // 添加新方法:测试音频输入
  2207. testAudioInput() {
  2208. if (!this.cameraStream) {
  2209. console.warn('没有可用的媒体流,无法测试音频');
  2210. return;
  2211. }
  2212. const audioTracks = this.cameraStream.getAudioTracks();
  2213. if (audioTracks.length === 0) {
  2214. console.warn('没有检测到音频轨道,尝试重新获取');
  2215. this.tryGetAudioOnly();
  2216. return;
  2217. }
  2218. console.log('音频轨道信息:', audioTracks[0].getSettings());
  2219. // 创建音频上下文和分析器
  2220. try {
  2221. const AudioContext = window.AudioContext || window.webkitAudioContext;
  2222. if (!AudioContext) {
  2223. console.warn('浏览器不支持AudioContext');
  2224. return;
  2225. }
  2226. const audioContext = new AudioContext();
  2227. const analyser = audioContext.createAnalyser();
  2228. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  2229. microphone.connect(analyser);
  2230. // 设置分析器
  2231. analyser.fftSize = 256;
  2232. const bufferLength = analyser.frequencyBinCount;
  2233. const dataArray = new Uint8Array(bufferLength);
  2234. // 检测音频输入
  2235. let silenceCounter = 0;
  2236. const checkAudio = () => {
  2237. if (this.isRecording) return; // 如果正在录制,停止检测
  2238. analyser.getByteFrequencyData(dataArray);
  2239. // 计算平均音量
  2240. let sum = 0;
  2241. for (let i = 0; i < bufferLength; i++) {
  2242. sum += dataArray[i];
  2243. }
  2244. const average = sum / bufferLength;
  2245. if (average > 10) {
  2246. console.log('检测到音频输入,音量:', average);
  2247. silenceCounter = 0;
  2248. } else {
  2249. silenceCounter++;
  2250. if (silenceCounter > 10) {
  2251. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  2252. silenceCounter = 0;
  2253. }
  2254. }
  2255. // 继续检测
  2256. requestAnimationFrame(checkAudio);
  2257. };
  2258. // 开始检测
  2259. checkAudio();
  2260. } catch (e) {
  2261. console.error('音频测试失败:', e);
  2262. }
  2263. },
  2264. // 添加新方法:尝试单独获取音频
  2265. tryGetAudioOnly() {
  2266. navigator.mediaDevices.getUserMedia({ audio: true })
  2267. .then(audioStream => {
  2268. // 如果已有视频流,合并音频和视频轨道
  2269. if (this.cameraStream) {
  2270. const videoTrack = this.cameraStream.getVideoTracks()[0];
  2271. const audioTrack = audioStream.getAudioTracks()[0];
  2272. // 创建新的媒体流,包含视频和音频轨道
  2273. const combinedStream = new MediaStream();
  2274. if (videoTrack) combinedStream.addTrack(videoTrack);
  2275. if (audioTrack) combinedStream.addTrack(audioTrack);
  2276. // 更新摄像头流
  2277. this.cameraStream = combinedStream;
  2278. // 更新视频元素的源
  2279. const videoElement = this.$refs.userCameraVideo;
  2280. if (videoElement) {
  2281. videoElement.srcObject = combinedStream;
  2282. videoElement.muted = true; // 避免回声,但仍然录制声音
  2283. }
  2284. console.log('成功合并音频和视频轨道');
  2285. } else {
  2286. console.warn('没有视频流可合并');
  2287. }
  2288. })
  2289. .catch(err => {
  2290. console.error('单独获取音频失败:', err);
  2291. });
  2292. },
  2293. // 添加新方法:显示权限对话框
  2294. showPermissionDialog(permissionType) {
  2295. uni.showModal({
  2296. title: '需要权限',
  2297. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  2298. confirmText: '去设置',
  2299. success: (res) => {
  2300. if (res.confirm) {
  2301. uni.openSetting({
  2302. success: (settingRes) => {
  2303. console.log('设置页面打开成功', settingRes);
  2304. }
  2305. });
  2306. }
  2307. }
  2308. });
  2309. },
  2310. // 添加重试上传方法
  2311. retryVideoUpload() {
  2312. if (this.lastVideoToRetry) {
  2313. // 隐藏重试按钮
  2314. this.showRetryButton = false;
  2315. // 显示加载提示
  2316. uni.showLoading({
  2317. title: '正在重新提交...',
  2318. mask: true
  2319. });
  2320. // 重新提交视频
  2321. this.submitVideoToInterview(this.lastVideoToRetry);
  2322. } else {
  2323. uni.showToast({
  2324. title: '没有可重试的视频',
  2325. icon: 'none'
  2326. });
  2327. }
  2328. },
  2329. // 添加一个新方法用于压缩视频
  2330. async compressVideo(videoBlob) {
  2331. // 如果视频大小小于某个阈值,直接返回原视频
  2332. if (videoBlob.size < 5 * 1024 * 1024) { // 小于5MB
  2333. return videoBlob;
  2334. }
  2335. console.log('开始压缩视频,原始大小:', videoBlob.size);
  2336. // 创建一个视频元素来加载视频
  2337. const videoElement = document.createElement('video');
  2338. videoElement.muted = true;
  2339. videoElement.autoplay = false;
  2340. // 创建一个canvas元素用于绘制视频帧
  2341. const canvas = document.createElement('canvas');
  2342. const ctx = canvas.getContext('2d');
  2343. // 设置视频源
  2344. videoElement.src = URL.createObjectURL(videoBlob);
  2345. return new Promise((resolve) => {
  2346. videoElement.onloadedmetadata = () => {
  2347. // 设置canvas尺寸为视频的一半
  2348. const width = Math.floor(videoElement.videoWidth / 2);
  2349. const height = Math.floor(videoElement.videoHeight / 2);
  2350. canvas.width = width;
  2351. canvas.height = height;
  2352. // 创建MediaRecorder来录制canvas
  2353. const stream = canvas.captureStream(15); // 15fps
  2354. // 如果原视频有音轨,添加到新流中
  2355. if (videoElement.captureStream) {
  2356. const originalStream = videoElement.captureStream();
  2357. const audioTracks = originalStream.getAudioTracks();
  2358. if (audioTracks.length > 0) {
  2359. stream.addTrack(audioTracks[0]);
  2360. }
  2361. }
  2362. // 设置较低的比特率
  2363. const options = {
  2364. mimeType: 'video/webm;codecs=vp8,opus',
  2365. audioBitsPerSecond: 64000,
  2366. videoBitsPerSecond: 800000 // 800kbps
  2367. };
  2368. const mediaRecorder = new MediaRecorder(stream, options);
  2369. const chunks = [];
  2370. mediaRecorder.ondataavailable = (e) => {
  2371. if (e.data.size > 0) {
  2372. chunks.push(e.data);
  2373. }
  2374. };
  2375. mediaRecorder.onstop = () => {
  2376. const compressedBlob = new Blob(chunks, { type: 'video/webm' });
  2377. console.log('视频压缩完成,压缩后大小:', compressedBlob.size);
  2378. resolve(compressedBlob);
  2379. };
  2380. // 开始播放视频并录制
  2381. videoElement.onplay = () => {
  2382. mediaRecorder.start(100);
  2383. // 绘制视频帧到canvas
  2384. const drawFrame = () => {
  2385. if (videoElement.paused || videoElement.ended) {
  2386. mediaRecorder.stop();
  2387. return;
  2388. }
  2389. ctx.drawImage(videoElement, 0, 0, width, height);
  2390. requestAnimationFrame(drawFrame);
  2391. };
  2392. drawFrame();
  2393. };
  2394. videoElement.play();
  2395. };
  2396. });
  2397. },
  2398. // 修改 checkIOSCameraRecordPermission 方法
  2399. checkIOSCameraRecordPermission() {
  2400. const systemInfo = uni.getSystemInfoSync();
  2401. if (systemInfo.platform !== 'ios') return;
  2402. // 在iOS上,需要同时请求相机和录音权限
  2403. uni.getSetting({
  2404. success: (res) => {
  2405. // 检查相机权限
  2406. if (!res.authSetting['scope.camera']) {
  2407. uni.authorize({
  2408. scope: 'scope.camera',
  2409. success: () => {
  2410. console.log('iOS相机权限已获取');
  2411. },
  2412. fail: (err) => {
  2413. console.error('iOS相机权限获取失败:', err);
  2414. this.showPermissionDialog('相机');
  2415. }
  2416. });
  2417. }
  2418. // 检查录音权限
  2419. if (!res.authSetting['scope.record']) {
  2420. uni.authorize({
  2421. scope: 'scope.record',
  2422. success: () => {
  2423. console.log('iOS录音权限已获取');
  2424. },
  2425. fail: (err) => {
  2426. console.error('iOS录音权限获取失败:', err);
  2427. this.showPermissionDialog('录音');
  2428. }
  2429. });
  2430. }
  2431. }
  2432. });
  2433. },
  2434. // 添加新方法:检查并修复渲染问题
  2435. checkAndFixRenderingIssues() {
  2436. // 检查全局对象,防止渲染错误
  2437. try {
  2438. // 检查是否有全局变量u
  2439. if (typeof u !== 'undefined' && u) {
  2440. // 检查currentQuestion属性
  2441. if (!u.currentQuestion) {
  2442. console.log('修复: 创建缺失的currentQuestion对象');
  2443. u.currentQuestion = {};
  2444. }
  2445. // 确保isImportant属性存在
  2446. if (u.currentQuestion && typeof u.currentQuestion.isImportant === 'undefined') {
  2447. console.log('修复: 设置缺失的isImportant属性');
  2448. u.currentQuestion.isImportant = false;
  2449. }
  2450. }
  2451. } catch (e) {
  2452. console.log('防御性检查异常:', e);
  2453. }
  2454. },
  2455. // 添加格式化时间的辅助方法
  2456. formatTime(seconds) {
  2457. if (!seconds && seconds !== 0) return '03:30'; // 默认显示03:30
  2458. const minutes = Math.floor(seconds / 60);
  2459. const remainingSeconds = seconds % 60;
  2460. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  2461. },
  2462. // 添加新方法:保存上传状态到本地存储
  2463. saveUploadStatus() {
  2464. const uploadStatus = {
  2465. isUploading: this.isUploading,
  2466. uploadQueue: this.uploadQueue.length,
  2467. timestamp: Date.now()
  2468. };
  2469. try {
  2470. uni.setStorageSync('videoUploadStatus', JSON.stringify(uploadStatus));
  2471. } catch (e) {
  2472. console.error('保存上传状态失败:', e);
  2473. }
  2474. },
  2475. // 修改 submitToAiVoiceInteraction 方法
  2476. submitToAiVoiceInteraction(videoUrl) {
  2477. console.log('提交视频到AI语音交互接口:', videoUrl);
  2478. // 显示加载提示
  2479. uni.showLoading({
  2480. title: '面试官正在思考...',
  2481. mask: true
  2482. });
  2483. // 获取用户信息和租户ID
  2484. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  2485. const application_id = uni.getStorageSync('appId') || '98'; // 默认为98(报告)
  2486. let positionConfigId = 1;
  2487. if(JSON.parse(uni.getStorageSync('configData'))){
  2488. positionConfigId = JSON.parse(uni.getStorageSync('configData')).id;
  2489. }
  2490. // 准备请求数据
  2491. const requestData = {
  2492. voice_url: videoUrl,
  2493. tenant_id: tenant_id,
  2494. application_id: application_id,
  2495. scene_type: 'interview', // 场景类型:面试
  2496. // voice_type: 'longxiaoxia', // 角色:龙小侠
  2497. position_config_id: positionConfigId,
  2498. conversation_history: this.conversationHistory // 添加对话历史
  2499. };
  2500. // 检查环境
  2501. const systemInfo = uni.getSystemInfoSync();
  2502. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2503. if (isMiniProgram) {
  2504. // 小程序环境使用 uni.request
  2505. uni.request({
  2506. url: `${apiBaseUrl}/api/system/voice/mcp/interaction`, // 使用新的接口地址
  2507. method: 'POST',
  2508. data: requestData,
  2509. header: {
  2510. 'content-type': 'application/json' // 修改为JSON格式
  2511. },
  2512. success: (res) => {
  2513. if (res.statusCode === 200) {
  2514. this.handleAiInteractionResponse(res.data);
  2515. } else {
  2516. this.handleAiInteractionError(res.data);
  2517. }
  2518. },
  2519. fail: (err) => {
  2520. this.handleAiInteractionError(err);
  2521. }
  2522. });
  2523. } else {
  2524. // H5/App环境使用 XMLHttpRequest
  2525. const xhr = new XMLHttpRequest();
  2526. xhr.open('POST', `${apiBaseUrl}/api/system/voice/mcp/interaction`, true);
  2527. xhr.timeout = 60000; // 60秒超时
  2528. xhr.setRequestHeader('Content-Type', 'application/json');
  2529. xhr.onload = () => {
  2530. if (xhr.status === 200) {
  2531. try {
  2532. const response = JSON.parse(xhr.responseText);
  2533. this.handleAiInteractionResponse(response);
  2534. } catch (e) {
  2535. console.error('解析AI响应失败:', e);
  2536. this.handleAiInteractionError({ errMsg: '解析响应失败' });
  2537. }
  2538. } else {
  2539. this.handleAiInteractionError({ errMsg: 'HTTP状态: ' + xhr.status });
  2540. }
  2541. };
  2542. xhr.onerror = () => {
  2543. this.handleAiInteractionError({ errMsg: '网络错误' });
  2544. };
  2545. xhr.ontimeout = () => {
  2546. this.handleAiInteractionError({ errMsg: '请求超时' });
  2547. };
  2548. // 发送JSON格式的数据
  2549. xhr.send(JSON.stringify(requestData));
  2550. }
  2551. },
  2552. // 添加处理AI交互响应的方法
  2553. handleAiInteractionResponse(data) {
  2554. console.log('AI语音交互接口响应:', data);
  2555. // 获取系统信息
  2556. const systemInfo = uni.getSystemInfoSync();
  2557. const isIOS = systemInfo.platform === 'ios';
  2558. // 标记正在播放AI语音
  2559. this.isPlayingAiVoice = true;
  2560. // 隐藏加载提示
  2561. uni.hideLoading();
  2562. // 重置备选方法尝试标志
  2563. this.hasTriedFallbackMethod = false;
  2564. if (data && data.success) {
  2565. // 保存AI回复数据
  2566. this.aiText = data.ai_text || '';
  2567. this.transcribedText = data.transcribed_text ? data.transcribed_text.text : '';
  2568. // 记录对话历史
  2569. this.conversationHistory.push({
  2570. role: 'user',
  2571. content: this.transcribedText
  2572. });
  2573. this.conversationHistory.push({
  2574. role: 'assistant',
  2575. content: this.aiText
  2576. });
  2577. // 获取AI语音URL
  2578. const aiVoiceUrl = data.ai_voice_url || '';
  2579. if (aiVoiceUrl) {
  2580. // 构建完整的URL
  2581. this.aiVoiceUrl = aiVoiceUrl.startsWith('http')
  2582. ? aiVoiceUrl
  2583. : `${apiBaseUrl}${aiVoiceUrl}`;
  2584. console.log('AI语音URL:', this.aiVoiceUrl);
  2585. // 显示AI回复文本作为字幕
  2586. this.currentSubtitle = this.aiText;
  2587. // 检查平台
  2588. const systemInfo = uni.getSystemInfoSync();
  2589. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2590. // 检查音频URL是否包含不支持的格式
  2591. const isUnsupportedFormat = this.checkUnsupportedAudioFormat(this.aiVoiceUrl);
  2592. if (isUnsupportedFormat && isMiniProgram) {
  2593. console.warn('检测到小程序不支持的音频格式,直接使用文本显示');
  2594. this.displayTextResponse();
  2595. return;
  2596. }
  2597. // 根据平台选择不同的播放方法
  2598. if (isMiniProgram) {
  2599. // 小程序环境使用原生API
  2600. if (isIOS) {
  2601. this.playIOSMiniProgramAudio();
  2602. } else {
  2603. this.playMiniProgramAudio();
  2604. }
  2605. /* this.playMiniProgramAudio(); */
  2606. } else {
  2607. // H5/App环境使用通用方法
  2608. this.playAiVoice();
  2609. }
  2610. // 重要:确保在AI语音播放期间不显示继续提问按钮
  2611. this.showContinueQuestionButton = false;
  2612. this.showEndInterviewButton = false;
  2613. } else {
  2614. console.warn('未获取到AI语音URL,使用文本显示');
  2615. // 如果没有语音URL,显示文本回复
  2616. this.displayTextResponse();
  2617. }
  2618. } else {
  2619. console.error('AI语音交互失败:', data);
  2620. // AI交互失败,显示继续提问按钮
  2621. this.showContinueQuestionOptions();
  2622. // 检查是否需要自动跳转
  2623. this.checkPendingNavigation();
  2624. }
  2625. },
  2626. // 修改 handleAiInteractionError 方法,不要直接跳转,而是显示继续提问选项
  2627. handleAiInteractionError(err) {
  2628. console.error('AI语音交互请求失败:', err);
  2629. uni.hideLoading();
  2630. // 显示错误提示
  2631. uni.showToast({
  2632. title: 'AI处理失败,请重试',
  2633. icon: 'none',
  2634. duration: 2000
  2635. });
  2636. // 请求失败,显示继续提问选项,而不是直接跳转
  2637. this.showContinueQuestionOptions();
  2638. },
  2639. // 添加新方法:播放AI语音
  2640. playAiVoice() {
  2641. console.log('开始播放AI语音:', this.aiVoiceUrl);
  2642. // 重置错误提示标记
  2643. this.hasShownTextFallbackToast = false;
  2644. // 标记正在播放AI语音
  2645. this.isPlayingAiVoice = true;
  2646. // 隐藏所有按钮
  2647. this.showContinueQuestionButton = false;
  2648. this.showEndInterviewButton = false;
  2649. // 显示AI回复文本作为字幕
  2650. this.currentSubtitle = this.aiText;
  2651. // 检查音频URL是否有效
  2652. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === '') {
  2653. console.error('无效的音频URL,直接显示文本');
  2654. this.useTextFallbackAndNavigate();
  2655. return;
  2656. }
  2657. // 获取系统信息
  2658. const systemInfo = uni.getSystemInfoSync();
  2659. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2660. const isIOS = systemInfo.platform === 'ios';
  2661. // 根据平台选择播放方式
  2662. if (isMiniProgram) {
  2663. if (isIOS) {
  2664. this.playIOSMiniProgramAudio();
  2665. } else {
  2666. this.playMiniProgramAudio();
  2667. }
  2668. } else if (isIOS) {
  2669. this.playAiVoiceForIOS();
  2670. } else {
  2671. this.playAiVoiceForAndroid();
  2672. }
  2673. },
  2674. // 新增方法:文本回退并导航
  2675. useTextFallbackAndNavigate() {
  2676. console.log('使用文本回退并准备导航');
  2677. // 标记播放结束
  2678. this.isPlayingAiVoice = false;
  2679. // 显示友好提示
  2680. if (!this.hasShownTextFallbackToast) {
  2681. this.hasShownTextFallbackToast = true;
  2682. uni.showToast({
  2683. title: '为您显示文字回复',
  2684. icon: 'none',
  2685. duration: 2000
  2686. });
  2687. }
  2688. // 显示完整文本
  2689. this.currentSubtitle = this.aiText || '抱歉,AI回复内容加载失败';
  2690. // 给用户一定时间阅读文本后自动跳转
  2691. setTimeout(() => {
  2692. this.navigateToNextPage();
  2693. }, 5000); // 5秒后跳转
  2694. },
  2695. // 修改 handleIOSAudioPlaybackComplete 方法
  2696. handleIOSAudioPlaybackComplete() {
  2697. // 标记播放结束
  2698. this.isPlayingAiVoice = false;
  2699. // 延迟清空字幕
  2700. setTimeout(() => {
  2701. this.currentSubtitle = '';
  2702. }, 1000);
  2703. // 销毁音频播放器
  2704. if (this.aiAudioPlayer) {
  2705. try {
  2706. this.aiAudioPlayer.destroy();
  2707. } catch (e) {
  2708. console.error('销毁iOS音频播放器失败:', e);
  2709. }
  2710. this.aiAudioPlayer = null;
  2711. }
  2712. // 直接导航到下一页
  2713. this.navigateToNextPage();
  2714. },
  2715. // 修改 playMiniProgramAudio 方法
  2716. playMiniProgramAudio() {
  2717. console.log('使用小程序原生音频API播放:', this.aiVoiceUrl);
  2718. try {
  2719. const audioContext = wx.createInnerAudioContext();
  2720. audioContext.autoplay = true;
  2721. audioContext.src = this.aiVoiceUrl;
  2722. audioContext.onPlay(() => {
  2723. console.log('AI语音开始播放');
  2724. this.currentSubtitle = this.aiText;
  2725. });
  2726. audioContext.onEnded(() => {
  2727. console.log('AI语音播放结束');
  2728. this.isPlayingAiVoice = false;
  2729. this.currentSubtitle = '';
  2730. audioContext.destroy();
  2731. // 播放结束后直接导航
  2732. this.navigateToNextPage();
  2733. });
  2734. audioContext.onError((err) => {
  2735. console.error('AI语音播放错误:', err);
  2736. audioContext.destroy();
  2737. this.useTextFallbackAndNavigate();
  2738. });
  2739. // 设置超时处理
  2740. setTimeout(() => {
  2741. if (this.isPlayingAiVoice) {
  2742. console.warn('音频播放超时,使用文本回退');
  2743. this.useTextFallbackAndNavigate();
  2744. }
  2745. }, 5000);
  2746. } catch (e) {
  2747. console.error('小程序播放AI语音失败:', e);
  2748. this.useTextFallbackAndNavigate();
  2749. }
  2750. },
  2751. // 新增方法:导航到下一页
  2752. navigateToNextPage() {
  2753. // 这里添加导航逻辑
  2754. console.log('准备导航到下一页');
  2755. // 根据实际需求设置导航目标
  2756. uni.navigateTo({
  2757. url: '/pages/success/success',//'/pages/posture-guide/posture-guide',
  2758. fail: (err) => {
  2759. console.error('导航失败:', err);
  2760. // 导航失败时的处理
  2761. uni.showToast({
  2762. title: '页面跳转失败',
  2763. icon: 'none',
  2764. duration: 2000
  2765. });
  2766. }
  2767. });
  2768. },
  2769. // 新增方法:专门处理iOS小程序音频播放
  2770. playIOSMiniProgramAudio() {
  2771. console.log('iOS小程序环境:使用增强的音频处理方法');
  2772. // 重置错误标记
  2773. this.hasShownTextFallbackToast = false;
  2774. try {
  2775. // 创建音频上下文,使用wx API确保兼容性
  2776. const audioContext = wx.createInnerAudioContext();
  2777. // iOS小程序优化配置
  2778. audioContext.autoplay = false;
  2779. audioContext.obeyMuteSwitch = true; // iOS遵循静音开关
  2780. audioContext.volume = 1.0;
  2781. // 设置播放成功事件
  2782. audioContext.onPlay(() => {
  2783. console.log('iOS小程序音频开始播放');
  2784. this.currentSubtitle = this.aiText;
  2785. // uni.showToast({
  2786. // title: '正在播放AI回复',
  2787. // icon: 'none',
  2788. // duration: 1500
  2789. // });
  2790. });
  2791. // 设置播放结束事件
  2792. audioContext.onEnded(() => {
  2793. console.log('iOS小程序音频播放结束');
  2794. this.handleIOSAudioComplete(audioContext);
  2795. });
  2796. // 设置播放停止事件
  2797. audioContext.onStop(() => {
  2798. console.log('iOS小程序音频播放停止');
  2799. this.handleIOSAudioComplete(audioContext);
  2800. });
  2801. // 重点:增强的iOS错误处理
  2802. audioContext.onError((err) => {
  2803. console.error('iOS小程序音频播放错误:', err);
  2804. this.handleIOSMiniProgramError(audioContext, err);
  2805. });
  2806. // 先尝试预下载音频文件(iOS小程序推荐做法)
  2807. this.preloadIOSMiniProgramAudio(this.aiVoiceUrl)
  2808. .then((localPath) => {
  2809. console.log('iOS小程序音频预下载成功:', localPath);
  2810. audioContext.src = localPath;
  2811. /* // 设置播放超时
  2812. const playTimeout = setTimeout(() => {
  2813. if (this.isPlayingAiVoice) {
  2814. console.log('iOS小程序音频播放超时,使用文本回退');
  2815. clearTimeout(playTimeout);
  2816. this.handleIOSPlaybackTimeout(audioContext);
  2817. }
  2818. }, 5000);
  2819. */
  2820. // 尝试播放
  2821. audioContext.play();
  2822. })
  2823. .catch((error) => {
  2824. console.error('iOS小程序音频预下载失败:', error);
  2825. // 预下载失败,尝试直接播放
  2826. this.fallbackIOSMiniProgramPlay(audioContext);
  2827. });
  2828. } catch (e) {
  2829. console.error('iOS小程序音频初始化失败:', e);
  2830. this.useIOSTextFallback();
  2831. }
  2832. },
  2833. // 新增方法:iOS小程序音频预下载
  2834. preloadIOSMiniProgramAudio(audioUrl) {
  2835. return new Promise((resolve, reject) => {
  2836. console.log('开始预下载iOS小程序音频:', audioUrl);
  2837. uni.downloadFile({
  2838. url: audioUrl,
  2839. timeout: 15000, // 15秒超时
  2840. success: (res) => {
  2841. if (res.statusCode === 200) {
  2842. console.log('iOS小程序音频下载成功:', res.tempFilePath);
  2843. resolve(res.tempFilePath);
  2844. } else {
  2845. console.error('iOS小程序音频下载失败,状态码:', res.statusCode);
  2846. reject(new Error(`下载失败,状态码: ${res.statusCode}`));
  2847. }
  2848. },
  2849. fail: (err) => {
  2850. console.error('iOS小程序音频下载请求失败:', err);
  2851. reject(err);
  2852. }
  2853. });
  2854. });
  2855. },
  2856. // 新增方法:iOS小程序错误处理
  2857. handleIOSMiniProgramError(audioContext, error) {
  2858. console.error('处理iOS小程序音频错误:', error);
  2859. // 销毁音频播放器
  2860. if (audioContext) {
  2861. try {
  2862. audioContext.destroy();
  2863. } catch (e) {
  2864. console.error('销毁音频播放器失败:', e);
  2865. }
  2866. }
  2867. // 根据错误类型显示不同的提示信息
  2868. let errorMessage = '音频播放异常,显示文字';
  2869. if (error.errMsg && error.errMsg.includes('-11850')) {
  2870. console.log('iOS小程序检测到系统音频错误(-11850)');
  2871. errorMessage = '系统音频服务暂停,显示文字';
  2872. } else if (error.errCode === 10002) {
  2873. console.log('iOS小程序检测到errCode:10002,网络或资源问题');
  2874. errorMessage = '音频加载中断,显示文字';
  2875. } else if (error.errCode === 10001) {
  2876. console.log('iOS小程序检测到errCode:10001,系统错误');
  2877. errorMessage = '系统音频错误,显示文字';
  2878. } else if (error.errCode === 10003) {
  2879. console.log('iOS小程序检测到errCode:10003,文件错误');
  2880. errorMessage = '音频文件错误,显示文字';
  2881. } else if (error.errCode === 10004) {
  2882. console.log('iOS小程序检测到errCode:10004,格式不支持');
  2883. errorMessage = '音频格式不支持,显示文字';
  2884. }
  2885. // 显示用户友好的错误提示
  2886. uni.showToast({
  2887. title: errorMessage,
  2888. icon: 'none',
  2889. duration: 2000
  2890. });
  2891. // 使用文本回退
  2892. this.useIOSTextFallback();
  2893. },
  2894. // 新增方法:iOS小程序播放超时处理
  2895. handleIOSPlaybackTimeout(audioContext) {
  2896. console.log('iOS小程序音频播放超时处理');
  2897. // 销毁音频播放器
  2898. if (audioContext) {
  2899. try {
  2900. audioContext.destroy();
  2901. } catch (e) {
  2902. console.error('销毁超时音频播放器失败:', e);
  2903. }
  2904. }
  2905. // 显示超时提示
  2906. /* uni.showToast({
  2907. title: '音频加载超时,显示文字',
  2908. icon: 'none',
  2909. duration: 2000
  2910. }); */
  2911. // 使用文本回退
  2912. this.useIOSTextFallback();
  2913. },
  2914. // 新增方法:iOS小程序备选播放方法
  2915. fallbackIOSMiniProgramPlay(audioContext) {
  2916. console.log('iOS小程序使用备选播放方法');
  2917. try {
  2918. // 直接设置原始URL
  2919. audioContext.src = this.aiVoiceUrl;
  2920. // 设置较短的超时时间
  2921. const fallbackTimeout = setTimeout(() => {
  2922. if (this.isPlayingAiVoice) {
  2923. console.log('iOS小程序备选方法也超时');
  2924. clearTimeout(fallbackTimeout);
  2925. this.handleIOSPlaybackTimeout(audioContext);
  2926. }
  2927. }, 3000);
  2928. // 尝试播放
  2929. audioContext.play();
  2930. } catch (e) {
  2931. console.error('iOS小程序备选播放方法失败:', e);
  2932. this.handleIOSPlaybackTimeout(audioContext);
  2933. }
  2934. },
  2935. // 新增方法:统一的iOS音频完成处理
  2936. handleIOSAudioComplete(audioContext) {
  2937. console.log('iOS音频播放完成');
  2938. // 标记播放结束
  2939. this.isPlayingAiVoice = false;
  2940. // 延迟清空字幕,让用户有时间阅读
  2941. setTimeout(() => {
  2942. this.currentSubtitle = '';
  2943. }, 2000);
  2944. // 销毁音频播放器
  2945. if (audioContext) {
  2946. try {
  2947. audioContext.destroy();
  2948. } catch (e) {
  2949. console.error('销毁iOS音频播放器失败:', e);
  2950. }
  2951. }
  2952. // 显示继续提问选项
  2953. this.showContinueQuestionOptions();
  2954. // 检查是否需要自动跳转
  2955. this.checkPendingNavigation();
  2956. },
  2957. // 移除原来的 playMiniProgramAudioForIOS 方法,因为已经被新的 playIOSMiniProgramAudio 替代
  2958. // playMiniProgramAudioForIOS() 方法可以删除或注释掉
  2959. // 保留原有的 useIOSTextFallback 方法,但确保它能正确处理所有iOS场景
  2960. useIOSTextFallback() {
  2961. console.log('iOS使用文本回退方案');
  2962. // 确保播放状态正确
  2963. this.isPlayingAiVoice = false;
  2964. // 显示友好提示(如果还没有显示过)
  2965. if (!this.hasShownTextFallbackToast) {
  2966. this.hasShownTextFallbackToast = true;
  2967. /* uni.showToast({
  2968. title: '为您显示文字回复',
  2969. icon: 'none',
  2970. duration: 2000
  2971. }); */
  2972. }
  2973. // 显示完整文本
  2974. this.currentSubtitle = this.aiText || '抱歉,AI回复内容加载失败';
  2975. // 根据文本长度计算合适的阅读时间
  2976. const textLength = this.aiText ? this.aiText.length : 20;
  2977. const readingTime = Math.max(5000, Math.min(textLength * 100, 15000)); // 最少5秒,最多15秒
  2978. console.log(`文本长度: ${textLength}, 预计阅读时间: ${readingTime}ms`);
  2979. setTimeout(() => {
  2980. this.handleIOSAudioComplete(null);
  2981. }, readingTime);
  2982. },
  2983. // 添加新方法:iOS音频播放完成处理
  2984. handleIOSAudioPlaybackComplete() {
  2985. // 标记播放结束
  2986. this.isPlayingAiVoice = false;
  2987. // 延迟清空字幕,让用户有时间阅读
  2988. setTimeout(() => {
  2989. this.currentSubtitle = '';
  2990. }, 2000);
  2991. // 销毁音频播放器
  2992. if (this.aiAudioPlayer) {
  2993. try {
  2994. this.aiAudioPlayer.destroy();
  2995. } catch (e) {
  2996. console.error('销毁iOS音频播放器失败:', e);
  2997. }
  2998. this.aiAudioPlayer = null;
  2999. }
  3000. // 显示继续提问选项
  3001. this.showContinueQuestionOptions();
  3002. // 检查是否需要自动跳转
  3003. this.checkPendingNavigation();
  3004. },
  3005. // 添加新方法:iOS音频错误处理
  3006. handleIOSAudioError(error) {
  3007. console.error('iOS音频播放错误:', error);
  3008. // 根据错误码进行不同处理
  3009. switch (error.errCode) {
  3010. case 10001:
  3011. console.log('iOS音频系统错误,尝试重新播放');
  3012. this.retryIOSAudioPlay();
  3013. break;
  3014. case 10002:
  3015. console.log('iOS音频网络错误,使用备选方案');
  3016. this.handleIOS10002Error();
  3017. break;
  3018. case 10003:
  3019. console.log('iOS音频文件错误,使用文本回退');
  3020. this.useIOSTextFallback();
  3021. break;
  3022. case 10004:
  3023. console.log('iOS音频格式不支持,使用文本回退');
  3024. this.useIOSTextFallback();
  3025. break;
  3026. default:
  3027. console.log('iOS音频未知错误,使用文本回退');
  3028. this.useIOSTextFallback();
  3029. break;
  3030. }
  3031. },
  3032. // 添加新方法:重试iOS音频播放
  3033. retryIOSAudioPlay() {
  3034. console.log('重试iOS音频播放');
  3035. // 销毁当前播放器
  3036. if (this.aiAudioPlayer) {
  3037. try {
  3038. this.aiAudioPlayer.destroy();
  3039. } catch (e) {
  3040. console.error('销毁播放器失败:', e);
  3041. }
  3042. this.aiAudioPlayer = null;
  3043. }
  3044. // 延迟后重试
  3045. setTimeout(() => {
  3046. this.fallbackIOSAudioPlay();
  3047. }, 1000);
  3048. },
  3049. // 添加新方法:iOS备选音频播放
  3050. fallbackIOSAudioPlay() {
  3051. console.log('iOS备选音频播放方案');
  3052. try {
  3053. // 创建新的音频播放器
  3054. this.aiAudioPlayer = uni.createInnerAudioContext();
  3055. // 使用更保守的设置
  3056. this.aiAudioPlayer.autoplay = true;
  3057. this.aiAudioPlayer.obeyMuteSwitch = true; // 遵循静音开关
  3058. this.aiAudioPlayer.volume = 0.8;
  3059. // 直接使用原URL
  3060. this.aiAudioPlayer.src = this.aiVoiceUrl;
  3061. // 设置简化的事件监听
  3062. this.aiAudioPlayer.onPlay(() => {
  3063. console.log('iOS备选方案:音频开始播放');
  3064. this.currentSubtitle = this.aiText;
  3065. });
  3066. this.aiAudioPlayer.onEnded(() => {
  3067. console.log('iOS备选方案:音频播放结束');
  3068. this.handleIOSAudioPlaybackComplete();
  3069. });
  3070. this.aiAudioPlayer.onError((err) => {
  3071. console.error('iOS备选方案:音频播放错误:', err);
  3072. this.useIOSTextFallback();
  3073. });
  3074. // 设置超时,如果5秒内没有开始播放,使用文本回退
  3075. setTimeout(() => {
  3076. if (this.isPlayingAiVoice && this.aiAudioPlayer) {
  3077. console.log('iOS音频播放超时,使用文本回退');
  3078. this.useIOSTextFallback();
  3079. }
  3080. }, 5000);
  3081. } catch (e) {
  3082. console.error('iOS备选音频播放失败:', e);
  3083. this.useIOSTextFallback();
  3084. }
  3085. },
  3086. // 修改 playMiniProgramAudio 方法,添加iOS特殊处理
  3087. playMiniProgramAudio() {
  3088. console.log('使用小程序原生音频API播放:', this.aiVoiceUrl);
  3089. // 标记正在播放AI语音
  3090. this.isPlayingAiVoice = true;
  3091. // 确保在播放期间不显示继续提问按钮
  3092. this.showContinueQuestionButton = false;
  3093. this.showEndInterviewButton = false;
  3094. // 检查音频URL是否有效
  3095. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === '') {
  3096. console.error('无效的音频URL');
  3097. this.handleAudioPlaybackFailure();
  3098. return;
  3099. }
  3100. // 检查平台
  3101. const systemInfo = uni.getSystemInfoSync();
  3102. const isIOS = systemInfo.platform === 'ios';
  3103. // iOS小程序特殊处理
  3104. if (isIOS) {
  3105. this.playMiniProgramAudioForIOS();
  3106. return;
  3107. }
  3108. // 非iOS平台的原有逻辑
  3109. try {
  3110. // 使用wx.createInnerAudioContext API (微信小程序)
  3111. const audioContext = wx.createInnerAudioContext();
  3112. // 设置音频属性
  3113. audioContext.autoplay = false;
  3114. audioContext.obeyMuteSwitch = false; // 忽略静音开关
  3115. // 监听播放开始事件
  3116. audioContext.onPlay(() => {
  3117. console.log('AI语音开始播放');
  3118. // 显示正在播放提示
  3119. /* uni.showToast({
  3120. title: '正在播放AI回复',
  3121. icon: 'none',
  3122. duration: 2000
  3123. }); */
  3124. });
  3125. // 监听播放结束事件
  3126. audioContext.onEnded(() => {
  3127. console.log('AI语音播放结束');
  3128. // 标记播放结束
  3129. this.isPlayingAiVoice = false;
  3130. // 清空字幕
  3131. this.currentSubtitle = '';
  3132. // 销毁音频播放器
  3133. audioContext.destroy();
  3134. // 播放结束后显示继续提问选项,而不是直接跳转
  3135. this.showContinueQuestionOptions();
  3136. });
  3137. // 监听播放错误事件
  3138. audioContext.onError((err) => {
  3139. console.error('AI语音播放错误:', err);
  3140. // 标记播放结束
  3141. this.isPlayingAiVoice = false;
  3142. // 清空字幕
  3143. this.currentSubtitle = '';
  3144. // 销毁音频播放器
  3145. audioContext.destroy();
  3146. // 播放错误时也显示继续提问选项
  3147. this.showContinueQuestionOptions();
  3148. });
  3149. // 设置音频源
  3150. audioContext.src = this.aiVoiceUrl;
  3151. // 添加超时处理,如果5秒内没有开始播放,则认为出错
  3152. const playTimeout = setTimeout(() => {
  3153. if (this.isPlayingAiVoice) {
  3154. console.warn('小程序音频播放超时,尝试备选方法');
  3155. this.tryMiniProgramFallbackAudioPlay();
  3156. }
  3157. }, 5000);
  3158. // 延迟一小段时间再播放,确保src已经设置完成
  3159. setTimeout(() => {
  3160. audioContext.play();
  3161. // 播放开始后清除超时
  3162. clearTimeout(playTimeout);
  3163. }, 300);
  3164. } catch (e) {
  3165. console.error('小程序播放AI语音时发生错误:', e);
  3166. // 标记播放结束
  3167. this.isPlayingAiVoice = false;
  3168. // 显示错误提示
  3169. /* uni.showToast({
  3170. title: '音频播放失败,请查看文字回复',
  3171. icon: 'none',
  3172. duration: 2000
  3173. }); */
  3174. // 使用文本显示方式作为备选方案
  3175. this.handleAudioPlaybackFailure();
  3176. }
  3177. },
  3178. // 添加新方法:iOS小程序音频播放
  3179. playMiniProgramAudioForIOS() {
  3180. console.log('iOS小程序环境:开始播放AI语音');
  3181. // 重置错误标记
  3182. this.hasShownTextFallbackToast = false;
  3183. try {
  3184. // 创建音频上下文
  3185. const audioContext = wx.createInnerAudioContext();
  3186. // iOS小程序特殊配置
  3187. audioContext.autoplay = false;
  3188. audioContext.obeyMuteSwitch = true; // iOS上遵循静音开关
  3189. audioContext.volume = 1.0;
  3190. // 设置错误处理
  3191. audioContext.onError((err) => {
  3192. console.error('iOS小程序音频播放错误:', err);
  3193. // 检查具体错误类型
  3194. if (err.errMsg && err.errMsg.includes('-11850')) {
  3195. console.log('iOS小程序检测到系统音频错误(-11850)');
  3196. /* uni.showToast({
  3197. title: '系统音频暂停,显示文字',
  3198. icon: 'none',
  3199. duration: 2000
  3200. }); */
  3201. } else if (err.errCode === 10002) {
  3202. console.log('iOS小程序检测到errCode:10002,网络或资源问题');
  3203. /* uni.showToast({
  3204. title: '音频加载中断,显示文字',
  3205. icon: 'none',
  3206. duration: 2000
  3207. }); */
  3208. } else {
  3209. console.log('iOS小程序其他音频错误:', err.errCode);
  3210. /* uni.showToast({
  3211. title: '音频播放异常,显示文字',
  3212. icon: 'none',
  3213. duration: 2000
  3214. }); */
  3215. }
  3216. this.useIOSMiniProgramTextFallback(audioContext);
  3217. });
  3218. // 先尝试直接播放,如果失败再下载
  3219. audioContext.src = this.aiVoiceUrl;
  3220. // 设置成功事件监听
  3221. audioContext.onPlay(() => {
  3222. console.log('iOS小程序音频开始播放');
  3223. this.currentSubtitle = this.aiText;
  3224. /* uni.showToast({
  3225. title: '正在播放AI回复',
  3226. icon: 'none',
  3227. duration: 1500
  3228. }); */
  3229. });
  3230. audioContext.onEnded(() => {
  3231. console.log('iOS小程序音频播放结束');
  3232. this.handleIOSMiniProgramAudioComplete(audioContext);
  3233. });
  3234. // 设置较短的播放超时时间
  3235. const playTimeout = setTimeout(() => {
  3236. if (this.isPlayingAiVoice) {
  3237. console.log('iOS小程序音频播放超时,切换到文本显示');
  3238. clearTimeout(playTimeout);
  3239. this.useIOSMiniProgramTextFallback(audioContext);
  3240. }
  3241. }, 3000); // 缩短到3秒
  3242. // 尝试播放
  3243. audioContext.play();
  3244. } catch (e) {
  3245. console.error('iOS小程序音频播放初始化失败:', e);
  3246. this.handleAudioPlaybackFailure();
  3247. }
  3248. },
  3249. // 添加新方法:iOS小程序音频播放完成
  3250. handleIOSMiniProgramAudioComplete(audioContext) {
  3251. this.isPlayingAiVoice = false;
  3252. setTimeout(() => {
  3253. this.currentSubtitle = '';
  3254. }, 2000);
  3255. if (audioContext) {
  3256. try {
  3257. audioContext.destroy();
  3258. } catch (e) {
  3259. console.error('销毁iOS小程序音频播放器失败:', e);
  3260. }
  3261. }
  3262. this.showContinueQuestionOptions();
  3263. this.checkPendingNavigation();
  3264. },
  3265. // 添加新方法:iOS小程序音频错误处理
  3266. handleIOSMiniProgramAudioError(audioContext, error) {
  3267. console.error('iOS小程序音频错误:', error);
  3268. // 根据错误码处理
  3269. if (error.errCode === 10002) {
  3270. this.useIOSMiniProgramTextFallback(audioContext);
  3271. } else {
  3272. // 其他错误也使用文本回退
  3273. this.useIOSMiniProgramTextFallback(audioContext);
  3274. }
  3275. },
  3276. // 添加新方法:iOS小程序文本回退
  3277. useIOSMiniProgramTextFallback(audioContext) {
  3278. console.log('iOS小程序使用文本回退方案');
  3279. // 销毁音频播放器
  3280. if (audioContext) {
  3281. try {
  3282. audioContext.destroy();
  3283. } catch (e) {
  3284. console.error('销毁音频播放器失败:', e);
  3285. }
  3286. }
  3287. // 确保播放状态正确
  3288. this.isPlayingAiVoice = false;
  3289. // 显示完整文本
  3290. this.currentSubtitle = this.aiText || '抱歉,AI回复内容加载失败';
  3291. // 根据文本长度计算阅读时间
  3292. const textLength = this.aiText ? this.aiText.length : 20;
  3293. const readingTime = Math.max(6000, Math.min(textLength * 120, 18000)); // 最少6秒,最多18秒
  3294. setTimeout(() => {
  3295. this.handleIOSMiniProgramAudioComplete(null);
  3296. }, readingTime);
  3297. },
  3298. // 添加新方法:处理音频播放错误
  3299. handleAudioPlaybackError(error) {
  3300. console.error('音频播放错误:', error);
  3301. // 尝试使用备选方法
  3302. if (!this.hasTriedFallbackMethod) {
  3303. this.hasTriedFallbackMethod = true;
  3304. this.tryFallbackAudioPlay();
  3305. return;
  3306. }
  3307. // 如果备选方法也失败,回退到文本显示
  3308. this.handleAudioPlaybackFailure();
  3309. },
  3310. // 添加新方法:尝试备选音频播放方法
  3311. tryFallbackAudioPlay() {
  3312. console.log('使用备选方法播放音频:', this.aiVoiceUrl);
  3313. // 如果当前播放器存在,先销毁
  3314. if (this.aiAudioPlayer) {
  3315. try {
  3316. this.aiAudioPlayer.destroy();
  3317. } catch (e) {
  3318. console.error('销毁音频播放器失败:', e);
  3319. }
  3320. this.aiAudioPlayer = null;
  3321. }
  3322. // 使用uni.downloadFile先下载音频文件
  3323. uni.downloadFile({
  3324. url: this.aiVoiceUrl,
  3325. success: (res) => {
  3326. if (res.statusCode === 200) {
  3327. console.log('音频文件下载成功:', res.tempFilePath);
  3328. // 使用新的音频上下文播放本地文件
  3329. const newAudioPlayer = uni.createInnerAudioContext();
  3330. this.aiAudioPlayer = newAudioPlayer;
  3331. newAudioPlayer.autoplay = true;
  3332. newAudioPlayer.src = res.tempFilePath;
  3333. // 设置事件监听
  3334. newAudioPlayer.onPlay(() => {
  3335. console.log('备选方法:音频开始播放');
  3336. });
  3337. newAudioPlayer.onEnded(() => {
  3338. console.log('备选方法:音频播放结束');
  3339. this.isPlayingAiVoice = false;
  3340. this.currentSubtitle = '';
  3341. newAudioPlayer.destroy();
  3342. this.aiAudioPlayer = null;
  3343. this.showContinueQuestionOptions();
  3344. this.checkPendingNavigation();
  3345. });
  3346. newAudioPlayer.onError((err) => {
  3347. console.error('备选方法:音频播放错误:', err);
  3348. this.isPlayingAiVoice = false;
  3349. this.currentSubtitle = '';
  3350. newAudioPlayer.destroy();
  3351. this.aiAudioPlayer = null;
  3352. // 显示错误提示
  3353. /* uni.showToast({
  3354. title: '无法播放音频,请查看文字回复',
  3355. icon: 'none',
  3356. duration: 2000
  3357. }); */
  3358. this.showContinueQuestionOptions();
  3359. this.checkPendingNavigation();
  3360. });
  3361. } else {
  3362. console.error('音频文件下载失败:', res);
  3363. this.handleAudioPlaybackFailure();
  3364. }
  3365. },
  3366. fail: (err) => {
  3367. console.error('音频文件下载请求失败:', err);
  3368. this.handleAudioPlaybackFailure();
  3369. }
  3370. });
  3371. },
  3372. // 添加新方法:处理音频播放失败的情况
  3373. handleAudioPlaybackFailure() {
  3374. // 标记播放结束
  3375. this.isPlayingAiVoice = false;
  3376. // 显示错误提示
  3377. /* uni.showToast({
  3378. title: '无法播放音频,将直接显示文字回复',
  3379. icon: 'none',
  3380. duration: 2000
  3381. }); */
  3382. // 确保字幕显示足够长的时间(10秒)让用户阅读
  3383. this.currentSubtitle = this.aiText;
  3384. // 设置定时器,10秒后清除字幕并显示继续提问选项
  3385. setTimeout(() => {
  3386. this.currentSubtitle = '';
  3387. this.showContinueQuestionOptions();
  3388. this.checkPendingNavigation();
  3389. }, 10000);
  3390. },
  3391. // 添加新方法:从视频中提取音频
  3392. async extractAudioFromVideo(videoBlob) {
  3393. console.log('开始从视频中提取音频');
  3394. return new Promise((resolve, reject) => {
  3395. try {
  3396. // 创建一个视频元素来加载视频
  3397. const videoElement = document.createElement('video');
  3398. videoElement.muted = false;
  3399. videoElement.autoplay = false;
  3400. // 创建音频上下文
  3401. const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  3402. let audioDestination = null;
  3403. let mediaRecorder = null;
  3404. const audioChunks = [];
  3405. // 设置视频源
  3406. videoElement.src = URL.createObjectURL(videoBlob);
  3407. videoElement.onloadedmetadata = () => {
  3408. console.log('视频元数据已加载,视频时长:', videoElement.duration);
  3409. // 创建媒体源和目标节点
  3410. const source = audioContext.createMediaElementSource(videoElement);
  3411. audioDestination = audioContext.createMediaStreamDestination();
  3412. source.connect(audioDestination);
  3413. // 创建MediaRecorder来录制音频
  3414. mediaRecorder = new MediaRecorder(audioDestination.stream);
  3415. mediaRecorder.ondataavailable = (event) => {
  3416. if (event.data.size > 0) {
  3417. audioChunks.push(event.data);
  3418. }
  3419. };
  3420. mediaRecorder.onstop = () => {
  3421. const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
  3422. console.log('音频提取完成,大小:', audioBlob.size);
  3423. resolve(audioBlob);
  3424. };
  3425. // 开始播放视频并录制音频
  3426. videoElement.onplay = () => {
  3427. mediaRecorder.start(100);
  3428. };
  3429. videoElement.onended = () => {
  3430. mediaRecorder.stop();
  3431. audioContext.close();
  3432. };
  3433. // 开始播放
  3434. videoElement.play();
  3435. };
  3436. videoElement.onerror = (err) => {
  3437. console.error('视频加载失败:', err);
  3438. reject(new Error('视频加载失败'));
  3439. };
  3440. } catch (err) {
  3441. console.error('提取音频失败:', err);
  3442. reject(err);
  3443. }
  3444. });
  3445. },
  3446. // 添加新方法:上传音频文件
  3447. async uploadAudioFile(audioBlob, videoUrl) {
  3448. console.log('准备上传音频文件');
  3449. // 创建FormData
  3450. const formData = new FormData();
  3451. // 将Blob转换为File对象
  3452. const fileName = `audio_${Date.now()}.wav`;
  3453. const audioFile = new File([audioBlob], fileName, { type: 'audio/wav' });
  3454. // 添加文件和其他参数
  3455. formData.append('audio_file', audioFile);
  3456. formData.append('video_url', videoUrl);
  3457. formData.append('tenant_id', uni.getStorageSync('tenant_id') || '1');
  3458. formData.append('openid', uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).openid : '');
  3459. formData.append('application_id', uni.getStorageSync('appId') || '');
  3460. return new Promise((resolve, reject) => {
  3461. // 创建XMLHttpRequest
  3462. const xhr = new XMLHttpRequest();
  3463. xhr.open('POST', `${apiBaseUrl}/api/voice_ai_interaction`, true);
  3464. xhr.timeout = 60000; // 60秒超时
  3465. xhr.onload = () => {
  3466. if (xhr.status === 200) {
  3467. try {
  3468. const response = JSON.parse(xhr.responseText);
  3469. resolve(response);
  3470. } catch (e) {
  3471. reject(new Error('解析响应失败'));
  3472. }
  3473. } else {
  3474. reject(new Error('HTTP状态: ' + xhr.status));
  3475. }
  3476. };
  3477. xhr.onerror = () => {
  3478. reject(new Error('网络错误'));
  3479. };
  3480. xhr.ontimeout = () => {
  3481. reject(new Error('请求超时'));
  3482. };
  3483. xhr.send(formData);
  3484. });
  3485. },
  3486. // 添加一个新方法:使用小程序原生音频API
  3487. playMiniProgramAudio() {
  3488. console.log('使用小程序原生音频API播放:', this.aiVoiceUrl);
  3489. // 标记正在播放AI语音
  3490. this.isPlayingAiVoice = true;
  3491. // 确保在播放期间不显示继续提问按钮
  3492. this.showContinueQuestionButton = false;
  3493. this.showEndInterviewButton = false;
  3494. // 检查音频URL是否有效
  3495. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === '') {
  3496. console.error('无效的音频URL');
  3497. // 显示错误提示
  3498. /* uni.showToast({
  3499. title: '音频加载失败',
  3500. icon: 'none',
  3501. duration: 2000
  3502. }); */
  3503. // 标记播放结束
  3504. this.isPlayingAiVoice = false;
  3505. // 显示继续提问选项
  3506. this.showContinueQuestionOptions();
  3507. return;
  3508. }
  3509. try {
  3510. // 使用wx.createInnerAudioContext API (微信小程序)
  3511. const audioContext = wx.createInnerAudioContext();
  3512. // 设置音频属性
  3513. audioContext.autoplay = false;
  3514. audioContext.obeyMuteSwitch = false; // 忽略静音开关
  3515. // 监听播放开始事件
  3516. audioContext.onPlay(() => {
  3517. console.log('AI语音开始播放');
  3518. // 显示正在播放提示
  3519. /* uni.showToast({
  3520. title: '正在播放AI回复',
  3521. icon: 'none',
  3522. duration: 2000
  3523. }); */
  3524. });
  3525. // 监听播放结束事件
  3526. audioContext.onEnded(() => {
  3527. console.log('AI语音播放结束');
  3528. // 标记播放结束
  3529. this.isPlayingAiVoice = false;
  3530. // 清空字幕
  3531. this.currentSubtitle = '';
  3532. // 销毁音频播放器
  3533. audioContext.destroy();
  3534. // 播放结束后显示继续提问选项,而不是直接跳转
  3535. this.showContinueQuestionOptions();
  3536. });
  3537. // 监听播放错误事件
  3538. audioContext.onError((err) => {
  3539. console.error('AI语音播放错误:', err);
  3540. // 标记播放结束
  3541. this.isPlayingAiVoice = false;
  3542. // 清空字幕
  3543. this.currentSubtitle = '';
  3544. // 销毁音频播放器
  3545. audioContext.destroy();
  3546. // 播放错误时也显示继续提问选项
  3547. this.showContinueQuestionOptions();
  3548. });
  3549. // 设置音频源
  3550. audioContext.src = this.aiVoiceUrl;
  3551. // 添加超时处理,如果5秒内没有开始播放,则认为出错
  3552. const playTimeout = setTimeout(() => {
  3553. if (this.isPlayingAiVoice) {
  3554. console.warn('小程序音频播放超时,尝试备选方法');
  3555. this.tryMiniProgramFallbackAudioPlay();
  3556. }
  3557. }, 5000);
  3558. // 延迟一小段时间再播放,确保src已经设置完成
  3559. setTimeout(() => {
  3560. audioContext.play();
  3561. // 播放开始后清除超时
  3562. clearTimeout(playTimeout);
  3563. }, 300);
  3564. } catch (e) {
  3565. console.error('小程序播放AI语音时发生错误:', e);
  3566. // 标记播放结束
  3567. this.isPlayingAiVoice = false;
  3568. // 显示错误提示
  3569. /* uni.showToast({
  3570. title: '音频播放失败,请查看文字回复',
  3571. icon: 'none',
  3572. duration: 2000
  3573. }); */
  3574. // 使用文本显示方式作为备选方案
  3575. this.handleAudioPlaybackFailure();
  3576. }
  3577. },
  3578. // 添加新方法:小程序环境的备选音频播放方法
  3579. tryMiniProgramFallbackAudioPlay() {
  3580. console.log('使用小程序备选方法播放音频:', this.aiVoiceUrl);
  3581. // 先下载音频文件到本地
  3582. uni.downloadFile({
  3583. url: this.aiVoiceUrl,
  3584. success: (res) => {
  3585. if (res.statusCode === 200) {
  3586. console.log('小程序音频文件下载成功:', res.tempFilePath);
  3587. // 使用新的音频上下文播放本地文件
  3588. const newAudioContext = wx.createInnerAudioContext();
  3589. newAudioContext.autoplay = true;
  3590. newAudioContext.src = res.tempFilePath;
  3591. // 设置事件监听
  3592. newAudioContext.onPlay(() => {
  3593. console.log('小程序备选方法:音频开始播放');
  3594. });
  3595. newAudioContext.onEnded(() => {
  3596. console.log('小程序备选方法:音频播放结束');
  3597. this.isPlayingAiVoice = false;
  3598. this.currentSubtitle = '';
  3599. newAudioContext.destroy();
  3600. this.showContinueQuestionOptions();
  3601. this.checkPendingNavigation();
  3602. });
  3603. newAudioContext.onError((err) => {
  3604. console.error('小程序备选方法:音频播放错误:', err);
  3605. this.isPlayingAiVoice = false;
  3606. this.currentSubtitle = '';
  3607. newAudioContext.destroy();
  3608. // 尝试使用系统播放器作为最后的备选方案
  3609. this.trySystemAudioPlayer();
  3610. });
  3611. } else {
  3612. console.error('小程序音频文件下载失败:', res);
  3613. this.handleAudioPlaybackFailure();
  3614. }
  3615. },
  3616. fail: (err) => {
  3617. console.error('小程序音频文件下载请求失败:', err);
  3618. this.handleAudioPlaybackFailure();
  3619. }
  3620. });
  3621. },
  3622. // 添加新方法:尝试使用系统播放器
  3623. trySystemAudioPlayer() {
  3624. console.log('尝试使用系统播放器播放音频');
  3625. // 检查平台
  3626. const systemInfo = uni.getSystemInfoSync();
  3627. if (systemInfo.platform === 'android' || systemInfo.platform === 'ios') {
  3628. // 在移动端尝试使用系统播放器
  3629. /* uni.showToast({
  3630. title: '正在使用系统播放器',
  3631. icon: 'none',
  3632. duration: 2000
  3633. }); */
  3634. // 使用uni.playVoice API (如果可用)
  3635. if (typeof uni.playVoice === 'function') {
  3636. uni.playVoice({
  3637. filePath: this.aiVoiceUrl,
  3638. success: () => {
  3639. console.log('系统播放器播放成功');
  3640. setTimeout(() => {
  3641. this.isPlayingAiVoice = false;
  3642. this.currentSubtitle = '';
  3643. this.showContinueQuestionOptions();
  3644. }, 5000); // 假设音频长度约为5秒
  3645. },
  3646. fail: (err) => {
  3647. console.error('系统播放器播放失败:', err);
  3648. this.handleAudioPlaybackFailure();
  3649. }
  3650. });
  3651. } else {
  3652. // 如果uni.playVoice不可用,尝试使用uni.openDocument
  3653. uni.openDocument({
  3654. filePath: this.aiVoiceUrl,
  3655. fileType: 'audio',
  3656. success: () => {
  3657. console.log('使用系统应用打开音频文件');
  3658. // 由于控制权交给了系统,我们需要手动处理后续流程
  3659. setTimeout(() => {
  3660. this.isPlayingAiVoice = false;
  3661. this.currentSubtitle = '';
  3662. this.showContinueQuestionOptions();
  3663. }, 8000); // 给用户足够的时间听完音频
  3664. },
  3665. fail: (err) => {
  3666. console.error('无法使用系统应用打开音频:', err);
  3667. this.handleAudioPlaybackFailure();
  3668. }
  3669. });
  3670. }
  3671. } else {
  3672. // 在其他平台上直接使用文本显示
  3673. this.handleAudioPlaybackFailure();
  3674. }
  3675. },
  3676. // 修改 showContinueQuestionOptions 方法,确保状态正确设置
  3677. showContinueQuestionOptions() {
  3678. // 如果正在播放AI语音,不执行此方法
  3679. if (this.isPlayingAiVoice) {
  3680. console.log('AI语音正在播放,暂不显示继续提问选项');
  3681. return;
  3682. }
  3683. // 增加提问轮次计数
  3684. this.questionRound++;
  3685. // 清空字幕
  3686. this.currentSubtitle = '';
  3687. // 直接跳转到下一页,不再显示继续提问按钮
  3688. console.log('准备跳转到下一页面');
  3689. // 延迟3秒后跳转,给用户一些时间查看状态
  3690. setTimeout(() => {
  3691. this.navigateToNextPage();
  3692. }, 3000);
  3693. // 确保所有录制状态被重置
  3694. this.isRecording = false;
  3695. if (this.recordingTimer) {
  3696. clearInterval(this.recordingTimer);
  3697. this.recordingTimer = null;
  3698. }
  3699. // 重置录制相关数据
  3700. this.recordedChunks = [];
  3701. this.recordingTimerCount = 0;
  3702. this.remainingTime = this.maxRecordingTime;
  3703. },
  3704. // 修复 handleContinueQuestionClick 方法,确保状态正确重置
  3705. handleContinueQuestionClick() {
  3706. // 如果AI语音仍在播放,不允许继续提问
  3707. if (this.isPlayingAiVoice) {
  3708. /* uni.showToast({
  3709. title: '请等待面试官回复完成',
  3710. icon: 'none',
  3711. duration: 2000
  3712. }); */
  3713. return;
  3714. }
  3715. // 隐藏继续提问和结束面试按钮
  3716. this.showContinueQuestionButton = false;
  3717. this.showEndInterviewButton = false;
  3718. // 重置录制相关状态
  3719. this.isRecording = false;
  3720. this.recordingTimerCount = 0;
  3721. this.remainingTime = this.maxRecordingTime;
  3722. this.recordingTimeDisplay = '00:00 / ' + this.formatTime(this.maxRecordingTime);
  3723. // 清空已录制的数据
  3724. this.recordedChunks = [];
  3725. // 如果有正在进行的录制,先停止
  3726. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  3727. this.mediaRecorder.stop();
  3728. }
  3729. if (this.recorder) {
  3730. try {
  3731. this.cameraContext.stopRecord();
  3732. } catch (e) {
  3733. console.error('停止录制失败:', e);
  3734. }
  3735. this.recorder = null;
  3736. }
  3737. // 延迟一小段时间再开始新的录制,确保之前的录制已完全停止
  3738. setTimeout(() => {
  3739. // 开始新一轮录制
  3740. this.startRecordingAnswer();
  3741. }, 500);
  3742. },
  3743. // 添加新方法:处理结束面试按钮点击
  3744. handleEndInterviewClick() {
  3745. // 隐藏继续提问和结束面试按钮
  3746. this.showContinueQuestionButton = false;
  3747. this.showEndInterviewButton = false;
  3748. // 显示结束提示
  3749. /* uni.showToast({
  3750. title: '面试已结束',
  3751. icon: 'success',
  3752. duration: 1500
  3753. }); */
  3754. // 执行现有的跳转逻辑
  3755. this.navigateToNextPage();
  3756. },
  3757. // 添加新方法:处理录制错误
  3758. handleRecordingError(error) {
  3759. console.error('录制错误:', error);
  3760. // 显示错误提示
  3761. /* uni.showToast({
  3762. title: '录制失败,请重试',
  3763. icon: 'none',
  3764. duration: 2000
  3765. }); */
  3766. // 重置录制状态
  3767. this.isRecording = false;
  3768. this.showStopRecordingButton = false;
  3769. // 显示重试按钮
  3770. this.showStartRecordingButton = true;
  3771. },
  3772. // 添加新方法:处理上传错误
  3773. handleUploadError(error) {
  3774. console.error('上传错误:', error);
  3775. // 显示错误提示
  3776. /* uni.showToast({
  3777. title: '上传失败,请重试',
  3778. icon: 'none',
  3779. duration: 2000
  3780. }); */
  3781. // 显示重试按钮
  3782. this.showRetryButton = true;
  3783. // 隐藏上传状态
  3784. this.showUploadStatus = false;
  3785. this.isUploading = false;
  3786. },
  3787. // 添加新方法:检查是否有待处理的自动跳转
  3788. checkPendingNavigation() {
  3789. // 确保所有处理都已完成
  3790. if (this.pendingAutoNavigation &&
  3791. !this.isPlayingAiVoice &&
  3792. !this.isUploading &&
  3793. !this.currentSubtitle) { // 确保字幕也已经清空
  3794. console.log('所有处理已完成,准备跳转');
  3795. // 重置标志
  3796. this.pendingAutoNavigation = false;
  3797. // 延迟跳转,给用户一点时间看到最终状态
  3798. setTimeout(() => {
  3799. this.navigateToNextPage();
  3800. }, 1000);
  3801. }
  3802. },
  3803. // 添加新方法:显示文本回复
  3804. displayTextResponse() {
  3805. // 标记为正在"播放"
  3806. this.isPlayingAiVoice = true;
  3807. // 显示AI回复文本
  3808. this.currentSubtitle = '';
  3809. // 计算每个字的显示时间(假设每个字需要200毫秒)
  3810. const charDisplayTime = 200;
  3811. let currentIndex = 0;
  3812. // 使用定时器逐字显示文本
  3813. const textInterval = setInterval(() => {
  3814. if (currentIndex <= this.aiText.length) {
  3815. this.currentSubtitle = this.aiText.substring(0, currentIndex);
  3816. currentIndex++;
  3817. } else {
  3818. // 文本显示完成
  3819. clearInterval(textInterval);
  3820. // 等待用户阅读完整文本
  3821. setTimeout(() => {
  3822. // 标记播放结束
  3823. this.isPlayingAiVoice = false;
  3824. // 清空字幕
  3825. this.currentSubtitle = '';
  3826. // 显示继续提问选项
  3827. this.showContinueQuestionOptions();
  3828. // 检查是否需要自动跳转
  3829. this.checkPendingNavigation();
  3830. }, 3000); // 给用户3秒时间阅读完整文本
  3831. }
  3832. }, charDisplayTime);
  3833. },
  3834. // 添加新方法:检查不支持的音频格式
  3835. checkUnsupportedAudioFormat(url) {
  3836. if (!url) return false;
  3837. // 获取URL的文件扩展名
  3838. const extension = url.split('.').pop().toLowerCase();
  3839. // 检查平台
  3840. const systemInfo = uni.getSystemInfoSync();
  3841. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  3842. if (isMiniProgram) {
  3843. // 微信小程序支持的音频格式有限,主要是mp3和wav
  3844. // 检查是否是不支持的格式
  3845. const unsupportedFormats = ['ogg', 'flac', 'aac', 'm4a', 'opus'];
  3846. return unsupportedFormats.includes(extension);
  3847. }
  3848. return false;
  3849. },
  3850. // 修改文本回退方法
  3851. useIOSTextFallback() {
  3852. console.log('切换到文本显示模式');
  3853. this.isPlayingAiVoice = false;
  3854. if (!this.hasShownTextFallbackToast) {
  3855. this.hasShownTextFallbackToast = true;
  3856. /* uni.showToast({
  3857. title: '已切换为文字显示',
  3858. icon: 'none',
  3859. duration: 2000
  3860. }); */
  3861. }
  3862. // 显示AI回复文本
  3863. this.currentSubtitle = this.aiText || '抱歉,无法获取AI回复内容';
  3864. // 根据文本长度计算阅读时间
  3865. const textLength = this.aiText ? this.aiText.length : 20;
  3866. const readingTime = Math.max(8000, Math.min(textLength * 150, 20000)); // 最少8秒,最多20秒
  3867. console.log(`文本长度: ${textLength}字,预计阅读时间: ${readingTime/1000}秒`);
  3868. // 等待用户阅读完成后再跳转
  3869. setTimeout(() => {
  3870. this.navigateToNextPage();
  3871. }, readingTime);
  3872. }
  3873. }
  3874. }
  3875. </script>
  3876. <style scoped>
  3877. .identity-verify-container {
  3878. padding: 0;
  3879. max-width: 100%;
  3880. margin: 0 auto;
  3881. height: 100vh;
  3882. display: flex;
  3883. flex-direction: column;
  3884. background-color: #f5f5f5;
  3885. }
  3886. .digital-human-container {
  3887. position: relative;
  3888. width: 100%;
  3889. height: 100vh;
  3890. overflow: hidden;
  3891. background-color: #f0f0f0;
  3892. }
  3893. .digital-human-video {
  3894. width: 100%;
  3895. height: 100%;
  3896. display: flex;
  3897. justify-content: center;
  3898. align-items: center;
  3899. }
  3900. /* 添加视频蒙层样式 */
  3901. .video-overlay {
  3902. position: absolute;
  3903. top: 0;
  3904. left: 0;
  3905. width: 100%;
  3906. height: 100%;
  3907. background-color: transparent; /* 完全透明 */
  3908. z-index: 6; /* 确保在视频之上,但在按钮和字幕之下 */
  3909. pointer-events: auto; /* 允许捕获点击事件 */
  3910. }
  3911. /* 用户摄像头容器样式 */
  3912. .user-camera-container {
  3913. position: absolute;
  3914. top: 50px;
  3915. right: 5px;
  3916. width: 110px; /* 稍微增加宽度 */
  3917. height: 160px; /* 稍微增加高度 */
  3918. border-radius: 4px; /* 减小圆角 */
  3919. overflow: hidden;
  3920. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  3921. z-index: 20;
  3922. }
  3923. /* 用户摄像头视频样式 */
  3924. .user-camera-video {
  3925. width: 100%;
  3926. height: 100%;
  3927. object-fit: cover;
  3928. background-color: #333;
  3929. }
  3930. .video-player {
  3931. width: 100%;
  3932. height: 100%;
  3933. object-fit: cover;
  3934. outline: none; /* 移除视频获得焦点时的轮廓 */
  3935. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  3936. }
  3937. /* 隐藏视频控制条 */
  3938. video::-webkit-media-controls {
  3939. display: none !important;
  3940. }
  3941. video::-webkit-media-controls-enclosure {
  3942. display: none !important;
  3943. }
  3944. video::-webkit-media-controls-panel {
  3945. display: none !important;
  3946. }
  3947. video::-webkit-media-controls-play-button {
  3948. display: none !important;
  3949. }
  3950. video::-webkit-media-controls-timeline {
  3951. display: none !important;
  3952. }
  3953. video::-webkit-media-controls-current-time-display {
  3954. display: none !important;
  3955. }
  3956. video::-webkit-media-controls-time-remaining-display {
  3957. display: none !important;
  3958. }
  3959. video::-webkit-media-controls-mute-button {
  3960. display: none !important;
  3961. }
  3962. video::-webkit-media-controls-volume-slider {
  3963. display: none !important;
  3964. }
  3965. video::-webkit-media-controls-fullscreen-button {
  3966. display: none !important;
  3967. }
  3968. /* 修改字幕覆盖层样式,使其与图片中的样式一致 */
  3969. .subtitle-overlay {
  3970. position: absolute;
  3971. bottom: 180px; /* 调整位置,使其更靠近底部 */
  3972. left: 5%; /* 从左侧留出5%的空间 */
  3973. width: 80%; /* 宽度设为90%,两侧各留5%实现居中 */
  3974. padding: 15px 20px; /* 增加左右内边距 */
  3975. border-radius: 15px;
  3976. background-color: rgba(255, 255, 255, 0.9); /* 白色半透明背景 */
  3977. color: #333; /* 文字颜色为深色 */
  3978. text-align: left; /* 文字左对齐 */
  3979. font-size: 16px;
  3980. line-height: 1.5;
  3981. z-index: 10;
  3982. margin: 0 auto; /* 添加自动边距实现居中 */
  3983. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 添加轻微阴影 */
  3984. }
  3985. /* 添加计时器样式,右侧显示橙色圆点和时间 */
  3986. .subtitle-overlay::after {
  3987. content: attr(data-time);
  3988. position: absolute;
  3989. right: 20px;
  3990. bottom: 15px;
  3991. color: #333;
  3992. font-size: 14px;
  3993. }
  3994. .control-panel {
  3995. padding: 15px;
  3996. display: flex;
  3997. justify-content: center;
  3998. }
  3999. .control-button {
  4000. padding: 10px 20px;
  4001. background-color: #4CAF50;
  4002. color: white;
  4003. border: none;
  4004. border-radius: 4px;
  4005. cursor: pointer;
  4006. }
  4007. .loading {
  4008. text-align: center;
  4009. margin: 20px 0;
  4010. font-size: 16px;
  4011. }
  4012. .response-container {
  4013. margin-top: 20px;
  4014. padding: 0 20px;
  4015. display: none; /* 默认隐藏调试信息 */
  4016. }
  4017. /* 当showDebugInfo为true时显示 */
  4018. .showDebugInfo .response-container {
  4019. display: block;
  4020. }
  4021. .response-item {
  4022. padding: 10px;
  4023. border: 1px solid #eee;
  4024. border-radius: 4px;
  4025. margin-bottom: 10px;
  4026. background-color: #f9f9f9;
  4027. }
  4028. .response-content {
  4029. display: flex;
  4030. flex-direction: column;
  4031. }
  4032. .answer-button-container {
  4033. position: absolute;
  4034. bottom: 50px; /* 将按钮放在底部而不是75%的位置 */
  4035. left: 50%;
  4036. transform: translateX(-50%); /* 只在X轴上平移,保持水平居中 */
  4037. z-index: 20;
  4038. }
  4039. /* 修改回答按钮样式 */
  4040. .answer-button {
  4041. width: 120px;
  4042. height: 40px; /* 改为矩形按钮 */
  4043. border-radius: 20px; /* 圆角矩形 */
  4044. background-color: #ffffff; /* 白色背景 */
  4045. color: #333; /* 深色文字 */
  4046. font-size: 16px;
  4047. border: none;
  4048. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4049. display: flex;
  4050. justify-content: center;
  4051. align-items: center;
  4052. cursor: pointer;
  4053. animation: none; /* 移除脉动动画 */
  4054. }
  4055. .stop-recording-button-container {
  4056. position: absolute;
  4057. bottom: 50px; /* 统一与其他按钮的位置 */
  4058. left: 50%;
  4059. transform: translateX(-50%);
  4060. z-index: 20;
  4061. }
  4062. .stop-recording-button {
  4063. width: 120px;
  4064. height: 40px; /* 改为矩形按钮 */
  4065. border-radius: 20px; /* 圆角矩形 */
  4066. background-color: #0039b3; /* 白色背景 */
  4067. color: #fff; /* 深色文字 */
  4068. font-size: 16px;
  4069. border: none;
  4070. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4071. display: flex;
  4072. justify-content: center;
  4073. align-items: center;
  4074. cursor: pointer;
  4075. animation: none; /* 移除脉动动画 */
  4076. }
  4077. .recording-indicator {
  4078. position: absolute;
  4079. top: 20px;
  4080. left: 20px;
  4081. display: flex;
  4082. align-items: center;
  4083. background-color: rgba(255, 255, 255, 0.9); /* 改为白色半透明背景 */
  4084. padding: 5px 10px;
  4085. border-radius: 15px;
  4086. z-index: 20;
  4087. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  4088. }
  4089. .recording-dot {
  4090. width: 12px;
  4091. height: 12px;
  4092. background-color: #ff6b00; /* 改为橙色,与图片中的颜色一致 */
  4093. border-radius: 50%;
  4094. margin-right: 8px;
  4095. animation: blink 1s infinite;
  4096. }
  4097. .recording-text {
  4098. color: #333; /* 改为深色文字 */
  4099. font-size: 14px;
  4100. }
  4101. .timer-text {
  4102. color: #333; /* 改为深色文字 */
  4103. font-size: 14px;
  4104. margin-left: 8px;
  4105. }
  4106. .start-recording-button-container {
  4107. position: absolute;
  4108. bottom: 50px; /* 统一与其他按钮的位置 */
  4109. left: 50%;
  4110. transform: translateX(-50%);
  4111. z-index: 101;
  4112. }
  4113. .start-recording-button {
  4114. width: 120px;
  4115. height: 40px; /* 改为矩形按钮 */
  4116. border-radius: 20px; /* 圆角矩形 */
  4117. background-color: #ffffff; /* 白色背景 */
  4118. color: #333; /* 深色文字 */
  4119. font-size: 16px;
  4120. border: none;
  4121. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4122. display: flex;
  4123. justify-content: center;
  4124. align-items: center;
  4125. cursor: pointer;
  4126. animation: none; /* 移除脉动动画 */
  4127. }
  4128. .retry-button-container {
  4129. position: absolute;
  4130. bottom: 30px; /* 统一与其他按钮的位置 */
  4131. left: 50%;
  4132. transform: translateX(-50%);
  4133. z-index: 20;
  4134. }
  4135. .retry-button {
  4136. padding: 10px 20px;
  4137. background-color: #ffffff; /* 白色背景 */
  4138. color: #333; /* 深色文字 */
  4139. font-size: 16px;
  4140. border: none;
  4141. border-radius: 20px; /* 圆角矩形 */
  4142. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4143. cursor: pointer;
  4144. }
  4145. .retry-button:hover {
  4146. background-color: #2980b9;
  4147. }
  4148. /* 录制时长显示 */
  4149. .recording-timer {
  4150. position: absolute;
  4151. top: 20px;
  4152. left: 130px; /* 放在录制指示器旁边 */
  4153. background-color: rgba(0, 0, 0, 0.6);
  4154. padding: 5px 10px;
  4155. border-radius: 15px;
  4156. z-index: 20;
  4157. display: flex;
  4158. flex-direction: column;
  4159. }
  4160. .timer-text {
  4161. color: #000;
  4162. font-size: 14px;
  4163. font-family: monospace; /* 使用等宽字体,使时间显示更稳定 */
  4164. }
  4165. .remaining-time {
  4166. color: white;
  4167. font-size: 12px;
  4168. margin-top: 2px;
  4169. }
  4170. .remaining-time.warning {
  4171. color: #ff6b6b; /* 剩余时间少时显示红色 */
  4172. animation: blink 1s infinite; /* 使用闪烁动画提醒用户 */
  4173. }
  4174. /* 添加上传状态指示器 */
  4175. .upload-status-indicator {
  4176. position: absolute;
  4177. top: 20px;
  4178. left: 130px; /* 放在录制指示器旁边 */
  4179. background-color: rgba(0, 0, 0, 0.6);
  4180. padding: 5px 10px;
  4181. border-radius: 15px;
  4182. z-index: 20;
  4183. }
  4184. .upload-status-content {
  4185. display: flex;
  4186. align-items: center;
  4187. }
  4188. .upload-status-icon {
  4189. width: 12px;
  4190. height: 12px;
  4191. border-radius: 50%;
  4192. margin-right: 8px;
  4193. }
  4194. .upload-status-text {
  4195. color: white;
  4196. font-size: 14px;
  4197. }
  4198. .uploading {
  4199. background-color: #e74c3c;
  4200. }
  4201. /* 添加倒计时蒙层样式 */
  4202. .countdown-overlay {
  4203. position: fixed;
  4204. top: 0;
  4205. left: 0;
  4206. width: 100%;
  4207. height: 100%;
  4208. background-color: rgba(0, 0, 0, 0.7);
  4209. z-index: 100;
  4210. display: flex;
  4211. justify-content: center;
  4212. align-items: center;
  4213. }
  4214. .countdown-content {
  4215. display: flex;
  4216. flex-direction: column;
  4217. align-items: center;
  4218. }
  4219. .countdown-number {
  4220. font-size: 80px;
  4221. color: #ffffff;
  4222. font-weight: bold;
  4223. margin-bottom: 20px;
  4224. }
  4225. .countdown-text {
  4226. font-size: 24px;
  4227. color: #ffffff;
  4228. }
  4229. /* 添加GIF播放器样式 */
  4230. .gif-player {
  4231. width: 100%;
  4232. height: 100%;
  4233. object-fit: cover;
  4234. position: absolute;
  4235. top: 0;
  4236. left: 0;
  4237. z-index: 5; /* 确保GIF在视频上方但在字幕和按钮下方 */
  4238. }
  4239. /* AI语音播放蒙层样式 */
  4240. .ai-voice-overlay {
  4241. position: fixed;
  4242. top: 0;
  4243. left: 0;
  4244. width: 100%;
  4245. height: 100%;
  4246. background-color: rgba(0, 0, 0, 0.8); /* 0.8的透明度 */
  4247. z-index: 99; /* 确保在其他元素之上,但在倒计时蒙层之下 */
  4248. display: flex;
  4249. justify-content: center;
  4250. align-items: center;
  4251. }
  4252. .ai-voice-content {
  4253. display: flex;
  4254. flex-direction: column;
  4255. align-items: center;
  4256. color: #ffffff;
  4257. }
  4258. .ai-voice-icon {
  4259. width: 40px;
  4260. height: 40px;
  4261. border-radius: 50%;
  4262. background-color: #ffffff;
  4263. margin-bottom: 15px;
  4264. position: relative;
  4265. animation: pulse 1.5s infinite;
  4266. }
  4267. .ai-voice-icon::before {
  4268. content: '';
  4269. position: absolute;
  4270. top: 50%;
  4271. left: 50%;
  4272. transform: translate(-50%, -50%);
  4273. width: 20px;
  4274. height: 20px;
  4275. background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23333"><path d="M12 15c1.66 0 3-1.34 3-3V6c0-1.66-1.34-3-3-3S9 4.34 9 6v6c0 1.66 1.34 3 3 3zm-1-9c0-.55.45-1 1-1s1 .45 1 1v6c0 .55-.45 1-1 1s-1-.45-1-1V6z"/><path d="M17 11c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"/></svg>');
  4276. background-repeat: no-repeat;
  4277. background-position: center;
  4278. }
  4279. .ai-voice-text {
  4280. font-size: 18px;
  4281. color: #ffffff;
  4282. }
  4283. @keyframes pulse {
  4284. 0% {
  4285. transform: scale(1);
  4286. opacity: 1;
  4287. }
  4288. 50% {
  4289. transform: scale(1.1);
  4290. opacity: 0.8;
  4291. }
  4292. 100% {
  4293. transform: scale(1);
  4294. opacity: 1;
  4295. }
  4296. }
  4297. /* 继续提问按钮样式 */
  4298. .continue-question-container {
  4299. position: absolute;
  4300. bottom: 100px; /* 位置高于结束面试按钮 */
  4301. left: 50%;
  4302. transform: translateX(-50%);
  4303. z-index: 20;
  4304. }
  4305. .continue-question-button {
  4306. width: 120px;
  4307. height: 40px;
  4308. border-radius: 20px;
  4309. background-color: #ffffff;
  4310. color: #333;
  4311. font-size: 16px;
  4312. border: none;
  4313. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4314. display: flex;
  4315. justify-content: center;
  4316. align-items: center;
  4317. cursor: pointer;
  4318. }
  4319. /* 结束面试按钮样式 */
  4320. .end-interview-container {
  4321. position: absolute;
  4322. bottom: 50px;
  4323. left: 50%;
  4324. transform: translateX(-50%);
  4325. z-index: 20;
  4326. }
  4327. .end-interview-button {
  4328. width: 120px;
  4329. height: 40px;
  4330. border-radius: 20px;
  4331. background-color: #e74c3c; /* 红色背景 */
  4332. color: #ffffff; /* 白色文字 */
  4333. font-size: 16px;
  4334. border: none;
  4335. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4336. display: flex;
  4337. justify-content: center;
  4338. align-items: center;
  4339. cursor: pointer;
  4340. }
  4341. /* 提问轮次指示器样式 */
  4342. .question-round-indicator {
  4343. position: absolute;
  4344. top: 20px;
  4345. right: 120px; /* 位置在用户摄像头左侧 */
  4346. background-color: rgba(255, 255, 255, 0.9);
  4347. padding: 5px 10px;
  4348. border-radius: 15px;
  4349. z-index: 20;
  4350. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  4351. }
  4352. .round-text {
  4353. color: #333;
  4354. font-size: 14px;
  4355. }
  4356. </style>