identity-verify.js 145 KB

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