identity-verify.vue 160 KB

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