identity-verify.vue 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  1. <template>
  2. <div class="identity-verify-container">
  3. <div class="digital-human-container">
  4. <!-- AI数字人视频/图像显示区域 -->
  5. <div class="digital-human-video">
  6. <!-- <image v-if="!videoPlaying" src="/static/images/digital-human-placeholder.jpg" mode="aspectFit"></image> -->
  7. <video
  8. :src="videoUrl"
  9. id="myVideo"
  10. ref="videoPlayer"
  11. autoplay
  12. playsinline
  13. disablePictureInPicture
  14. controlsList="nodownload nofullscreen noremoteplayback"
  15. class="video-player"
  16. :controls="false"
  17. @error="handleVideoError"
  18. @ended="handleVideoEnded"
  19. @timeupdate="handleTimeUpdate">
  20. </video>
  21. <!-- 添加字幕覆盖层 -->
  22. <div class="subtitle-overlay" v-if="currentSubtitle">
  23. {{ currentSubtitle }}
  24. </div>
  25. <!-- 添加答题按钮 -->
  26. <div class="answer-button-container" v-if="showAnswerButton">
  27. <button class="answer-button" @click="handleAnswerButtonClick">
  28. 开始面试
  29. </button>
  30. </div>
  31. </div>
  32. <!-- 用户摄像头视频显示区域 -->
  33. <div class="user-camera-container">
  34. <!-- 在小程序环境中使用camera组件 -->
  35. <camera v-if="useMiniProgramCameraComponent"
  36. device-position="front"
  37. flash="off"
  38. class="user-camera-video"
  39. @error="handleCameraError"
  40. :record-audio="true"
  41. frame-size="small"
  42. resolution="low"
  43. :disable-zoom="true"
  44. :enable-camera="true"
  45. :enable-record="true"
  46. mode="normal">
  47. </camera>
  48. <!-- 在H5/App环境中使用video元素 -->
  49. <video v-else
  50. id="userCamera"
  51. ref="userCameraVideo"
  52. autoplay
  53. playsinline
  54. muted
  55. class="user-camera-video"
  56. :controls="false">
  57. </video>
  58. </div>
  59. <!-- 字幕/文本覆盖区域 -->
  60. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  61. {{ assistantResponse }}
  62. </div> -->
  63. </div>
  64. <!-- 加载状态 -->
  65. <div v-if="loading" class="loading">加载中...</div>
  66. <!-- 控制面板(可选,可以隐藏) -->
  67. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  68. <div v-if="showDebugInfo" class="response-container">
  69. <div v-if="assistantResponse" class="response-item">
  70. <div class="response-content">
  71. <span>助手回复: {{ assistantResponse }}</span>
  72. </div>
  73. </div>
  74. <div v-if="audioTranscript" class="response-item">
  75. <div class="response-content">
  76. <span>音频转写: {{ audioTranscript }}</span>
  77. </div>
  78. </div>
  79. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  80. <div class="response-content">
  81. <span v-if="item.role">角色: {{ item.role }}</span>
  82. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  83. </div>
  84. </div>
  85. </div>
  86. <!-- 添加停止录制按钮 -->
  87. <div class="stop-recording-button-container" v-if="showStopRecordingButton">
  88. <button class="stop-recording-button" @click="stopRecordingAnswer">
  89. 完成作答
  90. </button>
  91. </div>
  92. <!-- 添加录制指示器 -->
  93. <div class="recording-indicator" v-if="isRecording">
  94. <div class="recording-dot"></div>
  95. <span class="recording-text">正在录制...</span>
  96. </div>
  97. <!-- 添加开始回答按钮 -->
  98. <div class="start-recording-button-container" v-if="showStartRecordingButton">
  99. <button class="start-recording-button" @click="handleStartRecordingClick">
  100. 开始作答
  101. </button>
  102. </div>
  103. <!-- 添加重试上传按钮 -->
  104. <div class="retry-button-container" v-if="showRetryButton">
  105. <button class="retry-button" @click="retryVideoUpload">
  106. 重新上传
  107. </button>
  108. </div>
  109. <!-- 在模板部分添加录制时长显示 -->
  110. <!-- <div class="recording-timer" v-if="isRecording">
  111. <span class="timer-text">{{ recordingTimeDisplay || '00:00' }}</span>
  112. </div> -->
  113. </div>
  114. </template>
  115. <script>
  116. import { apiBaseUrl } from '@/common/config.js';
  117. export default {
  118. name: 'IdentityVerify',
  119. data() {
  120. return {
  121. loading: false,
  122. responses: [],
  123. processedResponses: [],
  124. assistantResponse: '',
  125. audioTranscript: '',
  126. videoPlaying: false,
  127. showDebugInfo: false, // 设置为true可以显示调试信息
  128. videoUrl: 'http://121.36.251.245:9000/minlong/9c139c99-a613-49f2-986b-8a3ac20a9b1b.mp4', // 用于存储AI数字人视频URL
  129. showReplayButton: false,
  130. cameraStream: null, // 存储摄像头流
  131. cameraError: null, // 存储摄像头错误信息
  132. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  133. cameraContext: null, // 添加相机上下文
  134. currentSubtitle: '',
  135. subtitles: [
  136. {
  137. startTime: 0, // 开始时间(秒)
  138. endTime: 5, // 结束时间(秒)
  139. text: '你好,我是本次面试的面试官,欢迎参加本公司的线上面试!'
  140. },
  141. {
  142. startTime: 5,
  143. endTime: 13,
  144. text: '面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试'
  145. },
  146. {
  147. startTime: 13,
  148. endTime: 20,
  149. text: '以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。'
  150. }
  151. ],
  152. secondVideoSubtitles: [
  153. {
  154. startTime: 0,
  155. endTime: 10,
  156. text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  157. }
  158. ],
  159. thirdVideoSubtitles: [
  160. {
  161. startTime: 0,
  162. endTime: 3,
  163. text: '在工作中,你如何确保个人防护装备的正确使用?'
  164. }
  165. ],
  166. fourthVideoSubtitles: [
  167. {
  168. startTime: 0,
  169. endTime: 3,
  170. text: '描述一次你与团队合作改善生产流程的经历。'
  171. }
  172. ],
  173. fifthVideoSubtitles: [
  174. {
  175. startTime: 0,
  176. endTime: 6,
  177. text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  178. }
  179. ],
  180. sixthVideoSubtitles: [
  181. {
  182. startTime: 0,
  183. endTime: 5,
  184. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  185. }
  186. ],
  187. showAnswerButton: false, // 控制答题按钮显示
  188. currentVideoIndex: 0, // 当前播放的视频索引
  189. videoList: [
  190. 'http://121.36.251.245:9000/minlong/9c139c99-a613-49f2-986b-8a3ac20a9b1b.mp4', // 第一段视频
  191. 'http://121.36.251.245:9000/minlong/15467aca-2c1e-4d40-bebc-11fc984f0a24.mp4', // 第二段视频
  192. 'http://121.36.251.245:9000/minlong/a96f295c-ecba-4b08-b627-0ed0dc72d727.mp4', // 第三段视频
  193. 'http://121.36.251.245:9000/minlong/e152eb38-7e03-4bb0-8b8d-73d51342d2aa.mp4',
  194. 'http://121.36.251.245:9000/minlong/6381eec3-1373-400f-aa8c-e229b60522ea.mp4',
  195. 'http://121.36.251.245:9000/minlong/2a11fe78-38d5-4af3-91c8-a080feb122e0.mp4'//结束
  196. ],
  197. isRecording: false,
  198. recordingTimer: null,
  199. showStopRecordingButton: false,
  200. mediaRecorder: null,
  201. recordedChunks: [],
  202. recorder: null,
  203. lastUploadedVideoUrl: '',
  204. showStartRecordingButton: false,
  205. showRetryButton: false, // 控制重试按钮显示
  206. lastVideoToRetry: null, // 存储上次失败的视频URL,用于重试
  207. recordingStartTime: null, // 录制开始时间
  208. recordingTimerCount: 0, // 录制计时器计数
  209. recordingTimeDisplay: '00:00', // 格式化的录制时间显示
  210. }
  211. },
  212. mounted() {
  213. /* this.fetchData() */
  214. this.playDigitalHumanVideo();
  215. this.checkAudioPermission();
  216. this.initCamera();
  217. this.checkIOSCameraRecordPermission();
  218. // 添加防御性检查,避免渲染错误
  219. this.checkAndFixRenderingIssues();
  220. // 添加音频测试
  221. setTimeout(() => {
  222. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  223. this.testAudioInput();
  224. }
  225. }, 3000);
  226. },
  227. beforeDestroy() {
  228. // 组件销毁前停止摄像头
  229. this.stopUserCamera();
  230. },
  231. methods: {
  232. // 初始化相机
  233. async initCamera() {
  234. // 检查平台
  235. const systemInfo = uni.getSystemInfoSync();
  236. // 判断是否在小程序环境中
  237. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  238. systemInfo.uniPlatform === 'mp-alipay' ||
  239. systemInfo.uniPlatform === 'mp-baidu';
  240. if (isMiniProgram) {
  241. // 小程序环境使用小程序的相机API
  242. this.useMiniProgramCameraComponent = true;
  243. // 创建相机上下文
  244. this.cameraContext = uni.createCameraContext();
  245. // 确保已获取录音和相机权限
  246. uni.getSetting({
  247. success: (res) => {
  248. // 检查录音权限
  249. if (!res.authSetting['scope.record']) {
  250. uni.authorize({
  251. scope: 'scope.record',
  252. success: () => {
  253. console.log('录音权限已获取');
  254. },
  255. fail: (err) => {
  256. console.error('录音权限获取失败:', err);
  257. this.showPermissionDialog('录音');
  258. }
  259. });
  260. }
  261. // 检查相机权限
  262. if (!res.authSetting['scope.camera']) {
  263. uni.authorize({
  264. scope: 'scope.camera',
  265. success: () => {
  266. console.log('相机权限已获取');
  267. },
  268. fail: (err) => {
  269. console.error('相机权限获取失败:', err);
  270. this.showPermissionDialog('相机');
  271. }
  272. });
  273. }
  274. // 在iOS上,可能需要额外的权限检查
  275. const systemInfo = uni.getSystemInfoSync();
  276. if (systemInfo.platform === 'ios') {
  277. // 在iOS上,不要使用无效的权限范围
  278. // 移除以下代码块
  279. /*
  280. if (!res.authSetting['scope.camera.record']) {
  281. uni.authorize({
  282. scope: 'scope.camera.record',
  283. success: () => {
  284. console.log('相机录制权限已获取');
  285. },
  286. fail: (err) => {
  287. console.error('相机录制权限获取失败:', err);
  288. this.showPermissionDialog('相机录制');
  289. }
  290. });
  291. }
  292. */
  293. // 确保同时获取了相机和录音权限
  294. if (!res.authSetting['scope.camera'] || !res.authSetting['scope.record']) {
  295. console.log('iOS需要同时获取相机和录音权限');
  296. }
  297. }
  298. }
  299. });
  300. } else {
  301. // H5/App环境使用浏览器的API
  302. try {
  303. // 请求摄像头和麦克风权限并获取媒体流,添加更多约束
  304. const constraints = {
  305. audio: {
  306. echoCancellation: true,
  307. noiseSuppression: true,
  308. autoGainControl: true
  309. },
  310. video: {
  311. width: { ideal: 640, max: 1280 }, // 控制视频宽度
  312. height: { ideal: 480, max: 720 }, // 控制视频高度
  313. frameRate: { ideal: 15, max: 24 }, // 控制帧率
  314. facingMode: 'user'
  315. }
  316. };
  317. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  318. // 保存流以便后续使用
  319. this.cameraStream = stream;
  320. // 检查音频轨道
  321. const audioTracks = stream.getAudioTracks();
  322. console.log('音频轨道数量:', audioTracks.length);
  323. if (audioTracks.length > 0) {
  324. console.log('音频轨道已获取:', audioTracks[0].label);
  325. // 确保音频轨道已启用
  326. audioTracks[0].enabled = true;
  327. } else {
  328. console.warn('未检测到音频轨道,尝试单独获取音频');
  329. this.tryGetAudioOnly();
  330. }
  331. // 将流设置到视频元素
  332. const videoElement = this.$refs.userCameraVideo;
  333. if (videoElement) {
  334. videoElement.srcObject = stream;
  335. videoElement.muted = true; // 避免回声,但仍然录制声音
  336. }
  337. } catch (error) {
  338. console.error('获取摄像头失败:', error);
  339. this.cameraError = error.message || '无法访问摄像头';
  340. // 显示错误提示
  341. uni.showToast({
  342. title: '无法访问摄像头,请检查权限设置',
  343. icon: 'none'
  344. });
  345. }
  346. }
  347. },
  348. // 停止用户摄像头
  349. stopUserCamera() {
  350. if (this.cameraStream) {
  351. // 停止所有轨道
  352. this.cameraStream.getTracks().forEach(track => {
  353. track.stop();
  354. });
  355. this.cameraStream = null;
  356. }
  357. },
  358. async fetchData() {
  359. this.loading = true
  360. this.assistantResponse = ''
  361. this.audioTranscript = ''
  362. this.processedResponses = []
  363. try {
  364. // 使用uni.request代替fetch
  365. const requestTask = uni.request({
  366. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  367. method: 'POST',
  368. header: {
  369. 'Content-Type': 'application/json',
  370. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  371. },
  372. data: {
  373. "model": "qwen-omni-turbo",
  374. "messages": [
  375. {
  376. "role": "user",
  377. "content": [
  378. {
  379. "type": "input_audio",
  380. "input_audio": {
  381. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  382. "format": "wav"
  383. }
  384. },
  385. {
  386. "type": "text",
  387. "text": "这段音频在说什么"
  388. }
  389. ]
  390. }
  391. ],
  392. "stream":true,
  393. "stream_options":{
  394. "include_usage":true
  395. },
  396. "modalities":["text","audio"],
  397. "audio":{"voice":"Cherry","format":"wav"}
  398. },
  399. success: (res) => {
  400. console.log('请求成功,响应数据:', res.data);
  401. // 检查响应数据是否包含多个JSON对象
  402. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  403. // 处理包含多个JSON对象的情况
  404. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  405. chunks.forEach(chunk => {
  406. this.handleStreamResponse(chunk);
  407. });
  408. } else {
  409. // 处理单个响应对象的情况
  410. this.handleStreamResponse(res.data);
  411. }
  412. // 模拟获取到数字人视频并播放
  413. this.playDigitalHumanVideo();
  414. },
  415. fail: (err) => {
  416. console.error('请求失败:', err);
  417. },
  418. complete: () => {
  419. this.loading = false;
  420. }
  421. });
  422. } catch (error) {
  423. console.error('获取数据失败:', error);
  424. this.loading = false;
  425. }
  426. },
  427. handleStreamResponse(data) {
  428. // 处理流式响应数据
  429. if (typeof data === 'string') {
  430. // 处理字符串格式的响应
  431. if (data === '[DONE]') return;
  432. try {
  433. // 移除可能存在的换行符和多余空格
  434. const cleanData = data.trim();
  435. // 检查是否是有效的JSON字符串
  436. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  437. const jsonData = JSON.parse(cleanData);
  438. this.processStreamChunk(jsonData);
  439. }
  440. } catch (e) {
  441. console.error('解析JSON失败:', e, '原始数据:', data);
  442. }
  443. } else {
  444. // 处理对象格式的响应
  445. this.processStreamChunk(data);
  446. }
  447. },
  448. processStreamChunk(chunk) {
  449. if (chunk.choices && chunk.choices.length > 0) {
  450. const choice = chunk.choices[0];
  451. // 处理助手回复内容
  452. if (choice.delta && choice.delta.content) {
  453. this.assistantResponse += choice.delta.content;
  454. }
  455. // 处理音频转写内容
  456. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  457. this.audioTranscript += choice.delta.audio.transcript;
  458. }
  459. // 处理角色和音频转写
  460. if (choice.delta) {
  461. const result = {};
  462. if (choice.delta.role) {
  463. result.role = choice.delta.role;
  464. }
  465. if (choice.delta.audio && choice.delta.audio.transcript) {
  466. result.transcript = choice.delta.audio.transcript;
  467. }
  468. if (Object.keys(result).length > 0) {
  469. this.processedResponses.push(result);
  470. }
  471. }
  472. }
  473. },
  474. processResponseData() {
  475. // 处理返回的数据
  476. this.processedResponses = this.responses.map(item => {
  477. const result = {}
  478. // 处理角色信息
  479. if (item.delta && item.delta.role) {
  480. result.role = item.delta.role
  481. }
  482. // 处理音频转写文本
  483. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  484. result.transcript = item.delta.audio.transcript
  485. }
  486. return result
  487. }).filter(item => Object.keys(item).length > 0)
  488. },
  489. // 播放数字人视频
  490. playDigitalHumanVideo() {
  491. // 设置第一个视频
  492. this.videoUrl = this.videoList[this.currentVideoIndex];
  493. this.videoPlaying = true;
  494. // 使用 uni.createVideoContext 来控制视频
  495. this.$nextTick(() => {
  496. const videoContext = uni.createVideoContext('myVideo', this);
  497. if (videoContext) {
  498. videoContext.play();
  499. // 设置超时检查,确认视频是否真的在播放
  500. setTimeout(() => {
  501. if (this.videoPlaying && this.$refs.videoPlayer) {
  502. console.log('视频应该正在播放');
  503. } else {
  504. console.log('视频可能未成功播放,尝试替代方案');
  505. this.tryAlternativeVideoPath();
  506. }
  507. }, 1000);
  508. } else {
  509. console.error('无法创建视频上下文');
  510. this.tryAlternativeVideoPath();
  511. }
  512. });
  513. },
  514. // 修改 tryAlternativeVideoPath 方法
  515. tryAlternativeVideoPath() {
  516. console.log('尝试使用替代路径');
  517. // 尝试不同的路径格式
  518. const alternativePaths = [
  519. './static/demo.mp4',
  520. '../static/demo.mp4',
  521. 'static/demo.mp4',
  522. '/static/demo.mp4',
  523. // 添加绝对路径
  524. `${window.location.origin}/static/demo.mp4`
  525. ];
  526. // 获取当前路径索引
  527. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  528. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  529. // 设置下一个路径
  530. this.videoUrl = alternativePaths[nextPathIndex];
  531. console.log('尝试新路径:', this.videoUrl);
  532. this.$nextTick(() => {
  533. const videoContext = uni.createVideoContext('myVideo', this);
  534. if (videoContext) {
  535. videoContext.stop();
  536. videoContext.play();
  537. // 检查是否成功播放
  538. setTimeout(() => {
  539. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  540. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  541. this.checkVideoWithAPI();
  542. }
  543. }, 1000);
  544. }
  545. });
  546. },
  547. // 添加新方法:使用uni API检查视频
  548. checkVideoWithAPI() {
  549. // 尝试使用uni.getVideoInfo API检查视频是否可用
  550. uni.getVideoInfo({
  551. src: '/static/demo.mp4',
  552. success: (res) => {
  553. console.log('视频信息获取成功:', res);
  554. // 如果能获取到视频信息,再次尝试播放
  555. this.videoUrl = '/static/demo.mp4';
  556. this.$nextTick(() => {
  557. const videoContext = uni.createVideoContext('myVideo', this);
  558. if (videoContext) {
  559. videoContext.play();
  560. }
  561. });
  562. },
  563. fail: (err) => {
  564. console.error('视频信息获取失败:', err);
  565. // 最后尝试使用uni.chooseVideo API
  566. this.fallbackToLocalVideo();
  567. }
  568. });
  569. },
  570. // 添加新方法:回退到本地视频
  571. fallbackToLocalVideo() {
  572. console.log('尝试使用本地视频资源');
  573. // 检查平台
  574. const platform = uni.getSystemInfoSync().platform;
  575. if (platform === 'android' || platform === 'ios') {
  576. // 移动端可以尝试使用本地资源
  577. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  578. this.$nextTick(() => {
  579. const videoContext = uni.createVideoContext('myVideo', this);
  580. if (videoContext) {
  581. videoContext.play();
  582. }
  583. });
  584. } else {
  585. // 最终回退到静态图片
  586. this.videoPlaying = false;
  587. uni.showToast({
  588. title: '视频加载失败,显示静态图片',
  589. icon: 'none'
  590. });
  591. }
  592. },
  593. // 修改 handleVideoError 方法
  594. handleVideoError(e) {
  595. console.error('视频加载错误:', e);
  596. // 记录更详细的错误信息
  597. if (e && e.detail) {
  598. console.error('详细错误信息:', e.detail);
  599. }
  600. // 检查视频文件是否存在
  601. uni.getFileInfo({
  602. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  603. success: (res) => {
  604. console.log('文件存在,大小:', res.size);
  605. // 文件存在但播放失败,可能是格式问题
  606. this.tryDifferentFormat();
  607. },
  608. fail: (err) => {
  609. console.error('文件不存在或无法访问:', err);
  610. // 尝试不同路径
  611. this.tryAlternativeVideoPath();
  612. }
  613. });
  614. // 如果多次尝试后仍然失败,显示错误信息
  615. uni.showToast({
  616. title: '视频加载失败,请检查文件是否存在',
  617. icon: 'none',
  618. duration: 2000
  619. });
  620. },
  621. // 添加新方法:尝试不同格式
  622. tryDifferentFormat() {
  623. console.log('尝试不同的视频格式');
  624. // 尝试不同的视频格式
  625. const formats = [
  626. { ext: 'mp4', mime: 'video/mp4' },
  627. { ext: 'webm', mime: 'video/webm' },
  628. { ext: 'ogg', mime: 'video/ogg' },
  629. { ext: 'mov', mime: 'video/quicktime' }
  630. ];
  631. // 获取当前文件名(不含扩展名)
  632. const currentPath = this.videoUrl;
  633. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  634. // 尝试下一个格式
  635. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  636. if (nextFormat) {
  637. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  638. console.log('尝试新格式:', this.videoUrl);
  639. this.$nextTick(() => {
  640. const videoContext = uni.createVideoContext('myVideo', this);
  641. if (videoContext) {
  642. videoContext.stop();
  643. videoContext.play();
  644. }
  645. });
  646. } else {
  647. // 所有格式都尝试过了,使用内置资源
  648. this.useBuiltInResource();
  649. }
  650. },
  651. // 添加新方法:使用内置资源
  652. useBuiltInResource() {
  653. console.log('尝试使用内置资源');
  654. // 检查平台
  655. const platform = uni.getSystemInfoSync().platform;
  656. // 根据平台选择合适的视频源
  657. if (platform === 'windows') {
  658. // Windows平台特定处理
  659. // 尝试使用相对于应用根目录的路径
  660. const appRoot = process.env.UNI_INPUT_DIR || '';
  661. this.videoUrl = `./static/demo.mp4`;
  662. // 或者尝试使用file://协议
  663. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  664. console.log('Windows平台尝试路径:', this.videoUrl);
  665. } else if (platform === 'android' || platform === 'ios') {
  666. // 移动端
  667. this.useNativeVideo();
  668. } else {
  669. // Web平台
  670. // 尝试使用完整URL
  671. const baseUrl = window.location.origin;
  672. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  673. console.log('Web平台尝试URL:', this.videoUrl);
  674. }
  675. this.$nextTick(() => {
  676. const videoContext = uni.createVideoContext('myVideo', this);
  677. if (videoContext) {
  678. videoContext.play();
  679. }
  680. });
  681. },
  682. // 添加新方法:使用原生视频能力
  683. useNativeVideo() {
  684. console.log('尝试使用原生视频能力');
  685. // 在移动端,可以尝试使用原生视频播放器
  686. uni.chooseVideo({
  687. sourceType: ['album'],
  688. success: (res) => {
  689. this.videoUrl = res.tempFilePath;
  690. this.$nextTick(() => {
  691. const videoContext = uni.createVideoContext('myVideo', this);
  692. if (videoContext) {
  693. videoContext.play();
  694. }
  695. });
  696. },
  697. fail: () => {
  698. // 如果用户取消选择,回退到静态图片
  699. this.videoPlaying = false;
  700. uni.showToast({
  701. title: '无法加载视频,显示静态图片',
  702. icon: 'none'
  703. });
  704. }
  705. });
  706. },
  707. // 处理视频结束事件
  708. handleVideoEnded() {
  709. console.log('视频播放结束');
  710. this.videoPlaying = false;
  711. // 对于所有视频,都显示相应的按钮
  712. if (this.currentVideoIndex >= 1) {
  713. // 对于问题视频,显示"开始回答"按钮
  714. this.showStartRecordingButton = true;
  715. } else {
  716. // 对于介绍视频(第一个视频),显示"开始面试"按钮
  717. this.showAnswerButton = true;
  718. }
  719. },
  720. // 添加新方法:开始录制用户回答
  721. startRecordingAnswer() {
  722. console.log('开始录制用户回答');
  723. this.isRecording = true;
  724. // 记录录制开始时间
  725. this.recordingStartTime = Date.now();
  726. this.recordingTimerCount = 0;
  727. // 启动计时器,每秒更新计时
  728. this.recordingTimer = setInterval(() => {
  729. this.recordingTimerCount++;
  730. // 可以在这里更新UI显示录制时长
  731. // 例如:this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  732. }, 1000);
  733. // 显示录制中的提示
  734. // uni.showLoading({
  735. // title: '正在录制...',
  736. // mask: true
  737. // });
  738. // 根据平台选择不同的录制方法
  739. const systemInfo = uni.getSystemInfoSync();
  740. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  741. if (isMiniProgram) {
  742. // 小程序环境使用小程序的录制API
  743. this.startMiniProgramRecording();
  744. } else {
  745. // H5/App环境使用MediaRecorder API
  746. this.startBrowserRecording();
  747. }
  748. // 显示停止录制按钮
  749. this.showStopRecordingButton = true;
  750. },
  751. // 添加一个新方法:重置相机组件
  752. resetCamera() {
  753. console.log('重置相机组件');
  754. // 先完全移除相机组件
  755. this.useMiniProgramCameraComponent = false;
  756. // 释放相机上下文
  757. if (this.cameraContext) {
  758. this.cameraContext = null;
  759. }
  760. // 延迟后重新创建相机组件
  761. setTimeout(() => {
  762. this.useMiniProgramCameraComponent = true;
  763. // 再次延迟创建相机上下文,确保组件已完全渲染
  764. setTimeout(() => {
  765. this.cameraContext = uni.createCameraContext();
  766. console.log('相机组件已重置');
  767. }, 500);
  768. }, 500);
  769. },
  770. // 修改 startMiniProgramRecording 方法
  771. startMiniProgramRecording() {
  772. console.log('开始小程序录制方法');
  773. // 获取平台信息
  774. const systemInfo = uni.getSystemInfoSync();
  775. const isIOS = systemInfo.platform === 'ios';
  776. // 在iOS上,先重置相机
  777. if (isIOS) {
  778. // 先重置相机组件
  779. this.resetCamera();
  780. // 延迟执行录制,给相机组件足够的初始化时间
  781. setTimeout(() => {
  782. this.actualStartRecording(isIOS);
  783. }, 1000);
  784. } else {
  785. // Android直接开始
  786. this.actualStartRecording(isIOS);
  787. }
  788. },
  789. // 添加新方法:实际开始录制
  790. actualStartRecording(isIOS) {
  791. if (!this.cameraContext) {
  792. this.cameraContext = uni.createCameraContext();
  793. console.log('创建新的相机上下文');
  794. }
  795. // 确保有录音权限
  796. uni.getSetting({
  797. success: (res) => {
  798. const hasRecordAuth = res.authSetting['scope.record'];
  799. const hasCameraAuth = res.authSetting['scope.camera'];
  800. if (!hasRecordAuth || !hasCameraAuth) {
  801. console.warn('缺少必要权限,请求权限');
  802. this.requestMiniProgramPermissions();
  803. return;
  804. }
  805. // 在iOS上,先检查相机状态
  806. if (isIOS) {
  807. console.log('iOS: 检查相机状态');
  808. // 使用最简单的选项
  809. const options = {
  810. timeout: 30000, // 减少超时时间
  811. quality: 'low', // 降低质量
  812. compressed: true,
  813. success: () => {
  814. console.log('iOS录制开始成功');
  815. },
  816. fail: (err) => {
  817. console.error('iOS录制失败:', err);
  818. // 如果失败,尝试使用不同的方法
  819. this.useAlternativeRecordingMethod();
  820. }
  821. };
  822. try {
  823. console.log('尝试开始录制');
  824. this.recorder = this.cameraContext.startRecord(options);
  825. } catch (e) {
  826. console.error('开始录制异常:', e);
  827. this.useAlternativeRecordingMethod();
  828. }
  829. } else {
  830. // Android使用标准选项
  831. const options = {
  832. timeout: 60000,
  833. quality: 'medium',
  834. compressed: true,
  835. success: () => {
  836. console.log('Android录制开始成功');
  837. },
  838. fail: (err) => {
  839. console.error('Android录制失败:', err);
  840. uni.showToast({
  841. title: '录制失败,请检查相机权限',
  842. icon: 'none'
  843. });
  844. this.proceedToNextQuestion();
  845. }
  846. };
  847. this.recorder = this.cameraContext.startRecord(options);
  848. }
  849. }
  850. });
  851. },
  852. // 添加新方法:使用替代录制方法
  853. useAlternativeRecordingMethod() {
  854. console.log('使用替代录制方法');
  855. // 在iOS上,可以尝试使用chooseVideo API作为备选
  856. uni.showActionSheet({
  857. itemList: ['使用相册中的视频', '跳过此问题'],
  858. success: (res) => {
  859. if (res.tapIndex === 0) {
  860. // 选择相册中的视频
  861. uni.chooseVideo({
  862. sourceType: ['album'],
  863. maxDuration: 60,
  864. camera: 'front',
  865. success: (res) => {
  866. console.log('选择视频成功:', res.tempFilePath);
  867. // 停止录制状态
  868. this.isRecording = false;
  869. this.showStopRecordingButton = false;
  870. // 上传选择的视频
  871. this.uploadRecordedVideo(res.tempFilePath);
  872. },
  873. fail: () => {
  874. console.log('用户取消选择视频');
  875. this.proceedToNextQuestion();
  876. }
  877. });
  878. } else {
  879. // 跳过此问题
  880. console.log('用户选择跳过问题');
  881. this.proceedToNextQuestion();
  882. }
  883. },
  884. fail: () => {
  885. console.log('操作取消');
  886. this.proceedToNextQuestion();
  887. }
  888. });
  889. },
  890. // 添加新方法:请求小程序权限
  891. requestMiniProgramPermissions() {
  892. // 请求录音权限
  893. uni.authorize({
  894. scope: 'scope.record',
  895. success: () => {
  896. console.log('录音权限已获取');
  897. // 请求相机权限
  898. uni.authorize({
  899. scope: 'scope.camera',
  900. success: () => {
  901. console.log('相机权限已获取');
  902. // 权限都获取到后,开始录制
  903. this.startMiniProgramRecording();
  904. },
  905. fail: (err) => {
  906. console.error('相机权限获取失败:', err);
  907. this.showPermissionDialog('相机');
  908. }
  909. });
  910. },
  911. fail: (err) => {
  912. console.error('录音权限获取失败:', err);
  913. this.showPermissionDialog('录音');
  914. }
  915. });
  916. },
  917. // 修改浏览器环境下的录制方法
  918. startBrowserRecording() {
  919. if (!this.cameraStream) {
  920. console.error('没有可用的摄像头流');
  921. uni.showToast({
  922. title: '录制失败,摄像头未就绪',
  923. icon: 'none'
  924. });
  925. this.proceedToNextQuestion();
  926. return;
  927. }
  928. try {
  929. // 检查流中是否包含音频轨道
  930. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  931. if (!hasAudio) {
  932. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  933. // 尝试重新获取带音频的媒体流
  934. navigator.mediaDevices.getUserMedia({
  935. audio: {
  936. echoCancellation: true,
  937. noiseSuppression: true,
  938. autoGainControl: true
  939. },
  940. video: true
  941. })
  942. .then(newStream => {
  943. // 检查新流是否包含音频轨道
  944. const audioTracks = newStream.getAudioTracks();
  945. if (audioTracks.length > 0) {
  946. console.log('成功获取音频轨道:', audioTracks[0].label);
  947. // 合并视频和音频轨道
  948. const videoTrack = this.cameraStream.getVideoTracks()[0];
  949. const audioTrack = newStream.getAudioTracks()[0];
  950. // 创建新的媒体流,包含视频和音频轨道
  951. const combinedStream = new MediaStream();
  952. if (videoTrack) combinedStream.addTrack(videoTrack);
  953. if (audioTrack) combinedStream.addTrack(audioTrack);
  954. this.cameraStream = combinedStream;
  955. // 更新视频元素的源
  956. const videoElement = this.$refs.userCameraVideo;
  957. if (videoElement) {
  958. videoElement.srcObject = combinedStream;
  959. videoElement.muted = true; // 避免回声,但仍然录制声音
  960. }
  961. // 继续录制过程
  962. this.setupMediaRecorder(combinedStream);
  963. } else {
  964. console.warn('仍然无法获取音频轨道');
  965. // 使用现有流继续
  966. this.setupMediaRecorder(this.cameraStream);
  967. }
  968. })
  969. .catch(err => {
  970. console.error('获取音频失败:', err);
  971. // 使用现有流继续
  972. this.setupMediaRecorder(this.cameraStream);
  973. });
  974. } else {
  975. console.log('检测到音频轨道,直接使用');
  976. // 已有音频轨道,直接使用
  977. this.setupMediaRecorder(this.cameraStream);
  978. }
  979. } catch (error) {
  980. console.error('浏览器录制失败:', error);
  981. uni.showToast({
  982. title: '录制失败,浏览器可能不支持此功能',
  983. icon: 'none'
  984. });
  985. this.proceedToNextQuestion();
  986. }
  987. },
  988. // 修改 setupMediaRecorder 方法
  989. setupMediaRecorder(stream) {
  990. // 检查流中的轨道
  991. const videoTracks = stream.getVideoTracks();
  992. const audioTracks = stream.getAudioTracks();
  993. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  994. // 尝试使用支持度更广的编码格式
  995. let mimeType = '';
  996. const supportedTypes = [
  997. 'video/webm;codecs=vp9,opus',
  998. 'video/webm;codecs=vp8,opus',
  999. 'video/webm;codecs=h264,opus',
  1000. 'video/mp4;codecs=h264,aac',
  1001. 'video/webm',
  1002. 'video/mp4'
  1003. ];
  1004. for (const type of supportedTypes) {
  1005. if (MediaRecorder.isTypeSupported(type)) {
  1006. mimeType = type;
  1007. console.log('使用支持的MIME类型:', mimeType);
  1008. break;
  1009. }
  1010. }
  1011. // 创建MediaRecorder实例,设置较低的比特率以减小文件大小
  1012. const options = {
  1013. mimeType: mimeType || '',
  1014. audioBitsPerSecond: 64000, // 降低音频比特率
  1015. videoBitsPerSecond: 1000000, // 降低视频比特率到1Mbps
  1016. };
  1017. try {
  1018. this.mediaRecorder = new MediaRecorder(stream, options);
  1019. console.log('MediaRecorder创建成功,使用选项:', options);
  1020. } catch (e) {
  1021. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  1022. this.mediaRecorder = new MediaRecorder(stream);
  1023. }
  1024. // 存储录制的数据块
  1025. this.recordedChunks = [];
  1026. // 监听数据可用事件
  1027. this.mediaRecorder.ondataavailable = (event) => {
  1028. if (event.data && event.data.size > 0) {
  1029. this.recordedChunks.push(event.data);
  1030. console.log(`收到数据块: ${event.data.size} 字节`);
  1031. }
  1032. };
  1033. // 监听录制停止事件
  1034. this.mediaRecorder.onstop = async () => {
  1035. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  1036. if (this.recordedChunks.length === 0) {
  1037. console.error('没有录制到数据');
  1038. uni.showToast({
  1039. title: '录制失败,未捕获到数据',
  1040. icon: 'none'
  1041. });
  1042. this.proceedToNextQuestion();
  1043. return;
  1044. }
  1045. // 创建Blob对象
  1046. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  1047. const blob = new Blob(this.recordedChunks, { type: mimeType });
  1048. console.log('创建Blob,原始大小:', blob.size, '类型:', mimeType);
  1049. // 显示压缩中提示
  1050. uni.showLoading({
  1051. title: '正在处理视频...',
  1052. mask: true
  1053. });
  1054. try {
  1055. // 压缩视频
  1056. const compressedBlob = await this.compressVideo(blob);
  1057. // 将Blob转换为File对象
  1058. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1059. const file = new File([compressedBlob], fileName, { type: mimeType });
  1060. // 隐藏压缩提示
  1061. uni.hideLoading();
  1062. // 上传文件
  1063. this.uploadRecordedVideo(file);
  1064. } catch (error) {
  1065. console.error('视频处理失败:', error);
  1066. uni.hideLoading();
  1067. // 如果压缩失败,使用原始视频
  1068. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1069. const file = new File([blob], fileName, { type: mimeType });
  1070. this.uploadRecordedVideo(file);
  1071. }
  1072. };
  1073. // 监听错误
  1074. this.mediaRecorder.onerror = (event) => {
  1075. console.error('MediaRecorder错误:', event.error);
  1076. };
  1077. // 开始录制
  1078. try {
  1079. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  1080. console.log('MediaRecorder开始录制');
  1081. } catch (e) {
  1082. console.error('开始录制失败:', e);
  1083. }
  1084. },
  1085. // 添加新方法:停止录制用户回答
  1086. stopRecordingAnswer() {
  1087. console.log('停止录制用户回答');
  1088. // 检查录制时长
  1089. const recordingDuration = this.getRecordingDuration();
  1090. const minimumDuration = 3; // 最小录制时长(秒)
  1091. if (recordingDuration < minimumDuration) {
  1092. // 录制时间过短,显示提示
  1093. uni.showModal({
  1094. title: '录制时间过短',
  1095. content: '您的回答时间过短,请至少录制' + minimumDuration + '秒。是否重新录制?',
  1096. confirmText: '重新录制',
  1097. cancelText: '仍然提交',
  1098. success: (res) => {
  1099. if (res.confirm) {
  1100. // 用户选择重新录制
  1101. console.log('用户选择重新录制');
  1102. // 重置录制状态但不停止当前录制
  1103. this.resetRecording();
  1104. return;
  1105. } else {
  1106. // 用户选择仍然提交,继续执行停止录制逻辑
  1107. console.log('用户选择继续提交短视频');
  1108. this.completeRecordingStop();
  1109. }
  1110. }
  1111. });
  1112. } else {
  1113. // 录制时长足够,直接停止
  1114. this.completeRecordingStop();
  1115. }
  1116. },
  1117. // 添加新方法:获取录制时长
  1118. getRecordingDuration() {
  1119. // 如果有明确的录制开始时间,计算实际录制时长
  1120. if (this.recordingStartTime) {
  1121. return (Date.now() - this.recordingStartTime) / 1000;
  1122. }
  1123. // 如果没有明确记录开始时间,尝试从视频元素获取
  1124. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1125. return this.$refs.userCameraVideo.currentTime || 0;
  1126. }
  1127. // 如果无法获取准确时长,返回估计值(如果有计时器)
  1128. if (this.recordingTimerCount) {
  1129. return this.recordingTimerCount;
  1130. }
  1131. // 默认返回0
  1132. return 0;
  1133. },
  1134. // 添加新方法:重置录制
  1135. resetRecording() {
  1136. // 保持录制状态,但重置计时器
  1137. if (this.recordingTimer) {
  1138. clearTimeout(this.recordingTimer);
  1139. }
  1140. // 重置录制开始时间
  1141. this.recordingStartTime = Date.now();
  1142. this.recordingTimerCount = 0;
  1143. // 如果是浏览器环境,需要重置MediaRecorder
  1144. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1145. // 停止当前录制
  1146. this.mediaRecorder.stop();
  1147. // 清空已录制的数据块
  1148. this.recordedChunks = [];
  1149. // 短暂延迟后重新开始录制
  1150. setTimeout(() => {
  1151. this.startBrowserRecording();
  1152. }, 500);
  1153. }
  1154. // 如果是小程序环境,需要重置相机录制
  1155. else if (this.cameraContext) {
  1156. // 停止当前录制
  1157. this.cameraContext.stopRecord({
  1158. success: () => {
  1159. console.log('重置录制:停止当前录制成功');
  1160. // 短暂延迟后重新开始录制
  1161. setTimeout(() => {
  1162. this.startMiniProgramRecording();
  1163. }, 500);
  1164. },
  1165. fail: (err) => {
  1166. console.error('重置录制:停止当前录制失败', err);
  1167. // 尝试直接重新开始录制
  1168. this.startMiniProgramRecording();
  1169. }
  1170. });
  1171. }
  1172. // 显示提示
  1173. uni.showToast({
  1174. title: '请重新开始回答',
  1175. icon: 'none',
  1176. duration: 2000
  1177. });
  1178. },
  1179. // 添加新方法:完成录制停止流程
  1180. completeRecordingStop() {
  1181. this.isRecording = false;
  1182. // 清除定时器
  1183. if (this.recordingTimer) {
  1184. clearTimeout(this.recordingTimer);
  1185. this.recordingTimer = null;
  1186. }
  1187. // 隐藏录制中提示
  1188. uni.hideLoading();
  1189. // 隐藏停止录制按钮
  1190. this.showStopRecordingButton = false;
  1191. // 根据平台选择不同的停止录制方法
  1192. const systemInfo = uni.getSystemInfoSync();
  1193. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1194. if (isMiniProgram) {
  1195. // 小程序环境停止录制
  1196. this.stopMiniProgramRecording();
  1197. } else {
  1198. // H5/App环境停止录制
  1199. this.stopBrowserRecording();
  1200. }
  1201. },
  1202. // 修改 stopMiniProgramRecording 方法
  1203. stopMiniProgramRecording() {
  1204. if (!this.cameraContext) {
  1205. console.error('相机上下文不存在');
  1206. this.proceedToNextQuestion();
  1207. return;
  1208. }
  1209. const systemInfo = uni.getSystemInfoSync();
  1210. const isIOS = systemInfo.platform === 'ios';
  1211. // 在iOS上添加额外的错误处理
  1212. const stopOptions = {
  1213. success: (res) => {
  1214. console.log('小程序录像停止成功:', res);
  1215. // 获取临时文件路径
  1216. const tempFilePath = res.tempVideoPath;
  1217. if (!tempFilePath) {
  1218. console.error('未获取到视频文件路径');
  1219. uni.showToast({
  1220. title: '录制失败,未获取到视频文件',
  1221. icon: 'none'
  1222. });
  1223. this.proceedToNextQuestion();
  1224. return;
  1225. }
  1226. // 在iOS上,检查文件是否存在
  1227. if (isIOS) {
  1228. uni.getFileInfo({
  1229. filePath: tempFilePath,
  1230. success: () => {
  1231. // 文件存在,上传视频
  1232. this.uploadRecordedVideo(tempFilePath);
  1233. },
  1234. fail: (err) => {
  1235. console.error('视频文件不存在:', err);
  1236. uni.showToast({
  1237. title: '录制失败,视频文件不存在',
  1238. icon: 'none'
  1239. });
  1240. this.proceedToNextQuestion();
  1241. }
  1242. });
  1243. } else {
  1244. // Android直接上传
  1245. this.uploadRecordedVideo(tempFilePath);
  1246. }
  1247. },
  1248. fail: (err) => {
  1249. console.error('小程序录像停止失败:', err);
  1250. uni.showToast({
  1251. title: '录制失败',
  1252. icon: 'none'
  1253. });
  1254. this.proceedToNextQuestion();
  1255. }
  1256. };
  1257. // 停止录制
  1258. this.cameraContext.stopRecord(stopOptions);
  1259. },
  1260. // 添加新方法:停止浏览器录制
  1261. stopBrowserRecording() {
  1262. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1263. this.mediaRecorder.stop();
  1264. console.log('浏览器录制停止成功');
  1265. } else {
  1266. console.error('MediaRecorder不存在或已经停止');
  1267. this.proceedToNextQuestion();
  1268. }
  1269. },
  1270. // 修改上传录制的视频方法
  1271. uploadRecordedVideo(fileOrPath) {
  1272. console.log('准备上传视频:', typeof fileOrPath === 'string' ? fileOrPath : fileOrPath.name);
  1273. // 显示上传中提示
  1274. uni.showLoading({
  1275. title: '正在上传...',
  1276. mask: true
  1277. });
  1278. // 获取用户信息
  1279. const userInfo = uni.getStorageSync('userInfo');
  1280. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1281. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1282. // 根据当前视频索引映射到正确的问题ID
  1283. let questionId;
  1284. switch(this.currentVideoIndex) {
  1285. case 1: // 第一个问题
  1286. questionId = 10;
  1287. break;
  1288. case 2: // 第二个问题
  1289. questionId = 11;
  1290. break;
  1291. case 3: // 第三个问题
  1292. questionId = 12;
  1293. break;
  1294. case 4: // 第四个问题
  1295. questionId = 13;
  1296. break;
  1297. default:
  1298. questionId = 10; // 默认值
  1299. }
  1300. // 检查是否在浏览器环境中使用File对象
  1301. if (typeof fileOrPath !== 'string') {
  1302. // 在浏览器环境中,我们需要将File对象转换为FormData
  1303. const formData = new FormData();
  1304. formData.append('file', fileOrPath);
  1305. formData.append('openid', openid);
  1306. formData.append('tenant_id', tenant_id);
  1307. formData.append('application_id', uni.getStorageSync('appId'));
  1308. formData.append('question_id', questionId);
  1309. formData.append('video_duration', 0);
  1310. formData.append('has_audio', 'true'); // 明确标记视频包含音频
  1311. // 使用XMLHttpRequest直接上传
  1312. const xhr = new XMLHttpRequest();
  1313. xhr.open('POST', `${apiBaseUrl}/api/system/upload/`, true);
  1314. xhr.timeout = 120000; // 30秒超时
  1315. xhr.onload = () => {
  1316. uni.hideLoading();
  1317. if (xhr.status === 200) {
  1318. try {
  1319. const res = JSON.parse(xhr.responseText);
  1320. console.log('上传响应:', res);
  1321. if (res.code === 2000) {
  1322. // 获取上传后的视频URL
  1323. const videoUrl = res.data.url || res.data.photoUrl || '';
  1324. if (videoUrl) {
  1325. // 提交到面试接口
  1326. this.submitVideoToInterview(videoUrl);
  1327. } else {
  1328. uni.showToast({
  1329. title: '视频URL获取失败',
  1330. icon: 'none'
  1331. });
  1332. }
  1333. } else {
  1334. uni.showToast({
  1335. title: res.msg || '上传失败,请重试',
  1336. icon: 'none'
  1337. });
  1338. }
  1339. } catch (e) {
  1340. console.error('解析响应失败:', e);
  1341. uni.showToast({
  1342. title: '解析响应失败',
  1343. icon: 'none'
  1344. });
  1345. }
  1346. } else {
  1347. uni.showToast({
  1348. title: '上传失败,HTTP状态: ' + xhr.status,
  1349. icon: 'none'
  1350. });
  1351. }
  1352. };
  1353. xhr.onerror = () => {
  1354. uni.hideLoading();
  1355. console.error('上传网络错误');
  1356. uni.showToast({
  1357. title: '网络错误,请重试',
  1358. icon: 'none'
  1359. });
  1360. };
  1361. xhr.upload.onprogress = (event) => {
  1362. if (event.lengthComputable) {
  1363. const percentComplete = Math.round((event.loaded / event.total) * 100);
  1364. console.log('上传进度:', percentComplete + '%');
  1365. }
  1366. };
  1367. xhr.send(formData);
  1368. return;
  1369. }
  1370. // 对于小程序环境,使用uni.uploadFile
  1371. uni.uploadFile({
  1372. url: `${apiBaseUrl}/api/system/upload/`,
  1373. filePath: fileOrPath,
  1374. name: 'file',
  1375. formData: {
  1376. openid: openid,
  1377. tenant_id: tenant_id,
  1378. application_id: uni.getStorageSync('appId'),
  1379. question_id: questionId,
  1380. video_duration: 0,
  1381. has_audio: 'true' // 明确标记视频包含音频
  1382. },
  1383. success: (uploadRes) => {
  1384. try {
  1385. const res = JSON.parse(uploadRes.data);
  1386. console.log('上传响应:', res);
  1387. if (res.code === 2000) {
  1388. // 获取上传后的视频URL
  1389. const videoUrl = res.data.permanent_link || res.data.url || '';
  1390. if (videoUrl) {
  1391. // 提交到面试接口
  1392. this.submitVideoToInterview(videoUrl);
  1393. } else {
  1394. uni.showToast({
  1395. title: '视频URL获取失败',
  1396. icon: 'none'
  1397. });
  1398. }
  1399. } else {
  1400. uni.showToast({
  1401. title: res.msg || '上传失败,请重试',
  1402. icon: 'none'
  1403. });
  1404. }
  1405. } catch (e) {
  1406. console.error('解析响应失败:', e);
  1407. uni.showToast({
  1408. title: '解析响应失败',
  1409. icon: 'none'
  1410. });
  1411. }
  1412. },
  1413. fail: (err) => {
  1414. console.error('上传失败:', err);
  1415. uni.showToast({
  1416. title: '上传失败,请重试',
  1417. icon: 'none'
  1418. });
  1419. },
  1420. complete: () => {
  1421. uni.hideLoading();
  1422. }
  1423. });
  1424. },
  1425. // 修改 submitVideoToInterview 方法
  1426. submitVideoToInterview(videoUrl) {
  1427. console.log('提交视频URL到面试接口:', videoUrl);
  1428. // 显示加载提示
  1429. uni.showLoading({
  1430. title: '正在提交...',
  1431. mask: true
  1432. });
  1433. // 根据当前视频索引映射到正确的问题ID
  1434. let questionId;
  1435. switch(this.currentVideoIndex) {
  1436. case 1: // 第一个问题(索引1是第二个视频,第一个是介绍视频)
  1437. questionId = 10;
  1438. break;
  1439. case 2: // 第二个问题
  1440. questionId = 11;
  1441. break;
  1442. case 3: // 第三个问题
  1443. questionId = 12;
  1444. break;
  1445. case 4: // 第四个问题
  1446. questionId = 13;
  1447. break;
  1448. default:
  1449. questionId = 10; // 默认值,以防万一
  1450. }
  1451. // 准备请求参数,使用映射后的问题ID
  1452. const requestData = {
  1453. application_id: uni.getStorageSync('appId'),
  1454. question_id: questionId,
  1455. video_url: videoUrl,
  1456. tenant_id: uni.getStorageSync('tenant_id') || '1'
  1457. };
  1458. // 发送请求到面试接口,使用form-data格式
  1459. uni.request({
  1460. url: `${apiBaseUrl}/api/job/upload_question_video`,
  1461. method: 'POST',
  1462. data: requestData, // 使用data而不是formData
  1463. header: {
  1464. 'content-type': 'application/x-www-form-urlencoded' // 修改为form-data格式的content-type
  1465. },
  1466. success: (res) => {
  1467. console.log('面试接口提交成功:', res);
  1468. uni.hideLoading();
  1469. if (res.data.code === 0 || res.data.code === 2000) {
  1470. // 提交成功
  1471. uni.showToast({
  1472. title: '回答已提交',
  1473. icon: 'success'
  1474. });
  1475. // 保存最后上传的视频URL
  1476. this.lastUploadedVideoUrl = videoUrl;
  1477. // 隐藏重试按钮(如果之前显示了)
  1478. this.showRetryButton = false;
  1479. this.lastVideoToRetry = null;
  1480. // 延迟一下再进入下一题,让用户看到成功提示
  1481. setTimeout(() => {
  1482. // 检查是否完成了第五个视频问题(索引为4)
  1483. if (this.currentVideoIndex === 4) {
  1484. // 完成第五个问题后,跳转到相机页面
  1485. uni.navigateTo({
  1486. url: '/pages/camera/camera'
  1487. });
  1488. } else {
  1489. // 否则继续正常流程
  1490. this.proceedToNextQuestion();
  1491. }
  1492. }, 1500);
  1493. } else {
  1494. // 提交失败
  1495. uni.showToast({
  1496. title: res.data.msg || '提交失败,请重试',
  1497. icon: 'none'
  1498. });
  1499. // 保存失败的视频URL,用于重试
  1500. this.lastVideoToRetry = videoUrl;
  1501. // 显示重试按钮
  1502. this.showRetryButton = true;
  1503. }
  1504. },
  1505. fail: (err) => {
  1506. console.error('面试接口提交失败:', err);
  1507. uni.hideLoading();
  1508. uni.showToast({
  1509. title: '网络错误,请重试',
  1510. icon: 'none'
  1511. });
  1512. // 保存失败的视频URL,用于重试
  1513. this.lastVideoToRetry = videoUrl;
  1514. // 显示重试按钮
  1515. this.showRetryButton = true;
  1516. }
  1517. });
  1518. },
  1519. // 修改 proceedToNextQuestion 方法
  1520. proceedToNextQuestion() {
  1521. // 检查是否完成了第五个视频问题(索引为4)
  1522. if (this.currentVideoIndex === 4) {
  1523. // 完成第五个问题后,跳转到相机页面
  1524. uni.navigateTo({
  1525. url: '/pages/camera/camera'
  1526. });
  1527. return;
  1528. }
  1529. // 切换到下一个视频
  1530. this.currentVideoIndex++;
  1531. if (this.currentVideoIndex < this.videoList.length) {
  1532. // 还有下一段视频,播放它
  1533. this.videoUrl = this.videoList[this.currentVideoIndex];
  1534. this.videoPlaying = true;
  1535. // 重置当前字幕
  1536. this.currentSubtitle = '';
  1537. // 使用 nextTick 确保 DOM 更新后再播放视频
  1538. this.$nextTick(() => {
  1539. const videoContext = uni.createVideoContext('myVideo', this);
  1540. if (videoContext) {
  1541. videoContext.play();
  1542. }
  1543. });
  1544. } else {
  1545. // 所有视频都播放完毕,可以进行下一步操作
  1546. uni.showToast({
  1547. title: '面试完成',
  1548. icon: 'success',
  1549. duration: 2000
  1550. });
  1551. // 可以在这里添加面试完成后的逻辑,比如跳转到下一个页面
  1552. setTimeout(() => {
  1553. uni.navigateTo({
  1554. url: '/pages/interview-result/interview-result'
  1555. });
  1556. }, 2000);
  1557. }
  1558. },
  1559. // 修改 handleAnswerButtonClick 方法
  1560. handleAnswerButtonClick() {
  1561. // 隐藏答题按钮
  1562. this.showAnswerButton = false;
  1563. // 直接进入下一个问题
  1564. this.proceedToNextQuestion();
  1565. },
  1566. // 处理相机错误
  1567. handleCameraError(e) {
  1568. console.error('相机错误:', e);
  1569. // 获取平台信息
  1570. const systemInfo = uni.getSystemInfoSync();
  1571. const isIOS = systemInfo.platform === 'ios';
  1572. if (isIOS) {
  1573. console.log('iOS相机错误,尝试重新初始化');
  1574. // 显示提示
  1575. uni.showToast({
  1576. title: '相机初始化中...',
  1577. icon: 'loading',
  1578. duration: 2000
  1579. });
  1580. // 重置相机
  1581. this.resetCamera();
  1582. // 如果正在录制,停止录制
  1583. if (this.isRecording) {
  1584. this.isRecording = false;
  1585. this.showStopRecordingButton = false;
  1586. // 提供替代选项
  1587. setTimeout(() => {
  1588. this.useAlternativeRecordingMethod();
  1589. }, 1000);
  1590. }
  1591. } else {
  1592. // 显示错误提示
  1593. uni.showToast({
  1594. title: '相机初始化失败,请检查权限设置',
  1595. icon: 'none'
  1596. });
  1597. // 尝试备用选项
  1598. this.tryFallbackOptions();
  1599. }
  1600. },
  1601. // 添加新方法:尝试备用选项
  1602. tryFallbackOptions() {
  1603. // 检查环境
  1604. const systemInfo = uni.getSystemInfoSync();
  1605. // 在小程序环境中使用小程序API
  1606. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  1607. this.useMiniProgramCamera();
  1608. }
  1609. // 在H5环境中显示静态图像
  1610. else {
  1611. this.showStaticCameraPlaceholder();
  1612. }
  1613. },
  1614. // 添加新方法:使用小程序相机API
  1615. useMiniProgramCamera() {
  1616. console.log('尝试使用小程序相机组件');
  1617. // 这里需要在模板中添加小程序相机组件
  1618. // 并设置一个标志来控制显示
  1619. this.useMiniProgramCameraComponent = true;
  1620. },
  1621. // 添加新方法:显示静态图像
  1622. showStaticCameraPlaceholder() {
  1623. console.log('显示静态摄像头占位图');
  1624. // 创建一个图像元素
  1625. const img = document.createElement('img');
  1626. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  1627. img.className = 'static-camera-image';
  1628. img.style.width = '100%';
  1629. img.style.height = '100%';
  1630. img.style.objectFit = 'cover';
  1631. // 获取容器并添加图像
  1632. const container = this.$refs.userCameraVideo.parentNode;
  1633. container.appendChild(img);
  1634. },
  1635. // 处理视频时间更新事件
  1636. handleTimeUpdate(e) {
  1637. // 获取当前视频播放时间
  1638. const currentTime = e.target.currentTime;
  1639. // 如果正在录制,更新录制时间显示
  1640. if (this.isRecording && this.recordingTimerCount) {
  1641. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1642. }
  1643. // 根据当前播放的视频索引选择对应的字幕数组
  1644. let currentSubtitles;
  1645. if (this.currentVideoIndex === 0) {
  1646. currentSubtitles = this.subtitles;
  1647. } else if (this.currentVideoIndex === 1) {
  1648. currentSubtitles = this.secondVideoSubtitles;
  1649. }else if (this.currentVideoIndex === 2) {
  1650. currentSubtitles = this.thirdVideoSubtitles;
  1651. }else if (this.currentVideoIndex === 3) {
  1652. currentSubtitles = this.fourthVideoSubtitles;
  1653. }else if (this.currentVideoIndex === 4) {
  1654. currentSubtitles = this.fifthVideoSubtitles;
  1655. }else if (this.currentVideoIndex === 5) {
  1656. currentSubtitles = this.sixthVideoSubtitles;
  1657. }else {
  1658. // 如果有更多视频,可以继续添加条件
  1659. currentSubtitles = [];
  1660. }
  1661. // 查找当前时间应该显示的字幕
  1662. const subtitle = currentSubtitles.find(
  1663. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  1664. );
  1665. // 更新当前字幕
  1666. this.currentSubtitle = subtitle ? subtitle.text : '';
  1667. },
  1668. // Add a new method to handle the "Start Recording" button click
  1669. handleStartRecordingClick() {
  1670. // 隐藏"开始回答"按钮
  1671. this.showStartRecordingButton = false;
  1672. // 开始录制
  1673. this.startRecordingAnswer();
  1674. },
  1675. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1676. checkAudioPermission() {
  1677. // 在小程序环境中
  1678. const systemInfo = uni.getSystemInfoSync();
  1679. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1680. if (isMiniProgram) {
  1681. // 小程序环境下的权限请求
  1682. uni.getSetting({
  1683. success: (res) => {
  1684. if (!res.authSetting['scope.record']) {
  1685. uni.authorize({
  1686. scope: 'scope.record',
  1687. success: () => {
  1688. console.log('录音权限已获取');
  1689. },
  1690. fail: (err) => {
  1691. console.error('录音权限获取失败:', err);
  1692. this.showPermissionDialog('录音');
  1693. }
  1694. });
  1695. }
  1696. if (!res.authSetting['scope.camera']) {
  1697. uni.authorize({
  1698. scope: 'scope.camera',
  1699. success: () => {
  1700. console.log('相机权限已获取');
  1701. },
  1702. fail: (err) => {
  1703. console.error('相机权限获取失败:', err);
  1704. this.showPermissionDialog('相机');
  1705. }
  1706. });
  1707. }
  1708. }
  1709. });
  1710. } else {
  1711. // 浏览器环境下的权限请求
  1712. // ... 保持原有代码不变
  1713. }
  1714. },
  1715. // 添加新方法:测试音频输入
  1716. testAudioInput() {
  1717. if (!this.cameraStream) {
  1718. console.warn('没有可用的媒体流,无法测试音频');
  1719. return;
  1720. }
  1721. const audioTracks = this.cameraStream.getAudioTracks();
  1722. if (audioTracks.length === 0) {
  1723. console.warn('没有检测到音频轨道,尝试重新获取');
  1724. this.tryGetAudioOnly();
  1725. return;
  1726. }
  1727. console.log('音频轨道信息:', audioTracks[0].getSettings());
  1728. // 创建音频上下文和分析器
  1729. try {
  1730. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1731. if (!AudioContext) {
  1732. console.warn('浏览器不支持AudioContext');
  1733. return;
  1734. }
  1735. const audioContext = new AudioContext();
  1736. const analyser = audioContext.createAnalyser();
  1737. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1738. microphone.connect(analyser);
  1739. // 设置分析器
  1740. analyser.fftSize = 256;
  1741. const bufferLength = analyser.frequencyBinCount;
  1742. const dataArray = new Uint8Array(bufferLength);
  1743. // 检测音频输入
  1744. let silenceCounter = 0;
  1745. const checkAudio = () => {
  1746. if (this.isRecording) return; // 如果正在录制,停止检测
  1747. analyser.getByteFrequencyData(dataArray);
  1748. // 计算平均音量
  1749. let sum = 0;
  1750. for (let i = 0; i < bufferLength; i++) {
  1751. sum += dataArray[i];
  1752. }
  1753. const average = sum / bufferLength;
  1754. if (average > 10) {
  1755. console.log('检测到音频输入,音量:', average);
  1756. silenceCounter = 0;
  1757. } else {
  1758. silenceCounter++;
  1759. if (silenceCounter > 10) {
  1760. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  1761. silenceCounter = 0;
  1762. }
  1763. }
  1764. // 继续检测
  1765. requestAnimationFrame(checkAudio);
  1766. };
  1767. // 开始检测
  1768. checkAudio();
  1769. } catch (e) {
  1770. console.error('音频测试失败:', e);
  1771. }
  1772. },
  1773. // 添加新方法:尝试单独获取音频
  1774. tryGetAudioOnly() {
  1775. navigator.mediaDevices.getUserMedia({ audio: true })
  1776. .then(audioStream => {
  1777. // 如果已有视频流,合并音频和视频轨道
  1778. if (this.cameraStream) {
  1779. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1780. const audioTrack = audioStream.getAudioTracks()[0];
  1781. // 创建新的媒体流,包含视频和音频轨道
  1782. const combinedStream = new MediaStream();
  1783. if (videoTrack) combinedStream.addTrack(videoTrack);
  1784. if (audioTrack) combinedStream.addTrack(audioTrack);
  1785. // 更新摄像头流
  1786. this.cameraStream = combinedStream;
  1787. // 更新视频元素的源
  1788. const videoElement = this.$refs.userCameraVideo;
  1789. if (videoElement) {
  1790. videoElement.srcObject = combinedStream;
  1791. videoElement.muted = true; // 避免回声,但仍然录制声音
  1792. }
  1793. console.log('成功合并音频和视频轨道');
  1794. } else {
  1795. console.warn('没有视频流可合并');
  1796. }
  1797. })
  1798. .catch(err => {
  1799. console.error('单独获取音频失败:', err);
  1800. });
  1801. },
  1802. // 添加新方法:显示权限对话框
  1803. showPermissionDialog(permissionType) {
  1804. uni.showModal({
  1805. title: '需要权限',
  1806. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1807. confirmText: '去设置',
  1808. success: (res) => {
  1809. if (res.confirm) {
  1810. uni.openSetting({
  1811. success: (settingRes) => {
  1812. console.log('设置页面打开成功', settingRes);
  1813. }
  1814. });
  1815. }
  1816. }
  1817. });
  1818. },
  1819. // 添加重试上传方法
  1820. retryVideoUpload() {
  1821. if (this.lastVideoToRetry) {
  1822. // 隐藏重试按钮
  1823. this.showRetryButton = false;
  1824. // 显示加载提示
  1825. uni.showLoading({
  1826. title: '正在重新提交...',
  1827. mask: true
  1828. });
  1829. // 重新提交视频
  1830. this.submitVideoToInterview(this.lastVideoToRetry);
  1831. } else {
  1832. uni.showToast({
  1833. title: '没有可重试的视频',
  1834. icon: 'none'
  1835. });
  1836. }
  1837. },
  1838. // 添加一个新方法用于压缩视频
  1839. async compressVideo(videoBlob) {
  1840. // 如果视频大小小于某个阈值,直接返回原视频
  1841. if (videoBlob.size < 5 * 1024 * 1024) { // 小于5MB
  1842. return videoBlob;
  1843. }
  1844. console.log('开始压缩视频,原始大小:', videoBlob.size);
  1845. // 创建一个视频元素来加载视频
  1846. const videoElement = document.createElement('video');
  1847. videoElement.muted = true;
  1848. videoElement.autoplay = false;
  1849. // 创建一个canvas元素用于绘制视频帧
  1850. const canvas = document.createElement('canvas');
  1851. const ctx = canvas.getContext('2d');
  1852. // 设置视频源
  1853. videoElement.src = URL.createObjectURL(videoBlob);
  1854. return new Promise((resolve) => {
  1855. videoElement.onloadedmetadata = () => {
  1856. // 设置canvas尺寸为视频的一半
  1857. const width = Math.floor(videoElement.videoWidth / 2);
  1858. const height = Math.floor(videoElement.videoHeight / 2);
  1859. canvas.width = width;
  1860. canvas.height = height;
  1861. // 创建MediaRecorder来录制canvas
  1862. const stream = canvas.captureStream(15); // 15fps
  1863. // 如果原视频有音轨,添加到新流中
  1864. if (videoElement.captureStream) {
  1865. const originalStream = videoElement.captureStream();
  1866. const audioTracks = originalStream.getAudioTracks();
  1867. if (audioTracks.length > 0) {
  1868. stream.addTrack(audioTracks[0]);
  1869. }
  1870. }
  1871. // 设置较低的比特率
  1872. const options = {
  1873. mimeType: 'video/webm;codecs=vp8,opus',
  1874. audioBitsPerSecond: 64000,
  1875. videoBitsPerSecond: 800000 // 800kbps
  1876. };
  1877. const mediaRecorder = new MediaRecorder(stream, options);
  1878. const chunks = [];
  1879. mediaRecorder.ondataavailable = (e) => {
  1880. if (e.data.size > 0) {
  1881. chunks.push(e.data);
  1882. }
  1883. };
  1884. mediaRecorder.onstop = () => {
  1885. const compressedBlob = new Blob(chunks, { type: 'video/webm' });
  1886. console.log('视频压缩完成,压缩后大小:', compressedBlob.size);
  1887. resolve(compressedBlob);
  1888. };
  1889. // 开始播放视频并录制
  1890. videoElement.onplay = () => {
  1891. mediaRecorder.start(100);
  1892. // 绘制视频帧到canvas
  1893. const drawFrame = () => {
  1894. if (videoElement.paused || videoElement.ended) {
  1895. mediaRecorder.stop();
  1896. return;
  1897. }
  1898. ctx.drawImage(videoElement, 0, 0, width, height);
  1899. requestAnimationFrame(drawFrame);
  1900. };
  1901. drawFrame();
  1902. };
  1903. videoElement.play();
  1904. };
  1905. });
  1906. },
  1907. // 修改 checkIOSCameraRecordPermission 方法
  1908. checkIOSCameraRecordPermission() {
  1909. const systemInfo = uni.getSystemInfoSync();
  1910. if (systemInfo.platform !== 'ios') return;
  1911. // 在iOS上,需要同时请求相机和录音权限
  1912. uni.getSetting({
  1913. success: (res) => {
  1914. // 检查相机权限
  1915. if (!res.authSetting['scope.camera']) {
  1916. uni.authorize({
  1917. scope: 'scope.camera',
  1918. success: () => {
  1919. console.log('iOS相机权限已获取');
  1920. },
  1921. fail: (err) => {
  1922. console.error('iOS相机权限获取失败:', err);
  1923. this.showPermissionDialog('相机');
  1924. }
  1925. });
  1926. }
  1927. // 检查录音权限
  1928. if (!res.authSetting['scope.record']) {
  1929. uni.authorize({
  1930. scope: 'scope.record',
  1931. success: () => {
  1932. console.log('iOS录音权限已获取');
  1933. },
  1934. fail: (err) => {
  1935. console.error('iOS录音权限获取失败:', err);
  1936. this.showPermissionDialog('录音');
  1937. }
  1938. });
  1939. }
  1940. }
  1941. });
  1942. },
  1943. // 添加新方法:检查并修复渲染问题
  1944. checkAndFixRenderingIssues() {
  1945. // 检查全局对象,防止渲染错误
  1946. try {
  1947. // 检查是否有全局变量u
  1948. if (typeof u !== 'undefined' && u) {
  1949. // 检查currentQuestion属性
  1950. if (!u.currentQuestion) {
  1951. console.log('修复: 创建缺失的currentQuestion对象');
  1952. u.currentQuestion = {};
  1953. }
  1954. // 确保isImportant属性存在
  1955. if (u.currentQuestion && typeof u.currentQuestion.isImportant === 'undefined') {
  1956. console.log('修复: 设置缺失的isImportant属性');
  1957. u.currentQuestion.isImportant = false;
  1958. }
  1959. }
  1960. } catch (e) {
  1961. console.log('防御性检查异常:', e);
  1962. }
  1963. },
  1964. // 添加格式化时间的辅助方法
  1965. formatTime(seconds) {
  1966. const minutes = Math.floor(seconds / 60);
  1967. const remainingSeconds = seconds % 60;
  1968. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  1969. },
  1970. }
  1971. }
  1972. </script>
  1973. <style scoped>
  1974. .identity-verify-container {
  1975. padding: 0;
  1976. max-width: 100%;
  1977. margin: 0 auto;
  1978. height: 100vh;
  1979. display: flex;
  1980. flex-direction: column;
  1981. background-color: #f5f5f5;
  1982. }
  1983. .digital-human-container {
  1984. position: relative;
  1985. width: 100%;
  1986. height: 100vh;
  1987. overflow: hidden;
  1988. background-color: #f0f0f0;
  1989. }
  1990. .digital-human-video {
  1991. width: 100%;
  1992. height: 100%;
  1993. display: flex;
  1994. justify-content: center;
  1995. align-items: center;
  1996. }
  1997. /* 用户摄像头容器样式 */
  1998. .user-camera-container {
  1999. position: absolute;
  2000. top: 20px;
  2001. right: 20px;
  2002. width: 100px;
  2003. height: 150px;
  2004. border-radius: 8px;
  2005. overflow: hidden;
  2006. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  2007. z-index: 20;
  2008. border: 1px solid #fff;
  2009. }
  2010. /* 用户摄像头视频样式 */
  2011. .user-camera-video {
  2012. width: 100%;
  2013. height: 100%;
  2014. object-fit: cover;
  2015. background-color: #333;
  2016. }
  2017. .video-player {
  2018. width: 100%;
  2019. height: 100%;
  2020. object-fit: cover;
  2021. outline: none; /* 移除视频获得焦点时的轮廓 */
  2022. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  2023. }
  2024. /* 隐藏视频控制条 */
  2025. video::-webkit-media-controls {
  2026. display: none !important;
  2027. }
  2028. video::-webkit-media-controls-enclosure {
  2029. display: none !important;
  2030. }
  2031. video::-webkit-media-controls-panel {
  2032. display: none !important;
  2033. }
  2034. video::-webkit-media-controls-play-button {
  2035. display: none !important;
  2036. }
  2037. video::-webkit-media-controls-timeline {
  2038. display: none !important;
  2039. }
  2040. video::-webkit-media-controls-current-time-display {
  2041. display: none !important;
  2042. }
  2043. video::-webkit-media-controls-time-remaining-display {
  2044. display: none !important;
  2045. }
  2046. video::-webkit-media-controls-mute-button {
  2047. display: none !important;
  2048. }
  2049. video::-webkit-media-controls-volume-slider {
  2050. display: none !important;
  2051. }
  2052. video::-webkit-media-controls-fullscreen-button {
  2053. display: none !important;
  2054. }
  2055. .subtitle-overlay {
  2056. position: absolute;
  2057. bottom: 50px;
  2058. left: 0;
  2059. width: 100%;
  2060. padding: 15px;
  2061. background-color: rgba(0, 0, 0, 0.7);
  2062. color: white;
  2063. text-align: center;
  2064. font-size: 16px;
  2065. line-height: 1.5;
  2066. z-index: 10;
  2067. border-radius: 4px;
  2068. max-width: 90%;
  2069. margin: 0 auto;
  2070. left: 5%;
  2071. right: 5%;
  2072. }
  2073. .control-panel {
  2074. padding: 15px;
  2075. display: flex;
  2076. justify-content: center;
  2077. }
  2078. .control-button {
  2079. padding: 10px 20px;
  2080. background-color: #4CAF50;
  2081. color: white;
  2082. border: none;
  2083. border-radius: 4px;
  2084. cursor: pointer;
  2085. }
  2086. .loading {
  2087. text-align: center;
  2088. margin: 20px 0;
  2089. font-size: 16px;
  2090. }
  2091. .response-container {
  2092. margin-top: 20px;
  2093. padding: 0 20px;
  2094. display: none; /* 默认隐藏调试信息 */
  2095. }
  2096. /* 当showDebugInfo为true时显示 */
  2097. .showDebugInfo .response-container {
  2098. display: block;
  2099. }
  2100. .response-item {
  2101. padding: 10px;
  2102. border: 1px solid #eee;
  2103. border-radius: 4px;
  2104. margin-bottom: 10px;
  2105. background-color: #f9f9f9;
  2106. }
  2107. .response-content {
  2108. display: flex;
  2109. flex-direction: column;
  2110. }
  2111. .answer-button-container {
  2112. position: absolute;
  2113. top: 75%;
  2114. left: 50%;
  2115. transform: translate(-50%, -50%);
  2116. z-index: 20;
  2117. }
  2118. .answer-button {
  2119. width: 120px;
  2120. height: 120px;
  2121. border-radius: 50%;
  2122. background-color: #6c5ce7; /* 绿色背景,与图片中的颜色类似 */
  2123. color: white;
  2124. font-size: 18px;
  2125. border: none;
  2126. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2127. display: flex;
  2128. justify-content: center;
  2129. align-items: center;
  2130. cursor: pointer;
  2131. animation: pulse 2s infinite;
  2132. }
  2133. @keyframes pulse {
  2134. 0% {
  2135. transform: scale(1);
  2136. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2137. }
  2138. 50% {
  2139. transform: scale(1.05);
  2140. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  2141. }
  2142. 100% {
  2143. transform: scale(1);
  2144. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2145. }
  2146. }
  2147. .stop-recording-button-container {
  2148. position: absolute;
  2149. top: 75%;
  2150. left: 50%;
  2151. transform: translate(-50%, -50%);
  2152. z-index: 20;
  2153. }
  2154. .stop-recording-button {
  2155. width: 120px;
  2156. height: 120px;
  2157. border-radius: 50%;
  2158. background-color: #e74c3c; /* 红色背景 */
  2159. color: white;
  2160. font-size: 18px;
  2161. border: none;
  2162. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2163. display: flex;
  2164. justify-content: center;
  2165. align-items: center;
  2166. cursor: pointer;
  2167. animation: pulse 2s infinite;
  2168. }
  2169. @keyframes pulse {
  2170. 0% {
  2171. transform: scale(1);
  2172. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2173. }
  2174. 50% {
  2175. transform: scale(1.05);
  2176. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  2177. }
  2178. 100% {
  2179. transform: scale(1);
  2180. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2181. }
  2182. }
  2183. /* 录制中的指示器 */
  2184. .recording-indicator {
  2185. position: absolute;
  2186. top: 20px;
  2187. left: 20px;
  2188. display: flex;
  2189. align-items: center;
  2190. background-color: rgba(0, 0, 0, 0.6);
  2191. padding: 5px 10px;
  2192. border-radius: 15px;
  2193. z-index: 20;
  2194. }
  2195. .recording-dot {
  2196. width: 12px;
  2197. height: 12px;
  2198. background-color: #e74c3c;
  2199. border-radius: 50%;
  2200. margin-right: 8px;
  2201. animation: blink 1s infinite;
  2202. }
  2203. .recording-text {
  2204. color: white;
  2205. font-size: 14px;
  2206. }
  2207. @keyframes blink {
  2208. 0% { opacity: 1; }
  2209. 50% { opacity: 0.3; }
  2210. 100% { opacity: 1; }
  2211. }
  2212. .start-recording-button-container {
  2213. position: absolute;
  2214. top: 75%;
  2215. left: 50%;
  2216. transform: translate(-50%, -50%);
  2217. z-index: 20;
  2218. }
  2219. .start-recording-button {
  2220. width: 120px;
  2221. height: 120px;
  2222. border-radius: 50%;
  2223. background-color: #6c5ce7; /* 与开始面试按钮颜色保持一致 */
  2224. color: white;
  2225. font-size: 18px;
  2226. border: none;
  2227. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  2228. display: flex;
  2229. justify-content: center;
  2230. align-items: center;
  2231. cursor: pointer;
  2232. animation: pulse 2s infinite; /* 使用相同的动画 */
  2233. }
  2234. /* 可以删除pulse-green动画,直接使用与开始面试按钮相同的pulse动画 */
  2235. .retry-button-container {
  2236. position: absolute;
  2237. bottom: 50px;
  2238. left: 50%;
  2239. transform: translateX(-50%);
  2240. z-index: 20;
  2241. }
  2242. .retry-button {
  2243. padding: 10px 20px;
  2244. background-color: #3498db; /* 蓝色背景 */
  2245. color: white;
  2246. font-size: 16px;
  2247. border: none;
  2248. border-radius: 4px;
  2249. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  2250. cursor: pointer;
  2251. }
  2252. .retry-button:hover {
  2253. background-color: #2980b9;
  2254. }
  2255. /* 录制时长显示 */
  2256. .recording-timer {
  2257. position: absolute;
  2258. top: 20px;
  2259. left: 130px; /* 放在录制指示器旁边 */
  2260. background-color: rgba(0, 0, 0, 0.6);
  2261. padding: 5px 10px;
  2262. border-radius: 15px;
  2263. z-index: 20;
  2264. }
  2265. .timer-text {
  2266. color: white;
  2267. font-size: 14px;
  2268. font-family: monospace; /* 使用等宽字体,使时间显示更稳定 */
  2269. }
  2270. </style>