identity-verify.js 125 KB

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