identity-verify.js 120 KB

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