identity-verify.vue 219 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504
  1. <template>
  2. <view class="custom-navbar" :style="{paddingTop: statusBarHeight + 'px'}">
  3. <!-- <view class="navbar-title">我的标题</view>1 -->
  4. </view>
  5. <div class="identity-verify-container" :class="{ 'page-warning': showPageWarning }">
  6. <div class="digital-human-container">
  7. <!-- 添加题号显示 -->
  8. <!-- <div class="question-counter" v-if="currentVideoIndex > 0">
  9. <span class="question-number">{{ currentVideoIndex }}/{{ totalQuestions }}</span>
  10. </div> -->
  11. <!-- AI数字人视频/图像显示区域 -->
  12. <div class="digital-human-video">
  13. <!-- 添加GIF图片显示区域 -->
  14. <!-- <img v-if="showGif && gifUrl" :src="gifUrl" class="gif-player" alt="面试官GIF" /> -->
  15. <!-- <video
  16. v-if="showGif && gifUrl"
  17. :src="gifUrl"
  18. id="mpVideo"
  19. autoplay
  20. class="video-player"
  21. :controls="false"
  22. loop
  23. muted
  24. object-fit="contain"
  25. ></video> v-if="!showGif"-->
  26. <!-- 视频播放区域,当showGif为false时显示 -->
  27. <video
  28. :src="videoUrl"
  29. id="myVideo"
  30. ref="videoPlayer"
  31. autoplay
  32. playsinline
  33. disablePictureInPicture
  34. controlsList="nodownload nofullscreen noremoteplayback"
  35. class="video-player"
  36. :controls="false"
  37. @error="handleVideoError"
  38. @ended="handleVideoEnded"
  39. @timeupdate="handleTimeUpdate"
  40. @click.prevent="preventVideoControl"
  41. @touchstart.prevent="preventVideoControl">
  42. </video>
  43. <!-- 添加透明蒙层 -->
  44. <div class="video-overlay"></div>
  45. <div class="answer-button-container" v-if="showAnswerButton">
  46. <button class="answer-button" @click="handleAnswerButtonClick">
  47. 开始回答
  48. </button>
  49. </div>
  50. <!-- 添加字幕覆盖层 -->
  51. <div class="subtitle-overlay" v-if="currentSubtitle || (showSubtitleText && subtitleText)">
  52. <!-- 添加题号显示 -->
  53. <div class="subtitle-header" v-if="currentVideoIndex > 0">
  54. <span class="question-tag">{{ currentVideoIndex }}/{{ totalQuestions }}</span>
  55. </div>
  56. <!-- 显示父问题 -->
  57. <div class="subtitle-main" v-if="!isFollowUpQuestion">{{ currentSubtitle }}</div>
  58. <!-- 当是追问时显示父问题和追问问题 -->
  59. <template v-else>
  60. <div class="parent-question">{{ parentQuestion }}</div>
  61. <div class="followup-container">
  62. <div class="followup-content">
  63. <span class="followup-label">追问:</span>
  64. <span class="followup-text">{{ subtitleText || currentSubtitle }}</span>
  65. </div>
  66. </div>
  67. </template>
  68. </div>
  69. <!-- 添加答题按钮 -->
  70. </div>
  71. <!-- 用户摄像头视频显示区域 -->
  72. <div class="user-camera-container" :class="{ 'camera-warning': showPageWarning }">
  73. <!-- 在小程序环境中使用camera组件 -->
  74. <camera v-if="useMiniProgramCameraComponent"
  75. device-position="front"
  76. flash="off"
  77. class="user-camera-video"
  78. @error="handleCameraError"
  79. :record-audio="true"
  80. frame-size="small"
  81. resolution="low"
  82. :disable-zoom="true"
  83. :enable-camera="true"
  84. :enable-record="true"
  85. mode="normal">
  86. </camera>
  87. <!-- 在H5/App环境中使用video元素 -->
  88. <video v-else
  89. id="userCamera"
  90. ref="userCameraVideo"
  91. autoplay
  92. playsinline
  93. muted
  94. class="user-camera-video"
  95. :controls="false">
  96. </video>
  97. <!-- 隐藏的离屏canvas用于录制期间的帧转图片(不影响UI) -->
  98. <canvas v-if="useMiniProgramCameraComponent"
  99. id="hiddenCaptureCanvas"
  100. canvas-id="hiddenCaptureCanvas"
  101. type="2d"
  102. style="position:absolute;left:-9999px;top:-9999px;width:1px;height:1px;opacity:0;pointer-events:none;"
  103. :width="frameCanvasWidth"
  104. :height="frameCanvasHeight"></canvas>
  105. </div>
  106. <!-- 字幕/文本覆盖区域 -->
  107. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  108. {{ assistantResponse }}
  109. </div> -->
  110. </div>
  111. <!-- 加载状态 -->
  112. <div v-if="loading" class="loading">加载中...</div>
  113. <!-- 控制面板(可选,可以隐藏) -->
  114. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  115. <div v-if="showDebugInfo" class="response-container">
  116. <div v-if="assistantResponse" class="response-item">
  117. <div class="response-content">
  118. <span>助手回复: {{ assistantResponse }}</span>
  119. </div>
  120. </div>
  121. <div v-if="audioTranscript" class="response-item">
  122. <div class="response-content">
  123. <span>音频转写: {{ audioTranscript }}</span>
  124. </div>
  125. </div>
  126. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  127. <div class="response-content">
  128. <span v-if="item.role">角色: {{ item.role }}</span>
  129. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  130. </div>
  131. </div>
  132. </div>
  133. <!-- 添加停止录制按钮 -->
  134. <div class="stop-recording-button-container" v-if="showStopRecordingButton">
  135. <button class="stop-recording-button" @click="stopRecordingAnswer">
  136. 完成作答
  137. </button>
  138. </div>
  139. <!-- 添加录制指示器 -->
  140. <div class="recording-indicator" v-if="isRecording">
  141. <!-- 替换原有的progress组件为自定义环形进度条 -->
  142. <div class="circle-progress-container">
  143. <div class="circle-progress" :style="{
  144. background: `conic-gradient(${progressColor} ${progressPercent}%, ${progressBgColor} 0%)`
  145. }">
  146. <div class="circle-progress-inner">
  147. <span class="progress-text">{{ recordingTimeDisplay }}</span><!-- -->
  148. </div>
  149. </div>
  150. </div>
  151. <!-- <span class="timer-text">{{ recordingTimeDisplay || '00:00 / 05:00' }}</span> -->
  152. </div>
  153. <!-- 添加开始回答按钮 -->
  154. <div class="start-recording-button-container" v-if="showStartRecordingButton">
  155. <button class="start-recording-button" @click="handleStartRecordingClick">
  156. 开始作答
  157. </button>
  158. </div>
  159. <!-- 添加重试上传按钮 -->
  160. <div class="retry-button-container" v-if="showRetryButton">
  161. <button class="retry-button" @click="retryVideoUpload">
  162. 重新上传
  163. </button>
  164. </div>
  165. <!-- 在模板部分添加录制时长显示 -->
  166. <!-- <div class="recording-timer" v-if="isRecording">
  167. <span class="timer-text">{{ recordingTimeDisplay || '00:00' }}</span>
  168. </div> -->
  169. <!-- 添加上传状态指示器 -->
  170. <!-- <div class="upload-status-indicator" v-if="showUploadStatus && uploadStatusText">
  171. <div class="upload-status-content">
  172. <div class="upload-status-icon" :class="{'uploading': isUploading}"></div>
  173. <span class="upload-status-text">{{ uploadStatusText }}</span>
  174. </div>
  175. </div> -->
  176. <!-- 修改录制时长显示 -->
  177. <!-- <div class="recording-timer" v-if="isRecording">
  178. <span class="timer-text">{{ recordingTimeDisplay || '00:00 / 05:00' }}</span>
  179. <span class="remaining-time" :class="{'warning': remainingTime <= 10}">
  180. 剩余: {{ formatTime(remainingTime) }}
  181. </span>
  182. </div> -->
  183. <!-- 在模板部分添加倒计时蒙层 -->
  184. <div class="countdown-overlay" v-if="showCountdown">
  185. <div class="countdown-content">
  186. <div class="countdown-number">{{ countdownValue }}</div>
  187. <div class="countdown-text">准备开始回答</div>
  188. </div>
  189. </div>
  190. <!-- 添加重新录制按钮 -->
  191. <div class="rerecord-button-container" v-if="showRerecordButton">
  192. <button class="rerecord-button" @click="handleRerecordButtonClick">
  193. 重新作答
  194. </button>
  195. </div>
  196. </div>
  197. </template>
  198. <script>
  199. // 使用非响应式引用存放小程序 canvas node,避免被Vue代理后访问width/height触发get拦截
  200. let hiddenCanvasNodeRef = null;
  201. let hiddenCanvasCtxRef = null;
  202. import { apiBaseUrl, personDetectionWsUrl } from '@/common/config.js';
  203. export default {
  204. name: 'IdentityVerify',
  205. data() {
  206. return {
  207. followUpQuestion: '', // 追问问题
  208. followUpAudioUrl: '', // 追问音频URL
  209. audioContext: null, // 音频上下文
  210. followUpQuestions: [], // 追问问题列表
  211. // 帧抓取相关
  212. frameListener: null,
  213. isFrameCapturing: false,
  214. lastFrameCaptureAt: 0,
  215. frameCaptureIntervalMs: 5000,
  216. frameCanvasWidth: 320,
  217. frameCanvasHeight: 240,
  218. hiddenCanvasNode: null, // 仅用于调试展示,不在渲染中直接使用
  219. hiddenCanvasCtx: null, // 仅用于调试展示,不在渲染中直接使用
  220. hiddenCanvasReady: false,
  221. isWaitingForAnswer: false, // 添加新的状态来控制是否在等待用户回答
  222. currentFollowUpIndex: -1, // 当前追问问题索引
  223. showSubtitleText: false, // 是否显示字幕
  224. subtitleText: '', // 字幕文本
  225. parentQuestion: '', // 父问题文本
  226. isFollowUpQuestion: false, // 是否是追问问题
  227. loading: false,
  228. responses: [],
  229. processedResponses: [],
  230. assistantResponse: '',
  231. audioTranscript: '',
  232. videoPlaying: false,
  233. showDebugInfo: false, // 设置为true可以显示调试信息
  234. videoUrl: '', // 修改为空字符串,将通过API获取
  235. showReplayButton: false,
  236. cameraStream: null, // 存储摄像头流
  237. cameraError: null, // 存储摄像头错误信息
  238. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  239. cameraContext: null, // 添加相机上下文
  240. currentSubtitle: '',
  241. currentTranslation: '', // 添加翻译文本
  242. subtitles: [], // 修改为空数组,将通过API获取
  243. // 移除硬编码的字幕数组
  244. // secondVideoSubtitles: [...],
  245. // thirdVideoSubtitles: [...],
  246. // 等等...
  247. showAnswerButton: false, // 控制答题按钮显示
  248. currentVideoIndex: 0, // 当前播放的视频索引
  249. videoList: [], // 修改为空数组,将通过API获取
  250. // 其他属性保持不变...
  251. questions: [], // 添加新属性存储API返回的问题数据
  252. introVideoUrl: (() => {
  253. const DEFAULT_VIDEO_URL = 'https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4';
  254. try {
  255. const configStr = uni.getStorageSync('configData');
  256. if (!configStr) return DEFAULT_VIDEO_URL;
  257. const config = JSON.parse(configStr);
  258. return config.digital_human_opening_video_url || DEFAULT_VIDEO_URL;
  259. } catch (error) {
  260. console.warn('解析配置数据失败:', error);
  261. return DEFAULT_VIDEO_URL;
  262. }
  263. })(),//'https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4', // 保留介绍视频
  264. isRecording: false,
  265. recordingTimer: null,
  266. showStopRecordingButton: false,
  267. mediaRecorder: null,
  268. recordedChunks: [],
  269. recorder: null,
  270. lastUploadedVideoUrl: '',
  271. showStartRecordingButton: false,
  272. showRetryButton: false, // 控制重试按钮显示
  273. lastVideoToRetry: null, // 存储上次失败的视频URL,用于重试
  274. recordingStartTime: null, // 录制开始时间
  275. recordingTimerCount: 0, // 录制计时器计数
  276. recordingTimeDisplay: '00:00', // 格式化的录制时间显示
  277. // 添加上传队列相关数据
  278. uploadQueue: [], // 存储待上传的视频
  279. backgroundUploadQueue: [], // 存储后台上传的视频
  280. isUploading: false, // 标记是否正在上传
  281. uploadProgress: {}, // 存储每个视频的上传进度
  282. uploadStatus: {}, // 存储每个视频的上传状态
  283. showUploadStatus: false, // 是否显示上传状态指示器
  284. uploadStatusText: '', // 上传状态文本
  285. mediaRecorderTimeout: null, // 用于存储MediaRecorder的超时机制
  286. maxRecordingTime: 300, // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  287. remainingTime: 300, // 剩余录制时间(秒)- 从60秒改为300秒
  288. countdownValue: 10, // 倒计时数值
  289. showCountdown: false, // 是否显示倒计时蒙层
  290. countdownTimer: null,
  291. showGif: false, // 控制是否显示GIF
  292. gifUrl: '', // GIF图片的URL
  293. globalSocketTask: null, // 添加全局 WebSocket 连接对象
  294. lowScoreVideoUrl: (() => {
  295. const DEFAULT_VIDEO_URL = 'https://data.qicai321.com/minlong/latentsync/0530e7f5-1957-422d-8f34-ba4a92608081_result.mp4';
  296. try {
  297. const configStr = uni.getStorageSync('configData');
  298. if (!configStr) return DEFAULT_VIDEO_URL;
  299. const config = JSON.parse(configStr);
  300. return config.digital_human?.middle_video_url || DEFAULT_VIDEO_URL;
  301. } catch (error) {
  302. console.warn('解析配置数据失败:', error);
  303. return DEFAULT_VIDEO_URL;
  304. }
  305. })(),//'https://data.qicai321.com/minlong/latentsync/0530e7f5-1957-422d-8f34-ba4a92608081_result.mp4', // 低分提示视频URL
  306. showRerecordButton: false, // 控制重新录制按钮显示
  307. isPlayingLowScoreVideo: false, // 标记是否正在播放低分提示视频
  308. lowScoreVideoSubtitles: [
  309. {
  310. startTime: 0,
  311. endTime: 10, // 假设视频长度为10秒,可根据实际情况调整
  312. text: '我未听清楚您在说什么,请您再说一遍!'
  313. }
  314. ], // 用于存储低分提示视频的字幕
  315. retryCount: 0, // 添加重试次数计数器
  316. maxRetryAttempts: 2, // 最大重试次数限制
  317. needPlayLowScoreVideo: false, // 是否需要播放低分视频
  318. finalRecordingDuration: 0, // 最终录制时长
  319. historyTime: 0, // 添加历史时间记录
  320. followUpQuestions: [], // 存储追问问题数据
  321. currentParentQuestionId: null, // 当前父问题ID
  322. hasPlayedFollowUp: {}, // 记录已播放的追问,格式: {questionId: true}
  323. isFollowUpQuestion: false, // 标记当前是否正在播放追问问题
  324. currentFollowUpQuestion: null, // 添加当前追问问题的完整信息
  325. lastQuestionWasFollowUp: false,
  326. lastFollowUpQuestionId: null,
  327. // 追问次数控制 - 统一使用job_position_question_id作为key
  328. defaultFollowUpLimit: 1, // 默认每个主题目最多1次追问
  329. followUpLimitMap: {}, // { job_position_question_id: limit }
  330. followUpAskedCountMap: {}, // { job_position_question_id: askedCount }
  331. followUpRequestInFlight: {}, // { job_position_question_id: boolean }
  332. lastUpdateTime: Date.now(), // 添加最后更新时间戳
  333. subtitleMap: {}, // 用于存储字幕和翻译的映射
  334. progressPercent: 0, // 录制进度百分比
  335. progressColor: '#05dc8b', // 进度条颜色
  336. progressBgColor: 'rgba(0, 0, 0,0.3)', // 进度条背景色
  337. parentQuestion: '', // 添加父问题存储
  338. screenCaptureCount: 0, // 添加截屏次数记录
  339. personDetectionSocket: null, // WebSocket对象
  340. personDetectionInterval: null, // 定时器对象
  341. showCameraWarning: false, // 添加新的数据属性
  342. showPageWarning: false, // 添加新的数据属性
  343. showPageDRWarning:false,//多人提示
  344. lastIOSDetectionTime: 0, // iOS检测时间记录
  345. isIOSPhotoCapturing: false, // iOS拍照状态标记
  346. followUpQuestion: '', // 追问问题
  347. followUpAudioUrl: '', // 追问音频URL
  348. audioContext: null, // 音频上下文
  349. followUpQuestions: [], // 追问问题列表
  350. currentFollowUpIndex: -1, // 当前追问问题索引
  351. showSubtitleText: false, // 是否显示字幕
  352. subtitleText: '', // 字幕文本
  353. isAudioPlaying: false,
  354. isWaitingForAnswer: false, // 添加新的状态来控制是否在等待用户回答
  355. currentRecordingDuration: 0, // 添加当前录制时长
  356. parentJobPositionQuestionId: null, // 添加父问题的job_position_question_id
  357. isFollowUpMode: false, // 是否处于追问模式
  358. mainQuestionIndex: 0, // 当前主问题的索引
  359. isVideoSwitching: false, // 添加视频切换状态锁
  360. originalQuestionSubtitle: null, // 保存原始字幕信息
  361. isThinking: false, // 面试官思考中状态
  362. thinkingTimer: null, // 思考计时器
  363. questionRetryMap: {}, // 用于跟踪每个问题的重试次数
  364. maxQuestionRetries: 2, // 每个问题最大重试次数
  365. statusBarHeight: 0
  366. }
  367. },
  368. onLoad() {
  369. // 获取状态栏高度
  370. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 0
  371. const systemInfo = uni.getSystemInfoSync();
  372. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  373. if (isMiniProgram) {
  374. this.initPersonDetectionWebSocket();
  375. }
  376. },
  377. onReady() {
  378. // 页面初次渲染完成后再初始化隐藏canvas,保证节点已生成
  379. this.initHiddenCanvasNode(true);
  380. },
  381. mounted() {
  382. this.fetchQuestions();
  383. //this.fetchFollowUpQuestions();
  384. this.checkAudioPermission();
  385. this.initCamera();
  386. this.checkIOSCameraRecordPermission();
  387. this.checkAndFixRenderingIssues();
  388. setTimeout(() => {
  389. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  390. this.testAudioInput();
  391. }
  392. }, 3e3);
  393. this.historyTime = 0;
  394. uni.setKeepScreenOn({
  395. keepScreenOn: true
  396. });
  397. // 添加截屏监听
  398. uni.onUserCaptureScreen(() => {
  399. console.log('User captured screen');
  400. this.screenCaptureCount++;
  401. if (this.screenCaptureCount === 1) {
  402. uni.showModal({
  403. title: 'Warning',
  404. content: '检测到屏幕截图。如果你再次捕捉屏幕,你的面试将无效.',
  405. showCancel: false,
  406. confirmText: 'OK'
  407. });
  408. } else if (this.screenCaptureCount >= 2) {
  409. uni.showModal({
  410. title: 'Interview Invalid',
  411. content: '由于多次屏幕截图,您的面试已无效。请联系招聘人员寻求帮助.',
  412. showCancel: false,
  413. confirmText: 'OK',
  414. success: () => {
  415. this.invalidateInterview();
  416. }
  417. });
  418. }
  419. });
  420. // 兜底:mounted 后也尝试一次
  421. this.initHiddenCanvasNode(false);
  422. },
  423. beforeDestroy() {
  424. // 移除截屏监听
  425. uni.offUserCaptureScreen();
  426. this.cleanupPersonDetectionWebSocket();
  427. },
  428. methods: {
  429. // 初始化隐藏canvas,支持重试
  430. initHiddenCanvasNode(fromReady = false, retries = 3) {
  431. if (!this.useMiniProgramCameraComponent) return;
  432. try {
  433. const query = uni.createSelectorQuery().in(this);
  434. query.select('#hiddenCaptureCanvas')
  435. .fields({ node: true, size: true })
  436. .exec((res) => {
  437. const canvasNode = res && res[0] && res[0].node;
  438. if (canvasNode && canvasNode.getContext) {
  439. hiddenCanvasNodeRef = canvasNode;
  440. hiddenCanvasCtxRef = canvasNode.getContext('2d');
  441. // 可选:保留到data用于调试,但不要在渲染周期直接读写其属性
  442. this.hiddenCanvasNode = canvasNode;
  443. this.hiddenCanvasCtx = hiddenCanvasCtxRef;
  444. this.hiddenCanvasReady = true;
  445. console.info('隐藏canvas初始化成功(fromReady:', fromReady, ')');
  446. } else {
  447. console.warn('隐藏canvas未获取到node或不支持2D上下文(fromReady:', fromReady, ')');
  448. if (retries > 0) {
  449. setTimeout(() => this.initHiddenCanvasNode(fromReady, retries - 1), 300);
  450. }
  451. }
  452. });
  453. } catch (e) {
  454. console.warn('初始化隐藏canvas失败:', e);
  455. if (retries > 0) {
  456. setTimeout(() => this.initHiddenCanvasNode(fromReady, retries - 1), 300);
  457. }
  458. }
  459. },
  460. // 安全拍照再发送(非录制期)
  461. safeTakePhotoAndSend() {
  462. if (this.isFrameCapturing) return;
  463. if (!this.personDetectionSocket || this.personDetectionSocket.readyState !== 1) return;
  464. this.isFrameCapturing = true;
  465. try {
  466. this.cameraContext.takePhoto({
  467. quality: 'low',
  468. success: (res) => {
  469. const tempFilePath = res && res.tempImagePath;
  470. if (!tempFilePath) {
  471. this.isFrameCapturing = false;
  472. return;
  473. }
  474. uni.getFileSystemManager().readFile({
  475. filePath: tempFilePath,
  476. encoding: 'base64',
  477. success: (r) => {
  478. try {
  479. const base64Image = r.data;
  480. if (!this.personDetectionSocket || this.personDetectionSocket.readyState !== 1) {
  481. return;
  482. }
  483. this.personDetectionSocket.send({
  484. data: JSON.stringify({
  485. type: 'person_detection',
  486. image_data: base64Image
  487. })
  488. });
  489. } finally {
  490. this.isFrameCapturing = false;
  491. }
  492. },
  493. fail: () => {
  494. this.isFrameCapturing = false;
  495. }
  496. });
  497. },
  498. fail: () => {
  499. this.isFrameCapturing = false;
  500. }
  501. });
  502. } catch (e) {
  503. this.isFrameCapturing = false;
  504. }
  505. },
  506. // 录制期间抓帧并经隐藏canvas导出为图片再发送(持续监听 + 节流)
  507. startFrameStreamCapture() {
  508. if (!this.useMiniProgramCameraComponent) { console.warn('帧抓取:非小程序相机,跳过'); return; }
  509. if (!this.cameraContext || !this.cameraContext.onCameraFrame) { console.warn('帧抓取:cameraContext或onCameraFrame不可用'); return; }
  510. if (this.frameListener && this.frameListener.stop) { /* 已启动 */ return; }
  511. try {
  512. const listener = this.cameraContext.onCameraFrame((frame) => {
  513. const now = Date.now();
  514. if (now - this.lastFrameCaptureAt < (this.frameCaptureIntervalMs || 5000)) return;
  515. if (this.isFrameCapturing) return;
  516. if (!this.hiddenCanvasReady || !hiddenCanvasCtxRef || !hiddenCanvasNodeRef) {
  517. console.warn('帧抓取:隐藏canvas未就绪');
  518. // 触发一次重试初始化,避免偶发未就绪
  519. this.initHiddenCanvasNode(false, 2);
  520. return;
  521. }
  522. if (!this.personDetectionSocket || this.personDetectionSocket.readyState !== 1) { console.warn('帧抓取:WebSocket未就绪'); return; }
  523. this.isFrameCapturing = true;
  524. this.lastFrameCaptureAt = now;
  525. try {
  526. const raw = frame && frame.data;
  527. const frameW = (frame && frame.width) || this.frameCanvasWidth;
  528. const frameH = (frame && frame.height) || this.frameCanvasHeight;
  529. const canvas = hiddenCanvasNodeRef;
  530. if (!canvas) { throw new Error('hiddenCanvasNode is undefined'); }
  531. // 仅设置,不读取,避免未定义读取触发异常
  532. canvas.width = frameW;
  533. canvas.height = frameH;
  534. const ctx = hiddenCanvasCtxRef;
  535. if (!raw) { throw new Error('frame.data is undefined'); }
  536. const rgba = raw instanceof Uint8ClampedArray ? raw : new Uint8ClampedArray(raw);
  537. const imageData = ctx.createImageData(frameW, frameH);
  538. imageData.data.set(rgba);
  539. ctx.putImageData(imageData, 0, 0);
  540. const exportSuccess = (tempFilePath) => {
  541. uni.getFileSystemManager().readFile({
  542. filePath: tempFilePath,
  543. encoding: 'base64',
  544. success: (r) => {
  545. if (this.personDetectionSocket && this.personDetectionSocket.readyState === 1) {
  546. this.personDetectionSocket.send({
  547. data: JSON.stringify({ type: 'person_detection', image_data: r.data })
  548. });
  549. console.info('帧抓取:已发送图片');
  550. }
  551. this.isFrameCapturing = false;
  552. },
  553. fail: () => {
  554. console.warn('帧抓取:读取临时图片失败');
  555. this.isFrameCapturing = false;
  556. }
  557. });
  558. };
  559. const doExport = () => {
  560. // 优先使用 node.toTempFilePath,不可用时回退到 uni.canvasToTempFilePath(传入canvas与上下文)
  561. if (canvas && typeof canvas.toTempFilePath === 'function') {
  562. canvas.toTempFilePath({
  563. fileType: 'jpg',
  564. quality: 0.5,
  565. success: (res) => exportSuccess(res.tempFilePath),
  566. fail: (err) => {
  567. console.warn('帧抓取:node.toTempFilePath 失败,回退到uni.canvasToTempFilePath', err);
  568. uni.canvasToTempFilePath({
  569. canvas: hiddenCanvasNodeRef,
  570. x: 0,
  571. y: 0,
  572. width: frameW,
  573. height: frameH,
  574. destWidth: frameW,
  575. destHeight: frameH,
  576. fileType: 'jpg',
  577. quality: 0.5,
  578. success: (res2) => exportSuccess(res2.tempFilePath),
  579. fail: (err2) => { console.warn('帧抓取:uni.canvasToTempFilePath 也失败', err2); this.isFrameCapturing = false; }
  580. }, this);
  581. }
  582. });
  583. } else {
  584. uni.canvasToTempFilePath({
  585. canvas: hiddenCanvasNodeRef,
  586. x: 0,
  587. y: 0,
  588. width: frameW,
  589. height: frameH,
  590. destWidth: frameW,
  591. destHeight: frameH,
  592. fileType: 'jpg',
  593. quality: 0.5,
  594. success: (res3) => exportSuccess(res3.tempFilePath),
  595. fail: (err3) => { console.warn('帧抓取:uni.canvasToTempFilePath 失败', err3); this.isFrameCapturing = false; }
  596. }, this);
  597. }
  598. };
  599. // 确保绘制已提交到画布,再导出
  600. if (typeof canvas.requestAnimationFrame === 'function') {
  601. canvas.requestAnimationFrame(() => doExport());
  602. } else {
  603. setTimeout(() => doExport(), 16);
  604. }
  605. } catch (err) {
  606. console.warn('帧抓取:绘制或导出异常', err);
  607. this.isFrameCapturing = false;
  608. }
  609. });
  610. // 小程序需调用 start 启动监听
  611. if (listener && listener.start) { listener.start(); console.info('帧抓取:监听已启动'); }
  612. this.frameListener = listener;
  613. } catch (e) {
  614. console.warn('启动帧抓取监听失败,回退到拍照方案:', e);
  615. }
  616. },
  617. stopFrameStreamCapture() {
  618. try {
  619. if (this.frameListener && this.frameListener.stop) {
  620. this.frameListener.stop();
  621. }
  622. } catch (e) {}
  623. this.frameListener = null;
  624. },
  625. // ===== 追问次数控制:工具方法 =====
  626. getFollowUpLimit(jobPositionQuestionId) {
  627. if (!jobPositionQuestionId) return this.defaultFollowUpLimit;
  628. return this.followUpLimitMap[jobPositionQuestionId] || this.defaultFollowUpLimit;
  629. },
  630. setFollowUpLimit(jobPositionQuestionId, limit) {
  631. if (!jobPositionQuestionId) return;
  632. const parsed = parseInt(limit);
  633. this.followUpLimitMap[jobPositionQuestionId] = Number.isFinite(parsed) && parsed > 0 ? parsed : this.defaultFollowUpLimit;
  634. console.log(`设置问题 ${jobPositionQuestionId} 的追问上限为: ${this.followUpLimitMap[jobPositionQuestionId]}`);
  635. },
  636. resetFollowUpAskedCount(jobPositionQuestionId) {
  637. if (!jobPositionQuestionId) return;
  638. this.followUpAskedCountMap[jobPositionQuestionId] = 1;
  639. console.log(`重置问题 ${jobPositionQuestionId} 的追问次数为: 1`);
  640. },
  641. incrementFollowUpCount(jobPositionQuestionId) {
  642. if (!jobPositionQuestionId) return;
  643. const current = this.followUpAskedCountMap[jobPositionQuestionId] || 0;
  644. this.followUpAskedCountMap[jobPositionQuestionId] = current + 1;
  645. console.log(`问题 ${jobPositionQuestionId} 的追问次数增加到: ${this.followUpAskedCountMap[jobPositionQuestionId]}`);
  646. },
  647. canAskMoreFollowUps(jobPositionQuestionId) {
  648. if (!jobPositionQuestionId) return false;
  649. const asked = this.followUpAskedCountMap[jobPositionQuestionId] || 0;
  650. const limit = this.getFollowUpLimit(jobPositionQuestionId);
  651. const canAsk = asked < limit;
  652. console.log(`检查问题 ${jobPositionQuestionId} 是否可以继续追问: ${canAsk} (已问${asked}次/上限${limit}次)`);
  653. return canAsk;
  654. },
  655. async maybeRequestNextFollowUp(jobPositionQuestionId) {
  656. if (!jobPositionQuestionId) {
  657. console.log('缺少job_position_question_id,直接进入下一题');
  658. this.proceedToNextQuestion();
  659. return;
  660. }
  661. console.log(`检查是否需要继续追问,问题ID: ${jobPositionQuestionId}`);
  662. if (this.canAskMoreFollowUps(jobPositionQuestionId)) {
  663. // 避免重复触发并发请求
  664. if (this.followUpRequestInFlight[jobPositionQuestionId]) {
  665. console.log('已有追问请求进行中,跳过重复触发');
  666. return;
  667. }
  668. // 交由 callInterviewInteraction 统一处理并发标记
  669. const ok = await this.callInterviewInteraction(jobPositionQuestionId);
  670. if (!ok) {
  671. console.log('未获取到下一条追问或调用失败,进入下一题');
  672. this.isFollowUpMode = false;
  673. this.isFollowUpQuestion = false;
  674. this.currentFollowUpQuestion = null;
  675. this.proceedToNextQuestion();
  676. }
  677. } else {
  678. // 已达上限,进入下一题
  679. console.log(`问题 ${jobPositionQuestionId} 追问次数已达上限,进入下一题`);
  680. this.isFollowUpMode = false;
  681. this.isFollowUpQuestion = false;
  682. this.currentFollowUpQuestion = null;
  683. this.proceedToNextQuestion();
  684. }
  685. },
  686. // 获取当前问题信息
  687. getCurrentQuestionByIndex(index) {
  688. // 这里需要根据实际的问题数据结构来实现
  689. // 如果问题数据存储在某个数组中,返回对应索引的问题
  690. // 暂时返回null,需要根据实际数据结构调整
  691. return null;
  692. },
  693. // 处理音频播放完成
  694. handleAudioEnd() {
  695. console.log('音频播放完成');
  696. // 设置等待回答状态
  697. this.isWaitingForAnswer = true;
  698. // 显示开始作答按钮
  699. this.showStartRecordingButton = true;
  700. // 延迟一下再开始倒计时,避免重复触发
  701. setTimeout(() => {
  702. // 开始倒计时
  703. this.startCountdown();
  704. }, 100);
  705. },
  706. // 开始倒计时
  707. startCountdown() {
  708. this.showCountdown = true;
  709. this.countdownValue = 5; // 重置倒计时值
  710. this.countdownTimer = setInterval(() => {
  711. this.countdownValue--;
  712. if (this.countdownValue <= 0) {
  713. // 倒计时结束,清除定时器
  714. this.clearCountdown();
  715. // 隐藏开始作答按钮
  716. this.showStartRecordingButton = false;
  717. // 开始录制
  718. this.startRecordingAnswer();
  719. }
  720. }, 1000);
  721. },
  722. // 清除倒计时
  723. clearCountdown() {
  724. if (this.countdownTimer) {
  725. clearInterval(this.countdownTimer);
  726. this.countdownTimer = null;
  727. }
  728. this.showCountdown = false;
  729. },
  730. // 处理追问问题
  731. async handleFollowUpQuestion(questionData) {
  732. console.log('处理追问:', questionData);
  733. this.enterFollowUpMode();
  734. console.log('处理追问问题数据:', questionData);
  735. // 检查数据完整性
  736. if (!questionData || !questionData.follow_up_voice_url || !questionData.follow_up_question) {
  737. console.error('追问数据不完整:', questionData);
  738. uni.showToast({
  739. title: '获取追问数据失败',
  740. icon: 'none'
  741. });
  742. return;
  743. }
  744. // 保存父问题
  745. this.parentQuestion = questionData.original_question || '';
  746. // 确保音频URL正确
  747. let audioUrl = '';
  748. if (questionData.follow_up_voice_url.direct_url) {
  749. audioUrl = questionData.follow_up_voice_url.direct_url;
  750. } else if (questionData.follow_up_voice_url.file_url) {
  751. audioUrl = questionData.follow_up_voice_url.file_url;
  752. }
  753. // 确保音频URL是完整的
  754. if (!audioUrl.startsWith('http')) {
  755. audioUrl = apiBaseUrl + audioUrl;
  756. }
  757. console.log('处理后的音频URL:', audioUrl);
  758. // 创建追问问题对象
  759. const followUpQuestionObj = {
  760. id: Date.now(),
  761. question: questionData.follow_up_question.trim(), // 去除可能的空白字符
  762. audioUrl: audioUrl,
  763. originalQuestion: questionData.original_question,
  764. sessionId: questionData.session_id
  765. };
  766. console.log('创建的追问问题对象:', followUpQuestionObj);
  767. // 添加到追问问题列表
  768. this.followUpQuestions.push(followUpQuestionObj);
  769. this.currentFollowUpIndex = this.followUpQuestions.length - 1;
  770. // 更新当前追问问题
  771. this.followUpQuestion = followUpQuestionObj.question;
  772. this.followUpAudioUrl = followUpQuestionObj.audioUrl;
  773. // 标记当前是追问问题
  774. this.isFollowUpQuestion = true;
  775. this.currentFollowUpQuestion = followUpQuestionObj;
  776. // 显示字幕
  777. this.showSubtitle(followUpQuestionObj.question, true);
  778. // 等待音频播放完成
  779. try {
  780. await this.playFollowUpAudio();
  781. console.log('音频播放完成,准备录制回答');
  782. // 开始准备录制回答
  783. this.prepareToAnswer(true, followUpQuestionObj);
  784. } catch (error) {
  785. console.error('音频播放失败:', error);
  786. // 即使音频播放失败,也继续准备录制回答
  787. this.prepareToAnswer(true, followUpQuestionObj);
  788. }
  789. },
  790. // 准备回答问题
  791. prepareToAnswer(isFollowUp = false, questionData = null) {
  792. console.log('准备回答问题:', { isFollowUp, questionData });
  793. // 重置录制状态
  794. this.isRecording = false;
  795. this.recordedTime = 0;
  796. this.showStopRecordingButton = false;
  797. // 设置当前问题状态
  798. if (isFollowUp && questionData) {
  799. this.currentFollowUpQuestion = questionData;
  800. this.isFollowUpQuestion = true;
  801. }
  802. // 设置等待回答状态
  803. this.isWaitingForAnswer = true;
  804. // 显示开始回答按钮和倒计时
  805. this.handleAudioEnd();
  806. },
  807. // 调用面试互动接口
  808. async callInterviewInteraction(jobPositionQuestionId, retryCount = 0, maxRetries = 5) {
  809. // 防重入:同一父问题并发保护
  810. /* if (jobPositionQuestionId && this.followUpRequestInFlight[jobPositionQuestionId]) {
  811. console.log('追问请求已在进行中,跳过本次调用');
  812. return false;
  813. }
  814. if (jobPositionQuestionId) {
  815. this.followUpRequestInFlight[jobPositionQuestionId] = true;
  816. } */
  817. const userInfo = JSON.parse(uni.getStorageSync('userInfo'));
  818. const appId = uni.getStorageSync('appId');
  819. const positionConfigId = JSON.parse(uni.getStorageSync('configData')).id;
  820. try {
  821. // 显示思考中loading
  822. this.showThinkingLoading();
  823. console.log('开始调用面试互动接口', { jobPositionQuestionId, appId });
  824. const res = await uni.request({
  825. url: `${apiBaseUrl}/api/voice_interview_interaction/`,
  826. method: 'POST',
  827. data: {
  828. tenant_id: uni.getStorageSync('tenant_id') ||userInfo.tenant_id || 1,
  829. question_id: jobPositionQuestionId,
  830. position_config_id: positionConfigId,
  831. application_id: appId
  832. },
  833. header: {
  834. 'content-type': 'application/json'
  835. }
  836. });
  837. console.log('面试互动接口返回数据:', res);
  838. // 处理4000状态码(视频未转写完成)的情况
  839. if (res.statusCode === 400) {
  840. if (retryCount < maxRetries) {
  841. console.log(`视频转写未完成,${retryCount + 1}次重试中...`);
  842. // 等待3秒后重试
  843. await new Promise(resolve => setTimeout(resolve, 3000));
  844. return this.callInterviewInteraction(jobPositionQuestionId, retryCount + 1, maxRetries);
  845. } else {
  846. console.log('达到最大重试次数,视频转写仍未完成');
  847. uni.showToast({
  848. title: '视频转写处理中,请稍后再试',
  849. icon: 'none',
  850. duration: 2000
  851. });
  852. return false;
  853. }
  854. }
  855. if (res.data.success) {
  856. // 成功获取到一条追问,计数 +1(表示"已请求"这条追问)
  857. if (jobPositionQuestionId) {
  858. this.incrementFollowUpCount(jobPositionQuestionId);
  859. }
  860. // 处理本次追问(即便已达到上限,也应该播放本次追问)
  861. await this.handleFollowUpQuestion(res.data);
  862. this.hideThinkingLoading();
  863. return true;
  864. } else {
  865. console.error('面试互动接口返回错误:', res.data);
  866. this.hideThinkingLoading();
  867. return false;
  868. }
  869. } catch (error) {
  870. console.error('调用面试互动接口失败:', error);
  871. this.hideThinkingLoading();
  872. return false;
  873. } /* finally {
  874. // 清除并发标记
  875. if (jobPositionQuestionId) {
  876. this.followUpRequestInFlight[jobPositionQuestionId] = false;
  877. }
  878. } */
  879. },
  880. // 停止并销毁音频
  881. stopAndDestroyAudio() {
  882. if (!this.audioContext) {
  883. return;
  884. }
  885. const ctx = this.audioContext;
  886. this.audioContext = null; // 先清除引用
  887. try {
  888. if (this.isAudioPlaying) {
  889. ctx.stop();
  890. }
  891. } catch (error) {
  892. console.error('停止音频播放失败:', error);
  893. }
  894. try {
  895. ctx.destroy();
  896. } catch (error) {
  897. console.error('销毁音频实例失败:', error);
  898. }
  899. this.isAudioPlaying = false;
  900. },
  901. // 修改准备回答方法
  902. prepareToAnswer(isFollowUp = false, questionData = null) {
  903. // 重置录制状态
  904. this.isRecording = false;
  905. this.recordedTime = 0;
  906. this.showStopRecordingButton = false;
  907. // 设置当前问题状态
  908. if (isFollowUp && questionData) {
  909. this.currentFollowUpQuestion = questionData;
  910. this.isFollowUpQuestion = true;
  911. }
  912. // 设置等待回答状态
  913. this.isWaitingForAnswer = true;
  914. // 显示开始回答按钮和倒计时
  915. this.handleAudioEnd();
  916. },
  917. // 初始化相机
  918. async initCamera() {
  919. // 检查平台
  920. const systemInfo = uni.getSystemInfoSync();
  921. // 判断是否在小程序环境中
  922. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  923. systemInfo.uniPlatform === 'mp-alipay' ||
  924. systemInfo.uniPlatform === 'mp-baidu';
  925. if (isMiniProgram) {
  926. // 小程序环境使用小程序的相机API
  927. this.useMiniProgramCameraComponent = true;
  928. // 创建相机上下文
  929. this.cameraContext = uni.createCameraContext();
  930. // 确保已获取录音和相机权限
  931. uni.getSetting({
  932. success: (res) => {
  933. // 检查录音权限
  934. if (!res.authSetting['scope.record']) {
  935. uni.authorize({
  936. scope: 'scope.record',
  937. success: () => {
  938. console.log('录音权限已获取');
  939. },
  940. fail: (err) => {
  941. console.error('录音权限获取失败:', err);
  942. this.showPermissionDialog('录音');
  943. }
  944. });
  945. }
  946. // 检查相机权限
  947. if (!res.authSetting['scope.camera']) {
  948. uni.authorize({
  949. scope: 'scope.camera',
  950. success: () => {
  951. console.log('相机权限已获取');
  952. },
  953. fail: (err) => {
  954. console.error('相机权限获取失败:', err);
  955. this.showPermissionDialog('相机');
  956. }
  957. });
  958. }
  959. // 在iOS上,可能需要额外的权限检查
  960. const systemInfo = uni.getSystemInfoSync();
  961. if (systemInfo.platform === 'ios') {
  962. // 在iOS上,不要使用无效的权限范围
  963. // 移除以下代码块
  964. /*
  965. if (!res.authSetting['scope.camera.record']) {
  966. uni.authorize({
  967. scope: 'scope.camera.record',
  968. success: () => {
  969. console.log('相机录制权限已获取');
  970. },
  971. fail: (err) => {
  972. console.error('相机录制权限获取失败:', err);
  973. this.showPermissionDialog('相机录制');
  974. }
  975. });
  976. }
  977. */
  978. // 确保同时获取了相机和录音权限
  979. if (!res.authSetting['scope.camera'] || !res.authSetting['scope.record']) {
  980. console.log('iOS需要同时获取相机和录音权限');
  981. }
  982. }
  983. }
  984. });
  985. } else {
  986. // H5/App环境使用浏览器的API
  987. try {
  988. // 请求摄像头和麦克风权限并获取媒体流,添加更多约束
  989. const constraints = {
  990. audio: {
  991. echoCancellation: true,
  992. noiseSuppression: true,
  993. autoGainControl: true
  994. },
  995. video: {
  996. width: { ideal: 640, max: 1280 }, // 控制视频宽度
  997. height: { ideal: 480, max: 720 }, // 控制视频高度
  998. frameRate: { ideal: 15, max: 24 }, // 控制帧率
  999. facingMode: 'user'
  1000. }
  1001. };
  1002. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  1003. // 保存流以便后续使用
  1004. this.cameraStream = stream;
  1005. // 检查音频轨道
  1006. const audioTracks = stream.getAudioTracks();
  1007. console.log('音频轨道数量:', audioTracks.length);
  1008. if (audioTracks.length > 0) {
  1009. console.log('音频轨道已获取:', audioTracks[0].label);
  1010. // 确保音频轨道已启用
  1011. audioTracks[0].enabled = true;
  1012. } else {
  1013. console.warn('未检测到音频轨道,尝试单独获取音频');
  1014. this.tryGetAudioOnly();
  1015. }
  1016. // 将流设置到视频元素
  1017. const videoElement = this.$refs.userCameraVideo;
  1018. if (videoElement) {
  1019. videoElement.srcObject = stream;
  1020. videoElement.muted = true; // 避免回声,但仍然录制声音
  1021. }
  1022. } catch (error) {
  1023. console.error('获取摄像头失败:', error);
  1024. this.cameraError = error.message || '无法访问摄像头';
  1025. // 显示错误提示
  1026. uni.showToast({
  1027. title: '无法访问摄像头,请检查权限设置',
  1028. icon: 'none'
  1029. });
  1030. }
  1031. }
  1032. },
  1033. // 停止用户摄像头
  1034. stopUserCamera() {
  1035. if (this.cameraStream) {
  1036. // 停止所有轨道
  1037. this.cameraStream.getTracks().forEach(track => {
  1038. track.stop();
  1039. });
  1040. this.cameraStream = null;
  1041. }
  1042. },
  1043. async fetchData() {
  1044. this.loading = true
  1045. this.assistantResponse = ''
  1046. this.audioTranscript = ''
  1047. this.processedResponses = []
  1048. // 显示加载提示
  1049. uni.showLoading({
  1050. title: '面试官思考中...',
  1051. mask: true // 添加遮罩,防止用户触摸界面
  1052. })
  1053. try {
  1054. // 使用uni.request代替fetch
  1055. const requestTask = uni.request({
  1056. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  1057. method: 'POST',
  1058. header: {
  1059. 'Content-Type': 'application/json',
  1060. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  1061. },
  1062. data: {
  1063. "model": "qwen-omni-turbo",
  1064. "messages": [
  1065. {
  1066. "role": "user",
  1067. "content": [
  1068. {
  1069. "type": "input_audio",
  1070. "input_audio": {
  1071. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  1072. "format": "wav"
  1073. }
  1074. },
  1075. {
  1076. "type": "text",
  1077. "text": "这段音频在说什么"
  1078. }
  1079. ]
  1080. }
  1081. ],
  1082. "stream":true,
  1083. "stream_options":{
  1084. "include_usage":true
  1085. },
  1086. "modalities":["text","audio"],
  1087. "audio":{"voice":"Cherry","format":"wav"}
  1088. },
  1089. success: (res) => {
  1090. console.log('请求成功,响应数据:', res.data);
  1091. // 检查响应数据是否包含多个JSON对象
  1092. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  1093. // 处理包含多个JSON对象的情况
  1094. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  1095. chunks.forEach(chunk => {
  1096. this.handleStreamResponse(chunk);
  1097. });
  1098. } else {
  1099. // 处理单个响应对象的情况
  1100. this.handleStreamResponse(res.data);
  1101. }
  1102. // 模拟获取到数字人视频并播放
  1103. this.playDigitalHumanVideo();
  1104. },
  1105. fail: (err) => {
  1106. console.error('请求失败:', err);
  1107. uni.showToast({
  1108. title: '请求失败,请重试',
  1109. icon: 'none'
  1110. });
  1111. },
  1112. complete: () => {
  1113. this.loading = false;
  1114. // 隐藏加载提示
  1115. uni.hideLoading();
  1116. }
  1117. });
  1118. } catch (error) {
  1119. console.error('获取数据失败:', error);
  1120. this.loading = false;
  1121. // 隐藏加载提示
  1122. uni.hideLoading();
  1123. uni.showToast({
  1124. title: '系统错误,请重试',
  1125. icon: 'none'
  1126. });
  1127. }
  1128. },
  1129. handleStreamResponse(data) {
  1130. // 处理流式响应数据
  1131. if (typeof data === 'string') {
  1132. // 处理字符串格式的响应
  1133. if (data === '[DONE]') return;
  1134. try {
  1135. // 移除可能存在的换行符和多余空格
  1136. const cleanData = data.trim();
  1137. // 检查是否是有效的JSON字符串
  1138. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  1139. const jsonData = JSON.parse(cleanData);
  1140. this.processStreamChunk(jsonData);
  1141. }
  1142. } catch (e) {
  1143. console.error('解析JSON失败:', e, '原始数据:', data);
  1144. }
  1145. } else {
  1146. // 处理对象格式的响应
  1147. this.processStreamChunk(data);
  1148. }
  1149. },
  1150. processStreamChunk(chunk) {
  1151. if (chunk.choices && chunk.choices.length > 0) {
  1152. const choice = chunk.choices[0];
  1153. // 处理助手回复内容
  1154. if (choice.delta && choice.delta.content) {
  1155. this.assistantResponse += choice.delta.content;
  1156. }
  1157. // 处理音频转写内容
  1158. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  1159. this.audioTranscript += choice.delta.audio.transcript;
  1160. }
  1161. // 处理角色和音频转写
  1162. if (choice.delta) {
  1163. const result = {};
  1164. if (choice.delta.role) {
  1165. result.role = choice.delta.role;
  1166. }
  1167. if (choice.delta.audio && choice.delta.audio.transcript) {
  1168. result.transcript = choice.delta.audio.transcript;
  1169. }
  1170. if (Object.keys(result).length > 0) {
  1171. this.processedResponses.push(result);
  1172. }
  1173. }
  1174. }
  1175. },
  1176. processResponseData() {
  1177. // 处理返回的数据
  1178. this.processedResponses = this.responses.map(item => {
  1179. const result = {}
  1180. // 处理角色信息
  1181. if (item.delta && item.delta.role) {
  1182. result.role = item.delta.role
  1183. }
  1184. // 处理音频转写文本
  1185. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  1186. result.transcript = item.delta.audio.transcript
  1187. }
  1188. return result
  1189. }).filter(item => Object.keys(item).length > 0)
  1190. },
  1191. // 播放数字人视频
  1192. playDigitalHumanVideo() {
  1193. // 如果还没有加载问题数据,先获取问题数据
  1194. if (this.videoList.length === 0) {
  1195. this.fetchQuestions();
  1196. return;
  1197. }
  1198. // 设置当前视频
  1199. this.videoUrl = this.videoList[this.currentVideoIndex];
  1200. this.videoPlaying = true;
  1201. // 使用 uni.createVideoContext 来控制视频
  1202. this.$nextTick(() => {
  1203. const videoContext = uni.createVideoContext('myVideo', this);
  1204. if (videoContext) {
  1205. videoContext.play();
  1206. // 设置超时检查,确认视频是否真的在播放
  1207. setTimeout(() => {
  1208. if (this.videoPlaying && this.$refs.videoPlayer) {
  1209. console.log('视频应该正在播放');
  1210. } else {
  1211. console.log('视频可能未成功播放,尝试替代方案');
  1212. this.tryAlternativeVideoPath();
  1213. }
  1214. }, 1000);
  1215. } else {
  1216. console.error('无法创建视频上下文');
  1217. this.tryAlternativeVideoPath();
  1218. }
  1219. });
  1220. },
  1221. // 修改 tryAlternativeVideoPath 方法
  1222. tryAlternativeVideoPath() {
  1223. console.log('尝试使用替代路径');
  1224. // 尝试不同的路径格式
  1225. const alternativePaths = [
  1226. './static/demo.mp4',
  1227. '../static/demo.mp4',
  1228. 'static/demo.mp4',
  1229. '/static/demo.mp4',
  1230. // 添加绝对路径
  1231. `${window.location.origin}/static/demo.mp4`
  1232. ];
  1233. // 获取当前路径索引
  1234. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  1235. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  1236. // 设置下一个路径
  1237. this.videoUrl = alternativePaths[nextPathIndex];
  1238. console.log('尝试新路径:', this.videoUrl);
  1239. this.$nextTick(() => {
  1240. const videoContext = uni.createVideoContext('myVideo', this);
  1241. if (videoContext) {
  1242. videoContext.stop();
  1243. videoContext.play();
  1244. // 检查是否成功播放
  1245. setTimeout(() => {
  1246. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  1247. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  1248. this.checkVideoWithAPI();
  1249. }
  1250. }, 1000);
  1251. }
  1252. });
  1253. },
  1254. // 添加新方法:使用uni API检查视频
  1255. checkVideoWithAPI() {
  1256. // 尝试使用uni.getVideoInfo API检查视频是否可用
  1257. uni.getVideoInfo({
  1258. src: '/static/demo.mp4',
  1259. success: (res) => {
  1260. console.log('视频信息获取成功:', res);
  1261. // 如果能获取到视频信息,再次尝试播放
  1262. this.videoUrl = '/static/demo.mp4';
  1263. this.$nextTick(() => {
  1264. const videoContext = uni.createVideoContext('myVideo', this);
  1265. if (videoContext) {
  1266. videoContext.play();
  1267. }
  1268. });
  1269. },
  1270. fail: (err) => {
  1271. console.error('视频信息获取失败:', err);
  1272. // 最后尝试使用uni.chooseVideo API
  1273. this.fallbackToLocalVideo();
  1274. }
  1275. });
  1276. },
  1277. // 添加新方法:回退到本地视频
  1278. fallbackToLocalVideo() {
  1279. console.log('尝试使用本地视频资源');
  1280. // 检查平台
  1281. const platform = uni.getSystemInfoSync().platform;
  1282. if (platform === 'android' || platform === 'ios') {
  1283. // 移动端可以尝试使用本地资源
  1284. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  1285. this.$nextTick(() => {
  1286. const videoContext = uni.createVideoContext('myVideo', this);
  1287. if (videoContext) {
  1288. videoContext.play();
  1289. }
  1290. });
  1291. } else {
  1292. // 最终回退到静态图片
  1293. this.videoPlaying = false;
  1294. uni.showToast({
  1295. title: '视频加载失败,显示静态图片',
  1296. icon: 'none'
  1297. });
  1298. }
  1299. },
  1300. // 修改 handleVideoError 方法
  1301. handleVideoError(e) {
  1302. console.error('视频加载错误:', e);
  1303. // 记录更详细的错误信息
  1304. if (e && e.detail) {
  1305. console.error('详细错误信息:', e.detail);
  1306. }
  1307. // 检查视频文件是否存在
  1308. uni.getFileInfo({
  1309. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  1310. success: (res) => {
  1311. console.log('文件存在,大小:', res.size);
  1312. // 文件存在但播放失败,可能是格式问题
  1313. this.tryDifferentFormat();
  1314. },
  1315. fail: (err) => {
  1316. console.error('文件不存在或无法访问:', err);
  1317. // 尝试不同路径
  1318. this.tryAlternativeVideoPath();
  1319. }
  1320. });
  1321. // 如果多次尝试后仍然失败,显示错误信息
  1322. uni.showToast({
  1323. title: '视频加载失败,请检查文件是否存在',
  1324. icon: 'none',
  1325. duration: 2000
  1326. });
  1327. },
  1328. // 添加新方法:尝试不同格式
  1329. tryDifferentFormat() {
  1330. console.log('尝试不同的视频格式');
  1331. // 尝试不同的视频格式
  1332. const formats = [
  1333. { ext: 'mp4', mime: 'video/mp4' },
  1334. { ext: 'webm', mime: 'video/webm' },
  1335. { ext: 'ogg', mime: 'video/ogg' },
  1336. { ext: 'mov', mime: 'video/quicktime' }
  1337. ];
  1338. // 获取当前文件名(不含扩展名)
  1339. const currentPath = this.videoUrl;
  1340. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  1341. // 尝试下一个格式
  1342. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  1343. if (nextFormat) {
  1344. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  1345. console.log('尝试新格式:', this.videoUrl);
  1346. this.$nextTick(() => {
  1347. const videoContext = uni.createVideoContext('myVideo', this);
  1348. if (videoContext) {
  1349. videoContext.stop();
  1350. videoContext.play();
  1351. }
  1352. });
  1353. } else {
  1354. // 所有格式都尝试过了,使用内置资源
  1355. this.useBuiltInResource();
  1356. }
  1357. },
  1358. // 添加新方法:使用内置资源
  1359. useBuiltInResource() {
  1360. console.log('尝试使用内置资源');
  1361. // 检查平台
  1362. const platform = uni.getSystemInfoSync().platform;
  1363. // 根据平台选择合适的视频源
  1364. if (platform === 'windows') {
  1365. // Windows平台特定处理
  1366. // 尝试使用相对于应用根目录的路径
  1367. const appRoot = process.env.UNI_INPUT_DIR || '';
  1368. this.videoUrl = `./static/demo.mp4`;
  1369. // 或者尝试使用file://协议
  1370. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  1371. console.log('Windows平台尝试路径:', this.videoUrl);
  1372. } else if (platform === 'android' || platform === 'ios') {
  1373. // 移动端
  1374. this.useNativeVideo();
  1375. } else {
  1376. // Web平台
  1377. // 尝试使用完整URL
  1378. const baseUrl = window.location.origin;
  1379. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  1380. console.log('Web平台尝试URL:', this.videoUrl);
  1381. }
  1382. this.$nextTick(() => {
  1383. const videoContext = uni.createVideoContext('myVideo', this);
  1384. if (videoContext) {
  1385. videoContext.play();
  1386. }
  1387. });
  1388. },
  1389. // 添加新方法:使用原生视频能力
  1390. useNativeVideo() {
  1391. console.log('尝试使用原生视频能力');
  1392. // 在移动端,可以尝试使用原生视频播放器
  1393. uni.chooseVideo({
  1394. sourceType: ['album'],
  1395. success: (res) => {
  1396. this.videoUrl = res.tempFilePath;
  1397. this.$nextTick(() => {
  1398. const videoContext = uni.createVideoContext('myVideo', this);
  1399. if (videoContext) {
  1400. videoContext.play();
  1401. }
  1402. });
  1403. },
  1404. fail: () => {
  1405. // 如果用户取消选择,回退到静态图片
  1406. this.videoPlaying = false;
  1407. /* uni.showToast({
  1408. title: '无法加载视频,显示静态图片',
  1409. icon: 'none'
  1410. }); */
  1411. }
  1412. });
  1413. },
  1414. // 处理视频结束事件
  1415. handleVideoEnded() {
  1416. console.log('视频播放结束事件触发');
  1417. // 获取视频元素和当前时间
  1418. const videoElement = this.$refs.videoPlayer;
  1419. if (videoElement) {
  1420. const currentTime = videoElement.currentTime || 0;
  1421. const duration = videoElement.duration || 0;
  1422. // 更严格的检查:只有当前时间非常接近视频总时长时才认为是真正结束
  1423. if (duration > 0 && (duration - currentTime > 0.5)) {
  1424. console.log('检测到可能的误触发结束事件,继续播放', currentTime, duration);
  1425. // 继续播放视频
  1426. setTimeout(() => {
  1427. const videoContext = uni.createVideoContext('myVideo', this);
  1428. if (videoContext) {
  1429. // 如果距离结束还很远,回到历史位置
  1430. if (duration - currentTime > 2) {
  1431. videoContext.seek(this.historyTime || 0);
  1432. } else {
  1433. // 如果接近结束但还没完全结束,继续播放
  1434. videoContext.play();
  1435. }
  1436. }
  1437. }, 100);
  1438. return;
  1439. }
  1440. console.log('视频确认结束,当前时间:', currentTime, '总时长:', duration);
  1441. }
  1442. this.videoPlaying = false;
  1443. // 检查是否是低分提示视频
  1444. if (this.isPlayingLowScoreVideo) {
  1445. console.log('低分提示视频播放完成');
  1446. // 检查重试次数
  1447. if (this.retryCount < this.maxRetryAttempts) {
  1448. console.log(`显示重新录制按钮 (重试次数: ${this.retryCount + 1}/${this.maxRetryAttempts})`);
  1449. // 显示重新录制按钮
  1450. this.showRerecordButton = true;
  1451. // 恢复原始问题的字幕
  1452. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1453. this.currentSubtitle = this.currentFollowUpQuestion.question || this.currentFollowUpQuestion.question;
  1454. } else if (this.originalQuestionSubtitle) {
  1455. this.currentSubtitle = this.originalQuestionSubtitle;
  1456. }
  1457. } else {
  1458. console.log('已达到最大重试次数,继续下一题');
  1459. // 重置重试次数
  1460. this.retryCount = 0;
  1461. // 继续下一个问题
  1462. this.proceedToNextQuestion();
  1463. }
  1464. // 重置标记
  1465. this.isPlayingLowScoreVideo = false;
  1466. this.originalQuestionSubtitle = null;
  1467. return;
  1468. }
  1469. // 检查是否是追问问题视频
  1470. if (this.isFollowUpQuestion) {
  1471. console.log('追问问题视频播放完成');
  1472. // 不重置追问状态,保持显示
  1473. // this.isFollowUpQuestion = false;
  1474. // this.currentFollowUpQuestion = null;
  1475. // 显示"开始回答"按钮,准备回答追问
  1476. this.showStartRecordingButton = true;
  1477. this.startCountdown();
  1478. return;
  1479. }
  1480. // 恢复原有的视频结束处理逻辑
  1481. // 对于所有视频,都显示相应的按钮
  1482. if (this.currentVideoIndex >= 1) {
  1483. // 对于问题视频,显示"开始回答"按钮
  1484. this.showStartRecordingButton = true;
  1485. this.startCountdown();
  1486. } else {
  1487. // 对于介绍视频(第一个视频),显示"开始面试"按钮
  1488. this.showAnswerButton = true;
  1489. }
  1490. // 重置追问状态
  1491. if (this.isFollowUpQuestion) {
  1492. this.isFollowUpQuestion = false;
  1493. this.currentFollowUpQuestion = null;
  1494. }
  1495. },
  1496. // 添加新方法:开始倒计时
  1497. startCountdown() {
  1498. // 先清除可能存在的倒计时
  1499. this.clearCountdown();
  1500. // 显示倒计时蒙层
  1501. this.showCountdown = true;
  1502. this.countdownValue = 10;
  1503. // 开始倒计时
  1504. this.countdownTimer = setInterval(() => {
  1505. if (!this.showStartRecordingButton) {
  1506. // 如果开始回答按钮已经被点击,清除倒计时
  1507. this.clearCountdown();
  1508. return;
  1509. }
  1510. this.countdownValue--;
  1511. if (this.countdownValue <= 0) {
  1512. // 倒计时结束,清除定时器
  1513. this.clearCountdown();
  1514. // 自动开始录制
  1515. this.showStartRecordingButton = false;
  1516. this.startRecordingAnswer();
  1517. }
  1518. }, 1000);
  1519. },
  1520. // 添加新方法:清除倒计时
  1521. clearCountdown() {
  1522. if (this.countdownTimer) {
  1523. clearInterval(this.countdownTimer);
  1524. this.countdownTimer = null;
  1525. }
  1526. this.showCountdown = false;
  1527. },
  1528. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  1529. handleStartRecordingClick() {
  1530. // 隐藏"开始回答"按钮
  1531. this.showStartRecordingButton = false;
  1532. // 如果倒计时正在进行,清除倒计时
  1533. this.clearCountdown();
  1534. // 重置等待回答状态
  1535. this.isWaitingForAnswer = false;
  1536. // 直接开始录制
  1537. this.startRecordingAnswer();
  1538. },
  1539. // 修改 stopRecordingAnswer 方法,添加录制时长检查
  1540. stopRecordingAnswer() {
  1541. // 如果倒计时正在进行,先清除倒计时
  1542. this.clearCountdown();
  1543. // 重置等待回答状态
  1544. this.isWaitingForAnswer = false;
  1545. // 检查录制时长
  1546. const recordingDuration = this.getRecordingDuration();
  1547. const minimumDuration = 3; // 最小录制时长(秒),改为3秒
  1548. const lowScoreDuration = 7; // 低分阈值时长(秒),少于7秒视为低分
  1549. // 保存录制时长,用于上传
  1550. this.currentRecordingDuration = recordingDuration;
  1551. if (recordingDuration < minimumDuration) {
  1552. // 录制时间过短,显示提示
  1553. uni.showToast({
  1554. title: '录制时间过短,请至少录制3秒',
  1555. icon: 'none',
  1556. duration: 2000
  1557. });
  1558. // 不执行停止录制逻辑,继续录制
  1559. return;
  1560. }
  1561. // 检查是否需要播放低分视频(录制时间少于7秒)
  1562. if (recordingDuration < lowScoreDuration) {
  1563. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  1564. // 停止录制但不上传
  1565. this.completeRecordingStop(false);
  1566. // 直接播放低分视频
  1567. this.playLowScoreVideo();
  1568. } else {
  1569. // 录制时长足够,正常停止录制并上传
  1570. this.completeRecordingStop(true);
  1571. this.needPlayLowScoreVideo = false;
  1572. this.recordingTimeDisplay='00:00'
  1573. }
  1574. },
  1575. // 添加新方法:开始录制用户回答
  1576. startRecordingAnswer() {
  1577. console.log('开始录制用户回答');
  1578. this.isRecording = true;
  1579. // 录制开始:重置帧抓取节流时间
  1580. this.lastFrameCaptureAt = 0;
  1581. // 录制开始:重置iOS检测时间
  1582. this.lastIOSDetectionTime = 0;
  1583. // 录制开始:若人脸检测socket已就绪,立即启动帧抓取监听
  1584. try {
  1585. if (this.personDetectionSocket && this.personDetectionSocket.readyState === 1) {
  1586. // 检查是否为iOS平台
  1587. const systemInfo = uni.getSystemInfoSync();
  1588. const isIOS = systemInfo.platform === 'ios';
  1589. if (isIOS) {
  1590. console.log('iOS录制开始,使用特殊检测逻辑');
  1591. // iOS端不立即启动帧抓取,等待定时器触发
  1592. } else {
  1593. if (!this.hiddenCanvasReady) { this.initHiddenCanvasNode(false, 3); }
  1594. this.startFrameStreamCapture();
  1595. }
  1596. }
  1597. } catch (e) {
  1598. console.warn('录制开始时的检测初始化失败:', e);
  1599. }
  1600. // 记录录制开始时间
  1601. this.recordingStartTime = Date.now();
  1602. this.recordingTimerCount = 0;
  1603. // 获取当前问题的推荐录制时长
  1604. this.maxRecordingTime = this.getCurrentQuestionRecommendedDuration();
  1605. this.remainingTime = this.maxRecordingTime;
  1606. // 启动计时器,每秒更新计时
  1607. this.recordingTimer = setInterval(() => {
  1608. this.recordingTimerCount++;
  1609. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  1610. // 更新UI显示录制时长和剩余时间
  1611. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount)/* +
  1612. ' / ' + this.formatTime(this.maxRecordingTime) */;
  1613. // 更新进度百分比 - 确保值在0-100之间
  1614. this.progressPercent = Math.min((this.recordingTimerCount / this.maxRecordingTime) * 100, 100);
  1615. // 检查是否达到最大录制时间
  1616. if (this.recordingTimerCount >= this.maxRecordingTime) {
  1617. console.log(`已达到最大录制时间(${this.maxRecordingTime}秒),自动停止录制`);
  1618. this.stopRecordingAnswer();
  1619. }
  1620. }, 1000);
  1621. // 显示录制中的提示
  1622. // uni.showLoading({
  1623. // title: '正在录制...',
  1624. // mask: true
  1625. // });
  1626. // 根据平台选择不同的录制方法
  1627. const systemInfo = uni.getSystemInfoSync();
  1628. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1629. if (isMiniProgram) {
  1630. // 小程序环境使用小程序的录制API
  1631. this.startMiniProgramRecording();
  1632. } else {
  1633. // H5/App环境使用MediaRecorder API
  1634. this.startBrowserRecording();
  1635. }
  1636. // 显示停止录制按钮
  1637. this.showStopRecordingButton = true;
  1638. },
  1639. // 添加一个新方法:重置相机组件
  1640. resetCamera() {
  1641. console.log('重置相机组件');
  1642. // 先完全移除相机组件
  1643. this.useMiniProgramCameraComponent = false;
  1644. // 释放相机上下文
  1645. if (this.cameraContext) {
  1646. this.cameraContext = null;
  1647. }
  1648. // 延迟后重新创建相机组件
  1649. setTimeout(() => {
  1650. this.useMiniProgramCameraComponent = true;
  1651. // 再次延迟创建相机上下文,确保组件已完全渲染
  1652. setTimeout(() => {
  1653. this.cameraContext = uni.createCameraContext();
  1654. console.log('相机组件已重置');
  1655. }, 500);
  1656. }, 500);
  1657. },
  1658. // 修改 startMiniProgramRecording 方法
  1659. startMiniProgramRecording() {
  1660. console.log('开始小程序录制方法');
  1661. // 获取平台信息
  1662. const systemInfo = uni.getSystemInfoSync();
  1663. const isIOS = systemInfo.platform === 'ios';
  1664. // 在iOS上,先重置相机
  1665. if (isIOS) {
  1666. // 先重置相机组件
  1667. this.resetCamera();
  1668. // 延迟执行录制,给相机组件足够的初始化时间
  1669. setTimeout(() => {
  1670. this.actualStartRecording(isIOS);
  1671. }, 1000);
  1672. } else {
  1673. // Android直接开始
  1674. this.actualStartRecording(isIOS);
  1675. }
  1676. },
  1677. // 添加新方法:实际开始录制
  1678. actualStartRecording(isIOS) {
  1679. if (!this.cameraContext) {
  1680. this.cameraContext = uni.createCameraContext();
  1681. console.log('创建新的相机上下文');
  1682. }
  1683. // 确保有录音权限
  1684. uni.getSetting({
  1685. success: (res) => {
  1686. const hasRecordAuth = res.authSetting['scope.record'];
  1687. const hasCameraAuth = res.authSetting['scope.camera'];
  1688. if (!hasRecordAuth || !hasCameraAuth) {
  1689. console.warn('缺少必要权限,请求权限');
  1690. this.requestMiniProgramPermissions();
  1691. return;
  1692. }
  1693. // 在iOS上,先检查相机状态
  1694. if (isIOS) {
  1695. console.log('iOS: 检查相机状态');
  1696. // 使用最简单的选项,设置最大录制时间为当前问题的推荐时长
  1697. const maxDuration = this.getCurrentQuestionRecommendedDuration() * 1000; // 转换为毫秒
  1698. const options = {
  1699. timeout: maxDuration, // 使用当前问题的推荐时长
  1700. quality: 'low', // 降低质量
  1701. compressed: true,
  1702. success: () => {
  1703. console.log('iOS录制开始成功');
  1704. },
  1705. fail: (err) => {
  1706. console.error('iOS录制失败:', err);
  1707. // 如果失败,尝试使用不同的方法
  1708. this.useAlternativeRecordingMethod();
  1709. }
  1710. };
  1711. try {
  1712. console.log('尝试开始录制');
  1713. this.recorder = this.cameraContext.startRecord(options);
  1714. } catch (e) {
  1715. console.error('开始录制异常:', e);
  1716. this.useAlternativeRecordingMethod();
  1717. }
  1718. } else {
  1719. // Android使用标准选项,设置最大录制时间为当前问题的推荐时长
  1720. const maxDuration = this.getCurrentQuestionRecommendedDuration() * 1000; // 转换为毫秒
  1721. const options = {
  1722. timeout: maxDuration, // 使用当前问题的推荐时长
  1723. quality: 'medium',
  1724. compressed: true,
  1725. success: () => {
  1726. console.log('Android录制开始成功');
  1727. },
  1728. fail: (err) => {
  1729. console.error('Android录制失败:', err);
  1730. uni.showToast({
  1731. title: '录制失败,请检查相机权限',
  1732. icon: 'none'
  1733. });
  1734. this.proceedToNextQuestion();
  1735. }
  1736. };
  1737. this.recorder = this.cameraContext.startRecord(options);
  1738. }
  1739. }
  1740. });
  1741. },
  1742. // 添加新方法:使用替代录制方法
  1743. useAlternativeRecordingMethod() {
  1744. console.log('使用替代录制方法');
  1745. // 在iOS上,可以尝试使用chooseVideo API作为备选
  1746. uni.showActionSheet({
  1747. itemList: ['使用相册中的视频', '跳过此问题'],
  1748. success: (res) => {
  1749. if (res.tapIndex === 0) {
  1750. // 选择相册中的视频
  1751. uni.chooseVideo({
  1752. sourceType: ['album'],
  1753. maxDuration: this.getCurrentQuestionRecommendedDuration(), // 使用当前问题的推荐时长
  1754. camera: 'front',
  1755. success: (res) => {
  1756. console.log('选择视频成功:', res.tempFilePath);
  1757. // 停止录制状态
  1758. this.isRecording = false;
  1759. this.showStopRecordingButton = false;
  1760. // 上传选择的视频
  1761. this.uploadRecordedVideo(res.tempFilePath);
  1762. },
  1763. fail: () => {
  1764. console.log('用户取消选择视频');
  1765. this.proceedToNextQuestion();
  1766. }
  1767. });
  1768. } else {
  1769. // 跳过此问题
  1770. console.log('用户选择跳过问题');
  1771. this.proceedToNextQuestion();
  1772. }
  1773. },
  1774. fail: () => {
  1775. console.log('操作取消');
  1776. this.proceedToNextQuestion();
  1777. }
  1778. });
  1779. },
  1780. // 添加新方法:请求小程序权限
  1781. requestMiniProgramPermissions() {
  1782. // 请求录音权限
  1783. uni.authorize({
  1784. scope: 'scope.record',
  1785. success: () => {
  1786. console.log('录音权限已获取');
  1787. // 请求相机权限
  1788. uni.authorize({
  1789. scope: 'scope.camera',
  1790. success: () => {
  1791. console.log('相机权限已获取');
  1792. // 权限都获取到后,开始录制
  1793. this.startMiniProgramRecording();
  1794. },
  1795. fail: (err) => {
  1796. console.error('相机权限获取失败:', err);
  1797. this.showPermissionDialog('相机');
  1798. }
  1799. });
  1800. },
  1801. fail: (err) => {
  1802. console.error('录音权限获取失败:', err);
  1803. this.showPermissionDialog('录音');
  1804. }
  1805. });
  1806. },
  1807. // 修改浏览器环境下的录制方法
  1808. startBrowserRecording() {
  1809. if (!this.cameraStream) {
  1810. console.error('没有可用的摄像头流');
  1811. /* uni.showToast({
  1812. title: '录制失败,摄像头未就绪',
  1813. icon: 'none'
  1814. }); */
  1815. this.proceedToNextQuestion();
  1816. return;
  1817. }
  1818. try {
  1819. // 检查流中是否包含音频轨道
  1820. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  1821. if (!hasAudio) {
  1822. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  1823. // 尝试重新获取带音频的媒体流
  1824. navigator.mediaDevices.getUserMedia({
  1825. audio: {
  1826. echoCancellation: true,
  1827. noiseSuppression: true,
  1828. autoGainControl: true
  1829. },
  1830. video: true
  1831. })
  1832. .then(newStream => {
  1833. // 检查新流是否包含音频轨道
  1834. const audioTracks = newStream.getAudioTracks();
  1835. if (audioTracks.length > 0) {
  1836. console.log('成功获取音频轨道:', audioTracks[0].label);
  1837. // 合并视频和音频轨道
  1838. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1839. const audioTrack = newStream.getAudioTracks()[0];
  1840. // 创建新的媒体流,包含视频和音频轨道
  1841. const combinedStream = new MediaStream();
  1842. if (videoTrack) combinedStream.addTrack(videoTrack);
  1843. if (audioTrack) combinedStream.addTrack(audioTrack);
  1844. this.cameraStream = combinedStream;
  1845. // 更新视频元素的源
  1846. const videoElement = this.$refs.userCameraVideo;
  1847. if (videoElement) {
  1848. videoElement.srcObject = combinedStream;
  1849. videoElement.muted = true; // 避免回声,但仍然录制声音
  1850. }
  1851. // 继续录制过程
  1852. this.setupMediaRecorder(combinedStream);
  1853. } else {
  1854. console.warn('仍然无法获取音频轨道');
  1855. // 使用现有流继续
  1856. this.setupMediaRecorder(this.cameraStream);
  1857. }
  1858. })
  1859. .catch(err => {
  1860. console.error('获取音频失败:', err);
  1861. // 使用现有流继续
  1862. this.setupMediaRecorder(this.cameraStream);
  1863. });
  1864. } else {
  1865. console.log('检测到音频轨道,直接使用');
  1866. // 已有音频轨道,直接使用
  1867. this.setupMediaRecorder(this.cameraStream);
  1868. }
  1869. } catch (error) {
  1870. console.error('浏览器录制失败:', error);
  1871. uni.showToast({
  1872. title: '录制失败,浏览器可能不支持此功能',
  1873. icon: 'none'
  1874. });
  1875. this.proceedToNextQuestion();
  1876. }
  1877. },
  1878. // 修改 setupMediaRecorder 方法
  1879. setupMediaRecorder(stream) {
  1880. // 检查流中的轨道
  1881. const videoTracks = stream.getVideoTracks();
  1882. const audioTracks = stream.getAudioTracks();
  1883. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  1884. // 尝试使用支持度更广的编码格式
  1885. let mimeType = '';
  1886. const supportedTypes = [
  1887. 'video/webm;codecs=vp9,opus',
  1888. 'video/webm;codecs=vp8,opus',
  1889. 'video/webm;codecs=h264,opus',
  1890. 'video/mp4;codecs=h264,aac',
  1891. 'video/webm',
  1892. 'video/mp4'
  1893. ];
  1894. for (const type of supportedTypes) {
  1895. if (MediaRecorder.isTypeSupported(type)) {
  1896. mimeType = type;
  1897. console.log('使用支持的MIME类型:', mimeType);
  1898. break;
  1899. }
  1900. }
  1901. // 创建MediaRecorder实例,设置较低的比特率以减小文件大小
  1902. const options = {
  1903. mimeType: mimeType || '',
  1904. audioBitsPerSecond: 64000, // 降低音频比特率
  1905. videoBitsPerSecond: 1000000, // 降低视频比特率到1Mbps
  1906. };
  1907. try {
  1908. this.mediaRecorder = new MediaRecorder(stream, options);
  1909. console.log('MediaRecorder创建成功,使用选项:', options);
  1910. } catch (e) {
  1911. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  1912. this.mediaRecorder = new MediaRecorder(stream);
  1913. }
  1914. // 存储录制的数据块
  1915. this.recordedChunks = [];
  1916. // 监听数据可用事件
  1917. this.mediaRecorder.ondataavailable = (event) => {
  1918. if (event.data && event.data.size > 0) {
  1919. this.recordedChunks.push(event.data);
  1920. console.log(`收到数据块: ${event.data.size} 字节`);
  1921. }
  1922. };
  1923. // 监听录制停止事件
  1924. this.mediaRecorder.onstop = async () => {
  1925. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  1926. if (this.recordedChunks.length === 0) {
  1927. console.error('没有录制到数据');
  1928. /* uni.showToast({
  1929. title: '录制失败,未捕获到数据',
  1930. icon: 'none'
  1931. }); */
  1932. this.proceedToNextQuestion();
  1933. return;
  1934. }
  1935. // 创建Blob对象
  1936. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  1937. const blob = new Blob(this.recordedChunks, { type: mimeType });
  1938. console.log('创建Blob,原始大小:', blob.size, '类型:', mimeType);
  1939. // 显示压缩中提示
  1940. uni.showLoading({
  1941. title: '正在处理视频...',
  1942. mask: true
  1943. });
  1944. try {
  1945. // 压缩视频
  1946. const compressedBlob = await this.compressVideo(blob);
  1947. // 将Blob转换为File对象
  1948. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1949. const file = new File([compressedBlob], fileName, { type: mimeType });
  1950. // 隐藏压缩提示
  1951. uni.hideLoading();
  1952. // 上传文件
  1953. this.uploadRecordedVideo(file);
  1954. } catch (error) {
  1955. console.error('视频处理失败:', error);
  1956. uni.hideLoading();
  1957. // 如果压缩失败,使用原始视频
  1958. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1959. const file = new File([blob], fileName, { type: mimeType });
  1960. this.uploadRecordedVideo(file);
  1961. }
  1962. };
  1963. // 监听错误
  1964. this.mediaRecorder.onerror = (event) => {
  1965. console.error('MediaRecorder错误:', event.error);
  1966. };
  1967. // 开始录制
  1968. try {
  1969. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  1970. console.log('MediaRecorder开始录制');
  1971. // 设置最大录制时间为60秒
  1972. // 注意:我们已经在startRecordingAnswer方法中设置了全局计时器
  1973. // 这里作为备份机制,确保即使全局计时器失效,也能停止录制
  1974. this.mediaRecorderTimeout = setTimeout(() => {
  1975. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  1976. console.log('MediaRecorder备份超时机制触发,停止录制');
  1977. this.mediaRecorder.stop();
  1978. }
  1979. }, 300000); // 300秒 (5分钟)
  1980. } catch (e) {
  1981. console.error('开始录制失败:', e);
  1982. }
  1983. },
  1984. // 添加新方法:获取录制时长
  1985. getRecordingDuration() {
  1986. // 如果有明确的录制开始时间,计算实际录制时长
  1987. if (this.recordingStartTime) {
  1988. return (Date.now() - this.recordingStartTime) / 1000;
  1989. }
  1990. // 如果没有明确记录开始时间,尝试从视频元素获取
  1991. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1992. return this.$refs.userCameraVideo.currentTime || 0;
  1993. }
  1994. // 如果无法获取准确时长,返回估计值(如果有计时器)
  1995. if (this.recordingTimerCount) {
  1996. return this.recordingTimerCount;
  1997. }
  1998. // 默认返回0
  1999. return 0;
  2000. },
  2001. // 添加新方法:重置录制
  2002. resetRecording() {
  2003. // 保持录制状态,但重置计时器
  2004. if (this.recordingTimer) {
  2005. clearTimeout(this.recordingTimer);
  2006. }
  2007. // 重置录制开始时间
  2008. this.recordingStartTime = Date.now();
  2009. this.recordingTimerCount = 0;
  2010. // 如果是浏览器环境,需要重置MediaRecorder
  2011. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  2012. // 停止当前录制
  2013. this.mediaRecorder.stop();
  2014. // 清空已录制的数据块
  2015. this.recordedChunks = [];
  2016. // 短暂延迟后重新开始录制
  2017. setTimeout(() => {
  2018. this.startBrowserRecording();
  2019. }, 500);
  2020. }
  2021. // 如果是小程序环境,需要重置相机录制
  2022. else if (this.cameraContext) {
  2023. // 停止当前录制
  2024. this.cameraContext.stopRecord({
  2025. success: () => {
  2026. console.log('相机录制已停止');
  2027. },
  2028. fail: (err) => {
  2029. console.error('停止相机录制失败:', err);
  2030. }
  2031. });
  2032. }
  2033. // 显示提示
  2034. /* uni.showToast({
  2035. title: '请重新开始回答',
  2036. icon: 'none',
  2037. duration: 2000
  2038. }); */
  2039. },
  2040. // 修改 completeRecordingStop 方法,确保正确处理追问问题ID
  2041. completeRecordingStop(uploadVideo = true) {
  2042. console.log('完成录制停止');
  2043. // 停止录制计时器
  2044. if (this.recordingTimer) {
  2045. clearInterval(this.recordingTimer);
  2046. this.recordingTimer = null;
  2047. }
  2048. // 记录最终录制时长
  2049. this.finalRecordingDuration = this.recordingTimerCount;
  2050. // 隐藏停止录制按钮
  2051. this.showStopRecordingButton = false;
  2052. // 隐藏录制指示器
  2053. this.isRecording = false;
  2054. // 停止帧监听(如有)
  2055. this.stopFrameStreamCapture();
  2056. // 重置iOS相关状态
  2057. this.isIOSPhotoCapturing = false;
  2058. this.lastIOSDetectionTime = 0;
  2059. // 根据平台选择不同的停止录制方法
  2060. const systemInfo = uni.getSystemInfoSync();
  2061. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2062. if (isMiniProgram) {
  2063. // 小程序环境停止录制
  2064. if (uploadVideo) {
  2065. this.stopMiniProgramRecording();
  2066. } else {
  2067. // 如果不需要上传,直接停止录制
  2068. if (this.cameraContext) {
  2069. this.cameraContext.stopRecord({
  2070. success: () => {
  2071. console.log('相机录制已停止,不上传视频');
  2072. },
  2073. fail: (err) => {
  2074. console.error('停止相机录制失败:', err);
  2075. }
  2076. });
  2077. }
  2078. }
  2079. } else {
  2080. // H5/App环境停止录制
  2081. if (uploadVideo) {
  2082. this.stopBrowserRecording();
  2083. } else {
  2084. // 如果不需要上传,直接停止录制
  2085. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  2086. this.mediaRecorder.stop();
  2087. this.recordedChunks = []; // 清空已录制的数据
  2088. }
  2089. }
  2090. }
  2091. // 记录当前问题ID和类型,确保追问问题使用正确的ID
  2092. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  2093. console.log('当前是追问问题,记录追问问题ID:', this.currentFollowUpQuestion.id);
  2094. this.lastQuestionWasFollowUp = true;
  2095. this.lastFollowUpQuestionId = this.currentFollowUpQuestion.id;
  2096. } else {
  2097. this.lastQuestionWasFollowUp = false;
  2098. this.lastFollowUpQuestionId = null;
  2099. }
  2100. },
  2101. // 修改 stopMiniProgramRecording 方法,确保正确传递追问问题ID
  2102. stopMiniProgramRecording() {
  2103. console.log('停止小程序录制');
  2104. // 检查是否有录制上下文
  2105. if (!this.cameraContext) {
  2106. console.error('相机上下文不存在');
  2107. this.proceedToNextQuestion();
  2108. return;
  2109. }
  2110. // 设置超时保护机制
  2111. let isCompleted = false;
  2112. const timeoutDuration = 10000; // 10秒超时
  2113. const timeoutId = setTimeout(() => {
  2114. if (!isCompleted) {
  2115. console.warn('停止录制超时,强制继续流程');
  2116. isCompleted = true;
  2117. this.proceedToNextQuestion();
  2118. }
  2119. }, timeoutDuration);
  2120. // 停止录制
  2121. this.cameraContext.stopRecord({
  2122. success: async (res) => {
  2123. if (isCompleted) {
  2124. console.log('录制停止回调已超时,忽略结果');
  2125. return;
  2126. }
  2127. isCompleted = true;
  2128. clearTimeout(timeoutId);
  2129. console.log('小程序录制停止成功:', res);
  2130. // 获取录制的视频文件路径
  2131. const videoPath = res.tempVideoPath;
  2132. console.log('小程序录制停止成功:', videoPath);
  2133. // 上传视频文件
  2134. if (videoPath) {
  2135. try {
  2136. // 等待上传完成后再继续
  2137. await this.uploadRecordedVideo(videoPath);
  2138. } catch (uploadError) {
  2139. console.error('上传视频失败:', uploadError);
  2140. // 上传失败时继续流程,避免卡住
  2141. this.proceedToNextQuestion();
  2142. }
  2143. } else {
  2144. console.error('未获取到录制视频路径');
  2145. this.proceedToNextQuestion();
  2146. }
  2147. },
  2148. fail: (err) => {
  2149. if (isCompleted) {
  2150. console.log('录制停止失败回调已超时,忽略错误');
  2151. return;
  2152. }
  2153. isCompleted = true;
  2154. clearTimeout(timeoutId);
  2155. console.error('小程序录制停止失败:', err);
  2156. this.proceedToNextQuestion();
  2157. }
  2158. });
  2159. },
  2160. // 添加新方法:停止浏览器录制
  2161. stopBrowserRecording() {
  2162. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  2163. this.mediaRecorder.stop();
  2164. console.log('浏览器录制停止成功');
  2165. } else {
  2166. console.error('MediaRecorder不存在或已经停止');
  2167. this.proceedToNextQuestion();
  2168. }
  2169. },
  2170. // 修改 uploadRecordedVideo 方法,添加记录父问题ID的逻辑
  2171. uploadRecordedVideo(fileOrPath) {
  2172. console.log('准备上传视频:', typeof fileOrPath === 'string' ? fileOrPath : fileOrPath.name);
  2173. console.log('当前问题ID:', this.currentParentQuestionId);
  2174. console.log('当前问题类型:', this.isFollowUpQuestion);
  2175. console.log('当前追问问题:', this.currentFollowUpQuestion);
  2176. // 获取当前问题ID和相关信息
  2177. let questionId;
  2178. let isFollowUpQuestionUpload = false;
  2179. let questionText = '';
  2180. let questionForm = 0;
  2181. // 检查当前是否是追问问题
  2182. if (this.currentFollowUpQuestion &&
  2183. (this.currentFollowUpQuestion.question_form === 5 || this.isFollowUpQuestion)) {
  2184. // 使用追问问题的信息
  2185. questionId = this.currentFollowUpQuestion.id;
  2186. isFollowUpQuestionUpload = true;
  2187. questionText = this.currentFollowUpQuestion.question || '';
  2188. questionForm = 5; // 追问问题类型
  2189. console.log('正在上传追问问题的回答:', {
  2190. questionId,
  2191. questionText,
  2192. questionForm,
  2193. isFollowUp: true
  2194. });
  2195. } else {
  2196. // 显示思考中loading
  2197. this.showThinkingLoading();
  2198. // 使用常规问题的信息
  2199. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  2200. if (currentQuestion && currentQuestion.id) {
  2201. questionId = currentQuestion.id;
  2202. questionText = currentQuestion.question || '';
  2203. console.log('正在上传常规问题的回答:', {
  2204. questionId,
  2205. questionText,
  2206. questionForm: 0
  2207. });
  2208. } else {
  2209. questionId = this.getDefaultQuestionId(this.currentVideoIndex);
  2210. console.log('使用默认问题ID:', questionId);
  2211. }
  2212. }
  2213. // 创建上传任务对象
  2214. const uploadTask = {
  2215. id: Date.now().toString(),
  2216. file: fileOrPath,
  2217. questionId: questionId,
  2218. isFollowUp: isFollowUpQuestionUpload,
  2219. questionText: questionText,
  2220. questionForm: questionForm,
  2221. attempts: 0,
  2222. maxAttempts: 3,
  2223. parentQuestionId: this.currentParentQuestionId,
  2224. videoDuration: this.currentRecordingDuration || 0 // 添加视频时长
  2225. };
  2226. // 如果不是追问问题,记录当前问题ID作为父问题ID
  2227. if (!isFollowUpQuestionUpload) {
  2228. this.currentParentQuestionId = questionId;
  2229. }
  2230. // 添加到上传队列
  2231. this.uploadQueue.push(uploadTask);
  2232. // 初始化上传进度和状态
  2233. this.uploadProgress[uploadTask.id] = 0;
  2234. this.uploadStatus[uploadTask.id] = 'pending';
  2235. // 显示上传状态提示
  2236. /* uni.showToast({
  2237. title: '正在上传回答...',
  2238. icon: 'loading',
  2239. duration: 1500
  2240. }); */
  2241. // 更新上传状态文本
  2242. this.updateUploadStatusText();
  2243. // 如果是追问问题,使用后台上传,不阻塞流程
  2244. if (isFollowUpQuestionUpload) {
  2245. console.log('追问问题使用后台上传,不阻塞流程');
  2246. // 立即执行后续操作,不等待上传完成
  2247. this.handlePostUploadActions(uploadTask);
  2248. // 启动后台上传
  2249. this.startBackgroundUpload(uploadTask);
  2250. // 返回立即解决的Promise
  2251. return Promise.resolve();
  2252. } else {
  2253. // 常规问题保持原有逻辑,等待上传完成,但添加超时保护
  2254. return new Promise((resolve, reject) => {
  2255. let isCompleted = false;
  2256. const uploadTimeoutDuration = 30000; // 30秒上传超时
  2257. // 设置上传超时保护
  2258. const uploadTimeoutId = setTimeout(() => {
  2259. if (!isCompleted) {
  2260. console.warn('上传超时,强制继续流程');
  2261. isCompleted = true;
  2262. this.hideThinkingLoading(); // 隐藏思考中loading
  2263. resolve(); // 即使超时也继续流程
  2264. }
  2265. }, uploadTimeoutDuration);
  2266. const checkUploadStatus = () => {
  2267. if (isCompleted) {
  2268. return; // 已超时,停止检查
  2269. }
  2270. if (!this.isUploading && this.uploadQueue.length === 0) {
  2271. // 上传完成后执行后续操作
  2272. isCompleted = true;
  2273. clearTimeout(uploadTimeoutId);
  2274. this.handlePostUploadActions(uploadTask);
  2275. resolve();
  2276. } else {
  2277. // 如果还在上传,继续检查
  2278. setTimeout(checkUploadStatus, 100);
  2279. }
  2280. };
  2281. // 如果当前没有上传任务在进行,开始处理队列
  2282. if (!this.isUploading) {
  2283. this.processUploadQueue();
  2284. }
  2285. // 开始检查上传状态
  2286. checkUploadStatus();
  2287. });
  2288. }
  2289. },
  2290. // 添加新方法:启动后台上传
  2291. startBackgroundUpload(task) {
  2292. console.log('启动后台上传任务:', task.id);
  2293. // 标记为后台上传任务
  2294. task.isBackgroundUpload = true;
  2295. // 从主上传队列中移除,避免与常规上传冲突
  2296. const taskIndex = this.uploadQueue.findIndex(t => t.id === task.id);
  2297. if (taskIndex !== -1) {
  2298. this.uploadQueue.splice(taskIndex, 1);
  2299. }
  2300. // 添加到后台上传队列
  2301. if (!this.backgroundUploadQueue) {
  2302. this.backgroundUploadQueue = [];
  2303. }
  2304. this.backgroundUploadQueue.push(task);
  2305. // 启动后台上传处理
  2306. this.processBackgroundUploadQueue();
  2307. },
  2308. // 添加新方法:处理后台上传队列
  2309. processBackgroundUploadQueue() {
  2310. // 如果后台上传队列为空,结束处理
  2311. if (!this.backgroundUploadQueue || this.backgroundUploadQueue.length === 0) {
  2312. return;
  2313. }
  2314. // 获取队列中的第一个任务
  2315. const task = this.backgroundUploadQueue[0];
  2316. console.log('开始后台上传:', task.id);
  2317. // 更新任务状态
  2318. this.uploadStatus[task.id] = 'uploading';
  2319. this.updateUploadStatusText();
  2320. // 增加尝试次数
  2321. task.attempts++;
  2322. // 根据文件类型选择上传方法
  2323. if (typeof task.file === 'string') {
  2324. // 小程序环境,使用uni.uploadFile上传
  2325. this.uploadFileWithUniBackground(task);
  2326. } else {
  2327. // 浏览器环境,使用XMLHttpRequest上传
  2328. this.uploadFileWithXHRBackground(task);
  2329. }
  2330. },
  2331. // 添加新方法:使用XMLHttpRequest后台上传文件
  2332. uploadFileWithXHRBackground(task) {
  2333. // 获取用户信息
  2334. const userInfo = uni.getStorageSync('userInfo');
  2335. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  2336. const tenant_id = uni.getStorageSync('tenant_id') ||JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1';
  2337. // 创建FormData
  2338. const formData = new FormData();
  2339. formData.append('file', task.file);
  2340. formData.append('openid', openid);
  2341. formData.append('tenant_id', tenant_id);
  2342. formData.append('application_id', uni.getStorageSync('appId'));
  2343. formData.append('question_id', task.questionId);
  2344. formData.append('video_duration', task.videoDuration || 0);
  2345. formData.append('has_audio', 'true');
  2346. // 创建XMLHttpRequest
  2347. const xhr = new XMLHttpRequest();
  2348. // 监听上传进度
  2349. xhr.upload.onprogress = (event) => {
  2350. if (event.lengthComputable) {
  2351. const progress = Math.round((event.loaded / event.total) * 100);
  2352. this.uploadProgress[task.id] = progress;
  2353. this.updateUploadStatusText();
  2354. }
  2355. };
  2356. // 监听请求完成
  2357. xhr.onload = () => {
  2358. if (xhr.status === 200) {
  2359. try {
  2360. const res = JSON.parse(xhr.responseText);
  2361. console.log('后台上传响应:', res);
  2362. if (res.code === 2000) {
  2363. // 获取上传后的视频URL
  2364. const videoUrl = res.data.url || res.data.photoUrl || '';
  2365. if (videoUrl) {
  2366. // 上传成功,更新状态
  2367. this.uploadStatus[task.id] = 'success';
  2368. this.updateUploadStatusText();
  2369. // 提交到面试接口
  2370. this.submitVideoToInterviewBackground(videoUrl, task);
  2371. } else {
  2372. this.handleBackgroundUploadFailure(task, '视频URL获取失败');
  2373. }
  2374. } else {
  2375. this.handleBackgroundUploadFailure(task, res.msg || '上传失败');
  2376. }
  2377. } catch (e) {
  2378. this.handleBackgroundUploadFailure(task, '解析响应失败');
  2379. }
  2380. } else {
  2381. this.handleBackgroundUploadFailure(task, 'HTTP状态: ' + xhr.status);
  2382. }
  2383. };
  2384. // 监听错误
  2385. xhr.onerror = () => {
  2386. this.handleBackgroundUploadFailure(task, '网络错误');
  2387. };
  2388. // 监听超时
  2389. xhr.ontimeout = () => {
  2390. this.handleBackgroundUploadFailure(task, '上传超时');
  2391. };
  2392. // 发送请求
  2393. xhr.open('POST', `${apiBaseUrl}/api/upload/`);
  2394. xhr.send(formData);
  2395. },
  2396. // 添加新方法:使用uni.uploadFile后台上传文件
  2397. uploadFileWithUniBackground(task) {
  2398. // 获取用户信息
  2399. const userInfo = uni.getStorageSync('userInfo');
  2400. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  2401. const tenant_id = uni.getStorageSync('tenant_id') ||JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1';
  2402. // 创建上传任务
  2403. const uploadTask = uni.uploadFile({
  2404. url: `${apiBaseUrl}/api/upload/`,
  2405. filePath: task.file,
  2406. name: 'file',
  2407. formData: {
  2408. openid: openid,
  2409. tenant_id: tenant_id,
  2410. application_id: uni.getStorageSync('appId'),
  2411. question_id: task.questionId,
  2412. video_duration: task.videoDuration || 0,
  2413. has_audio: 'true'
  2414. },
  2415. success: (uploadRes) => {
  2416. try {
  2417. const res = JSON.parse(uploadRes.data);
  2418. console.log('后台上传响应:', res);
  2419. if (res.code === 2000) {
  2420. // 获取上传后的视频URL
  2421. const videoUrl = res.data.permanent_link || res.data.url || '';
  2422. if (videoUrl) {
  2423. // 上传成功,更新状态
  2424. this.uploadStatus[task.id] = 'success';
  2425. this.updateUploadStatusText();
  2426. // 提交到面试接口
  2427. this.submitVideoToInterviewBackground(videoUrl, task);
  2428. } else {
  2429. this.handleBackgroundUploadFailure(task, '视频URL获取失败');
  2430. }
  2431. } else {
  2432. this.handleBackgroundUploadFailure(task, res.msg || '上传失败');
  2433. }
  2434. } catch (e) {
  2435. this.handleBackgroundUploadFailure(task, '解析响应失败');
  2436. }
  2437. },
  2438. fail: (err) => {
  2439. this.handleBackgroundUploadFailure(task, err.errMsg || '上传失败');
  2440. }
  2441. });
  2442. // 监听上传进度
  2443. uploadTask.onProgressUpdate((res) => {
  2444. this.uploadProgress[task.id] = res.progress;
  2445. this.updateUploadStatusText();
  2446. });
  2447. },
  2448. // 添加新方法:处理后台上传失败
  2449. handleBackgroundUploadFailure(task, errorMsg) {
  2450. console.error('后台上传失败:', errorMsg);
  2451. // 更新任务状态
  2452. this.uploadStatus[task.id] = 'failed';
  2453. this.updateUploadStatusText();
  2454. // 检查是否还有重试机会
  2455. if (task.attempts < task.maxAttempts) {
  2456. console.log(`将在5秒后重试后台上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  2457. // 5秒后重试
  2458. setTimeout(() => {
  2459. // 重置进度
  2460. this.uploadProgress[task.id] = 0;
  2461. // 重新开始上传
  2462. if (typeof task.file !== 'string') {
  2463. this.uploadFileWithXHRBackground(task);
  2464. } else {
  2465. this.uploadFileWithUniBackground(task);
  2466. }
  2467. }, 5000);
  2468. } else {
  2469. // 超过最大重试次数,移除任务
  2470. console.log('超过最大重试次数,放弃后台上传');
  2471. // 从后台上传队列中移除当前任务
  2472. if (this.backgroundUploadQueue) {
  2473. const taskIndex = this.backgroundUploadQueue.findIndex(t => t.id === task.id);
  2474. if (taskIndex !== -1) {
  2475. this.backgroundUploadQueue.splice(taskIndex, 1);
  2476. }
  2477. }
  2478. // 继续处理队列中的下一个任务
  2479. this.processBackgroundUploadQueue();
  2480. }
  2481. },
  2482. // 添加新方法:后台上传提交到面试接口
  2483. submitVideoToInterviewBackground(videoUrl, task) {
  2484. // 准备请求参数
  2485. const followUpRequestData = {
  2486. application_id: uni.getStorageSync('appId'),
  2487. tenant_id: uni.getStorageSync('tenant_id') ||JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1',
  2488. video_url: videoUrl,
  2489. original_question_id: this.parentJobPositionQuestionId, // 使用保存的job_position_question_id
  2490. follow_up_question: task.questionText,
  2491. video_duration: task.videoDuration,
  2492. openid: JSON.parse(uni.getStorageSync('userInfo')).openid || ''
  2493. };
  2494. console.log('后台上传提交追问视频:', followUpRequestData);
  2495. uni.request({
  2496. url: `${apiBaseUrl}/voice_interview/upload_follow_up_video/`,
  2497. method: 'POST',
  2498. data: followUpRequestData,
  2499. header: {
  2500. 'content-type': 'application/x-www-form-urlencoded'
  2501. },
  2502. success: (res) => {
  2503. if (res.data.code === 200 || res.data.code === 2000) {
  2504. console.log('后台上传追问视频提交成功');
  2505. // 从后台上传队列中移除任务
  2506. if (this.backgroundUploadQueue) {
  2507. const taskIndex = this.backgroundUploadQueue.findIndex(t => t.id === task.id);
  2508. if (taskIndex !== -1) {
  2509. this.backgroundUploadQueue.splice(taskIndex, 1);
  2510. }
  2511. }
  2512. // 继续处理下一个后台上传任务
  2513. this.processBackgroundUploadQueue();
  2514. } else {
  2515. this.handleBackgroundSubmitFailure(task, '提交失败: ' + (res.data.msg || '未知错误'));
  2516. }
  2517. },
  2518. fail: (err) => {
  2519. console.error('后台上传提交失败:', err);
  2520. this.handleBackgroundSubmitFailure(task, '提交失败: ' + err.errMsg);
  2521. }
  2522. });
  2523. },
  2524. // 添加新方法:处理后台上传提交失败
  2525. handleBackgroundSubmitFailure(task, errorMsg) {
  2526. console.error('后台上传提交失败:', errorMsg);
  2527. // 检查是否还有重试机会
  2528. if (task.attempts < task.maxAttempts) {
  2529. console.log(`将在5秒后重试后台上传提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  2530. // 5秒后重试
  2531. setTimeout(() => {
  2532. this.submitVideoToInterviewBackground(task.videoUrl, task);
  2533. }, 5000);
  2534. } else {
  2535. // 超过最大重试次数,移除任务
  2536. console.log('超过最大重试次数,放弃后台上传提交');
  2537. // 从后台上传队列中移除当前任务
  2538. if (this.backgroundUploadQueue) {
  2539. const taskIndex = this.backgroundUploadQueue.findIndex(t => t.id === task.id);
  2540. if (taskIndex !== -1) {
  2541. this.backgroundUploadQueue.splice(taskIndex, 1);
  2542. }
  2543. }
  2544. // 继续处理队列中的下一个任务
  2545. this.processBackgroundUploadQueue();
  2546. }
  2547. },
  2548. // 添加新方法:处理上传后的逻辑
  2549. handlePostUploadActions(task) {
  2550. // 隐藏思考中loading
  2551. this.hideThinkingLoading();
  2552. // 检查是否需要播放低分视频
  2553. if (this.needPlayLowScoreVideo && this.retryCount < 1) {
  2554. this.playLowScoreVideo();
  2555. this.needPlayLowScoreVideo = false;
  2556. } else {
  2557. // 如果是追问问题的回答,检查是否继续追问
  2558. if (task.isFollowUp) {
  2559. console.log('追问问题回答完成,检查是否继续追问');
  2560. this.retryCount = 0; // 重置重试次数
  2561. this.isFollowUpQuestion = false; // 重置追问标记
  2562. this.currentFollowUpQuestion = null; // 清除当前追问问题信息
  2563. this.videoUrl=''
  2564. // 若未达到上限则继续请求下一条追问,否则进入下一题
  2565. this.maybeRequestNextFollowUp(this.parentJobPositionQuestionId);
  2566. } else {
  2567. // 如果是常规问题,检查追问问题
  2568. console.log('常规问题回答完成,准备检查追问,父问题ID:', this.parentJobPositionQuestionId);
  2569. // 注意:这里应该使用从后端返回的job_position_question_id,而不是前端的questionId
  2570. // parentJobPositionQuestionId应该在提交视频成功后就已经设置好了
  2571. if (this.parentJobPositionQuestionId) {
  2572. // 从题目读取追问上限(使用follow_up_count字段),否则使用默认
  2573. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  2574. if (currentQuestion && typeof currentQuestion.follow_up_count !== 'undefined') {
  2575. this.setFollowUpLimit(this.parentJobPositionQuestionId, currentQuestion.follow_up_count);
  2576. } else if (typeof this.defaultFollowUpLimit !== 'undefined') {
  2577. this.setFollowUpLimit(this.parentJobPositionQuestionId, this.defaultFollowUpLimit);
  2578. }
  2579. this.resetFollowUpAskedCount(this.parentJobPositionQuestionId);
  2580. this.checkAndPlayFollowUpQuestion(this.parentJobPositionQuestionId);
  2581. } else {
  2582. console.error('缺少parentJobPositionQuestionId,无法检查追问,直接进入下一题');
  2583. this.proceedToNextQuestion();
  2584. }
  2585. }
  2586. }
  2587. },
  2588. // 修改 processUploadQueue 方法
  2589. processUploadQueue() {
  2590. // 如果队列为空,结束处理
  2591. if (this.uploadQueue.length === 0) {
  2592. this.isUploading = false;
  2593. this.showUploadStatus = false;
  2594. this.hideThinkingLoading(); // 隐藏思考中loading
  2595. return;
  2596. }
  2597. // 标记为正在上传
  2598. this.isUploading = true;
  2599. this.showUploadStatus = true;
  2600. // 获取队列中的第一个任务
  2601. const task = this.uploadQueue[0];
  2602. // 更新任务状态
  2603. this.uploadStatus[task.id] = 'uploading';
  2604. this.updateUploadStatusText();
  2605. // 增加尝试次数
  2606. task.attempts++;
  2607. // 根据文件类型选择上传方法
  2608. if (typeof task.file === 'string') {
  2609. // 小程序环境,使用uni.uploadFile上传
  2610. this.uploadFileWithUni(task);
  2611. } else {
  2612. // 浏览器环境,使用XMLHttpRequest上传
  2613. this.uploadFileWithXHR(task);
  2614. }
  2615. },
  2616. // 添加新方法:使用XMLHttpRequest上传文件
  2617. uploadFileWithXHR(task) {
  2618. // 获取用户信息
  2619. const userInfo = uni.getStorageSync('userInfo');
  2620. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  2621. const tenant_id =uni.getStorageSync('tenant_id') || JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1';
  2622. // 创建FormData
  2623. const formData = new FormData();
  2624. formData.append('file', task.file);
  2625. formData.append('openid', openid);
  2626. formData.append('tenant_id', tenant_id);
  2627. formData.append('application_id', uni.getStorageSync('appId'));
  2628. formData.append('question_id', task.questionId);
  2629. formData.append('video_duration', task.videoDuration);
  2630. formData.append('has_audio', 'true');
  2631. // 创建XMLHttpRequest
  2632. const xhr = new XMLHttpRequest();
  2633. xhr.open('POST', `${apiBaseUrl}/api/upload/`, true);
  2634. xhr.timeout = 120000; // 2分钟超时
  2635. // 监听上传进度
  2636. xhr.upload.onprogress = (event) => {
  2637. if (event.lengthComputable) {
  2638. const percentComplete = Math.round((event.loaded / event.total) * 100);
  2639. this.uploadProgress[task.id] = percentComplete;
  2640. this.updateUploadStatusText();
  2641. }
  2642. };
  2643. // 监听请求完成
  2644. xhr.onload = () => {
  2645. if (xhr.status === 200) {
  2646. try {
  2647. const res = JSON.parse(xhr.responseText);
  2648. console.log('上传响应:', res);
  2649. if (res.code === 2000) {
  2650. // 获取上传后的视频URL
  2651. const videoUrl = res.data.url || res.data.photoUrl || '';
  2652. if (videoUrl) {
  2653. // 上传成功,更新状态
  2654. this.uploadStatus[task.id] = 'success';
  2655. this.updateUploadStatusText();
  2656. // 提交到面试接口
  2657. this.submitVideoToInterview(videoUrl, task);
  2658. } else {
  2659. this.handleUploadFailure(task, '视频URL获取失败');
  2660. }
  2661. } else {
  2662. this.handleUploadFailure(task, res.msg || '上传失败');
  2663. }
  2664. } catch (e) {
  2665. this.handleUploadFailure(task, '解析响应失败');
  2666. }
  2667. } else {
  2668. this.handleUploadFailure(task, 'HTTP状态: ' + xhr.status);
  2669. }
  2670. };
  2671. // 监听错误
  2672. xhr.onerror = () => {
  2673. this.handleUploadFailure(task, '网络错误');
  2674. };
  2675. // 监听超时
  2676. xhr.ontimeout = () => {
  2677. this.handleUploadFailure(task, '上传超时');
  2678. };
  2679. // 发送请求
  2680. xhr.send(formData);
  2681. },
  2682. // 添加新方法:使用uni.uploadFile上传文件
  2683. uploadFileWithUni(task) {
  2684. // 获取用户信息
  2685. const userInfo = uni.getStorageSync('userInfo');
  2686. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  2687. const tenant_id =uni.getStorageSync('tenant_id') || JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1';
  2688. // 创建上传任务
  2689. const uploadTask = uni.uploadFile({
  2690. url: `${apiBaseUrl}/api/upload/`,
  2691. filePath: task.file,
  2692. name: 'file',
  2693. formData: {
  2694. openid: openid,
  2695. tenant_id: tenant_id,
  2696. application_id: uni.getStorageSync('appId'),
  2697. question_id: task.questionId,
  2698. video_duration: 0,
  2699. has_audio: 'true'
  2700. },
  2701. success: (uploadRes) => {
  2702. try {
  2703. const res = JSON.parse(uploadRes.data);
  2704. console.log('上传响应:', res);
  2705. if (res.code === 2000) {
  2706. // 获取上传后的视频URL
  2707. const videoUrl = res.data.permanent_link || res.data.url || '';
  2708. if (videoUrl) {
  2709. // 上传成功,更新状态
  2710. this.uploadStatus[task.id] = 'success';
  2711. this.updateUploadStatusText();
  2712. // 提交到面试接口
  2713. this.submitVideoToInterview(videoUrl, task);
  2714. } else {
  2715. this.handleUploadFailure(task, '视频URL获取失败');
  2716. }
  2717. } else {
  2718. this.handleUploadFailure(task, res.msg || '上传失败');
  2719. }
  2720. } catch (e) {
  2721. this.handleUploadFailure(task, '解析响应失败');
  2722. }
  2723. },
  2724. fail: (err) => {
  2725. this.handleUploadFailure(task, err.errMsg || '上传失败');
  2726. }
  2727. });
  2728. // 监听上传进度
  2729. uploadTask.onProgressUpdate((res) => {
  2730. this.uploadProgress[task.id] = res.progress;
  2731. this.updateUploadStatusText();
  2732. });
  2733. },
  2734. // 添加新方法:处理上传失败
  2735. handleUploadFailure(task, errorMsg) {
  2736. console.error('上传失败:', errorMsg);
  2737. // 更新任务状态
  2738. this.uploadStatus[task.id] = 'failed';
  2739. this.updateUploadStatusText();
  2740. // 检查是否还有重试机会
  2741. if (task.attempts < task.maxAttempts) {
  2742. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  2743. // 5秒后重试
  2744. setTimeout(() => {
  2745. // 重置进度
  2746. this.uploadProgress[task.id] = 0;
  2747. // 重新开始上传
  2748. if (typeof task.file !== 'string') {
  2749. this.uploadFileWithXHR(task);
  2750. } else {
  2751. this.uploadFileWithUni(task);
  2752. }
  2753. }, 5000);
  2754. } else {
  2755. // 超过最大重试次数,移除任务并继续处理队列
  2756. console.log('超过最大重试次数,放弃上传');
  2757. // 显示错误提示
  2758. /* uni.showToast({
  2759. title: '视频上传失败,请稍后重试',
  2760. icon: 'none',
  2761. duration: 2000
  2762. }); */
  2763. // 从队列中移除当前任务
  2764. this.uploadQueue.shift();
  2765. // 继续处理队列中的下一个任务
  2766. this.processUploadQueue();
  2767. }
  2768. },
  2769. // 修改 submitVideoToInterview 方法
  2770. submitVideoToInterview(videoUrl, task) {
  2771. // 准备请求参数
  2772. const requestData = {
  2773. application_id: uni.getStorageSync('appId'),
  2774. question_id: task.questionId,
  2775. video_url: videoUrl,
  2776. tenant_id: uni.getStorageSync('tenant_id') ||JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1',
  2777. is_follow_up: task.isFollowUp ? 1 : 0,
  2778. question_form: task.questionForm,
  2779. question_text: task.questionText
  2780. };
  2781. console.log('提交视频到面试接口', requestData);
  2782. // 发送请求到面试接口
  2783. if (task.isFollowUp) {
  2784. // 追问视频上传
  2785. const followUpRequestData = {
  2786. application_id: uni.getStorageSync('appId'),
  2787. tenant_id:uni.getStorageSync('tenant_id') || JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1',
  2788. video_url: videoUrl,
  2789. original_question_id: this.parentJobPositionQuestionId, // 使用保存的job_position_question_id
  2790. follow_up_question: task.questionText,
  2791. video_duration: task.videoDuration,
  2792. openid: JSON.parse(uni.getStorageSync('userInfo')).openid || ''
  2793. };
  2794. console.log('准备提交追问视频:', followUpRequestData);
  2795. uni.request({
  2796. url: `${apiBaseUrl}/voice_interview/upload_follow_up_video/`,
  2797. method: 'POST',
  2798. data: followUpRequestData,
  2799. header: {
  2800. 'content-type': 'application/x-www-form-urlencoded'
  2801. },
  2802. success: async (res) => {
  2803. try {
  2804. if (res.data.code === 200 || res.data.code === 2000) {
  2805. if (res.data.data && res.data.data.transcription_status === 'pending') {
  2806. // 如果转写状态为pending,5秒后重试
  2807. console.log('视频转写进行中,5秒后重试');
  2808. setTimeout(() => {
  2809. this.checkTranscriptionStatus(task, followUpRequestData);
  2810. }, 5000);
  2811. return;
  2812. }
  2813. console.log('追问视频提交成功');
  2814. this.isFollowUpMode = false;
  2815. this.currentVideoIndex = this.mainQuestionIndex;
  2816. /* uni.showToast({
  2817. title: '回答已提交',
  2818. icon: 'success',
  2819. duration: 1500
  2820. }); */
  2821. this.uploadQueue.shift();
  2822. this.processUploadQueue();
  2823. this.$nextTick(() => {
  2824. if (this.currentVideoIndex < this.videoList.length) {
  2825. this.videoUrl = this.videoList[this.currentVideoIndex];
  2826. this.videoPlaying = true;
  2827. const videoContext = uni.createVideoContext('myVideo', this);
  2828. if (videoContext) {
  2829. videoContext.play();
  2830. }
  2831. }
  2832. });
  2833. } else {
  2834. this.handleSubmitFailure(task, '提交失败: ' + (res.data.msg || '未知错误'));
  2835. }
  2836. } catch (e) {
  2837. this.handleSubmitFailure(task, '解析响应失败: ' + e.message);
  2838. }
  2839. },
  2840. fail: (err) => {
  2841. console.error('提交视频失败:', err);
  2842. this.handleSubmitFailure(task, '提交失败: ' + err.errMsg);
  2843. }
  2844. });
  2845. } else {
  2846. // 原有的视频上传逻辑
  2847. uni.request({
  2848. url: `${apiBaseUrl}/api/job/upload_video`,
  2849. method: 'POST',
  2850. data: requestData,
  2851. header: {
  2852. 'content-type': 'application/x-www-form-urlencoded'
  2853. },
  2854. success: async (res) => {
  2855. if (res.data.code === 200 || res.data.code === 2000) {
  2856. console.log('视频提交成功,准备调用面试互动接口');
  2857. try {
  2858. // 保存job_position_question_id用于后续追问
  2859. this.parentJobPositionQuestionId = res.data.data.job_position_question_id;
  2860. console.log('保存父问题ID:', this.parentJobPositionQuestionId);
  2861. // 初始化该题的追问上限与已问次数
  2862. // 注意:这里使用服务器返回的follow_up_limit字段,如果服务器返回的是follow_up_count,需要相应修改
  2863. const limitFromServer = res.data.data.follow_up_limit;
  2864. if (typeof limitFromServer !== 'undefined') {
  2865. this.setFollowUpLimit(this.parentJobPositionQuestionId, limitFromServer);
  2866. } else {
  2867. this.setFollowUpLimit(this.parentJobPositionQuestionId, this.defaultFollowUpLimit);
  2868. }
  2869. this.resetFollowUpAskedCount(this.parentJobPositionQuestionId);
  2870. await this.callInterviewInteraction(res.data.data.job_position_question_id);
  2871. console.log('面试互动接口调用成功');
  2872. } catch (error) {
  2873. console.error('面试互动接口调用失败:', error);
  2874. this.proceedToNextQuestion();
  2875. }
  2876. /* uni.showToast({
  2877. title: '回答已提交',
  2878. icon: 'success',
  2879. duration: 1500
  2880. }); */
  2881. this.uploadQueue.shift();
  2882. this.processUploadQueue();
  2883. } else {
  2884. this.handleSubmitFailure(task, '提交失败: ' + (res.data.msg || '未知错误'));
  2885. }
  2886. },
  2887. fail: (err) => {
  2888. console.error('提交视频失败:', err);
  2889. this.handleSubmitFailure(task, '提交失败: ' + err.errMsg);
  2890. }
  2891. });
  2892. }
  2893. },
  2894. // 添加新方法:处理提交失败
  2895. handleSubmitFailure(task, errorMsg) {
  2896. console.error('提交失败:', errorMsg);
  2897. // 更新任务状态
  2898. this.uploadStatus[task.id] = 'failed';
  2899. this.updateUploadStatusText();
  2900. // 检查是否还有重试机会
  2901. if (task.attempts < task.maxAttempts) {
  2902. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  2903. // 5秒后重试
  2904. setTimeout(() => {
  2905. // 重新提交
  2906. this.submitVideoToInterview(task.videoUrl, task);
  2907. }, 5000);
  2908. } else {
  2909. // 超过最大重试次数,移除任务并继续处理队列
  2910. console.log('超过最大重试次数,放弃提交');
  2911. // 显示错误提示
  2912. /* uni.showToast({
  2913. title: '视频提交失败,请稍后重试',
  2914. icon: 'none',
  2915. duration: 2000
  2916. }); */
  2917. // 从队列中移除当前任务
  2918. this.uploadQueue.shift();
  2919. // 继续处理队列中的下一个任务
  2920. this.processUploadQueue();
  2921. }
  2922. },
  2923. // 添加检查转写状态的方法
  2924. checkTranscriptionStatus(task, requestData, retryCount = 0) {
  2925. const maxRetries = 24; // 最大重试次数(2分钟)
  2926. const retryInterval = 5000; // 重试间隔时间(5秒)
  2927. if (retryCount >= maxRetries) {
  2928. console.log('转写超时,继续下一个问题');
  2929. this.isFollowUpMode = false;
  2930. this.currentVideoIndex = this.mainQuestionIndex;
  2931. this.uploadQueue.shift();
  2932. this.processUploadQueue();
  2933. return;
  2934. }
  2935. uni.request({
  2936. url: `${apiBaseUrl}/voice_interview/check_transcription_status/`,
  2937. method: 'POST',
  2938. data: {
  2939. application_id: requestData.application_id,
  2940. original_question_id: requestData.original_question_id,
  2941. video_url: requestData.video_url
  2942. },
  2943. header: {
  2944. 'content-type': 'application/x-www-form-urlencoded'
  2945. },
  2946. success: (res) => {
  2947. if (res.data.code === 200 || res.data.code === 2000) {
  2948. if (res.data.data && res.data.data.status === 'completed') {
  2949. console.log('视频转写完成');
  2950. this.isFollowUpMode = false;
  2951. this.currentVideoIndex = this.mainQuestionIndex;
  2952. this.uploadQueue.shift();
  2953. this.processUploadQueue();
  2954. this.$nextTick(() => {
  2955. if (this.currentVideoIndex < this.videoList.length) {
  2956. this.videoUrl = this.videoList[this.currentVideoIndex];
  2957. this.videoPlaying = true;
  2958. const videoContext = uni.createVideoContext('myVideo', this);
  2959. if (videoContext) {
  2960. videoContext.play();
  2961. }
  2962. }
  2963. });
  2964. } else {
  2965. // 状态仍为pending,继续重试
  2966. console.log(`转写进行中,${retryInterval/1000}秒后重试 (${retryCount + 1}/${maxRetries})`);
  2967. setTimeout(() => {
  2968. this.checkTranscriptionStatus(task, requestData, retryCount + 1);
  2969. }, retryInterval);
  2970. }
  2971. } else if (res.data.message === "面试视频尚未完成转写" || res.data.success === false) {
  2972. // 特殊处理转写未完成的情况
  2973. console.log(`转写未完成,${retryInterval/1000}秒后重试 (${retryCount + 1}/${maxRetries})`);
  2974. setTimeout(() => {
  2975. this.checkTranscriptionStatus(task, requestData, retryCount + 1);
  2976. }, retryInterval);
  2977. } else {
  2978. this.handleSubmitFailure(task, '检查转写状态失败: ' + (res.data.msg || '未知错误'));
  2979. }
  2980. },
  2981. fail: (err) => {
  2982. console.error('检查转写状态失败:', err);
  2983. // 网络错误时也进行重试
  2984. console.log(`网络请求失败,${retryInterval/1000}秒后重试 (${retryCount + 1}/${maxRetries})`);
  2985. setTimeout(() => {
  2986. this.checkTranscriptionStatus(task, requestData, retryCount + 1);
  2987. }, retryInterval);
  2988. }
  2989. });
  2990. },
  2991. // 添加新方法:更新上传状态文本
  2992. updateUploadStatusText() {
  2993. // 检查主上传队列
  2994. let mainQueueText = '';
  2995. if (this.uploadQueue.length > 0) {
  2996. const currentTask = this.uploadQueue[0];
  2997. const progress = this.uploadProgress[currentTask.id] || 0;
  2998. const status = this.uploadStatus[currentTask.id] || 'pending';
  2999. let statusText = '';
  3000. switch (status) {
  3001. case 'pending':
  3002. statusText = '等待上传';
  3003. break;
  3004. case 'uploading':
  3005. statusText = `上传中 ${progress}%`;
  3006. break;
  3007. case 'success':
  3008. statusText = '上传成功,提交中...';
  3009. break;
  3010. case 'failed':
  3011. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? '即将重试' : '已放弃'}`;
  3012. break;
  3013. }
  3014. // 添加问题类型和文本的中文释义
  3015. const questionTypeText = currentTask.isFollowUp ? '追问' : '问题';
  3016. const questionShortText = currentTask.questionText ?
  3017. (currentTask.questionText.length > 10 ? currentTask.questionText.substring(0, 10) + '...' : currentTask.questionText) :
  3018. `${questionTypeText}${currentTask.questionId}`;
  3019. mainQueueText = `${questionTypeText}「${questionShortText}」:${statusText}`;
  3020. // 如果队列中有多个任务,显示总数
  3021. if (this.uploadQueue.length > 1) {
  3022. mainQueueText += ` (${this.uploadQueue.length}个视频待处理)`;
  3023. }
  3024. }
  3025. // 检查后台上传队列
  3026. let backgroundQueueText = '';
  3027. if (this.backgroundUploadQueue && this.backgroundUploadQueue.length > 0) {
  3028. const currentTask = this.backgroundUploadQueue[0];
  3029. const progress = this.uploadProgress[currentTask.id] || 0;
  3030. const status = this.uploadStatus[currentTask.id] || 'pending';
  3031. let statusText = '';
  3032. switch (status) {
  3033. case 'pending':
  3034. statusText = '等待后台上传';
  3035. break;
  3036. case 'uploading':
  3037. statusText = `后台上传中 ${progress}%`;
  3038. break;
  3039. case 'success':
  3040. statusText = '后台上传成功,提交中...';
  3041. break;
  3042. case 'failed':
  3043. statusText = `后台上传失败,${currentTask.attempts < currentTask.maxAttempts ? '即将重试' : '已放弃'}`;
  3044. break;
  3045. }
  3046. const questionShortText = currentTask.questionText ?
  3047. (currentTask.questionText.length > 10 ? currentTask.questionText.substring(0, 10) + '...' : currentTask.questionText) :
  3048. `追问${currentTask.questionId}`;
  3049. backgroundQueueText = `追问「${questionShortText}」:${statusText}`;
  3050. // 如果后台上传队列中有多个任务,显示总数
  3051. if (this.backgroundUploadQueue.length > 1) {
  3052. backgroundQueueText += ` (${this.backgroundUploadQueue.length}个追问待后台上传)`;
  3053. }
  3054. }
  3055. // 合并状态文本
  3056. if (mainQueueText && backgroundQueueText) {
  3057. this.uploadStatusText = `${mainQueueText} | ${backgroundQueueText}`;
  3058. } else if (mainQueueText) {
  3059. this.uploadStatusText = mainQueueText;
  3060. } else if (backgroundQueueText) {
  3061. this.uploadStatusText = backgroundQueueText;
  3062. } else {
  3063. this.uploadStatusText = '';
  3064. }
  3065. },
  3066. // 修改 proceedToNextQuestion 方法,确保在切换视频时重置历史时间
  3067. proceedToNextQuestion() {
  3068. // 如果正在切换视频,则返回
  3069. if (this.isVideoSwitching) {
  3070. console.log('正在切换视频中,请等待...');
  3071. return;
  3072. }
  3073. console.log('继续下一个问题');
  3074. this.isVideoSwitching = true; // 设置切换状态锁
  3075. // 停止当前视频播放
  3076. const currentVideo = uni.createVideoContext('myVideo', this);
  3077. if (currentVideo) {
  3078. currentVideo.stop();
  3079. this.videoPlaying = false;
  3080. }
  3081. // 检查是否已经是最后一个视频
  3082. if (this.currentVideoIndex + 1 >= this.videoList.length) {
  3083. // 清理资源
  3084. this.stopUserCamera();
  3085. this.clearCountdown();
  3086. // 获取当前职位ID
  3087. const currentJobDetail = JSON.parse(uni.getStorageSync('selectedJob'));
  3088. const jobId = currentJobDetail ? currentJobDetail.id : null;
  3089. this.handleVideoCompletion(jobId);
  3090. return;
  3091. }
  3092. // 增加当前视频索引,切换到下一个视频
  3093. this.currentVideoIndex++;
  3094. // 重置历史时间,避免触发异常跳转检测
  3095. this.historyTime = 0;
  3096. // 如果还有视频,播放下一个视频
  3097. if (this.currentVideoIndex < this.videoList.length) {
  3098. // 确保先更新视频URL
  3099. this.videoUrl = this.videoList[this.currentVideoIndex];
  3100. // 使用setTimeout确保DOM更新完成
  3101. this.$nextTick(() => {
  3102. this.videoPlaying = true;
  3103. const videoContext = uni.createVideoContext('myVideo', this);
  3104. if (videoContext) {
  3105. videoContext.play();
  3106. }
  3107. this.isVideoSwitching = false; // 重置切换状态锁
  3108. });
  3109. } else {
  3110. // 所有视频都播放完毕,显示完成页面或返回
  3111. console.log('所有视频已播放完毕');
  3112. // 清理资源
  3113. this.stopUserCamera();
  3114. this.clearCountdown();
  3115. // 获取当前职位ID
  3116. const currentJobDetail = JSON.parse(uni.getStorageSync('selectedJob'));
  3117. console.log('当前职位ID:', currentJobDetail);
  3118. const jobId = currentJobDetail ? currentJobDetail.id : null;
  3119. console.log('当前职位ID:', jobId);
  3120. // 延迟后根据职位ID跳转到不同页面
  3121. setTimeout(() => {
  3122. this.isVideoSwitching = false; // 重置切换状态锁
  3123. // 如果职位ID为9,跳转到interview-question页面
  3124. if (jobId === 9) {
  3125. uni.navigateTo({
  3126. url: '/pages/interview-question/interview-question',
  3127. success: () => {
  3128. console.log('成功跳转到interview-question页面');
  3129. },
  3130. fail: (err) => {
  3131. console.error('跳转到interview-question页面失败:', err);
  3132. // 如果跳转失败,尝试使用redirectTo
  3133. uni.redirectTo({
  3134. url: '/pages/interview-question/interview-question',
  3135. fail: (redirectErr) => {
  3136. console.error('重定向到interview-question页面也失败:', redirectErr);
  3137. // 如果所有跳转方式都失败,尝试返回上一页
  3138. uni.navigateBack({
  3139. delta: 1
  3140. });
  3141. }
  3142. });
  3143. }
  3144. });
  3145. } else {
  3146. // 其他职位ID跳转到camera页面
  3147. uni.navigateTo({
  3148. url: '/pages/camera/camera',
  3149. success: () => {
  3150. console.log('成功跳转到camera页面');
  3151. },
  3152. fail: (err) => {
  3153. console.error('跳转到camera页面失败:', err);
  3154. // 如果跳转失败,尝试使用redirectTo
  3155. uni.redirectTo({
  3156. url: '/pages/camera/camera',
  3157. fail: (redirectErr) => {
  3158. console.error('重定向到camera页面也失败:', redirectErr);
  3159. // 最后尝试使用switchTab(如果camera是tabBar页面)
  3160. uni.switchTab({
  3161. url: '/pages/camera/camera',
  3162. fail: (switchErr) => {
  3163. console.error('所有跳转方式都失败:', switchErr);
  3164. // 如果所有跳转方式都失败,尝试返回上一页
  3165. uni.navigateBack({
  3166. delta: 1
  3167. });
  3168. }
  3169. });
  3170. }
  3171. });
  3172. }
  3173. });
  3174. }
  3175. }, 0);
  3176. }
  3177. },
  3178. // 修改 handleAnswerButtonClick 方法,确保在切换视频时重置历史时间
  3179. handleAnswerButtonClick() {
  3180. console.log('点击开始回答按钮');
  3181. // 隐藏开始回答按钮
  3182. this.showAnswerButton = false;
  3183. // 增加当前视频索引,切换到下一个视频
  3184. this.currentVideoIndex++;
  3185. // 重置历史时间,避免触发异常跳转检测
  3186. this.historyTime = 0;
  3187. // 如果还有视频,播放下一个视频
  3188. if (this.currentVideoIndex < this.videoList.length) {
  3189. this.videoUrl = this.videoList[this.currentVideoIndex];
  3190. this.videoPlaying = true;
  3191. this.$nextTick(() => {
  3192. const videoContext = uni.createVideoContext('myVideo', this);
  3193. if (videoContext) {
  3194. videoContext.play();
  3195. }
  3196. });
  3197. }
  3198. },
  3199. // 处理相机错误
  3200. handleCameraError(e) {
  3201. console.error('相机错误:', e);
  3202. // 获取平台信息
  3203. const systemInfo = uni.getSystemInfoSync();
  3204. const isIOS = systemInfo.platform === 'ios';
  3205. if (isIOS) {
  3206. console.log('iOS相机错误,尝试重新初始化');
  3207. // 显示提示
  3208. uni.showToast({
  3209. title: '相机初始化中...',
  3210. icon: 'loading',
  3211. duration: 2000
  3212. });
  3213. // 重置相机
  3214. this.resetCamera();
  3215. // 如果正在录制,停止录制
  3216. if (this.isRecording) {
  3217. this.isRecording = false;
  3218. this.showStopRecordingButton = false;
  3219. // 提供替代选项
  3220. setTimeout(() => {
  3221. this.useAlternativeRecordingMethod();
  3222. }, 1000);
  3223. }
  3224. } else {
  3225. // 显示错误提示
  3226. uni.showToast({
  3227. title: '相机初始化失败,请检查权限设置',
  3228. icon: 'none'
  3229. });
  3230. // 尝试备用选项
  3231. this.tryFallbackOptions();
  3232. }
  3233. },
  3234. // 添加新方法:尝试备用选项
  3235. tryFallbackOptions() {
  3236. // 检查环境
  3237. const systemInfo = uni.getSystemInfoSync();
  3238. // 在小程序环境中使用小程序API
  3239. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  3240. this.useMiniProgramCamera();
  3241. }
  3242. // 在H5环境中显示静态图像
  3243. else {
  3244. this.showStaticCameraPlaceholder();
  3245. }
  3246. },
  3247. // 添加新方法:使用小程序相机API
  3248. useMiniProgramCamera() {
  3249. console.log('尝试使用小程序相机组件');
  3250. // 这里需要在模板中添加小程序相机组件
  3251. // 并设置一个标志来控制显示
  3252. this.useMiniProgramCameraComponent = true;
  3253. },
  3254. // 添加新方法:显示静态图像
  3255. showStaticCameraPlaceholder() {
  3256. console.log('显示静态摄像头占位图');
  3257. // 创建一个图像元素
  3258. const img = document.createElement('img');
  3259. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  3260. img.className = 'static-camera-image';
  3261. img.style.width = '100%';
  3262. img.style.height = '100%';
  3263. img.style.objectFit = 'cover';
  3264. // 获取容器并添加图像
  3265. const container = this.$refs.userCameraVideo.parentNode;
  3266. container.appendChild(img);
  3267. },
  3268. // 修改 handleTimeUpdate 方法
  3269. handleTimeUpdate(e) {
  3270. // 获取当前视频播放时间
  3271. const currentTime = e.detail.currentTime || e.target.currentTime;
  3272. // 如果正在录制,更新录制时间显示
  3273. if (this.isRecording && this.recordingTimerCount) {
  3274. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  3275. }
  3276. // 如果正在播放低分提示视频,使用专门的字幕数组
  3277. if (this.isPlayingLowScoreVideo) {
  3278. if (this.lowScoreVideoSubtitles && this.lowScoreVideoSubtitles.length > 0) {
  3279. const subtitle = this.lowScoreVideoSubtitles[0]; // 使用第一个字幕
  3280. this.currentSubtitle = subtitle.text;
  3281. }
  3282. return;
  3283. }
  3284. // 获取当前视频对应的字幕数组
  3285. let currentSubtitles = null;
  3286. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  3287. // 追问问题字幕处理
  3288. const subtitleKey = `followUpSubtitles_${this.currentFollowUpQuestion.id}`;
  3289. currentSubtitles = this[subtitleKey] || [{
  3290. startTime: 0,
  3291. endTime: 30,
  3292. text: this.currentFollowUpQuestion.question ||
  3293. this.currentFollowUpQuestion.question
  3294. }];
  3295. } else {
  3296. // 常规问题字幕处理
  3297. if (this.currentVideoIndex === 0) {
  3298. currentSubtitles = this.subtitles;
  3299. } else {
  3300. const subtitleArrayName = `question${this.currentVideoIndex}Subtitles`;
  3301. currentSubtitles = this[subtitleArrayName];
  3302. }
  3303. }
  3304. // 确保字幕持续显示
  3305. if (currentSubtitles && currentSubtitles.length > 0) {
  3306. // 找到当前时间对应的字幕
  3307. const subtitle = currentSubtitles.find(sub =>
  3308. currentTime >= sub.startTime && currentTime <= sub.endTime
  3309. ) || currentSubtitles[0]; // 如果找不到对应时间的字幕,使用第一个字幕
  3310. // 只显示字幕文本
  3311. if (subtitle) {
  3312. this.currentSubtitle = subtitle.text;
  3313. }
  3314. }
  3315. // 更新历史时间和时间戳
  3316. this.historyTime = currentTime;
  3317. this.lastUpdateTime = Date.now();
  3318. },
  3319. // Add a new method to handle the "Start Recording" button click
  3320. handleStartRecordingClick() {
  3321. // 隐藏"开始回答"按钮
  3322. this.showStartRecordingButton = false;
  3323. // 如果倒计时正在进行,清除倒计时
  3324. this.clearCountdown();
  3325. // 重置等待回答状态
  3326. this.isWaitingForAnswer = false;
  3327. // 直接开始录制
  3328. this.startRecordingAnswer();
  3329. },
  3330. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  3331. checkAudioPermission() {
  3332. // 在小程序环境中
  3333. const systemInfo = uni.getSystemInfoSync();
  3334. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  3335. if (isMiniProgram) {
  3336. // 小程序环境下的权限请求
  3337. uni.getSetting({
  3338. success: (res) => {
  3339. if (!res.authSetting['scope.record']) {
  3340. uni.authorize({
  3341. scope: 'scope.record',
  3342. success: () => {
  3343. console.log('录音权限已获取');
  3344. },
  3345. fail: (err) => {
  3346. console.error('录音权限获取失败:', err);
  3347. this.showPermissionDialog('录音');
  3348. }
  3349. });
  3350. }
  3351. if (!res.authSetting['scope.camera']) {
  3352. uni.authorize({
  3353. scope: 'scope.camera',
  3354. success: () => {
  3355. console.log('相机权限已获取');
  3356. },
  3357. fail: (err) => {
  3358. console.error('相机权限获取失败:', err);
  3359. this.showPermissionDialog('相机');
  3360. }
  3361. });
  3362. }
  3363. }
  3364. });
  3365. } else {
  3366. // 浏览器环境下的权限请求
  3367. // ... 保持原有代码不变
  3368. }
  3369. },
  3370. // 添加新方法:测试音频输入
  3371. testAudioInput() {
  3372. if (!this.cameraStream) {
  3373. console.warn('没有可用的媒体流,无法测试音频');
  3374. return;
  3375. }
  3376. const audioTracks = this.cameraStream.getAudioTracks();
  3377. if (audioTracks.length === 0) {
  3378. console.warn('没有检测到音频轨道,尝试重新获取');
  3379. this.tryGetAudioOnly();
  3380. return;
  3381. }
  3382. console.log('音频轨道信息:', audioTracks[0].getSettings());
  3383. // 创建音频上下文和分析器
  3384. try {
  3385. const AudioContext = window.AudioContext || window.webkitAudioContext;
  3386. if (!AudioContext) {
  3387. console.warn('浏览器不支持AudioContext');
  3388. return;
  3389. }
  3390. const audioContext = new AudioContext();
  3391. const analyser = audioContext.createAnalyser();
  3392. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  3393. microphone.connect(analyser);
  3394. // 设置分析器
  3395. analyser.fftSize = 256;
  3396. const bufferLength = analyser.frequencyBinCount;
  3397. const dataArray = new Uint8Array(bufferLength);
  3398. // 检测音频输入
  3399. let silenceCounter = 0;
  3400. const checkAudio = () => {
  3401. if (this.isRecording) return; // 如果正在录制,停止检测
  3402. analyser.getByteFrequencyData(dataArray);
  3403. // 计算平均音量
  3404. let sum = 0;
  3405. for (let i = 0; i < bufferLength; i++) {
  3406. sum += dataArray[i];
  3407. }
  3408. const average = sum / bufferLength;
  3409. if (average > 10) {
  3410. console.log('检测到音频输入,音量:', average);
  3411. silenceCounter = 0;
  3412. } else {
  3413. silenceCounter++;
  3414. if (silenceCounter > 10) {
  3415. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  3416. silenceCounter = 0;
  3417. }
  3418. }
  3419. // 继续检测
  3420. requestAnimationFrame(checkAudio);
  3421. };
  3422. // 开始检测
  3423. checkAudio();
  3424. } catch (e) {
  3425. console.error('音频测试失败:', e);
  3426. }
  3427. },
  3428. // 添加新方法:尝试单独获取音频
  3429. tryGetAudioOnly() {
  3430. navigator.mediaDevices.getUserMedia({ audio: true })
  3431. .then(audioStream => {
  3432. // 如果已有视频流,合并音频和视频轨道
  3433. if (this.cameraStream) {
  3434. const videoTrack = this.cameraStream.getVideoTracks()[0];
  3435. const audioTrack = audioStream.getAudioTracks()[0];
  3436. // 创建新的媒体流,包含视频和音频轨道
  3437. const combinedStream = new MediaStream();
  3438. if (videoTrack) combinedStream.addTrack(videoTrack);
  3439. if (audioTrack) combinedStream.addTrack(audioTrack);
  3440. // 更新摄像头流
  3441. this.cameraStream = combinedStream;
  3442. // 更新视频元素的源
  3443. const videoElement = this.$refs.userCameraVideo;
  3444. if (videoElement) {
  3445. videoElement.srcObject = combinedStream;
  3446. videoElement.muted = true; // 避免回声,但仍然录制声音
  3447. }
  3448. console.log('成功合并音频和视频轨道');
  3449. } else {
  3450. console.warn('没有视频流可合并');
  3451. }
  3452. })
  3453. .catch(err => {
  3454. console.error('单独获取音频失败:', err);
  3455. });
  3456. },
  3457. // 添加新方法:显示权限对话框
  3458. showPermissionDialog(permissionType) {
  3459. uni.showModal({
  3460. title: '需要权限',
  3461. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  3462. confirmText: '去设置',
  3463. success: (res) => {
  3464. if (res.confirm) {
  3465. uni.openSetting({
  3466. success: (settingRes) => {
  3467. console.log('设置页面打开成功', settingRes);
  3468. }
  3469. });
  3470. }
  3471. }
  3472. });
  3473. },
  3474. // 添加重试上传方法
  3475. retryVideoUpload() {
  3476. if (this.lastVideoToRetry) {
  3477. // 隐藏重试按钮
  3478. this.showRetryButton = false;
  3479. // 显示加载提示
  3480. uni.showLoading({
  3481. title: '正在重新提交...',
  3482. mask: true
  3483. });
  3484. // 重新提交视频
  3485. this.submitVideoToInterview(this.lastVideoToRetry);
  3486. } else {
  3487. uni.showToast({
  3488. title: '没有可重试的视频',
  3489. icon: 'none'
  3490. });
  3491. }
  3492. },
  3493. // 添加一个新方法用于压缩视频
  3494. async compressVideo(videoBlob) {
  3495. // 如果视频大小小于某个阈值,直接返回原视频
  3496. if (videoBlob.size < 5 * 1024 * 1024) { // 小于5MB
  3497. return videoBlob;
  3498. }
  3499. console.log('开始压缩视频,原始大小:', videoBlob.size);
  3500. // 创建一个视频元素来加载视频
  3501. const videoElement = document.createElement('video');
  3502. videoElement.muted = true;
  3503. videoElement.autoplay = false;
  3504. // 创建一个canvas元素用于绘制视频帧
  3505. const canvas = document.createElement('canvas');
  3506. const ctx = canvas.getContext('2d');
  3507. // 设置视频源
  3508. videoElement.src = URL.createObjectURL(videoBlob);
  3509. return new Promise((resolve) => {
  3510. videoElement.onloadedmetadata = () => {
  3511. // 设置canvas尺寸为视频的一半
  3512. const width = Math.floor(videoElement.videoWidth / 2);
  3513. const height = Math.floor(videoElement.videoHeight / 2);
  3514. canvas.width = width;
  3515. canvas.height = height;
  3516. // 创建MediaRecorder来录制canvas
  3517. const stream = canvas.captureStream(15); // 15fps
  3518. // 如果原视频有音轨,添加到新流中
  3519. if (videoElement.captureStream) {
  3520. const originalStream = videoElement.captureStream();
  3521. const audioTracks = originalStream.getAudioTracks();
  3522. if (audioTracks.length > 0) {
  3523. stream.addTrack(audioTracks[0]);
  3524. }
  3525. }
  3526. // 设置较低的比特率
  3527. const options = {
  3528. mimeType: 'video/webm;codecs=vp8,opus',
  3529. audioBitsPerSecond: 64000,
  3530. videoBitsPerSecond: 800000 // 800kbps
  3531. };
  3532. const mediaRecorder = new MediaRecorder(stream, options);
  3533. const chunks = [];
  3534. mediaRecorder.ondataavailable = (e) => {
  3535. if (e.data.size > 0) {
  3536. chunks.push(e.data);
  3537. }
  3538. };
  3539. mediaRecorder.onstop = () => {
  3540. const compressedBlob = new Blob(chunks, { type: 'video/webm' });
  3541. console.log('视频压缩完成,压缩后大小:', compressedBlob.size);
  3542. resolve(compressedBlob);
  3543. };
  3544. // 开始播放视频并录制
  3545. videoElement.onplay = () => {
  3546. mediaRecorder.start(10);
  3547. // 绘制视频帧到canvas
  3548. const drawFrame = () => {
  3549. if (videoElement.paused || videoElement.ended) {
  3550. mediaRecorder.stop();
  3551. return;
  3552. }
  3553. ctx.drawImage(videoElement, 0, 0, width, height);
  3554. requestAnimationFrame(drawFrame);
  3555. };
  3556. drawFrame();
  3557. };
  3558. videoElement.play();
  3559. };
  3560. });
  3561. },
  3562. // 修改 checkIOSCameraRecordPermission 方法
  3563. checkIOSCameraRecordPermission() {
  3564. const systemInfo = uni.getSystemInfoSync();
  3565. if (systemInfo.platform !== 'ios') return;
  3566. // 在iOS上,需要同时请求相机和录音权限
  3567. uni.getSetting({
  3568. success: (res) => {
  3569. // 检查相机权限
  3570. if (!res.authSetting['scope.camera']) {
  3571. uni.authorize({
  3572. scope: 'scope.camera',
  3573. success: () => {
  3574. console.log('iOS相机权限已获取');
  3575. },
  3576. fail: (err) => {
  3577. console.error('iOS相机权限获取失败:', err);
  3578. this.showPermissionDialog('相机');
  3579. }
  3580. });
  3581. }
  3582. // 检查录音权限
  3583. if (!res.authSetting['scope.record']) {
  3584. uni.authorize({
  3585. scope: 'scope.record',
  3586. success: () => {
  3587. console.log('iOS录音权限已获取');
  3588. },
  3589. fail: (err) => {
  3590. console.error('iOS录音权限获取失败:', err);
  3591. this.showPermissionDialog('录音');
  3592. }
  3593. });
  3594. }
  3595. }
  3596. });
  3597. },
  3598. // 添加新方法:检查并修复渲染问题
  3599. checkAndFixRenderingIssues() {
  3600. // 检查全局对象,防止渲染错误
  3601. try {
  3602. // 检查是否有全局变量u
  3603. if (typeof u !== 'undefined' && u) {
  3604. // 检查currentQuestion属性
  3605. if (!u.currentQuestion) {
  3606. console.log('修复: 创建缺失的currentQuestion对象');
  3607. u.currentQuestion = {};
  3608. }
  3609. // 确保isImportant属性存在
  3610. if (u.currentQuestion && typeof u.currentQuestion.isImportant === 'undefined') {
  3611. console.log('修复: 设置缺失的isImportant属性');
  3612. u.currentQuestion.isImportant = false;
  3613. }
  3614. }
  3615. } catch (e) {
  3616. console.log('防御性检查异常:', e);
  3617. }
  3618. },
  3619. // 添加格式化时间的辅助方法
  3620. formatTime(seconds) {
  3621. if (!seconds && seconds !== 0) return '03:30'; // 默认显示03:30
  3622. const minutes = Math.floor(seconds / 60);
  3623. const remainingSeconds = seconds % 60;
  3624. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  3625. },
  3626. // 添加获取问题数据的方法
  3627. fetchQuestions() {
  3628. this.loading = true;
  3629. // 获取tenant_id,如果没有则使用默认值1
  3630. const tenant_id =uni.getStorageSync('tenant_id') || JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1';
  3631. // 获取当前选择的职位ID
  3632. let position_id = 1; // 默认值
  3633. try {
  3634. const selectedJob = JSON.parse(uni.getStorageSync('selectedJob') || '{}');
  3635. if (selectedJob && selectedJob.id) {
  3636. position_id = selectedJob.id;
  3637. console.log('使用选择的职位ID获取问题:', position_id);
  3638. } else {
  3639. console.warn('未找到选择的职位信息,使用默认职位ID:', position_id);
  3640. }
  3641. } catch (e) {
  3642. console.error('解析职位信息失败:', e);
  3643. }
  3644. // 构建完整的请求URL,便于调试
  3645. const timestamp = Date.now();
  3646. const requestUrl = `${apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=0&_t=${timestamp}`;
  3647. console.log('完整的请求URL:', requestUrl);
  3648. uni.request({
  3649. url: requestUrl,
  3650. method: 'GET',
  3651. success: (res) => {
  3652. if (res.data && res.data.code === 2000 && res.data.data && res.data.data.items) {
  3653. console.log('获取问题数据成功:', res.data.data.items);
  3654. // 存储问题数据
  3655. this.questions = res.data.data.items;
  3656. // 处理视频列表和字幕
  3657. this.processQuestionData();
  3658. // 播放介绍视频
  3659. this.playIntroVideo();
  3660. } else {
  3661. console.error('获取问题数据失败:', res.data);
  3662. uni.showToast({
  3663. title: '获取问题数据失败',
  3664. icon: 'none'
  3665. });
  3666. /* // 使用默认视频和字幕
  3667. this.useDefaultVideosAndSubtitles(); */
  3668. }
  3669. },
  3670. fail: (err) => {
  3671. console.error('请求问题数据失败:', err);
  3672. uni.showToast({
  3673. title: '网络错误,请检查网络连接',
  3674. icon: 'none'
  3675. });
  3676. /* // 使用默认视频和字幕
  3677. this.useDefaultVideosAndSubtitles(); */
  3678. },
  3679. complete: () => {
  3680. this.loading = false;
  3681. }
  3682. });
  3683. },
  3684. // 处理问题数据,提取视频URL和字幕
  3685. processQuestionData() {
  3686. // 清空视频列表和字幕
  3687. this.videoList = [];
  3688. // 首先添加介绍视频
  3689. this.videoList.push(this.introVideoUrl);
  3690. // 获取配置数据中的开场白
  3691. const configStr = uni.getStorageSync('configData');
  3692. let openingSpeech = [];
  3693. if (configStr) {
  3694. try {
  3695. const configData = JSON.parse(configStr);
  3696. if (configData && configData.digital_human_opening_speech) {
  3697. // 检查是否为数组格式
  3698. if (Array.isArray(configData.digital_human_opening_speech)) {
  3699. // 将配置数据转换为字幕格式
  3700. openingSpeech = configData.digital_human_opening_speech.map((item, index, arr) => {
  3701. const startTime = index === 0 ? 0 : arr[index - 1].end_time || index * 5;
  3702. const endTime = item.end_time || (index + 1) * 5;
  3703. return {
  3704. startTime,
  3705. endTime,
  3706. text: item.content
  3707. };
  3708. });
  3709. } else if (typeof configData.digital_human_opening_speech === 'string') {
  3710. // 使用新的方法处理字符串类型的开场白
  3711. openingSpeech = this.convertStringToSubtitles(configData.digital_human_opening_speech);
  3712. }
  3713. }
  3714. } catch (error) {
  3715. console.error('解析configData失败:', error);
  3716. }
  3717. }
  3718. // 如果成功获取到开场白配置则使用配置的内容,否则使用默认字幕
  3719. this.subtitles = openingSpeech.length > 0 ? openingSpeech : [
  3720. {
  3721. startTime: 0,
  3722. endTime: 5,
  3723. text: '你好,我是本次面试的面试官,欢迎参加本公司的线上面试!'
  3724. },
  3725. {
  3726. startTime: 5,
  3727. endTime: 13,
  3728. text: '面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试'
  3729. },
  3730. {
  3731. startTime: 13,
  3732. endTime: 20,
  3733. text: '以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。'
  3734. }
  3735. ];
  3736. console.log('处理后的字幕数据:', this.subtitles);
  3737. // 按照sequence_number排序问题
  3738. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  3739. // 只处理前5个问题(或者更少,如果问题总数不足5个)
  3740. const questionsToUse = sortedQuestions.slice(0, 5);
  3741. // 为每个问题添加视频URL和字幕
  3742. questionsToUse.forEach((question, index) => {
  3743. if (question.digital_human_video_url) {
  3744. this.videoList.push(question.digital_human_video_url);
  3745. // 创建字幕映射,只保留文本
  3746. const subtitleArray = [{
  3747. startTime: 0,
  3748. endTime: 30, // 延长字幕显示时间到30秒
  3749. text: question.question || question.question
  3750. }];
  3751. const videoIndex = this.videoList.length - 1;
  3752. this[`question${videoIndex}Subtitles`] = subtitleArray;
  3753. // 存储字幕映射
  3754. this.subtitleMap[question.digital_human_video_url] = subtitleArray;
  3755. }
  3756. });
  3757. // 如果没有获取到任何问题视频,使用默认视频
  3758. if (this.videoList.length <= 1) {
  3759. /* this.useDefaultVideosAndSubtitles(); */
  3760. } else {
  3761. console.log('处理后的视频列表:', this.videoList);
  3762. console.log('处理后的字幕:', this.subtitles);
  3763. }
  3764. },
  3765. // 将字符串类型的文本转换为字幕格式
  3766. convertStringToSubtitles(text) {
  3767. if (!text || typeof text !== 'string') {
  3768. return [];
  3769. }
  3770. // 按照标点符号分割文本,保留标点符号
  3771. const sentences = text.split(/([。!?!?])/)
  3772. .filter(part => part.trim() !== '')
  3773. .reduce((acc, part, index, arr) => {
  3774. if (index % 2 === 0) {
  3775. // 文本部分
  3776. const nextPart = arr[index + 1] || '';
  3777. acc.push(part + nextPart);
  3778. }
  3779. return acc;
  3780. }, [])
  3781. .filter(sentence => sentence.trim().length > 0);
  3782. // 如果没有明显的分句标点,按长度分割
  3783. if (sentences.length <= 1) {
  3784. const maxLength = 30; // 每段最大字符数
  3785. const textParts = [];
  3786. for (let i = 0; i < text.length; i += maxLength) {
  3787. textParts.push(text.substring(i, i + maxLength));
  3788. }
  3789. sentences.splice(0, sentences.length, ...textParts);
  3790. }
  3791. // 为每个句子分配时间戳
  3792. let currentStartTime = 0;
  3793. return sentences.map((sentence) => {
  3794. const duration = Math.max(3, Math.ceil(sentence.length / 6)); // 根据文本长度计算时长
  3795. const startTime = currentStartTime;
  3796. const endTime = startTime + duration;
  3797. currentStartTime = endTime;
  3798. return {
  3799. startTime,
  3800. endTime,
  3801. text: sentence.trim()
  3802. };
  3803. });
  3804. },
  3805. // 使用默认视频和字幕(作为备用)
  3806. useDefaultVideosAndSubtitles() {
  3807. console.log('使用默认视频和字幕');
  3808. // 设置默认字幕
  3809. this.subtitles = [
  3810. {
  3811. startTime: 0,
  3812. endTime: 5,
  3813. text: '你好,我是本次面试的面试官,欢迎参加本公司的线上面试!'
  3814. },
  3815. {
  3816. startTime: 5,
  3817. endTime: 13,
  3818. text: '面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试'
  3819. },
  3820. {
  3821. startTime: 13,
  3822. endTime: 20,
  3823. text: '以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。'
  3824. }
  3825. ];
  3826. // 设置默认问题字幕
  3827. this.question1Subtitles = [{
  3828. startTime: 0,
  3829. endTime: 10,
  3830. text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  3831. }];
  3832. this.question2Subtitles = [{
  3833. startTime: 0,
  3834. endTime: 4,
  3835. text: '在工作中,你如何确保个人防护装备的正确使用?'
  3836. }];
  3837. this.question3Subtitles = [{
  3838. startTime: 0,
  3839. endTime: 4,
  3840. text: '描述一次你与团队合作改善生产流程的经历。'
  3841. }];
  3842. this.question4Subtitles = [{
  3843. startTime: 0,
  3844. endTime: 6,
  3845. text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  3846. }];
  3847. this.question5Subtitles = [{
  3848. startTime: 0,
  3849. endTime: 5,
  3850. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  3851. }];
  3852. },
  3853. // 播放介绍视频
  3854. playIntroVideo() {
  3855. // 设置第一个视频(介绍视频)
  3856. this.videoUrl = this.videoList[0];
  3857. this.videoPlaying = true;
  3858. this.$nextTick(() => {
  3859. const videoContext = uni.createVideoContext('myVideo', this);
  3860. if (videoContext) {
  3861. videoContext.play();
  3862. }
  3863. });
  3864. },
  3865. // 添加新方法:获取默认问题ID
  3866. getDefaultQuestionId(videoIndex) {
  3867. // 根据视频索引映射到默认问题ID
  3868. switch(videoIndex) {
  3869. case 1: // 第一个问题
  3870. return 10;
  3871. case 2: // 第二个问题
  3872. return 11;
  3873. case 3: // 第三个问题
  3874. return 12;
  3875. case 4: // 第四个问题
  3876. return 13;
  3877. default:
  3878. return 10; // 默认值
  3879. }
  3880. },
  3881. // 添加新方法:根据索引获取当前问题
  3882. getCurrentQuestionByIndex(videoIndex) {
  3883. // 视频索引减1,因为第一个视频是介绍视频
  3884. const questionIndex = videoIndex - 1;
  3885. // 按照sequence_number排序问题
  3886. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  3887. // 检查索引是否有效
  3888. if (questionIndex >= 0 && questionIndex < sortedQuestions.length) {
  3889. return sortedQuestions[questionIndex];
  3890. }
  3891. return null;
  3892. },
  3893. // 添加新方法:获取当前问题的推荐录制时长
  3894. getCurrentQuestionRecommendedDuration() {
  3895. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  3896. if (currentQuestion && currentQuestion.recommended_duration) {
  3897. // 如果问题有推荐时长,使用推荐时长(转换为秒)
  3898. const recommendedDuration = parseInt(currentQuestion.recommended_duration);
  3899. console.log(`使用问题推荐时长: ${recommendedDuration}秒`);
  3900. return recommendedDuration;
  3901. }
  3902. // 如果没有推荐时长,使用默认值300秒(5分钟)
  3903. console.log('使用默认录制时长: 300秒');
  3904. return 300;
  3905. },
  3906. // 添加新方法:重置录制状态,准备重新回答
  3907. resetForRerecording() {
  3908. console.log('重置录制状态,准备重新回答');
  3909. // 重置录制状态
  3910. this.isRecording = false;
  3911. this.showStopRecordingButton = false;
  3912. // 显示"开始回答"按钮
  3913. this.showStartRecordingButton = true;
  3914. // 清除倒计时
  3915. this.clearCountdown();
  3916. // 重置录制计时器
  3917. if (this.recordingTimer) {
  3918. clearInterval(this.recordingTimer);
  3919. this.recordingTimer = null;
  3920. }
  3921. // 重置录制时间显示
  3922. this.recordingTimerCount = 0;
  3923. this.recordingTimeDisplay = '00:00 ';/* / 05:00 */
  3924. // 重新获取当前问题的推荐录制时长
  3925. this.maxRecordingTime = this.getCurrentQuestionRecommendedDuration();
  3926. this.remainingTime = this.maxRecordingTime;
  3927. // 如果是浏览器环境,停止MediaRecorder
  3928. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  3929. this.mediaRecorder.stop();
  3930. this.recordedChunks = [];
  3931. }
  3932. // 如果是小程序环境,停止相机录制
  3933. if (this.cameraContext) {
  3934. this.cameraContext.stopRecord({
  3935. success: () => {
  3936. console.log('相机录制已停止');
  3937. },
  3938. fail: (err) => {
  3939. console.error('停止相机录制失败:', err);
  3940. }
  3941. });
  3942. }
  3943. // 重置问题重试次数映射
  3944. this.questionRetryMap = {};
  3945. },
  3946. // 修改 playLowScoreVideo 方法,优化字幕显示并添加重试次数限制
  3947. playLowScoreVideo() {
  3948. console.log('播放低分提示视频');
  3949. // 获取当前问题的重试次数
  3950. const currentQuestionId = this.getCurrentQuestionByIndex(this.currentVideoIndex)?.id;
  3951. if (!currentQuestionId) {
  3952. console.error('无法获取当前问题ID');
  3953. return;
  3954. }
  3955. // 初始化或增加重试次数
  3956. if (!this.questionRetryMap[currentQuestionId]) {
  3957. this.questionRetryMap[currentQuestionId] = 1;
  3958. } else {
  3959. this.questionRetryMap[currentQuestionId]++;
  3960. }
  3961. // 检查是否超过最大重试次数
  3962. if (this.questionRetryMap[currentQuestionId] > this.maxQuestionRetries) {
  3963. console.log(`问题 ${currentQuestionId} 已超过最大重试次数,自动进入下一题`);
  3964. // 重置低分视频状态
  3965. this.isPlayingLowScoreVideo = false;
  3966. // 重置追问相关状态
  3967. this.isFollowUpQuestion = false;
  3968. this.currentFollowUpQuestion = null;
  3969. this.followUpQuestion = '';
  3970. this.parentQuestion = '';
  3971. // 增加视频索引,进入下一题
  3972. this.currentVideoIndex++;
  3973. // 检查是否还有下一题
  3974. if (this.currentVideoIndex < this.videoList.length) {
  3975. // 获取下一题的字幕
  3976. const nextQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  3977. if (nextQuestion) {
  3978. this.currentSubtitle = nextQuestion.question || nextQuestion.question;
  3979. }
  3980. // 播放下一题的视频
  3981. this.videoUrl = this.videoList[this.currentVideoIndex];
  3982. this.videoPlaying = true;
  3983. this.$nextTick(() => {
  3984. const videoContext = uni.createVideoContext('myVideo', this);
  3985. if (videoContext) {
  3986. videoContext.play();
  3987. }
  3988. });
  3989. } else {
  3990. // 如果没有下一题,结束面试
  3991. this.finishInterview();
  3992. }
  3993. return;
  3994. }
  3995. // 标记正在播放低分提示视频
  3996. this.isPlayingLowScoreVideo = true;
  3997. // 隐藏其他按钮
  3998. this.showStartRecordingButton = false;
  3999. this.showStopRecordingButton = false;
  4000. this.showRerecordButton = false;
  4001. // 设置视频URL
  4002. this.videoUrl = this.lowScoreVideoUrl;
  4003. this.videoPlaying = true;
  4004. // 保存当前问题的字幕
  4005. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  4006. const originalSubtitle = currentQuestion ? (currentQuestion.question || currentQuestion.question) : this.currentSubtitle;
  4007. // 清除现有字幕
  4008. this.currentSubtitle = '';
  4009. // 使用 nextTick 确保 DOM 更新后再播放视频
  4010. this.$nextTick(() => {
  4011. const videoContext = uni.createVideoContext('myVideo', this);
  4012. if (videoContext) {
  4013. videoContext.play();
  4014. // 视频开始播放后短暂延迟显示字幕,确保用户注意到
  4015. setTimeout(() => {
  4016. // 设置低分提示视频的字幕
  4017. this.currentSubtitle = '我未听清楚您在说什么,请您再说一遍!';
  4018. // 创建专门的字幕数组用于低分提示视频
  4019. this.lowScoreVideoSubtitles = [
  4020. {
  4021. startTime: 0,
  4022. endTime: 30,
  4023. text: '我未听清楚您在说什么,请您再说一遍!',
  4024. translation: 'I didn\'t quite catch what you said, could you please repeat it?'
  4025. }
  4026. ];
  4027. }, 500);
  4028. }
  4029. });
  4030. // 保存原始字幕信息到实例属性,以便在视频结束时恢复
  4031. this.originalQuestionSubtitle = originalSubtitle;
  4032. },
  4033. // 添加新方法:处理重新录制按钮点击
  4034. handleRerecordButtonClick() {
  4035. console.log('点击重新录制按钮');
  4036. // 增加重试计数
  4037. this.retryCount++;
  4038. console.log(`当前重试次数: ${this.retryCount}/${this.maxRetryAttempts}`);
  4039. // 隐藏重新录制按钮
  4040. this.showRerecordButton = false;
  4041. // 直接调用开始录制的方法
  4042. this.handleStartRecordingClick();
  4043. },
  4044. // 页面卸载时关闭WebSocket连接
  4045. onUnload() {
  4046. console.log('页面卸载,关闭WebSocket连接');
  4047. this.cleanupPersonDetectionWebSocket();
  4048. // 清理其他资源
  4049. this.stopUserCamera();
  4050. this.clearCountdown();
  4051. // 清理录制相关定时器
  4052. if (this.recordingTimer) {
  4053. clearInterval(this.recordingTimer);
  4054. this.recordingTimer = null;
  4055. }
  4056. // 停止MediaRecorder
  4057. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  4058. this.mediaRecorder.stop();
  4059. }
  4060. // 关闭屏幕常亮
  4061. uni.setKeepScreenOn({
  4062. keepScreenOn: false
  4063. });
  4064. },
  4065. // 页面隐藏时关闭WebSocket连接
  4066. onHide() {
  4067. console.log('页面隐藏,关闭WebSocket连接');
  4068. // 完全关闭WebSocket连接和相关定时器
  4069. this.cleanupPersonDetectionWebSocket();
  4070. // 如果正在录制,给用户提示
  4071. if (this.isRecording) {
  4072. console.log('页面隐藏时正在录制,录制将继续');
  4073. // 可以选择暂停录制或者给用户提示
  4074. uni.showToast({
  4075. title: '请不要离开面试页面',
  4076. icon: 'none',
  4077. duration: 2000
  4078. });
  4079. }
  4080. },
  4081. // 页面显示时重新初始化WebSocket连接
  4082. onShow() {
  4083. console.log('identity-verify页面onShow');
  4084. console.log('当前本地存储中的职位信息:', uni.getStorageSync('selectedJob'));
  4085. // 每次页面显示时重新获取问题
  4086. this.fetchQuestions();
  4087. // this.fetchFollowUpQuestions(); // 添加获取追问问题的调用
  4088. // 重新初始化WebSocket连接(仅在小程序环境中)
  4089. const systemInfo = uni.getSystemInfoSync();
  4090. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  4091. if (isMiniProgram) {
  4092. // 如果WebSocket未连接,重新初始化
  4093. if (!this.personDetectionSocket) {
  4094. console.log('重新初始化WebSocket连接');
  4095. this.initPersonDetectionWebSocket();
  4096. }
  4097. // 如果WebSocket已连接但检测定时器被停止,重新启动检测
  4098. else if (!this.personDetectionInterval) {
  4099. console.log('重新启动人脸检测定时器');
  4100. this.startPersonDetectionInterval();
  4101. }
  4102. }
  4103. uni.hideHomeButton(); // 隐藏左上角返回按钮
  4104. },
  4105. // 添加新方法:阻止视频控制
  4106. preventVideoControl(e) {
  4107. // 阻止默认行为和事件冒泡
  4108. e.preventDefault();
  4109. e.stopPropagation();
  4110. console.log('阻止视频控制操作');
  4111. return false;
  4112. },
  4113. // 添加获取追问问题的方法
  4114. fetchFollowUpQuestions() {
  4115. // 获取tenant_id,如果没有则使用默认值1
  4116. const tenant_id =uni.getStorageSync('tenant_id') || JSON.parse(uni.getStorageSync('userInfo')).tenant_id || '1';
  4117. // 获取当前选择的职位ID
  4118. let position_id = 1; // 默认值
  4119. try {
  4120. const selectedJob = JSON.parse(uni.getStorageSync('selectedJob') || '{}');
  4121. if (selectedJob && selectedJob.id) {
  4122. position_id = selectedJob.id;
  4123. console.log('使用选择的职位ID获取追问问题:', position_id);
  4124. } else {
  4125. console.warn('未找到选择的职位信息,使用默认职位ID:', position_id);
  4126. }
  4127. } catch (e) {
  4128. console.error('解析职位信息失败:', e);
  4129. }
  4130. // 构建完整的请求URL,便于调试
  4131. const timestamp = Date.now();
  4132. const requestUrl = `${apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=5&_t=${timestamp}`;
  4133. console.log('获取追问问题的请求URL:', requestUrl);
  4134. uni.request({
  4135. url: requestUrl,
  4136. method: 'GET',
  4137. success: (res) => {
  4138. if (res.data && res.data.code === 2000 && res.data.data && res.data.data.items) {
  4139. console.log('获取追问问题数据成功:', res.data.data.items);
  4140. // 存储追问问题数据,并确保每个问题都有question_form=5标记
  4141. this.followUpQuestions = res.data.data.items.map(item => ({
  4142. ...item,
  4143. question_form: 5 // 确保每个追问问题都有正确的question_form值
  4144. }));
  4145. // 处理追问问题数据,建立与父问题的关联
  4146. this.processFollowUpQuestions();
  4147. } else {
  4148. console.error('获取追问问题数据失败:', res.data);
  4149. // 初始化为空数组,避免undefined错误
  4150. this.followUpQuestions = [];
  4151. }
  4152. },
  4153. fail: (err) => {
  4154. console.error('请求追问问题数据失败:', err);
  4155. // 初始化为空数组,避免undefined错误
  4156. this.followUpQuestions = [];
  4157. }
  4158. });
  4159. },
  4160. // 改进处理追问问题数据的方法
  4161. processFollowUpQuestions() {
  4162. if (!this.followUpQuestions || this.followUpQuestions.length === 0) {
  4163. console.log('没有追问问题数据');
  4164. this.followUpQuestions = []; // 确保是数组
  4165. return;
  4166. }
  4167. console.log('处理追问问题数据,总数:', this.followUpQuestions.length);
  4168. // 过滤掉没有parent_question_id的追问问题
  4169. this.followUpQuestions = this.followUpQuestions.filter(q => q.parent_question_id);
  4170. // 可以在这里进行额外的数据处理,如按照parent_question_id分组等
  4171. const parentQuestionIds = new Set();
  4172. this.followUpQuestions.forEach(question => {
  4173. if (question.parent_question_id) {
  4174. parentQuestionIds.add(question.parent_question_id);
  4175. }
  4176. });
  4177. console.log('有追问的父问题ID:', Array.from(parentQuestionIds));
  4178. // 初始化已播放追问记录对象
  4179. this.hasPlayedFollowUp = {};
  4180. },
  4181. // 修改检查并播放追问问题的方法
  4182. checkAndPlayFollowUpQuestion(parentQuestionId) {
  4183. console.log('检查是否有追问问题,父问题ID:', parentQuestionId);
  4184. // 如果正在等待用户回答,不进行任何操作
  4185. if (this.isWaitingForAnswer) {
  4186. console.log('正在等待用户回答,暂不处理追问');
  4187. return;
  4188. }
  4189. if (!parentQuestionId) {
  4190. console.warn('没有父问题ID,无法检查追问问题');
  4191. this.proceedToNextQuestion();
  4192. return;
  4193. }
  4194. // 如果已经播放过这个问题的追问,则直接进入下一个问题
  4195. if (this.hasPlayedFollowUp[parentQuestionId]) {
  4196. console.log('已经播放过此问题的追问,直接进入下一个问题');
  4197. this.retryCount = 0;
  4198. this.proceedToNextQuestion();
  4199. return;
  4200. }
  4201. // 查找对应的追问问题
  4202. const followUpQuestion = this.followUpQuestions.find(q =>
  4203. q.parent_question_id === parentQuestionId &&
  4204. q.digital_human_video_url &&
  4205. q.question_form === 5
  4206. );
  4207. if (followUpQuestion) {
  4208. console.log('找到追问问题:', followUpQuestion);
  4209. // 检查录制时长
  4210. const recordingDuration = this.getRecordingDuration();
  4211. const lowScoreDuration = 7; // 低分阈值时长(秒),少于7秒视为低分
  4212. const minDuration = 3; // 最小录制时长阈值(秒)
  4213. // 如果录制时间少于最小时长或者在最小时长和低分阈值之间,播放"未听清楚"提示视频
  4214. if (recordingDuration < lowScoreDuration) {
  4215. let message = recordingDuration < minDuration ?
  4216. `录制时间 ${recordingDuration} 秒,少于最小时长 ${minDuration} 秒` :
  4217. `录制时间 ${recordingDuration} 秒,少于标准时长 ${lowScoreDuration} 秒`;
  4218. console.log(message + ',将播放低分提示视频');
  4219. // 增加重试次数
  4220. this.retryCount++;
  4221. // 设置需要播放低分提示视频
  4222. this.needPlayLowScoreVideo = true;
  4223. // 播放低分提示视频
  4224. this.playLowScoreVideo();
  4225. return;
  4226. }
  4227. // 如果录制时长足够,重置重试次数
  4228. this.retryCount = 0;
  4229. // 标记此追问已播放
  4230. this.hasPlayedFollowUp[parentQuestionId] = true;
  4231. // 标记当前是追问问题
  4232. this.isFollowUpQuestion = true;
  4233. // 记录当前父问题ID
  4234. this.currentParentQuestionId = parentQuestionId;
  4235. // 保存当前追问问题的完整信息
  4236. this.currentFollowUpQuestion = followUpQuestion;
  4237. // 播放追问问题视频
  4238. this.playFollowUpQuestionVideo(followUpQuestion);
  4239. } else {
  4240. console.log('没有找到对应的追问问题,继续下一个问题');
  4241. this.retryCount = 0;
  4242. this.proceedToNextQuestion();
  4243. }
  4244. },
  4245. // 修改播放追问问题视频的方法
  4246. playFollowUpQuestionVideo(followUpQuestion) {
  4247. console.log('播放追问问题视频:', followUpQuestion.digital_human_video_url);
  4248. // 保存父问题内容
  4249. this.parentQuestion = this.currentSubtitle;
  4250. this.videoUrl = followUpQuestion.digital_human_video_url;
  4251. this.videoPlaying = true;
  4252. this.isFollowUpQuestion = true;
  4253. this.currentFollowUpQuestion = followUpQuestion;
  4254. // 设置追问问题的字幕
  4255. const followUpSubtitles = [{
  4256. startTime: 0,
  4257. endTime: 60, // 延长显示时间到60秒
  4258. text: followUpQuestion.question || followUpQuestion.question
  4259. }];
  4260. const subtitleKey = `followUpSubtitles_${followUpQuestion.id}`;
  4261. this[subtitleKey] = followUpSubtitles;
  4262. this.$nextTick(() => {
  4263. const videoContext = uni.createVideoContext('myVideo', this);
  4264. if (videoContext) {
  4265. videoContext.play();
  4266. // 更新当前字幕为追问内容
  4267. this.currentSubtitle = followUpSubtitles[0].text;
  4268. }
  4269. });
  4270. },
  4271. // 添加作废面试的方法
  4272. invalidateInterview() {
  4273. // 停止录制(如果正在录制)
  4274. if (this.isRecording) {
  4275. this.stopRecordingAnswer();
  4276. }
  4277. // 清除所有计时器
  4278. if (this.recordingTimer) {
  4279. clearInterval(this.recordingTimer);
  4280. }
  4281. // 重置状态
  4282. this.isRecording = false;
  4283. this.showStopRecordingButton = false;
  4284. this.showStartRecordingButton = false;
  4285. // 显示作废提示
  4286. // uni.showToast({
  4287. // title: 'Interview invalidated',
  4288. // icon: 'none',
  4289. // duration: 2000
  4290. // });
  4291. // 延迟后返回上一页
  4292. setTimeout(() => {
  4293. uni.switchTab({
  4294. url: '/pages/index/index'
  4295. });
  4296. }, 500);
  4297. },
  4298. initPersonDetectionWebSocket() {
  4299. if (this.personDetectionSocket) {
  4300. this.cleanupPersonDetectionWebSocket();
  4301. }
  4302. console.log('YOLO分析开始');
  4303. try {
  4304. this.personDetectionSocket = uni.connectSocket({
  4305. url: `${personDetectionWsUrl}/ws/interview-room/room_${uni.getStorageSync('appId')}/${uni.getStorageSync('appId')}/`,
  4306. success: () => {
  4307. console.log('WebSocket connection initiated');
  4308. },
  4309. fail: (error) => {
  4310. console.error('WebSocket connection failed:', error);
  4311. }
  4312. });
  4313. this.personDetectionSocket.onOpen(() => {
  4314. console.log('WebSocket connection opened');
  4315. this.startPersonDetectionInterval();
  4316. });
  4317. this.personDetectionSocket.onError((error) => {
  4318. console.error('WebSocket error:', error);
  4319. this.cleanupPersonDetectionWebSocket();
  4320. });
  4321. this.personDetectionSocket.onClose(() => {
  4322. console.log('WebSocket connection closed');
  4323. this.cleanupPersonDetectionWebSocket();
  4324. });
  4325. this.personDetectionSocket.onMessage((res) => {
  4326. try {
  4327. const data = JSON.parse(res.data);
  4328. /* console.log('data1', data); */
  4329. if (data.type === 'person_detection_result') {
  4330. this.handlePersonDetectionResult(data);
  4331. }
  4332. } catch (error) {
  4333. // console.error('Error parsing WebSocket message:', error);
  4334. }
  4335. });
  4336. } catch (error) {
  4337. console.error('Error initializing WebSocket:', error);
  4338. this.cleanupPersonDetectionWebSocket();
  4339. }
  4340. },
  4341. startPersonDetectionInterval() {
  4342. if (this.personDetectionInterval) {
  4343. clearInterval(this.personDetectionInterval);
  4344. }
  4345. // 录制时走帧抓取,否则走拍照
  4346. this.personDetectionInterval = setInterval(() => {
  4347. try {
  4348. if (!this.personDetectionSocket || !this.cameraContext) {
  4349. console.warn('人脸检测:相机上下文或WebSocket连接未就绪');
  4350. return;
  4351. }
  4352. // 获取系统信息,检查是否为iOS平台
  4353. const systemInfo = uni.getSystemInfoSync();
  4354. const isIOS = systemInfo.platform === 'ios';
  4355. if (this.isRecording && this.useMiniProgramCameraComponent) {
  4356. // iOS端录制时的特殊处理
  4357. if (isIOS) {
  4358. // 在iOS录制期间,降低检测频率并增加错误处理
  4359. this.handleIOSRecordingDetection();
  4360. } else {
  4361. this.startFrameStreamCapture();
  4362. }
  4363. } else {
  4364. this.stopFrameStreamCapture();
  4365. this.safeTakePhotoAndSend();
  4366. }
  4367. } catch (mainError) {
  4368. console.error('人脸检测:主流程执行出错:', mainError);
  4369. }
  4370. }, this.frameCaptureIntervalMs || 5000);
  4371. },
  4372. // 新增方法:处理iOS端录制时的检测逻辑
  4373. handleIOSRecordingDetection() {
  4374. // 检查是否正在录制
  4375. if (!this.isRecording) {
  4376. return;
  4377. }
  4378. // 检查WebSocket连接状态
  4379. if (!this.personDetectionSocket || this.personDetectionSocket.readyState !== 1) {
  4380. console.warn('iOS录制检测:WebSocket连接未就绪');
  4381. return;
  4382. }
  4383. // 检查相机上下文
  4384. if (!this.cameraContext) {
  4385. console.warn('iOS录制检测:相机上下文不存在');
  4386. return;
  4387. }
  4388. // 在iOS录制期间,使用更安全的检测方式
  4389. // 降低检测频率,避免与录制功能冲突
  4390. const now = Date.now();
  4391. if (this.lastIOSDetectionTime && (now - this.lastIOSDetectionTime) < 5000) {
  4392. // 5秒内只检测一次,避免频繁调用
  4393. return;
  4394. }
  4395. this.lastIOSDetectionTime = now;
  4396. // 使用try-catch包装,确保错误不会影响录制
  4397. try {
  4398. // 尝试使用帧抓取方式
  4399. if (this.hiddenCanvasReady) {
  4400. this.startFrameStreamCapture();
  4401. } else {
  4402. // 如果帧抓取不可用,使用安全的拍照方式
  4403. this.safeTakePhotoForIOSRecording();
  4404. }
  4405. } catch (error) {
  4406. console.warn('iOS录制检测出错,但不影响录制:', error);
  4407. // 不抛出错误,确保录制继续进行
  4408. }
  4409. },
  4410. // 新增方法:iOS录制期间的安全拍照
  4411. safeTakePhotoForIOSRecording() {
  4412. // 防止重复调用
  4413. if (this.isIOSPhotoCapturing) {
  4414. return;
  4415. }
  4416. this.isIOSPhotoCapturing = true;
  4417. try {
  4418. this.cameraContext.takePhoto({
  4419. quality: 'low',
  4420. success: (res) => {
  4421. const tempFilePath = res && res.tempImagePath;
  4422. if (!tempFilePath) {
  4423. this.isIOSPhotoCapturing = false;
  4424. return;
  4425. }
  4426. // 异步处理文件读取,不阻塞主流程
  4427. setTimeout(() => {
  4428. uni.getFileSystemManager().readFile({
  4429. filePath: tempFilePath,
  4430. encoding: 'base64',
  4431. success: (r) => {
  4432. try {
  4433. const base64Image = r.data;
  4434. if (this.personDetectionSocket && this.personDetectionSocket.readyState === 1) {
  4435. this.personDetectionSocket.send({
  4436. data: JSON.stringify({
  4437. type: 'person_detection',
  4438. image_data: base64Image,
  4439. platform: 'ios_recording' // 标记为iOS录制期间的检测
  4440. })
  4441. });
  4442. console.log('iOS录制期间检测图片已发送');
  4443. }
  4444. } catch (sendError) {
  4445. console.warn('iOS录制期间发送检测图片失败:', sendError);
  4446. } finally {
  4447. this.isIOSPhotoCapturing = false;
  4448. }
  4449. },
  4450. fail: (readError) => {
  4451. console.warn('iOS录制期间读取图片文件失败:', readError);
  4452. this.isIOSPhotoCapturing = false;
  4453. }
  4454. });
  4455. }, 100); // 延迟100ms处理,避免阻塞录制
  4456. },
  4457. fail: (photoError) => {
  4458. console.warn('iOS录制期间拍照失败:', photoError);
  4459. this.isIOSPhotoCapturing = false;
  4460. }
  4461. });
  4462. } catch (error) {
  4463. console.warn('iOS录制期间拍照异常:', error);
  4464. this.isIOSPhotoCapturing = false;
  4465. }
  4466. },
  4467. cleanupPersonDetectionWebSocket() {
  4468. if (this.personDetectionInterval) {
  4469. clearInterval(this.personDetectionInterval);
  4470. this.personDetectionInterval = null;
  4471. }
  4472. if (this.personDetectionSocket) {
  4473. try {
  4474. this.personDetectionSocket.close();
  4475. } catch (error) {
  4476. console.error('Error closing WebSocket:', error);
  4477. }
  4478. this.personDetectionSocket = null;
  4479. }
  4480. },
  4481. handlePersonDetectionResult(data) {
  4482. /*console.log(data.data.detection);
  4483. console.log(data.data.identity.status);//identity_verified */
  4484. /* // 首先检查是否有人
  4485. if (!data.data.detection.has_person) {
  4486. this.showPageWarning = true;
  4487. uni.showToast({
  4488. title: '请勿离开摄像头',
  4489. icon: 'none',
  4490. duration: 3000
  4491. });
  4492. // 添加震动反馈
  4493. uni.vibrateLong({
  4494. success: function () {
  4495. console.log('Vibration successful');
  4496. },
  4497. fail: function (err) {
  4498. console.error('Vibration failed:', err);
  4499. }
  4500. });
  4501. setTimeout(() => {
  4502. this.showPageWarning = false;
  4503. }, 3000);
  4504. }
  4505. // 然后检查身份验证状态
  4506. else */
  4507. if (data.data.identity.status == "no_face") {
  4508. this.showPageWarning = true;
  4509. uni.showToast({/* data.data.identity.message */
  4510. title: '请保持面部完整出现在镜头内',
  4511. icon: 'none',
  4512. duration: 3000
  4513. });
  4514. uni.vibrateLong({
  4515. success: function () {
  4516. console.log('Vibration successful');
  4517. },
  4518. fail: function (err) {
  4519. console.error('Vibration failed:', err);
  4520. }
  4521. });
  4522. setTimeout(() => {
  4523. this.showPageWarning = false;
  4524. }, 3000);
  4525. }else if(data.data.identity.status == "multiple_faces"){
  4526. this.showPageDRWarning = true;
  4527. uni.showToast({/* data.data.identity.message */
  4528. title: '请保持独立面试环境',
  4529. icon: 'none',
  4530. duration: 3000
  4531. });
  4532. uni.vibrateLong({
  4533. success: function () {
  4534. console.log('Vibration successful');
  4535. },
  4536. fail: function (err) {
  4537. console.error('Vibration failed:', err);
  4538. }
  4539. });
  4540. setTimeout(() => {
  4541. this.showPageDRWarning = false;
  4542. }, 3000);
  4543. }
  4544. },
  4545. // 进入追问模式
  4546. enterFollowUpMode() {
  4547. this.isFollowUpMode = true;
  4548. this.mainQuestionIndex = this.currentVideoIndex; // 保存当前主问题的索引
  4549. console.log('进入追问模式,保存主问题索引:', this.mainQuestionIndex);
  4550. },
  4551. // 修改 handleFollowUpQuestion 方法
  4552. async handleFollowUpQuestion(questionData) {
  4553. console.log('处理追问:', questionData);
  4554. this.enterFollowUpMode();
  4555. console.log('处理追问问题数据:', questionData);
  4556. // 检查数据完整性
  4557. if (!questionData || !questionData.follow_up_voice_url || !questionData.follow_up_question) {
  4558. console.error('追问数据不完整:', questionData);
  4559. uni.showToast({
  4560. title: '获取追问数据失败',
  4561. icon: 'none'
  4562. });
  4563. return;
  4564. }
  4565. // 保存父问题
  4566. this.parentQuestion = questionData.original_question || '';
  4567. // 确保音频URL正确
  4568. let audioUrl = '';
  4569. if (questionData.follow_up_voice_url.direct_url) {
  4570. audioUrl = questionData.follow_up_voice_url.direct_url;
  4571. } else if (questionData.follow_up_voice_url.file_url) {
  4572. audioUrl = questionData.follow_up_voice_url.file_url;
  4573. }
  4574. // 确保音频URL是完整的
  4575. if (!audioUrl.startsWith('http')) {
  4576. audioUrl = apiBaseUrl + audioUrl;
  4577. }
  4578. console.log('处理后的音频URL:', audioUrl);
  4579. // 创建追问问题对象
  4580. const followUpQuestionObj = {
  4581. id: Date.now(),
  4582. question: questionData.follow_up_question.trim(), // 去除可能的空白字符
  4583. audioUrl: audioUrl,
  4584. originalQuestion: questionData.original_question,
  4585. sessionId: questionData.session_id
  4586. };
  4587. console.log('创建的追问问题对象:', followUpQuestionObj);
  4588. // 添加到追问问题列表
  4589. this.followUpQuestions.push(followUpQuestionObj);
  4590. this.currentFollowUpIndex = this.followUpQuestions.length - 1;
  4591. // 更新当前追问问题
  4592. this.followUpQuestion = followUpQuestionObj.question;
  4593. this.followUpAudioUrl = followUpQuestionObj.audioUrl;
  4594. // 标记当前是追问问题
  4595. this.isFollowUpQuestion = true;
  4596. this.currentFollowUpQuestion = followUpQuestionObj;
  4597. // 显示字幕
  4598. this.showSubtitle(followUpQuestionObj.question, true);
  4599. // 等待音频播放完成
  4600. try {
  4601. await this.playFollowUpAudio();
  4602. console.log('音频播放完成,准备录制回答');
  4603. // 开始准备录制回答
  4604. this.prepareToAnswer(true, followUpQuestionObj);
  4605. } catch (error) {
  4606. console.error('音频播放失败:', error);
  4607. // 即使音频播放失败,也继续准备录制回答
  4608. this.prepareToAnswer(true, followUpQuestionObj);
  4609. }
  4610. },
  4611. // 播放追问音频
  4612. async playFollowUpAudio() {
  4613. return new Promise((resolve, reject) => {
  4614. console.log('开始播放追问音频, URL:', this.followUpAudioUrl);
  4615. // 检查音频URL
  4616. if (!this.followUpAudioUrl) {
  4617. console.error('没有音频URL');
  4618. reject(new Error('没有音频URL'));
  4619. return;
  4620. }
  4621. // 显示加载提示
  4622. uni.showLoading({
  4623. title: '思考中...'
  4624. });
  4625. // 停止并销毁之前的音频实例
  4626. this.stopAndDestroyAudio();
  4627. try {
  4628. // 获取系统信息,判断平台
  4629. const systemInfo = uni.getSystemInfoSync();
  4630. const isIOS = systemInfo.platform === 'ios';
  4631. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  4632. console.log('平台信息:', { isIOS, isMiniProgram, platform: systemInfo.platform });
  4633. // 根据平台选择播放方式
  4634. if (isMiniProgram && isIOS) {
  4635. // iOS小程序环境使用BackgroundAudioManager
  4636. this.playIOSMiniProgramFollowUpAudio(resolve, reject);
  4637. } else if (isIOS) {
  4638. // iOS App/H5环境使用专门的iOS播放方法
  4639. this.playIOSFollowUpAudio(resolve, reject);
  4640. } else {
  4641. // 其他平台使用通用方法
  4642. this.playGeneralFollowUpAudio(resolve, reject);
  4643. }
  4644. } catch (error) {
  4645. console.error('创建或播放音频失败:', error);
  4646. this.isAudioPlaying = false;
  4647. uni.hideLoading();
  4648. uni.showToast({
  4649. title: '音频播放失败',
  4650. icon: 'none'
  4651. });
  4652. this.stopAndDestroyAudio();
  4653. reject(error);
  4654. }
  4655. });
  4656. },
  4657. // iOS小程序专用音频播放方法
  4658. playIOSMiniProgramFollowUpAudio(resolve, reject) {
  4659. try {
  4660. const bgAudio = uni.getBackgroundAudioManager();
  4661. // iOS 小程序需要设置 title 等元信息,否则可能不触发播放
  4662. bgAudio.title = '追问语音';
  4663. bgAudio.epname = 'Follow Up Question';
  4664. bgAudio.singer = 'Interviewer';
  4665. // 先绑定事件再设置 src,防止错过 canplay 回调
  4666. bgAudio.onCanplay(() => {
  4667. try {
  4668. bgAudio.play();
  4669. } catch (e) {
  4670. console.warn('iOS小程序调用 play 失败,稍后重试', e);
  4671. setTimeout(() => {
  4672. try { bgAudio.play(); } catch (_) {}
  4673. }, 50);
  4674. }
  4675. });
  4676. bgAudio.onPlay(() => {
  4677. console.log('iOS小程序追问音频开始播放');
  4678. uni.hideLoading();
  4679. this.isAudioPlaying = true;
  4680. });
  4681. bgAudio.onEnded(() => {
  4682. console.log('iOS小程序追问音频播放完成');
  4683. this.isAudioPlaying = false;
  4684. try { bgAudio.stop(); } catch (_) {}
  4685. resolve();
  4686. // 延迟清理资源,确保不会影响播放完成的回调
  4687. setTimeout(() => {
  4688. this.stopAndDestroyAudio();
  4689. // 音频播放完成后,自动准备录制回答
  4690. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  4691. }, 100);
  4692. });
  4693. bgAudio.onError((err) => {
  4694. console.error('iOS小程序追问音频播放错误:', err);
  4695. this.isAudioPlaying = false;
  4696. uni.hideLoading();
  4697. try { bgAudio.stop(); } catch (_) {}
  4698. // 尝试备选方案
  4699. this.fallbackIOSFollowUpAudio(resolve, reject);
  4700. });
  4701. bgAudio.autoplay = true;
  4702. bgAudio.src = this.followUpAudioUrl;
  4703. } catch (error) {
  4704. console.error('iOS小程序音频播放失败:', error);
  4705. this.fallbackIOSFollowUpAudio(resolve, reject);
  4706. }
  4707. },
  4708. // iOS App/H5专用音频播放方法
  4709. playIOSFollowUpAudio(resolve, reject) {
  4710. try {
  4711. // 创建新的音频播放器
  4712. this.audioContext = uni.createInnerAudioContext();
  4713. // 配置播放器 - iOS特殊配置
  4714. this.audioContext.autoplay = true;
  4715. this.audioContext.obeyMuteSwitch = false; // 不遵循静音开关,确保播放
  4716. this.audioContext.volume = 1.0;
  4717. // 设置音频源
  4718. this.audioContext.src = this.followUpAudioUrl;
  4719. // 监听事件
  4720. this.audioContext.onPlay(() => {
  4721. console.log('iOS追问音频开始播放');
  4722. uni.hideLoading();
  4723. this.isAudioPlaying = true;
  4724. });
  4725. this.audioContext.onEnded(() => {
  4726. console.log('iOS追问音频播放完成');
  4727. this.isAudioPlaying = false;
  4728. resolve();
  4729. // 延迟清理资源,确保不会影响播放完成的回调
  4730. setTimeout(() => {
  4731. this.stopAndDestroyAudio();
  4732. // 音频播放完成后,自动准备录制回答
  4733. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  4734. }, 100);
  4735. });
  4736. this.audioContext.onError((err) => {
  4737. console.error('iOS追问音频播放错误:', err);
  4738. this.isAudioPlaying = false;
  4739. uni.hideLoading();
  4740. // 尝试备选方案
  4741. this.fallbackIOSFollowUpAudio(resolve, reject);
  4742. });
  4743. this.audioContext.onWaiting(() => {
  4744. console.log('iOS追问音频加载中...');
  4745. });
  4746. this.audioContext.onCanplay(() => {
  4747. console.log('iOS追问音频准备就绪');
  4748. });
  4749. // 设置超时保护
  4750. setTimeout(() => {
  4751. if (this.isAudioPlaying && this.audioContext) {
  4752. console.log('iOS追问音频播放超时,尝试备选方案');
  4753. this.fallbackIOSFollowUpAudio(resolve, reject);
  4754. }
  4755. }, 10000); // 10秒超时
  4756. } catch (error) {
  4757. console.error('创建iOS追问音频播放器失败:', error);
  4758. this.fallbackIOSFollowUpAudio(resolve, reject);
  4759. }
  4760. },
  4761. // iOS备选音频播放方案
  4762. fallbackIOSFollowUpAudio(resolve, reject) {
  4763. console.log('iOS追问音频备选播放方案');
  4764. try {
  4765. // 停止当前播放器
  4766. this.stopAndDestroyAudio();
  4767. // 创建新的音频播放器
  4768. this.audioContext = uni.createInnerAudioContext();
  4769. // 使用更保守的设置
  4770. this.audioContext.autoplay = true;
  4771. this.audioContext.obeyMuteSwitch = true; // 遵循静音开关
  4772. this.audioContext.volume = 0.8;
  4773. // 直接使用原URL
  4774. this.audioContext.src = this.followUpAudioUrl;
  4775. // 设置简化的事件监听
  4776. this.audioContext.onPlay(() => {
  4777. console.log('iOS备选方案:追问音频开始播放');
  4778. uni.hideLoading();
  4779. this.isAudioPlaying = true;
  4780. });
  4781. this.audioContext.onEnded(() => {
  4782. console.log('iOS备选方案:追问音频播放结束');
  4783. this.isAudioPlaying = false;
  4784. resolve();
  4785. setTimeout(() => {
  4786. this.stopAndDestroyAudio();
  4787. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  4788. }, 100);
  4789. });
  4790. this.audioContext.onError((err) => {
  4791. console.error('iOS备选方案:追问音频播放错误:', err);
  4792. this.useIOSFollowUpTextFallback(resolve, reject);
  4793. });
  4794. // 设置超时,如果5秒内没有开始播放,使用文本回退
  4795. setTimeout(() => {
  4796. if (this.isAudioPlaying && this.audioContext) {
  4797. console.log('iOS追问音频播放超时,使用文本回退');
  4798. this.useIOSFollowUpTextFallback(resolve, reject);
  4799. }
  4800. }, 5000);
  4801. } catch (e) {
  4802. console.error('iOS备选追问音频播放失败:', e);
  4803. this.useIOSFollowUpTextFallback(resolve, reject);
  4804. }
  4805. },
  4806. // iOS文本回退方案
  4807. useIOSFollowUpTextFallback(resolve, reject) {
  4808. console.log('iOS追问音频使用文本回退方案');
  4809. this.isAudioPlaying = false;
  4810. uni.hideLoading();
  4811. // 显示文本提示
  4812. uni.showToast({
  4813. title: '音频播放失败,请查看文字问题',
  4814. icon: 'none',
  4815. duration: 3000
  4816. });
  4817. // 直接解析,继续流程
  4818. resolve();
  4819. setTimeout(() => {
  4820. this.stopAndDestroyAudio();
  4821. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  4822. }, 100);
  4823. },
  4824. // 通用音频播放方法(非iOS平台)
  4825. playGeneralFollowUpAudio(resolve, reject) {
  4826. try {
  4827. // 创建新的音频上下文
  4828. const innerAudioContext = uni.createInnerAudioContext();
  4829. this.audioContext = innerAudioContext;
  4830. // 设置音频属性
  4831. innerAudioContext.autoplay = true;
  4832. innerAudioContext.obeyMuteSwitch = false;
  4833. innerAudioContext.volume = 1.0;
  4834. // 监听音频加载状态
  4835. innerAudioContext.onCanplay(() => {
  4836. console.log('音频可以播放');
  4837. uni.hideLoading();
  4838. this.isAudioPlaying = true;
  4839. });
  4840. // 监听音频播放完成
  4841. innerAudioContext.onEnded(() => {
  4842. console.log('追问音频播放完成');
  4843. this.isAudioPlaying = false;
  4844. resolve();
  4845. // 延迟清理资源,确保不会影响播放完成的回调
  4846. setTimeout(() => {
  4847. this.stopAndDestroyAudio();
  4848. // 音频播放完成后,自动准备录制回答
  4849. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  4850. }, 100);
  4851. });
  4852. // 监听音频播放错误
  4853. innerAudioContext.onError((res) => {
  4854. console.error('音频播放错误:', res);
  4855. this.isAudioPlaying = false;
  4856. uni.hideLoading();
  4857. uni.showToast({
  4858. title: '音频播放失败',
  4859. icon: 'none'
  4860. });
  4861. reject(res);
  4862. this.stopAndDestroyAudio();
  4863. });
  4864. // 设置音频源
  4865. console.log('设置音频源:', this.followUpAudioUrl);
  4866. innerAudioContext.src = this.followUpAudioUrl;
  4867. } catch (error) {
  4868. console.error('创建或播放音频失败:', error);
  4869. this.isAudioPlaying = false;
  4870. uni.hideLoading();
  4871. uni.showToast({
  4872. title: '音频播放失败',
  4873. icon: 'none'
  4874. });
  4875. this.stopAndDestroyAudio();
  4876. reject(error);
  4877. }
  4878. },
  4879. // 停止并销毁音频
  4880. stopAndDestroyAudio() {
  4881. if (!this.audioContext) {
  4882. return;
  4883. }
  4884. const ctx = this.audioContext;
  4885. this.audioContext = null; // 先清除引用
  4886. try {
  4887. if (this.isAudioPlaying) {
  4888. ctx.stop();
  4889. }
  4890. } catch (error) {
  4891. console.error('停止音频播放失败:', error);
  4892. }
  4893. try {
  4894. ctx.destroy();
  4895. } catch (error) {
  4896. console.error('销毁音频实例失败:', error);
  4897. }
  4898. this.isAudioPlaying = false;
  4899. },
  4900. // 显示字幕
  4901. showSubtitle(text, isFollowUp = true) {
  4902. console.log('显示字幕:', text);
  4903. this.subtitleText = text;
  4904. this.showSubtitleText = true;
  4905. this.isFollowUpQuestion = isFollowUp;
  4906. // 确保字幕显示在界面上
  4907. this.$nextTick(() => {
  4908. // 如果需要,可以添加字幕动画效果
  4909. setTimeout(() => {
  4910. this.showSubtitleText = false;
  4911. }, 8000); // 8秒后隐藏字幕
  4912. });
  4913. },
  4914. // 显示问题对话框
  4915. showQuestionDialog() {
  4916. if (this.followUpQuestion) {
  4917. uni.showModal({
  4918. title: '追问问题',
  4919. content: this.followUpQuestion,
  4920. showCancel: false,
  4921. success: () => {
  4922. // 准备开始回答
  4923. this.prepareToAnswer(true, this.followUpQuestions[this.currentFollowUpIndex]);
  4924. }
  4925. });
  4926. }
  4927. },
  4928. // 显示面试官思考中loading
  4929. showThinkingLoading() {
  4930. this.isThinking = true;
  4931. uni.showLoading({
  4932. title: '面试官正在思考中',
  4933. mask: false
  4934. });
  4935. },
  4936. // 隐藏面试官思考中loading
  4937. hideThinkingLoading() {
  4938. this.isThinking = false;
  4939. uni.hideLoading();
  4940. if (this.thinkingTimer) {
  4941. clearTimeout(this.thinkingTimer);
  4942. this.thinkingTimer = null;
  4943. }
  4944. },
  4945. // 处理视频完成
  4946. handleVideoCompletion(jobId) {
  4947. this.isVideoSwitching = false; // 重置切换状态锁
  4948. // 延迟执行跳转,确保资源清理完成
  4949. // 如果职位ID为9,跳转到interview-question页面
  4950. if (jobId === 9) {
  4951. uni.navigateTo({
  4952. url: '/pages/interview-question/interview-question',
  4953. success: () => {
  4954. console.log('成功跳转到interview-question页面');
  4955. },
  4956. fail: (err) => {
  4957. console.error('跳转到interview-question页面失败:', err);
  4958. // 如果跳转失败,尝试使用redirectTo
  4959. uni.redirectTo({
  4960. url: '/pages/interview-question/interview-question',
  4961. fail: (redirectErr) => {
  4962. console.error('重定向到interview-question页面也失败:', redirectErr);
  4963. // 如果所有跳转方式都失败,尝试返回上一页
  4964. uni.navigateBack({
  4965. delta: 1
  4966. });
  4967. }
  4968. });
  4969. }
  4970. });
  4971. } else {
  4972. // 获取本地存储的配置数据
  4973. let configData = {};
  4974. try {
  4975. const configStr = uni.getStorageSync('configData');
  4976. if (configStr && configStr.trim()) {
  4977. configData = JSON.parse(configStr);
  4978. }
  4979. } catch (error) {
  4980. console.error('解析configData失败:', error);
  4981. configData = {};
  4982. }
  4983. console.log('获取到的配置数据:', configData);
  4984. // 获取问题形式开关配置
  4985. const questionFormSwitches = configData?.question_form_switches || {};
  4986. // 检查是否启用了选择题相关功能
  4987. const hasChoiceQuestions = questionFormSwitches.enable_fill_blank ||
  4988. questionFormSwitches.enable_image_choice ||
  4989. questionFormSwitches.enable_multiple_choice ||
  4990. questionFormSwitches.enable_single_choice;
  4991. // 检查是否启用了评分题功能
  4992. const hasScoringQuestions = questionFormSwitches.enable_scoring_questions;
  4993. //肢体检测
  4994. const hasPostureDetection = configData?.enable_posture_check;
  4995. //候选人提问
  4996. const hasCandidateQuestions = questionFormSwitches.enable_candidate_questions;
  4997. let targetUrl = '/pages/camera/camera'; // 默认跳转到camera页面
  4998. let pageName = 'camera页面';
  4999. console.log('当前配置数据:', hasScoringQuestions);
  5000. // 根据配置决定跳转页面
  5001. if (hasChoiceQuestions || hasScoringQuestions) {
  5002. // 选择题或评分题任一开启,跳转到camera页面
  5003. targetUrl = '/pages/camera/camera';
  5004. pageName = 'camera页面';
  5005. }else if(hasPostureDetection){
  5006. targetUrl = '/pages/posture-guide/posture-guide';
  5007. pageName = 'posture-guide页面';
  5008. }else if(hasCandidateQuestions){
  5009. targetUrl = '/pages/interview-question/interview-question';
  5010. pageName = 'interview-question页面';
  5011. } else {
  5012. // 如果启用了选择题相关功能,跳转到camera页面
  5013. targetUrl = '/pages/success/success';
  5014. pageName = 'camera页面';
  5015. }
  5016. console.log('根据配置跳转到:', targetUrl, '配置项:', questionFormSwitches);
  5017. // 执行页面跳转
  5018. uni.navigateTo({
  5019. url: targetUrl,
  5020. success: () => {
  5021. console.log(`成功跳转到${pageName}`);
  5022. },
  5023. fail: (err) => {
  5024. console.error(`跳转到${pageName}失败:`, err);
  5025. // 如果跳转失败,尝试使用redirectTo
  5026. uni.redirectTo({
  5027. url: targetUrl,
  5028. fail: (redirectErr) => {
  5029. console.error(`重定向到${pageName}也失败:`, redirectErr);
  5030. // 最后尝试使用switchTab(如果是tabBar页面)
  5031. uni.switchTab({
  5032. url: targetUrl,
  5033. fail: (switchErr) => {
  5034. console.error('所有跳转方式都失败:', switchErr);
  5035. // 如果所有跳转方式都失败,尝试返回上一页
  5036. uni.navigateBack({
  5037. delta: 1
  5038. });
  5039. }
  5040. });
  5041. }
  5042. });
  5043. }
  5044. });
  5045. }
  5046. },
  5047. },
  5048. computed: {
  5049. // 计算进度比例
  5050. progressRatio() {
  5051. return this.recordingTimerCount / this.maxRecordingTime;
  5052. },
  5053. // 右半圆旋转角度
  5054. half1Turn() {
  5055. // 初始状态为0度,小于50%时,右半圆按比例旋转
  5056. if (this.progressRatio <= 0.5) {
  5057. return this.progressRatio * 2 * 180;
  5058. } else {
  5059. // 大于50%时,右半圆固定在180度
  5060. return 180;
  5061. }
  5062. },
  5063. // 左半圆旋转角度
  5064. half2Turn() {
  5065. // 小于50%时,左半圆固定在0度
  5066. if (this.progressRatio <= 0.5) {
  5067. return 0;
  5068. } else {
  5069. // 大于50%时,左半圆按超出50%的比例旋转
  5070. return (this.progressRatio - 0.5) * 2 * 180;
  5071. }
  5072. },
  5073. // 修改进度百分比的计算方式
  5074. progressPercent() {
  5075. // 将时间进度转换为百分比(0-100)
  5076. return Math.min(Math.round((this.recordingTimerCount / this.maxRecordingTime) * 100), 100);
  5077. },
  5078. // 添加总题数计算属性
  5079. totalQuestions() {
  5080. // 获取问题总数,不包括介绍视频
  5081. return this.questions.length || 5; // 如果没有问题数据,默认显示5
  5082. }
  5083. }
  5084. }
  5085. </script>
  5086. <style scoped>
  5087. .subtitle-container {
  5088. position: fixed;
  5089. bottom: 20%;
  5090. left: 0;
  5091. right: 0;
  5092. display: flex;
  5093. justify-content: center;
  5094. align-items: center;
  5095. z-index: 1000;
  5096. }
  5097. .subtitle-text {
  5098. background-color: rgba(0, 0, 0, 0.7);
  5099. color: white;
  5100. padding: 10px 20px;
  5101. border-radius: 5px;
  5102. font-size: 16px;
  5103. max-width: 80%;
  5104. text-align: center;
  5105. animation: fadeIn 0.5s ease-in-out;
  5106. }
  5107. @keyframes fadeIn {
  5108. from {
  5109. opacity: 0;
  5110. transform: translateY(20px);
  5111. }
  5112. to {
  5113. opacity: 1;
  5114. transform: translateY(0);
  5115. }
  5116. }
  5117. .identity-verify-container {
  5118. padding: 0;
  5119. max-width: 100%;
  5120. margin: 0 auto;
  5121. height: 100vh;
  5122. display: flex;
  5123. flex-direction: column;
  5124. background-color: #f5f5f5;
  5125. transition: all 0.3s ease;
  5126. position: relative;
  5127. }
  5128. .page-warning {
  5129. animation: page-warning-flash 1s ease-in-out infinite;
  5130. }
  5131. @keyframes page-warning-flash {
  5132. 0% {
  5133. background-color: rgba(255, 0, 0, 0.2);
  5134. box-shadow: 0 0 20px rgba(255, 0, 0, 0.3);
  5135. }
  5136. 50% {
  5137. background-color: rgba(255, 0, 0, 0.1);
  5138. box-shadow: 0 0 10px rgba(255, 0, 0, 0.2);
  5139. }
  5140. 100% {
  5141. background-color: rgba(255, 0, 0, 0.2);
  5142. box-shadow: 0 0 20px rgba(255, 0, 0, 0.3);
  5143. }
  5144. }
  5145. .digital-human-container {
  5146. position: relative;
  5147. width: 100%;
  5148. height: 100vh;
  5149. overflow: hidden;
  5150. background-color: #f0f0f0;
  5151. }
  5152. .digital-human-video {
  5153. width: 100%;
  5154. height: 100%;
  5155. margin-top: 32px;
  5156. display: flex;
  5157. justify-content: center;
  5158. align-items: center;
  5159. }
  5160. /* 用户摄像头容器样式 */
  5161. .user-camera-container {
  5162. position: absolute;
  5163. top: 100px;
  5164. right: 5px;
  5165. width: 110px;
  5166. height: 160px;
  5167. border-radius: 4px;
  5168. overflow: hidden;
  5169. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  5170. z-index: 20;
  5171. transition: all 0.3s ease;
  5172. }
  5173. .camera-warning {
  5174. animation: camera-warning-flash 1s ease-in-out infinite;
  5175. border: 4px solid #ff0000;
  5176. }
  5177. @keyframes camera-warning-flash {
  5178. 0% {
  5179. border-color: #ff0000;
  5180. box-shadow: 0 0 15px rgba(255, 0, 0, 0.8);
  5181. }
  5182. 50% {
  5183. border-color: rgba(255, 0, 0, 0.3);
  5184. box-shadow: 0 0 5px rgba(255, 0, 0, 0.4);
  5185. }
  5186. 100% {
  5187. border-color: #ff0000;
  5188. box-shadow: 0 0 15px rgba(255, 0, 0, 0.8);
  5189. }
  5190. }
  5191. /* 用户摄像头视频样式 */
  5192. .user-camera-video {
  5193. width: 100%;
  5194. height: 100%;
  5195. object-fit: cover;
  5196. background-color: #333;
  5197. }
  5198. .video-player {
  5199. width: 100%;
  5200. height: 100%;
  5201. object-fit: cover;
  5202. outline: none; /* 移除视频获得焦点时的轮廓 */
  5203. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  5204. }
  5205. /* 隐藏视频控制条 */
  5206. video::-webkit-media-controls {
  5207. display: none !important;
  5208. }
  5209. video::-webkit-media-controls-enclosure {
  5210. display: none !important;
  5211. }
  5212. video::-webkit-media-controls-panel {
  5213. display: none !important;
  5214. }
  5215. video::-webkit-media-controls-play-button {
  5216. display: none !important;
  5217. }
  5218. video::-webkit-media-controls-timeline {
  5219. display: none !important;
  5220. }
  5221. video::-webkit-media-controls-current-time-display {
  5222. display: none !important;
  5223. }
  5224. video::-webkit-media-controls-time-remaining-display {
  5225. display: none !important;
  5226. }
  5227. video::-webkit-media-controls-mute-button {
  5228. display: none !important;
  5229. }
  5230. video::-webkit-media-controls-volume-slider {
  5231. display: none !important;
  5232. }
  5233. video::-webkit-media-controls-fullscreen-button {
  5234. display: none !important;
  5235. }
  5236. /* 修改字幕覆盖层样式,使其与图片中的样式一致 */
  5237. .subtitle-overlay {
  5238. position: absolute;
  5239. bottom: 180px;
  5240. left: 10%;
  5241. width: 80%;
  5242. padding: 0; /* 移除内边距,由内部元素控制 */
  5243. border-radius: 15px;
  5244. background-color: rgba(255, 255, 255, 0.9);
  5245. color: #333;
  5246. text-align: left;
  5247. font-size: 16px;
  5248. line-height: 1.5;
  5249. z-index: 10;
  5250. margin: 0 auto;
  5251. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  5252. display: flex;
  5253. flex-direction: column;
  5254. overflow: hidden; /* 确保圆角效果 */
  5255. }
  5256. /* 添加题号header样式 */
  5257. .subtitle-header {
  5258. /* background-color: rgba(0, 0, 0, 0.05); */
  5259. padding: 8px 20px;
  5260. /* border-bottom: 1px solid rgba(0, 0, 0, 0.05); */
  5261. }
  5262. .question-tag {
  5263. font-size: 14px;
  5264. color: #666;
  5265. font-weight: 500;
  5266. }
  5267. /* 调整主字幕样式 */
  5268. .subtitle-main {
  5269. padding: 15px 20px ;
  5270. font-weight: 500;
  5271. }
  5272. /* 移除之前的题号显示器 */
  5273. .question-counter {
  5274. display: none;
  5275. }
  5276. .control-panel {
  5277. padding: 15px;
  5278. display: flex;
  5279. justify-content: center;
  5280. }
  5281. .control-button {
  5282. padding: 10px 20px;
  5283. background-color: #4CAF50;
  5284. color: white;
  5285. border: none;
  5286. border-radius: 4px;
  5287. cursor: pointer;
  5288. }
  5289. .loading {
  5290. text-align: center;
  5291. margin: 20px 0;
  5292. font-size: 16px;
  5293. }
  5294. .response-container {
  5295. margin-top: 20px;
  5296. padding: 0 20px;
  5297. display: none; /* 默认隐藏调试信息 */
  5298. }
  5299. /* 当showDebugInfo为true时显示 */
  5300. .showDebugInfo .response-container {
  5301. display: block;
  5302. }
  5303. .response-item {
  5304. padding: 10px;
  5305. border: 1px solid #eee;
  5306. border-radius: 4px;
  5307. margin-bottom: 10px;
  5308. background-color: #f9f9f9;
  5309. }
  5310. .response-content {
  5311. display: flex;
  5312. flex-direction: column;
  5313. }
  5314. .answer-button-container {
  5315. position: absolute;
  5316. bottom: 50px; /* 将按钮放在底部而不是75%的位置 */
  5317. left: 50%;
  5318. transform: translateX(-50%); /* 只在X轴上平移,保持水平居中 */
  5319. z-index: 20;
  5320. }
  5321. /* 修改回答按钮样式 */
  5322. .answer-button {
  5323. width: 120px;
  5324. height: 40px; /* 改为矩形按钮 */
  5325. border-radius: 20px; /* 圆角矩形 */
  5326. background-color: #ffffff; /* 白色背景 */
  5327. color: #333; /* 深色文字 */
  5328. font-size: 16px;
  5329. border: none;
  5330. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  5331. display: flex;
  5332. justify-content: center;
  5333. align-items: center;
  5334. cursor: pointer;
  5335. animation: none; /* 移除脉动动画 */
  5336. }
  5337. .stop-recording-button-container {
  5338. position: absolute;
  5339. bottom: 50px; /* 统一与其他按钮的位置 */
  5340. left: 50%;
  5341. transform: translateX(-50%);
  5342. z-index: 20;
  5343. }
  5344. .stop-recording-button {
  5345. width: 120px;
  5346. height: 40px; /* 改为矩形按钮 */
  5347. border-radius: 20px; /* 圆角矩形 */
  5348. background-color: #0039b3; /* 白色背景 */
  5349. color: #fff;/* 深色文字 */
  5350. font-size: 16px;
  5351. border: none;
  5352. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  5353. display: flex;
  5354. justify-content: center;
  5355. align-items: center;
  5356. cursor: pointer;
  5357. animation: none; /* 移除脉动动画 */
  5358. }
  5359. .recording-indicator {
  5360. position: fixed;
  5361. bottom: 20px;
  5362. left: 17%;
  5363. transform: translateX(-50%);
  5364. display: flex;
  5365. flex-direction: column;
  5366. align-items: center;
  5367. /* background-color: rgba(0, 0, 0, 0.6); */
  5368. padding: 10px 15px;
  5369. border-radius: 10px;
  5370. z-index: 100;
  5371. }
  5372. .timer-text {
  5373. color: #05dc8b;
  5374. font-size: 14px;
  5375. margin-top: 5px;
  5376. }
  5377. .start-recording-button-container {
  5378. position: absolute;
  5379. bottom: 50px; /* 统一与其他按钮的位置 */
  5380. left: 50%;
  5381. transform: translateX(-50%);
  5382. z-index: 101;
  5383. }
  5384. .start-recording-button {
  5385. width: 120px;
  5386. height: 40px; /* 改为矩形按钮 */
  5387. border-radius: 20px; /* 圆角矩形 */
  5388. background-color: #ffffff; /* 白色背景 */
  5389. color: #333; /* 深色文字 */
  5390. font-size: 16px;
  5391. border: none;
  5392. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  5393. display: flex;
  5394. justify-content: center;
  5395. align-items: center;
  5396. cursor: pointer;
  5397. animation: none; /* 移除脉动动画 */
  5398. }
  5399. .retry-button-container {
  5400. position: absolute;
  5401. bottom: 30px; /* 统一与其他按钮的位置 */
  5402. left: 50%;
  5403. transform: translateX(-50%);
  5404. z-index: 20;
  5405. }
  5406. .retry-button {
  5407. padding: 10px 20px;
  5408. background-color: #ffffff; /* 白色背景 */
  5409. color: #333; /* 深色文字 */
  5410. font-size: 16px;
  5411. border: none;
  5412. border-radius: 20px; /* 圆角矩形 */
  5413. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  5414. cursor: pointer;
  5415. }
  5416. .retry-button:hover {
  5417. background-color: #2980b9;
  5418. }
  5419. /* 录制时长显示 */
  5420. .recording-timer {
  5421. position: absolute;
  5422. top: 20px;
  5423. left: 130px; /* 放在录制指示器旁边 */
  5424. background-color: rgba(0, 0, 0, 0.6);
  5425. padding: 5px 10px;
  5426. border-radius: 15px;
  5427. z-index: 20;
  5428. display: flex;
  5429. flex-direction: column;
  5430. }
  5431. .timer-text {
  5432. color: #000;
  5433. font-size: 14px;
  5434. font-family: monospace; /* 使用等宽字体,使时间显示更稳定 */
  5435. }
  5436. .remaining-time {
  5437. color: white;
  5438. font-size: 12px;
  5439. margin-top: 2px;
  5440. }
  5441. .remaining-time.warning {
  5442. color: #ff6b6b; /* 剩余时间少时显示红色 */
  5443. animation: blink 1s infinite; /* 使用闪烁动画提醒用户 */
  5444. }
  5445. /* 添加上传状态指示器 */
  5446. .upload-status-indicator {
  5447. position: absolute;
  5448. top: 20px;
  5449. left: 130px; /* 放在录制指示器旁边 */
  5450. background-color: rgba(0, 0, 0, 0.6);
  5451. padding: 5px 10px;
  5452. border-radius: 15px;
  5453. z-index: 20;
  5454. }
  5455. .upload-status-content {
  5456. display: flex;
  5457. align-items: center;
  5458. }
  5459. .upload-status-icon {
  5460. width: 12px;
  5461. height: 12px;
  5462. border-radius: 50%;
  5463. margin-right: 8px;
  5464. }
  5465. .upload-status-text {
  5466. color: white;
  5467. font-size: 14px;
  5468. }
  5469. .uploading {
  5470. background-color: #e74c3c;
  5471. }
  5472. /* 添加倒计时蒙层样式 */
  5473. .countdown-overlay {
  5474. position: fixed;
  5475. top: 0;
  5476. left: 0;
  5477. width: 100%;
  5478. height: 100%;
  5479. background-color: rgba(0, 0, 0, 0.7);
  5480. z-index: 100;
  5481. display: flex;
  5482. justify-content: center;
  5483. align-items: center;
  5484. }
  5485. .countdown-content {
  5486. display: flex;
  5487. flex-direction: column;
  5488. align-items: center;
  5489. }
  5490. .countdown-number {
  5491. font-size: 80px;
  5492. color: #ffffff;
  5493. font-weight: bold;
  5494. margin-bottom: 20px;
  5495. }
  5496. .countdown-text {
  5497. font-size: 24px;
  5498. color: #ffffff;
  5499. }
  5500. /* 添加GIF播放器样式 */
  5501. .gif-player {
  5502. width: 100%;
  5503. height: 100%;
  5504. object-fit: cover;
  5505. position: absolute;
  5506. top: 0;
  5507. left: 0;
  5508. z-index: 5; /* 确保GIF在视频上方但在字幕和按钮下方 */
  5509. }
  5510. /* 重新录制按钮容器样式 */
  5511. .rerecord-button-container {
  5512. position: absolute;
  5513. bottom: 50px; /* 统一与其他按钮的位置 */
  5514. left: 50%;
  5515. transform: translateX(-50%);
  5516. z-index: 20;
  5517. }
  5518. /* 重新录制按钮样式 */
  5519. .rerecord-button {
  5520. width: 120px;
  5521. height: 40px;
  5522. border-radius: 20px;
  5523. background-color: #ffffff;
  5524. color: #333;
  5525. font-size: 16px;
  5526. border: none;
  5527. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  5528. display: flex;
  5529. justify-content: center;
  5530. align-items: center;
  5531. cursor: pointer;
  5532. }
  5533. .rerecord-button:hover {
  5534. background-color: #f0f0f0;
  5535. }
  5536. /* 添加视频蒙层样式 */
  5537. .video-overlay {
  5538. position: absolute;
  5539. top: 0;
  5540. left: 0;
  5541. width: 100%;
  5542. height: 100%;
  5543. background-color: transparent; /* 完全透明 */
  5544. z-index: 6; /* 确保在视频之上,但在按钮和字幕之下 */
  5545. pointer-events: auto; /* 允许捕获点击事件 */
  5546. }
  5547. /* 调整其他元素的z-index以确保正确的层级关系 */
  5548. .subtitle-overlay {
  5549. /* ... 现有样式 ... */
  5550. z-index: 10; /* 确保字幕在蒙层之上 */
  5551. }
  5552. .answer-button-container,
  5553. .stop-recording-button-container,
  5554. .start-recording-button-container,
  5555. .retry-button-container,
  5556. .rerecord-button-container {
  5557. /* ... 现有样式 ... */
  5558. z-index: 999; /* 确保按钮在蒙层之上 */
  5559. }
  5560. .user-camera-container {
  5561. /* ... 现有样式 ... */
  5562. z-index: 20; /* 确保摄像头窗口在蒙层之上 */
  5563. }
  5564. .recording-indicator {
  5565. /* ... 现有样式 ... */
  5566. z-index: 20; /* 确保录制指示器在蒙层之上 */
  5567. }
  5568. /* 环形进度条样式 */
  5569. .circle-progress-container {
  5570. width: 60px;
  5571. height: 60px;
  5572. position: relative;
  5573. margin: 0 auto 10px;
  5574. }
  5575. .circle-progress {
  5576. width: 100%;
  5577. height: 100%;
  5578. border-radius: 50%;
  5579. display: flex;
  5580. align-items: center;
  5581. justify-content: center;
  5582. position: relative;
  5583. transform: rotate(-90deg); /* 从顶部开始进度 */
  5584. }
  5585. .circle-progress-inner {
  5586. width: 80%;
  5587. height: 80%;
  5588. background-color: rgba(255, 255, 255);
  5589. border-radius: 50%;
  5590. display: flex;
  5591. align-items: center;
  5592. justify-content: center;
  5593. transform: rotate(90deg); /* 修正文本方向 */
  5594. }
  5595. .progress-text {
  5596. font-size: 14px;
  5597. font-weight: bold;
  5598. color: #05dc8b;
  5599. }
  5600. /* 添加题号显示 */
  5601. .question-counter {
  5602. position: absolute;
  5603. top: 20px;
  5604. right: 130px; /* 调整位置,避免与用户摄像头重叠 */
  5605. background-color: rgba(255, 255, 255, 0.9);
  5606. padding: 6px 12px;
  5607. border-radius: 15px;
  5608. z-index: 20;
  5609. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  5610. display: flex;
  5611. align-items: center;
  5612. }
  5613. .question-number {
  5614. color: #333;
  5615. font-size: 14px;
  5616. font-weight: 500;
  5617. }
  5618. /* 追问问题容器样式 */
  5619. .followup-container {
  5620. width: 100%;
  5621. }
  5622. /* 追问分隔线 */
  5623. .followup-divider {
  5624. height: 1px;
  5625. background-color: rgba(0, 0, 0, 0.05);
  5626. margin: 0;
  5627. }
  5628. /* 追问内容样式 */
  5629. .followup-content {
  5630. padding: 12px 20px;
  5631. display: flex;
  5632. align-items: flex-start;
  5633. gap: 8px;
  5634. }
  5635. /* 追问标签样式 */
  5636. .followup-label {
  5637. color: #fb752f;
  5638. font-size: 14px;
  5639. font-weight: 500;
  5640. }
  5641. /* 追问文本样式 */
  5642. .followup-text {
  5643. color: #333;
  5644. font-size: 14px;
  5645. line-height: 1.5;
  5646. flex: 1;
  5647. }
  5648. /* 调整主字幕样式,确保有足够空间 */
  5649. .subtitle-main {
  5650. padding:15px 20px ;
  5651. font-weight: 500;
  5652. border-bottom: none; /* 移除底部边框,由追问分隔线处理 */
  5653. }
  5654. /* 父问题样式 */
  5655. .parent-question {
  5656. padding: 15px 20px;
  5657. font-weight: 500;
  5658. color: #333;
  5659. }
  5660. /* 追问容器样式调整 */
  5661. .followup-container {
  5662. width: 100%;
  5663. /* background-color: rgba(0, 0, 0, 0.02); *//* 轻微的背景色区分 */
  5664. }
  5665. /* 分隔线样式 */
  5666. .followup-divider {
  5667. height: 1px;
  5668. background-color: rgba(0, 0, 0, 0.05);
  5669. margin: 0;
  5670. }
  5671. /* 追问内容样式 */
  5672. .followup-content {
  5673. padding: 12px 20px;
  5674. display: flex;
  5675. align-items: flex-start;
  5676. gap: 8px;
  5677. }
  5678. /* 追问标签样式 */
  5679. .followup-label {
  5680. color: #fb752f;
  5681. font-size: 14px;
  5682. font-weight: 500;
  5683. }
  5684. /* 追问文本样式 */
  5685. .followup-text {
  5686. color: #333;
  5687. font-size: 14px;
  5688. line-height: 1.5;
  5689. flex: 1;
  5690. }
  5691. .custom-navbar {
  5692. position: fixed;
  5693. top: 0;
  5694. left: 0;
  5695. right: 0;
  5696. height: 44px;
  5697. background-color: #ffffff;
  5698. display: flex;
  5699. align-items: center;
  5700. justify-content: center;
  5701. z-index: 999;
  5702. }
  5703. .navbar-title {
  5704. font-size: 16px;
  5705. font-weight: bold;
  5706. }
  5707. .content {
  5708. margin-top: 44px;
  5709. padding-top: var(--status-bar-height);
  5710. }
  5711. </style>