identity-verify.js 163 KB

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