identity-verify.js 154 KB

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