identity-verify.vue 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  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. </camera>
  42. <!-- 在H5/App环境中使用video元素 -->
  43. <video v-else
  44. id="userCamera"
  45. ref="userCameraVideo"
  46. autoplay
  47. playsinline
  48. muted
  49. class="user-camera-video"
  50. :controls="false">
  51. </video>
  52. </div>
  53. <!-- 字幕/文本覆盖区域 -->
  54. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  55. {{ assistantResponse }}
  56. </div> -->
  57. </div>
  58. <!-- 加载状态 -->
  59. <div v-if="loading" class="loading">加载中...</div>
  60. <!-- 控制面板(可选,可以隐藏) -->
  61. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  62. <div v-if="showDebugInfo" class="response-container">
  63. <div v-if="assistantResponse" class="response-item">
  64. <div class="response-content">
  65. <span>助手回复: {{ assistantResponse }}</span>
  66. </div>
  67. </div>
  68. <div v-if="audioTranscript" class="response-item">
  69. <div class="response-content">
  70. <span>音频转写: {{ audioTranscript }}</span>
  71. </div>
  72. </div>
  73. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  74. <div class="response-content">
  75. <span v-if="item.role">角色: {{ item.role }}</span>
  76. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  77. </div>
  78. </div>
  79. </div>
  80. <!-- 添加停止录制按钮 -->
  81. <div class="stop-recording-button-container" v-if="showStopRecordingButton">
  82. <button class="stop-recording-button" @click="stopRecordingAnswer">
  83. 完成作答
  84. </button>
  85. </div>
  86. <!-- 添加录制指示器 -->
  87. <div class="recording-indicator" v-if="isRecording">
  88. <div class="recording-dot"></div>
  89. <span class="recording-text">正在录制...</span>
  90. </div>
  91. <!-- 添加开始回答按钮 -->
  92. <div class="start-recording-button-container" v-if="showStartRecordingButton">
  93. <button class="start-recording-button" @click="handleStartRecordingClick">
  94. 开始作答
  95. </button>
  96. </div>
  97. </div>
  98. </template>
  99. <script>
  100. export default {
  101. name: 'IdentityVerify',
  102. data() {
  103. return {
  104. loading: false,
  105. responses: [],
  106. processedResponses: [],
  107. assistantResponse: '',
  108. audioTranscript: '',
  109. videoPlaying: false,
  110. showDebugInfo: false, // 设置为true可以显示调试信息
  111. videoUrl: 'http://121.36.251.245:9000/minlong/0a0b3516-e0bb-4f6c-874c-8aaaca9d7f8f.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T135206Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=4a79bc77f80ae7344f339717dbe505a3b152140251a6b2c76c1fd5c047b39c74', // 用于存储AI数字人视频URL
  112. showReplayButton: false,
  113. cameraStream: null, // 存储摄像头流
  114. cameraError: null, // 存储摄像头错误信息
  115. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  116. cameraContext: null, // 添加相机上下文
  117. currentSubtitle: '',
  118. subtitles: [
  119. {
  120. startTime: 0, // 开始时间(秒)
  121. endTime: 5, // 结束时间(秒)
  122. text: '你好,我是本次面试的面试官,欢迎参加本公司的线上面试!'
  123. },
  124. {
  125. startTime: 5,
  126. endTime: 13,
  127. text: '面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试'
  128. },
  129. {
  130. startTime: 13,
  131. endTime: 20,
  132. text: '以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。'
  133. }
  134. ],
  135. secondVideoSubtitles: [
  136. {
  137. startTime: 0,
  138. endTime: 10,
  139. text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  140. }
  141. ],
  142. thirdVideoSubtitles: [
  143. {
  144. startTime: 0,
  145. endTime: 3,
  146. text: '在工作中,你如何确保个人防护装备的正确使用?'
  147. }
  148. ],
  149. fourthVideoSubtitles: [
  150. {
  151. startTime: 0,
  152. endTime: 3,
  153. text: '描述一次你与团队合作改善生产流程的经历。'
  154. }
  155. ],
  156. fifthVideoSubtitles: [
  157. {
  158. startTime: 0,
  159. endTime: 6,
  160. text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  161. }
  162. ],
  163. sixthVideoSubtitles: [
  164. {
  165. startTime: 0,
  166. endTime: 5,
  167. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  168. }
  169. ],
  170. showAnswerButton: false, // 控制答题按钮显示
  171. currentVideoIndex: 0, // 当前播放的视频索引
  172. videoList: [
  173. 'http://121.36.251.245:9000/minlong/0a0b3516-e0bb-4f6c-874c-8aaaca9d7f8f.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T135206Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=4a79bc77f80ae7344f339717dbe505a3b152140251a6b2c76c1fd5c047b39c74', // 第一段视频
  174. 'http://121.36.251.245:9000/minlong/9ab3fd68-a2e9-47a7-a05e-a6e2253ef22c.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T143129Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=7a0ebf1058252b5f76895b31a5293d4781b33ede63d32dce148350846aa20621', // 第二段视频
  175. 'http://121.36.251.245:9000/minlong/69406ce9-8d8e-48aa-ba2f-3b12ea5b6a6c.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T144114Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=a43a06217a63f9bb6af975b1a7aa419b6f8e3d77d8c6c9c67d1070edfe60dc43', // 第三段视频
  176. 'http://121.36.251.245:9000/minlong/1cd448b2-16ea-4565-be25-2cf71d1bf7b2.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T144554Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=6ef5b0b160feab053e7d95952cdac62266d1a1eb48999efb17d7a3c4e0b495ed',
  177. 'http://121.36.251.245:9000/minlong/5a9ad6b2-0de8-48e3-8eb7-141a9bee4a9b.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T145623Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=8a2120a4c6b059d64f90620377fdda2536f9fff9c455d5140210ef35990758c6',
  178. 'http://121.36.251.245:9000/minlong/7aafb07e-ab0d-477e-9124-3263d0b7bf6f.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20250416%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250416T145857Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=54ebe67d751c7e44a0f608b26175c9d076685a0e647e7e134cda22bbca2639eb'//结束
  179. ],
  180. isRecording: false,
  181. recordingTimer: null,
  182. showStopRecordingButton: false,
  183. mediaRecorder: null,
  184. recordedChunks: [],
  185. recorder: null,
  186. lastUploadedVideoUrl: '',
  187. showStartRecordingButton: false,
  188. }
  189. },
  190. mounted() {
  191. /* this.fetchData() */
  192. this.playDigitalHumanVideo();
  193. this.checkAudioPermission();
  194. this.initCamera();
  195. // 添加音频测试
  196. setTimeout(() => {
  197. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  198. this.testAudioInput();
  199. }
  200. }, 3000);
  201. },
  202. beforeDestroy() {
  203. // 组件销毁前停止摄像头
  204. this.stopUserCamera();
  205. },
  206. methods: {
  207. // 初始化相机
  208. async initCamera() {
  209. // 检查平台
  210. const systemInfo = uni.getSystemInfoSync();
  211. // 判断是否在小程序环境中
  212. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  213. systemInfo.uniPlatform === 'mp-alipay' ||
  214. systemInfo.uniPlatform === 'mp-baidu';
  215. if (isMiniProgram) {
  216. // 小程序环境使用小程序的相机API
  217. this.useMiniProgramCameraComponent = true;
  218. // 创建相机上下文
  219. this.cameraContext = uni.createCameraContext();
  220. // 确保已获取录音权限
  221. uni.getSetting({
  222. success: (res) => {
  223. if (!res.authSetting['scope.record']) {
  224. uni.authorize({
  225. scope: 'scope.record',
  226. success: () => {
  227. console.log('录音权限已获取');
  228. },
  229. fail: (err) => {
  230. console.error('录音权限获取失败:', err);
  231. this.showPermissionDialog('录音');
  232. }
  233. });
  234. }
  235. if (!res.authSetting['scope.camera']) {
  236. uni.authorize({
  237. scope: 'scope.camera',
  238. success: () => {
  239. console.log('相机权限已获取');
  240. },
  241. fail: (err) => {
  242. console.error('相机权限获取失败:', err);
  243. this.showPermissionDialog('相机');
  244. }
  245. });
  246. }
  247. }
  248. });
  249. } else {
  250. // H5/App环境使用浏览器的API
  251. try {
  252. // 请求摄像头和麦克风权限并获取媒体流
  253. const constraints = {
  254. audio: {
  255. echoCancellation: true,
  256. noiseSuppression: true,
  257. autoGainControl: true
  258. },
  259. video: {
  260. width: { ideal: 640 },
  261. height: { ideal: 480 },
  262. facingMode: 'user'
  263. }
  264. };
  265. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  266. // 保存流以便后续使用
  267. this.cameraStream = stream;
  268. // 检查音频轨道
  269. const audioTracks = stream.getAudioTracks();
  270. console.log('音频轨道数量:', audioTracks.length);
  271. if (audioTracks.length > 0) {
  272. console.log('音频轨道已获取:', audioTracks[0].label);
  273. // 确保音频轨道已启用
  274. audioTracks[0].enabled = true;
  275. } else {
  276. console.warn('未检测到音频轨道,尝试单独获取音频');
  277. this.tryGetAudioOnly();
  278. }
  279. // 将流设置到视频元素
  280. const videoElement = this.$refs.userCameraVideo;
  281. if (videoElement) {
  282. videoElement.srcObject = stream;
  283. videoElement.muted = true; // 避免回声,但仍然录制声音
  284. }
  285. } catch (error) {
  286. console.error('获取摄像头失败:', error);
  287. this.cameraError = error.message || '无法访问摄像头';
  288. // 显示错误提示
  289. uni.showToast({
  290. title: '无法访问摄像头,请检查权限设置',
  291. icon: 'none'
  292. });
  293. }
  294. }
  295. },
  296. // 停止用户摄像头
  297. stopUserCamera() {
  298. if (this.cameraStream) {
  299. // 停止所有轨道
  300. this.cameraStream.getTracks().forEach(track => {
  301. track.stop();
  302. });
  303. this.cameraStream = null;
  304. }
  305. },
  306. async fetchData() {
  307. this.loading = true
  308. this.assistantResponse = ''
  309. this.audioTranscript = ''
  310. this.processedResponses = []
  311. try {
  312. // 使用uni.request代替fetch
  313. const requestTask = uni.request({
  314. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  315. method: 'POST',
  316. header: {
  317. 'Content-Type': 'application/json',
  318. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  319. },
  320. data: {
  321. "model": "qwen-omni-turbo",
  322. "messages": [
  323. {
  324. "role": "user",
  325. "content": [
  326. {
  327. "type": "input_audio",
  328. "input_audio": {
  329. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  330. "format": "wav"
  331. }
  332. },
  333. {
  334. "type": "text",
  335. "text": "这段音频在说什么"
  336. }
  337. ]
  338. }
  339. ],
  340. "stream":true,
  341. "stream_options":{
  342. "include_usage":true
  343. },
  344. "modalities":["text","audio"],
  345. "audio":{"voice":"Cherry","format":"wav"}
  346. },
  347. success: (res) => {
  348. console.log('请求成功,响应数据:', res.data);
  349. // 检查响应数据是否包含多个JSON对象
  350. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  351. // 处理包含多个JSON对象的情况
  352. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  353. chunks.forEach(chunk => {
  354. this.handleStreamResponse(chunk);
  355. });
  356. } else {
  357. // 处理单个响应对象的情况
  358. this.handleStreamResponse(res.data);
  359. }
  360. // 模拟获取到数字人视频并播放
  361. this.playDigitalHumanVideo();
  362. },
  363. fail: (err) => {
  364. console.error('请求失败:', err);
  365. },
  366. complete: () => {
  367. this.loading = false;
  368. }
  369. });
  370. } catch (error) {
  371. console.error('获取数据失败:', error);
  372. this.loading = false;
  373. }
  374. },
  375. handleStreamResponse(data) {
  376. // 处理流式响应数据
  377. if (typeof data === 'string') {
  378. // 处理字符串格式的响应
  379. if (data === '[DONE]') return;
  380. try {
  381. // 移除可能存在的换行符和多余空格
  382. const cleanData = data.trim();
  383. // 检查是否是有效的JSON字符串
  384. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  385. const jsonData = JSON.parse(cleanData);
  386. this.processStreamChunk(jsonData);
  387. }
  388. } catch (e) {
  389. console.error('解析JSON失败:', e, '原始数据:', data);
  390. }
  391. } else {
  392. // 处理对象格式的响应
  393. this.processStreamChunk(data);
  394. }
  395. },
  396. processStreamChunk(chunk) {
  397. if (chunk.choices && chunk.choices.length > 0) {
  398. const choice = chunk.choices[0];
  399. // 处理助手回复内容
  400. if (choice.delta && choice.delta.content) {
  401. this.assistantResponse += choice.delta.content;
  402. }
  403. // 处理音频转写内容
  404. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  405. this.audioTranscript += choice.delta.audio.transcript;
  406. }
  407. // 处理角色和音频转写
  408. if (choice.delta) {
  409. const result = {};
  410. if (choice.delta.role) {
  411. result.role = choice.delta.role;
  412. }
  413. if (choice.delta.audio && choice.delta.audio.transcript) {
  414. result.transcript = choice.delta.audio.transcript;
  415. }
  416. if (Object.keys(result).length > 0) {
  417. this.processedResponses.push(result);
  418. }
  419. }
  420. }
  421. },
  422. processResponseData() {
  423. // 处理返回的数据
  424. this.processedResponses = this.responses.map(item => {
  425. const result = {}
  426. // 处理角色信息
  427. if (item.delta && item.delta.role) {
  428. result.role = item.delta.role
  429. }
  430. // 处理音频转写文本
  431. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  432. result.transcript = item.delta.audio.transcript
  433. }
  434. return result
  435. }).filter(item => Object.keys(item).length > 0)
  436. },
  437. // 播放数字人视频
  438. playDigitalHumanVideo() {
  439. // 设置第一个视频
  440. this.videoUrl = this.videoList[this.currentVideoIndex];
  441. this.videoPlaying = true;
  442. // 使用 uni.createVideoContext 来控制视频
  443. this.$nextTick(() => {
  444. const videoContext = uni.createVideoContext('myVideo', this);
  445. if (videoContext) {
  446. videoContext.play();
  447. // 设置超时检查,确认视频是否真的在播放
  448. setTimeout(() => {
  449. if (this.videoPlaying && this.$refs.videoPlayer) {
  450. console.log('视频应该正在播放');
  451. } else {
  452. console.log('视频可能未成功播放,尝试替代方案');
  453. this.tryAlternativeVideoPath();
  454. }
  455. }, 1000);
  456. } else {
  457. console.error('无法创建视频上下文');
  458. this.tryAlternativeVideoPath();
  459. }
  460. });
  461. },
  462. // 修改 tryAlternativeVideoPath 方法
  463. tryAlternativeVideoPath() {
  464. console.log('尝试使用替代路径');
  465. // 尝试不同的路径格式
  466. const alternativePaths = [
  467. './static/demo.mp4',
  468. '../static/demo.mp4',
  469. 'static/demo.mp4',
  470. '/static/demo.mp4',
  471. // 添加绝对路径
  472. `${window.location.origin}/static/demo.mp4`
  473. ];
  474. // 获取当前路径索引
  475. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  476. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  477. // 设置下一个路径
  478. this.videoUrl = alternativePaths[nextPathIndex];
  479. console.log('尝试新路径:', this.videoUrl);
  480. this.$nextTick(() => {
  481. const videoContext = uni.createVideoContext('myVideo', this);
  482. if (videoContext) {
  483. videoContext.stop();
  484. videoContext.play();
  485. // 检查是否成功播放
  486. setTimeout(() => {
  487. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  488. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  489. this.checkVideoWithAPI();
  490. }
  491. }, 1000);
  492. }
  493. });
  494. },
  495. // 添加新方法:使用uni API检查视频
  496. checkVideoWithAPI() {
  497. // 尝试使用uni.getVideoInfo API检查视频是否可用
  498. uni.getVideoInfo({
  499. src: '/static/demo.mp4',
  500. success: (res) => {
  501. console.log('视频信息获取成功:', res);
  502. // 如果能获取到视频信息,再次尝试播放
  503. this.videoUrl = '/static/demo.mp4';
  504. this.$nextTick(() => {
  505. const videoContext = uni.createVideoContext('myVideo', this);
  506. if (videoContext) {
  507. videoContext.play();
  508. }
  509. });
  510. },
  511. fail: (err) => {
  512. console.error('视频信息获取失败:', err);
  513. // 最后尝试使用uni.chooseVideo API
  514. this.fallbackToLocalVideo();
  515. }
  516. });
  517. },
  518. // 添加新方法:回退到本地视频
  519. fallbackToLocalVideo() {
  520. console.log('尝试使用本地视频资源');
  521. // 检查平台
  522. const platform = uni.getSystemInfoSync().platform;
  523. if (platform === 'android' || platform === 'ios') {
  524. // 移动端可以尝试使用本地资源
  525. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  526. this.$nextTick(() => {
  527. const videoContext = uni.createVideoContext('myVideo', this);
  528. if (videoContext) {
  529. videoContext.play();
  530. }
  531. });
  532. } else {
  533. // 最终回退到静态图片
  534. this.videoPlaying = false;
  535. uni.showToast({
  536. title: '视频加载失败,显示静态图片',
  537. icon: 'none'
  538. });
  539. }
  540. },
  541. // 修改 handleVideoError 方法
  542. handleVideoError(e) {
  543. console.error('视频加载错误:', e);
  544. // 记录更详细的错误信息
  545. if (e && e.detail) {
  546. console.error('详细错误信息:', e.detail);
  547. }
  548. // 检查视频文件是否存在
  549. uni.getFileInfo({
  550. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  551. success: (res) => {
  552. console.log('文件存在,大小:', res.size);
  553. // 文件存在但播放失败,可能是格式问题
  554. this.tryDifferentFormat();
  555. },
  556. fail: (err) => {
  557. console.error('文件不存在或无法访问:', err);
  558. // 尝试不同路径
  559. this.tryAlternativeVideoPath();
  560. }
  561. });
  562. // 如果多次尝试后仍然失败,显示错误信息
  563. uni.showToast({
  564. title: '视频加载失败,请检查文件是否存在',
  565. icon: 'none',
  566. duration: 2000
  567. });
  568. },
  569. // 添加新方法:尝试不同格式
  570. tryDifferentFormat() {
  571. console.log('尝试不同的视频格式');
  572. // 尝试不同的视频格式
  573. const formats = [
  574. { ext: 'mp4', mime: 'video/mp4' },
  575. { ext: 'webm', mime: 'video/webm' },
  576. { ext: 'ogg', mime: 'video/ogg' },
  577. { ext: 'mov', mime: 'video/quicktime' }
  578. ];
  579. // 获取当前文件名(不含扩展名)
  580. const currentPath = this.videoUrl;
  581. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  582. // 尝试下一个格式
  583. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  584. if (nextFormat) {
  585. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  586. console.log('尝试新格式:', this.videoUrl);
  587. this.$nextTick(() => {
  588. const videoContext = uni.createVideoContext('myVideo', this);
  589. if (videoContext) {
  590. videoContext.stop();
  591. videoContext.play();
  592. }
  593. });
  594. } else {
  595. // 所有格式都尝试过了,使用内置资源
  596. this.useBuiltInResource();
  597. }
  598. },
  599. // 添加新方法:使用内置资源
  600. useBuiltInResource() {
  601. console.log('尝试使用内置资源');
  602. // 检查平台
  603. const platform = uni.getSystemInfoSync().platform;
  604. // 根据平台选择合适的视频源
  605. if (platform === 'windows') {
  606. // Windows平台特定处理
  607. // 尝试使用相对于应用根目录的路径
  608. const appRoot = process.env.UNI_INPUT_DIR || '';
  609. this.videoUrl = `./static/demo.mp4`;
  610. // 或者尝试使用file://协议
  611. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  612. console.log('Windows平台尝试路径:', this.videoUrl);
  613. } else if (platform === 'android' || platform === 'ios') {
  614. // 移动端
  615. this.useNativeVideo();
  616. } else {
  617. // Web平台
  618. // 尝试使用完整URL
  619. const baseUrl = window.location.origin;
  620. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  621. console.log('Web平台尝试URL:', this.videoUrl);
  622. }
  623. this.$nextTick(() => {
  624. const videoContext = uni.createVideoContext('myVideo', this);
  625. if (videoContext) {
  626. videoContext.play();
  627. }
  628. });
  629. },
  630. // 添加新方法:使用原生视频能力
  631. useNativeVideo() {
  632. console.log('尝试使用原生视频能力');
  633. // 在移动端,可以尝试使用原生视频播放器
  634. uni.chooseVideo({
  635. sourceType: ['album'],
  636. success: (res) => {
  637. this.videoUrl = res.tempFilePath;
  638. this.$nextTick(() => {
  639. const videoContext = uni.createVideoContext('myVideo', this);
  640. if (videoContext) {
  641. videoContext.play();
  642. }
  643. });
  644. },
  645. fail: () => {
  646. // 如果用户取消选择,回退到静态图片
  647. this.videoPlaying = false;
  648. uni.showToast({
  649. title: '无法加载视频,显示静态图片',
  650. icon: 'none'
  651. });
  652. }
  653. });
  654. },
  655. // 处理视频结束事件
  656. handleVideoEnded() {
  657. console.log('视频播放结束');
  658. this.videoPlaying = false;
  659. // 对于所有视频,都显示相应的按钮
  660. if (this.currentVideoIndex >= 1) {
  661. // 对于问题视频,显示"开始回答"按钮
  662. this.showStartRecordingButton = true;
  663. } else {
  664. // 对于介绍视频(第一个视频),显示"开始面试"按钮
  665. this.showAnswerButton = true;
  666. }
  667. },
  668. // 添加新方法:开始录制用户回答
  669. startRecordingAnswer() {
  670. console.log('开始录制用户回答');
  671. this.isRecording = true;
  672. // 显示录制中的提示
  673. // uni.showLoading({
  674. // title: '正在录制...',
  675. // mask: true
  676. // });
  677. // 根据平台选择不同的录制方法
  678. const systemInfo = uni.getSystemInfoSync();
  679. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  680. if (isMiniProgram) {
  681. // 小程序环境使用小程序的录制API
  682. this.startMiniProgramRecording();
  683. } else {
  684. // H5/App环境使用MediaRecorder API
  685. this.startBrowserRecording();
  686. }
  687. // 显示停止录制按钮
  688. this.showStopRecordingButton = true;
  689. },
  690. // 修改小程序环境下的录制方法
  691. startMiniProgramRecording() {
  692. if (!this.cameraContext) {
  693. this.cameraContext = uni.createCameraContext();
  694. }
  695. // 确保有录音权限
  696. uni.getSetting({
  697. success: (res) => {
  698. const hasRecordAuth = res.authSetting['scope.record'];
  699. const hasCameraAuth = res.authSetting['scope.camera'];
  700. if (!hasRecordAuth || !hasCameraAuth) {
  701. console.warn('缺少必要权限,请求权限');
  702. this.requestMiniProgramPermissions();
  703. return;
  704. }
  705. // 开始录像,确保同时录制音频
  706. const options = {
  707. timeout: 60000, // 最长录制60秒
  708. success: () => {
  709. console.log('小程序录像开始成功');
  710. },
  711. fail: (err) => {
  712. console.error('小程序录像开始失败:', err);
  713. uni.showToast({
  714. title: '录制失败,请检查相机权限',
  715. icon: 'none'
  716. });
  717. this.proceedToNextQuestion();
  718. },
  719. timeoutCallback: () => {
  720. // 录制超时自动停止时的回调
  721. console.log('录制超时自动停止');
  722. this.stopRecordingAnswer();
  723. }
  724. };
  725. this.recorder = this.cameraContext.startRecord(options);
  726. }
  727. });
  728. },
  729. // 添加新方法:请求小程序权限
  730. requestMiniProgramPermissions() {
  731. // 请求录音权限
  732. uni.authorize({
  733. scope: 'scope.record',
  734. success: () => {
  735. console.log('录音权限已获取');
  736. // 请求相机权限
  737. uni.authorize({
  738. scope: 'scope.camera',
  739. success: () => {
  740. console.log('相机权限已获取');
  741. // 权限都获取到后,开始录制
  742. this.startMiniProgramRecording();
  743. },
  744. fail: (err) => {
  745. console.error('相机权限获取失败:', err);
  746. this.showPermissionDialog('相机');
  747. }
  748. });
  749. },
  750. fail: (err) => {
  751. console.error('录音权限获取失败:', err);
  752. this.showPermissionDialog('录音');
  753. }
  754. });
  755. },
  756. // 修改浏览器环境下的录制方法
  757. startBrowserRecording() {
  758. if (!this.cameraStream) {
  759. console.error('没有可用的摄像头流');
  760. uni.showToast({
  761. title: '录制失败,摄像头未就绪',
  762. icon: 'none'
  763. });
  764. this.proceedToNextQuestion();
  765. return;
  766. }
  767. try {
  768. // 检查流中是否包含音频轨道
  769. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  770. if (!hasAudio) {
  771. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  772. // 尝试重新获取带音频的媒体流
  773. navigator.mediaDevices.getUserMedia({
  774. audio: {
  775. echoCancellation: true,
  776. noiseSuppression: true,
  777. autoGainControl: true
  778. },
  779. video: true
  780. })
  781. .then(newStream => {
  782. // 检查新流是否包含音频轨道
  783. const audioTracks = newStream.getAudioTracks();
  784. if (audioTracks.length > 0) {
  785. console.log('成功获取音频轨道:', audioTracks[0].label);
  786. // 合并视频和音频轨道
  787. const videoTrack = this.cameraStream.getVideoTracks()[0];
  788. const audioTrack = newStream.getAudioTracks()[0];
  789. // 创建新的媒体流,包含视频和音频轨道
  790. const combinedStream = new MediaStream();
  791. if (videoTrack) combinedStream.addTrack(videoTrack);
  792. if (audioTrack) combinedStream.addTrack(audioTrack);
  793. this.cameraStream = combinedStream;
  794. // 更新视频元素的源
  795. const videoElement = this.$refs.userCameraVideo;
  796. if (videoElement) {
  797. videoElement.srcObject = combinedStream;
  798. videoElement.muted = true; // 避免回声,但仍然录制声音
  799. }
  800. // 继续录制过程
  801. this.setupMediaRecorder(combinedStream);
  802. } else {
  803. console.warn('仍然无法获取音频轨道');
  804. // 使用现有流继续
  805. this.setupMediaRecorder(this.cameraStream);
  806. }
  807. })
  808. .catch(err => {
  809. console.error('获取音频失败:', err);
  810. // 使用现有流继续
  811. this.setupMediaRecorder(this.cameraStream);
  812. });
  813. } else {
  814. console.log('检测到音频轨道,直接使用');
  815. // 已有音频轨道,直接使用
  816. this.setupMediaRecorder(this.cameraStream);
  817. }
  818. } catch (error) {
  819. console.error('浏览器录制失败:', error);
  820. uni.showToast({
  821. title: '录制失败,浏览器可能不支持此功能',
  822. icon: 'none'
  823. });
  824. this.proceedToNextQuestion();
  825. }
  826. },
  827. // 修改 setupMediaRecorder 方法
  828. setupMediaRecorder(stream) {
  829. // 检查流中的轨道
  830. const videoTracks = stream.getVideoTracks();
  831. const audioTracks = stream.getAudioTracks();
  832. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  833. // 尝试使用支持度更广的编码格式
  834. let mimeType = '';
  835. const supportedTypes = [
  836. 'video/webm;codecs=vp9,opus',
  837. 'video/webm;codecs=vp8,opus',
  838. 'video/webm;codecs=h264,opus',
  839. 'video/mp4;codecs=h264,aac',
  840. 'video/webm',
  841. 'video/mp4'
  842. ];
  843. for (const type of supportedTypes) {
  844. if (MediaRecorder.isTypeSupported(type)) {
  845. mimeType = type;
  846. console.log('使用支持的MIME类型:', mimeType);
  847. break;
  848. }
  849. }
  850. // 创建MediaRecorder实例,设置较高的音频比特率
  851. const options = {
  852. mimeType: mimeType || '',
  853. audioBitsPerSecond: 128000,
  854. videoBitsPerSecond: 2500000
  855. };
  856. try {
  857. this.mediaRecorder = new MediaRecorder(stream, options);
  858. console.log('MediaRecorder创建成功,使用选项:', options);
  859. } catch (e) {
  860. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  861. this.mediaRecorder = new MediaRecorder(stream);
  862. }
  863. // 存储录制的数据块
  864. this.recordedChunks = [];
  865. // 监听数据可用事件
  866. this.mediaRecorder.ondataavailable = (event) => {
  867. if (event.data && event.data.size > 0) {
  868. this.recordedChunks.push(event.data);
  869. console.log(`收到数据块: ${event.data.size} 字节`);
  870. }
  871. };
  872. // 监听录制停止事件
  873. this.mediaRecorder.onstop = () => {
  874. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  875. if (this.recordedChunks.length === 0) {
  876. console.error('没有录制到数据');
  877. uni.showToast({
  878. title: '录制失败,未捕获到数据',
  879. icon: 'none'
  880. });
  881. this.proceedToNextQuestion();
  882. return;
  883. }
  884. // 创建Blob对象
  885. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  886. const blob = new Blob(this.recordedChunks, { type: mimeType });
  887. console.log('创建Blob,大小:', blob.size, '类型:', mimeType);
  888. // 将Blob转换为File对象
  889. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  890. const file = new File([blob], fileName, { type: mimeType });
  891. // 上传文件
  892. this.uploadRecordedVideo(file);
  893. };
  894. // 监听错误
  895. this.mediaRecorder.onerror = (event) => {
  896. console.error('MediaRecorder错误:', event.error);
  897. };
  898. // 开始录制
  899. try {
  900. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  901. console.log('MediaRecorder开始录制');
  902. } catch (e) {
  903. console.error('开始录制失败:', e);
  904. }
  905. },
  906. // 添加新方法:停止录制用户回答
  907. stopRecordingAnswer() {
  908. console.log('停止录制用户回答');
  909. this.isRecording = false;
  910. // 清除定时器
  911. if (this.recordingTimer) {
  912. clearTimeout(this.recordingTimer);
  913. this.recordingTimer = null;
  914. }
  915. // 隐藏录制中提示
  916. uni.hideLoading();
  917. // 隐藏停止录制按钮
  918. this.showStopRecordingButton = false;
  919. // 根据平台选择不同的停止录制方法
  920. const systemInfo = uni.getSystemInfoSync();
  921. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  922. if (isMiniProgram) {
  923. // 小程序环境停止录制
  924. this.stopMiniProgramRecording();
  925. } else {
  926. // H5/App环境停止录制
  927. this.stopBrowserRecording();
  928. }
  929. },
  930. // 添加新方法:停止小程序录制
  931. stopMiniProgramRecording() {
  932. if (!this.cameraContext) {
  933. console.error('相机上下文不存在');
  934. this.proceedToNextQuestion();
  935. return;
  936. }
  937. this.cameraContext.stopRecord({
  938. success: (res) => {
  939. console.log('小程序录像停止成功:', res);
  940. // 获取临时文件路径
  941. const tempFilePath = res.tempVideoPath;
  942. // 上传视频
  943. this.uploadRecordedVideo(tempFilePath);
  944. },
  945. fail: (err) => {
  946. console.error('小程序录像停止失败:', err);
  947. uni.showToast({
  948. title: '录制失败',
  949. icon: 'none'
  950. });
  951. this.proceedToNextQuestion();
  952. }
  953. });
  954. },
  955. // 添加新方法:停止浏览器录制
  956. stopBrowserRecording() {
  957. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  958. this.mediaRecorder.stop();
  959. console.log('浏览器录制停止成功');
  960. } else {
  961. console.error('MediaRecorder不存在或已经停止');
  962. this.proceedToNextQuestion();
  963. }
  964. },
  965. // 修改上传录制的视频方法
  966. uploadRecordedVideo(fileOrPath) {
  967. console.log('准备上传视频:', typeof fileOrPath === 'string' ? fileOrPath : fileOrPath.name);
  968. // 显示上传中提示
  969. uni.showLoading({
  970. title: '正在上传...',
  971. mask: true
  972. });
  973. // 获取用户信息
  974. const userInfo = uni.getStorageSync('userInfo');
  975. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  976. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  977. // 检查是否在浏览器环境中使用File对象
  978. if (typeof fileOrPath !== 'string') {
  979. // 在浏览器环境中,我们需要将File对象转换为FormData
  980. const formData = new FormData();
  981. formData.append('file', fileOrPath);
  982. formData.append('openid', openid);
  983. formData.append('tenant_id', tenant_id);
  984. formData.append('application_id', 1);
  985. formData.append('question_id', this.currentVideoIndex);
  986. formData.append('video_duration', 0);
  987. formData.append('has_audio', 'true'); // 明确标记视频包含音频
  988. // 使用XMLHttpRequest直接上传
  989. const xhr = new XMLHttpRequest();
  990. xhr.open('POST', 'http://192.168.66.187:8083/api/system/upload/', true);
  991. xhr.onload = () => {
  992. uni.hideLoading();
  993. if (xhr.status === 200) {
  994. try {
  995. const res = JSON.parse(xhr.responseText);
  996. console.log('上传响应:', res);
  997. if (res.code === 2000) {
  998. // 获取上传后的视频URL
  999. const videoUrl = res.data.url || res.data.photoUrl || '';
  1000. if (videoUrl) {
  1001. // 提交到面试接口
  1002. this.submitVideoToInterview(videoUrl);
  1003. } else {
  1004. uni.showToast({
  1005. title: '视频URL获取失败',
  1006. icon: 'none'
  1007. });
  1008. }
  1009. } else {
  1010. uni.showToast({
  1011. title: res.msg || '上传失败,请重试',
  1012. icon: 'none'
  1013. });
  1014. }
  1015. } catch (e) {
  1016. console.error('解析响应失败:', e);
  1017. uni.showToast({
  1018. title: '解析响应失败',
  1019. icon: 'none'
  1020. });
  1021. }
  1022. } else {
  1023. uni.showToast({
  1024. title: '上传失败,HTTP状态: ' + xhr.status,
  1025. icon: 'none'
  1026. });
  1027. }
  1028. };
  1029. xhr.onerror = () => {
  1030. uni.hideLoading();
  1031. console.error('上传网络错误');
  1032. uni.showToast({
  1033. title: '网络错误,请重试',
  1034. icon: 'none'
  1035. });
  1036. };
  1037. xhr.upload.onprogress = (event) => {
  1038. if (event.lengthComputable) {
  1039. const percentComplete = Math.round((event.loaded / event.total) * 100);
  1040. console.log('上传进度:', percentComplete + '%');
  1041. }
  1042. };
  1043. xhr.send(formData);
  1044. return;
  1045. }
  1046. // 对于小程序环境,使用uni.uploadFile
  1047. uni.uploadFile({
  1048. url: 'http://192.168.66.187:8083/api/system/upload/',
  1049. filePath: fileOrPath,
  1050. name: 'file',
  1051. formData: {
  1052. openid: openid,
  1053. tenant_id: tenant_id,
  1054. application_id: 1,
  1055. question_id: this.currentVideoIndex,
  1056. video_duration: 0,
  1057. has_audio: 'true' // 明确标记视频包含音频
  1058. },
  1059. success: (uploadRes) => {
  1060. try {
  1061. const res = JSON.parse(uploadRes.data);
  1062. console.log('上传响应:', res);
  1063. if (res.code === 2000) {
  1064. // 获取上传后的视频URL
  1065. const videoUrl = res.data.url || res.data.photoUrl || '';
  1066. if (videoUrl) {
  1067. // 提交到面试接口
  1068. this.submitVideoToInterview(videoUrl);
  1069. } else {
  1070. uni.showToast({
  1071. title: '视频URL获取失败',
  1072. icon: 'none'
  1073. });
  1074. }
  1075. } else {
  1076. uni.showToast({
  1077. title: res.msg || '上传失败,请重试',
  1078. icon: 'none'
  1079. });
  1080. }
  1081. } catch (e) {
  1082. console.error('解析响应失败:', e);
  1083. uni.showToast({
  1084. title: '解析响应失败',
  1085. icon: 'none'
  1086. });
  1087. }
  1088. },
  1089. fail: (err) => {
  1090. console.error('上传失败:', err);
  1091. uni.showToast({
  1092. title: '上传失败,请重试',
  1093. icon: 'none'
  1094. });
  1095. },
  1096. complete: () => {
  1097. uni.hideLoading();
  1098. }
  1099. });
  1100. },
  1101. // 添加新方法:将视频URL提交到面试接口
  1102. submitVideoToInterview(videoUrl) {
  1103. console.log('提交视频URL到面试接口:', videoUrl);
  1104. // 准备请求参数,按照图片中的格式
  1105. const requestData = {
  1106. application_id: 1,
  1107. question_id: this.currentVideoIndex,
  1108. video_url: videoUrl,
  1109. video_duration: 0,
  1110. tenant_id: uni.getStorageSync('tenant_id') || '1'
  1111. };
  1112. // 发送请求到面试接口
  1113. uni.request({
  1114. url: 'http://192.168.66.187:8083/api/job/upload_video',
  1115. method: 'POST',
  1116. data: requestData,
  1117. header: {
  1118. 'content-type': 'application/json'
  1119. },
  1120. success: (res) => {
  1121. console.log('面试接口提交成功:', res);
  1122. uni.hideLoading();
  1123. if (res.data.code === 0 || res.data.code === 2000) {
  1124. // 提交成功
  1125. uni.showToast({
  1126. title: '回答已提交',
  1127. icon: 'success'
  1128. });
  1129. // 保存最后上传的视频URL
  1130. this.lastUploadedVideoUrl = videoUrl;
  1131. // 延迟一下再进入下一题,让用户看到成功提示
  1132. setTimeout(() => {
  1133. this.proceedToNextQuestion();
  1134. }, 1500);
  1135. } else {
  1136. // 提交失败
  1137. uni.showToast({
  1138. title: res.data.msg || '提交失败,请重试',
  1139. icon: 'none'
  1140. });
  1141. }
  1142. },
  1143. fail: (err) => {
  1144. console.error('面试接口提交失败:', err);
  1145. uni.hideLoading();
  1146. uni.showToast({
  1147. title: '网络错误,请重试',
  1148. icon: 'none'
  1149. });
  1150. }
  1151. });
  1152. },
  1153. // 添加新方法:进入下一个问题
  1154. proceedToNextQuestion() {
  1155. // 切换到下一个视频
  1156. this.currentVideoIndex++;
  1157. if (this.currentVideoIndex < this.videoList.length) {
  1158. // 还有下一段视频,播放它
  1159. this.videoUrl = this.videoList[this.currentVideoIndex];
  1160. this.videoPlaying = true;
  1161. // 重置当前字幕
  1162. this.currentSubtitle = '';
  1163. // 使用 nextTick 确保 DOM 更新后再播放视频
  1164. this.$nextTick(() => {
  1165. const videoContext = uni.createVideoContext('myVideo', this);
  1166. if (videoContext) {
  1167. videoContext.play();
  1168. }
  1169. });
  1170. } else {
  1171. // 所有视频都播放完毕,可以进行下一步操作
  1172. uni.showToast({
  1173. title: '面试完成',
  1174. icon: 'success',
  1175. duration: 2000
  1176. });
  1177. // 可以在这里添加面试完成后的逻辑,比如跳转到下一个页面
  1178. setTimeout(() => {
  1179. uni.navigateTo({
  1180. url: '/pages/interview-result/interview-result'
  1181. });
  1182. }, 2000);
  1183. }
  1184. },
  1185. // 修改 handleAnswerButtonClick 方法
  1186. handleAnswerButtonClick() {
  1187. // 隐藏答题按钮
  1188. this.showAnswerButton = false;
  1189. // 直接进入下一个问题
  1190. this.proceedToNextQuestion();
  1191. },
  1192. // 处理相机错误
  1193. handleCameraError(e) {
  1194. console.error('相机错误:', e);
  1195. // 显示错误提示
  1196. uni.showToast({
  1197. title: '相机初始化失败,请检查权限设置',
  1198. icon: 'none'
  1199. });
  1200. // 尝试备用选项
  1201. this.tryFallbackOptions();
  1202. },
  1203. // 添加新方法:尝试备用选项
  1204. tryFallbackOptions() {
  1205. // 检查环境
  1206. const systemInfo = uni.getSystemInfoSync();
  1207. // 在小程序环境中使用小程序API
  1208. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  1209. this.useMiniProgramCamera();
  1210. }
  1211. // 在H5环境中显示静态图像
  1212. else {
  1213. this.showStaticCameraPlaceholder();
  1214. }
  1215. },
  1216. // 添加新方法:使用小程序相机API
  1217. useMiniProgramCamera() {
  1218. console.log('尝试使用小程序相机组件');
  1219. // 这里需要在模板中添加小程序相机组件
  1220. // 并设置一个标志来控制显示
  1221. this.useMiniProgramCameraComponent = true;
  1222. },
  1223. // 添加新方法:显示静态图像
  1224. showStaticCameraPlaceholder() {
  1225. console.log('显示静态摄像头占位图');
  1226. // 创建一个图像元素
  1227. const img = document.createElement('img');
  1228. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  1229. img.className = 'static-camera-image';
  1230. img.style.width = '100%';
  1231. img.style.height = '100%';
  1232. img.style.objectFit = 'cover';
  1233. // 获取容器并添加图像
  1234. const container = this.$refs.userCameraVideo.parentNode;
  1235. container.appendChild(img);
  1236. },
  1237. // 处理视频时间更新事件
  1238. handleTimeUpdate(e) {
  1239. // 获取当前视频播放时间
  1240. const currentTime = e.target.currentTime;
  1241. // 根据当前播放的视频索引选择对应的字幕数组
  1242. let currentSubtitles;
  1243. if (this.currentVideoIndex === 0) {
  1244. currentSubtitles = this.subtitles;
  1245. } else if (this.currentVideoIndex === 1) {
  1246. currentSubtitles = this.secondVideoSubtitles;
  1247. }else if (this.currentVideoIndex === 2) {
  1248. currentSubtitles = this.thirdVideoSubtitles;
  1249. }else if (this.currentVideoIndex === 3) {
  1250. currentSubtitles = this.fourthVideoSubtitles;
  1251. }else if (this.currentVideoIndex === 4) {
  1252. currentSubtitles = this.fifthVideoSubtitles;
  1253. }else if (this.currentVideoIndex === 5) {
  1254. currentSubtitles = this.sixthVideoSubtitles;
  1255. }else {
  1256. // 如果有更多视频,可以继续添加条件
  1257. currentSubtitles = [];
  1258. }
  1259. // 查找当前时间应该显示的字幕
  1260. const subtitle = currentSubtitles.find(
  1261. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  1262. );
  1263. // 更新当前字幕
  1264. this.currentSubtitle = subtitle ? subtitle.text : '';
  1265. },
  1266. // Add a new method to handle the "Start Recording" button click
  1267. handleStartRecordingClick() {
  1268. // 隐藏"开始回答"按钮
  1269. this.showStartRecordingButton = false;
  1270. // 开始录制
  1271. this.startRecordingAnswer();
  1272. },
  1273. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1274. checkAudioPermission() {
  1275. // 在小程序环境中
  1276. const systemInfo = uni.getSystemInfoSync();
  1277. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1278. if (isMiniProgram) {
  1279. // 小程序环境下的权限请求
  1280. uni.getSetting({
  1281. success: (res) => {
  1282. if (!res.authSetting['scope.record']) {
  1283. uni.authorize({
  1284. scope: 'scope.record',
  1285. success: () => {
  1286. console.log('录音权限已获取');
  1287. },
  1288. fail: (err) => {
  1289. console.error('录音权限获取失败:', err);
  1290. this.showPermissionDialog('录音');
  1291. }
  1292. });
  1293. }
  1294. if (!res.authSetting['scope.camera']) {
  1295. uni.authorize({
  1296. scope: 'scope.camera',
  1297. success: () => {
  1298. console.log('相机权限已获取');
  1299. },
  1300. fail: (err) => {
  1301. console.error('相机权限获取失败:', err);
  1302. this.showPermissionDialog('相机');
  1303. }
  1304. });
  1305. }
  1306. }
  1307. });
  1308. } else {
  1309. // 浏览器环境下的权限请求
  1310. // ... 保持原有代码不变
  1311. }
  1312. },
  1313. // 添加新方法:测试音频输入
  1314. testAudioInput() {
  1315. if (!this.cameraStream) {
  1316. console.warn('没有可用的媒体流,无法测试音频');
  1317. return;
  1318. }
  1319. const audioTracks = this.cameraStream.getAudioTracks();
  1320. if (audioTracks.length === 0) {
  1321. console.warn('没有检测到音频轨道,尝试重新获取');
  1322. this.tryGetAudioOnly();
  1323. return;
  1324. }
  1325. console.log('音频轨道信息:', audioTracks[0].getSettings());
  1326. // 创建音频上下文和分析器
  1327. try {
  1328. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1329. if (!AudioContext) {
  1330. console.warn('浏览器不支持AudioContext');
  1331. return;
  1332. }
  1333. const audioContext = new AudioContext();
  1334. const analyser = audioContext.createAnalyser();
  1335. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1336. microphone.connect(analyser);
  1337. // 设置分析器
  1338. analyser.fftSize = 256;
  1339. const bufferLength = analyser.frequencyBinCount;
  1340. const dataArray = new Uint8Array(bufferLength);
  1341. // 检测音频输入
  1342. let silenceCounter = 0;
  1343. const checkAudio = () => {
  1344. if (this.isRecording) return; // 如果正在录制,停止检测
  1345. analyser.getByteFrequencyData(dataArray);
  1346. // 计算平均音量
  1347. let sum = 0;
  1348. for (let i = 0; i < bufferLength; i++) {
  1349. sum += dataArray[i];
  1350. }
  1351. const average = sum / bufferLength;
  1352. if (average > 10) {
  1353. console.log('检测到音频输入,音量:', average);
  1354. silenceCounter = 0;
  1355. } else {
  1356. silenceCounter++;
  1357. if (silenceCounter > 10) {
  1358. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  1359. silenceCounter = 0;
  1360. }
  1361. }
  1362. // 继续检测
  1363. requestAnimationFrame(checkAudio);
  1364. };
  1365. // 开始检测
  1366. checkAudio();
  1367. } catch (e) {
  1368. console.error('音频测试失败:', e);
  1369. }
  1370. },
  1371. // 添加新方法:尝试单独获取音频
  1372. tryGetAudioOnly() {
  1373. navigator.mediaDevices.getUserMedia({ audio: true })
  1374. .then(audioStream => {
  1375. // 如果已有视频流,合并音频和视频轨道
  1376. if (this.cameraStream) {
  1377. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1378. const audioTrack = audioStream.getAudioTracks()[0];
  1379. // 创建新的媒体流,包含视频和音频轨道
  1380. const combinedStream = new MediaStream();
  1381. if (videoTrack) combinedStream.addTrack(videoTrack);
  1382. if (audioTrack) combinedStream.addTrack(audioTrack);
  1383. // 更新摄像头流
  1384. this.cameraStream = combinedStream;
  1385. // 更新视频元素的源
  1386. const videoElement = this.$refs.userCameraVideo;
  1387. if (videoElement) {
  1388. videoElement.srcObject = combinedStream;
  1389. videoElement.muted = true; // 避免回声,但仍然录制声音
  1390. }
  1391. console.log('成功合并音频和视频轨道');
  1392. } else {
  1393. console.warn('没有视频流可合并');
  1394. }
  1395. })
  1396. .catch(err => {
  1397. console.error('单独获取音频失败:', err);
  1398. });
  1399. },
  1400. // 添加新方法:显示权限对话框
  1401. showPermissionDialog(permissionType) {
  1402. uni.showModal({
  1403. title: '需要权限',
  1404. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1405. confirmText: '去设置',
  1406. success: (res) => {
  1407. if (res.confirm) {
  1408. uni.openSetting({
  1409. success: (settingRes) => {
  1410. console.log('设置页面打开成功', settingRes);
  1411. }
  1412. });
  1413. }
  1414. }
  1415. });
  1416. },
  1417. }
  1418. }
  1419. </script>
  1420. <style scoped>
  1421. .identity-verify-container {
  1422. padding: 0;
  1423. max-width: 100%;
  1424. margin: 0 auto;
  1425. height: 100vh;
  1426. display: flex;
  1427. flex-direction: column;
  1428. background-color: #f5f5f5;
  1429. }
  1430. .digital-human-container {
  1431. position: relative;
  1432. width: 100%;
  1433. height: 100vh;
  1434. overflow: hidden;
  1435. background-color: #f0f0f0;
  1436. }
  1437. .digital-human-video {
  1438. width: 100%;
  1439. height: 100%;
  1440. display: flex;
  1441. justify-content: center;
  1442. align-items: center;
  1443. }
  1444. /* 用户摄像头容器样式 */
  1445. .user-camera-container {
  1446. position: absolute;
  1447. top: 20px;
  1448. right: 20px;
  1449. width: 100px;
  1450. height: 150px;
  1451. border-radius: 8px;
  1452. overflow: hidden;
  1453. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  1454. z-index: 20;
  1455. border: 1px solid #fff;
  1456. }
  1457. /* 用户摄像头视频样式 */
  1458. .user-camera-video {
  1459. width: 100%;
  1460. height: 100%;
  1461. object-fit: cover;
  1462. background-color: #333;
  1463. }
  1464. .video-player {
  1465. width: 100%;
  1466. height: 100%;
  1467. object-fit: cover;
  1468. outline: none; /* 移除视频获得焦点时的轮廓 */
  1469. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  1470. }
  1471. /* 隐藏视频控制条 */
  1472. video::-webkit-media-controls {
  1473. display: none !important;
  1474. }
  1475. video::-webkit-media-controls-enclosure {
  1476. display: none !important;
  1477. }
  1478. video::-webkit-media-controls-panel {
  1479. display: none !important;
  1480. }
  1481. video::-webkit-media-controls-play-button {
  1482. display: none !important;
  1483. }
  1484. video::-webkit-media-controls-timeline {
  1485. display: none !important;
  1486. }
  1487. video::-webkit-media-controls-current-time-display {
  1488. display: none !important;
  1489. }
  1490. video::-webkit-media-controls-time-remaining-display {
  1491. display: none !important;
  1492. }
  1493. video::-webkit-media-controls-mute-button {
  1494. display: none !important;
  1495. }
  1496. video::-webkit-media-controls-volume-slider {
  1497. display: none !important;
  1498. }
  1499. video::-webkit-media-controls-fullscreen-button {
  1500. display: none !important;
  1501. }
  1502. .subtitle-overlay {
  1503. position: absolute;
  1504. bottom: 50px;
  1505. left: 0;
  1506. width: 100%;
  1507. padding: 15px;
  1508. background-color: rgba(0, 0, 0, 0.7);
  1509. color: white;
  1510. text-align: center;
  1511. font-size: 16px;
  1512. line-height: 1.5;
  1513. z-index: 10;
  1514. border-radius: 4px;
  1515. max-width: 90%;
  1516. margin: 0 auto;
  1517. left: 5%;
  1518. right: 5%;
  1519. }
  1520. .control-panel {
  1521. padding: 15px;
  1522. display: flex;
  1523. justify-content: center;
  1524. }
  1525. .control-button {
  1526. padding: 10px 20px;
  1527. background-color: #4CAF50;
  1528. color: white;
  1529. border: none;
  1530. border-radius: 4px;
  1531. cursor: pointer;
  1532. }
  1533. .loading {
  1534. text-align: center;
  1535. margin: 20px 0;
  1536. font-size: 16px;
  1537. }
  1538. .response-container {
  1539. margin-top: 20px;
  1540. padding: 0 20px;
  1541. display: none; /* 默认隐藏调试信息 */
  1542. }
  1543. /* 当showDebugInfo为true时显示 */
  1544. .showDebugInfo .response-container {
  1545. display: block;
  1546. }
  1547. .response-item {
  1548. padding: 10px;
  1549. border: 1px solid #eee;
  1550. border-radius: 4px;
  1551. margin-bottom: 10px;
  1552. background-color: #f9f9f9;
  1553. }
  1554. .response-content {
  1555. display: flex;
  1556. flex-direction: column;
  1557. }
  1558. .answer-button-container {
  1559. position: absolute;
  1560. top: 75%;
  1561. left: 50%;
  1562. transform: translate(-50%, -50%);
  1563. z-index: 20;
  1564. }
  1565. .answer-button {
  1566. width: 120px;
  1567. height: 120px;
  1568. border-radius: 50%;
  1569. background-color: #6c5ce7; /* 绿色背景,与图片中的颜色类似 */
  1570. color: white;
  1571. font-size: 18px;
  1572. border: none;
  1573. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1574. display: flex;
  1575. justify-content: center;
  1576. align-items: center;
  1577. cursor: pointer;
  1578. animation: pulse 2s infinite;
  1579. }
  1580. @keyframes pulse {
  1581. 0% {
  1582. transform: scale(1);
  1583. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1584. }
  1585. 50% {
  1586. transform: scale(1.05);
  1587. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  1588. }
  1589. 100% {
  1590. transform: scale(1);
  1591. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1592. }
  1593. }
  1594. .stop-recording-button-container {
  1595. position: absolute;
  1596. top: 75%;
  1597. left: 50%;
  1598. transform: translate(-50%, -50%);
  1599. z-index: 20;
  1600. }
  1601. .stop-recording-button {
  1602. width: 120px;
  1603. height: 120px;
  1604. border-radius: 50%;
  1605. background-color: #e74c3c; /* 红色背景 */
  1606. color: white;
  1607. font-size: 18px;
  1608. border: none;
  1609. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1610. display: flex;
  1611. justify-content: center;
  1612. align-items: center;
  1613. cursor: pointer;
  1614. animation: pulse 2s infinite;
  1615. }
  1616. @keyframes pulse {
  1617. 0% {
  1618. transform: scale(1);
  1619. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1620. }
  1621. 50% {
  1622. transform: scale(1.05);
  1623. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  1624. }
  1625. 100% {
  1626. transform: scale(1);
  1627. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1628. }
  1629. }
  1630. /* 录制中的指示器 */
  1631. .recording-indicator {
  1632. position: absolute;
  1633. top: 20px;
  1634. left: 20px;
  1635. display: flex;
  1636. align-items: center;
  1637. background-color: rgba(0, 0, 0, 0.6);
  1638. padding: 5px 10px;
  1639. border-radius: 15px;
  1640. z-index: 20;
  1641. }
  1642. .recording-dot {
  1643. width: 12px;
  1644. height: 12px;
  1645. background-color: #e74c3c;
  1646. border-radius: 50%;
  1647. margin-right: 8px;
  1648. animation: blink 1s infinite;
  1649. }
  1650. .recording-text {
  1651. color: white;
  1652. font-size: 14px;
  1653. }
  1654. @keyframes blink {
  1655. 0% { opacity: 1; }
  1656. 50% { opacity: 0.3; }
  1657. 100% { opacity: 1; }
  1658. }
  1659. .start-recording-button-container {
  1660. position: absolute;
  1661. top: 75%;
  1662. left: 50%;
  1663. transform: translate(-50%, -50%);
  1664. z-index: 20;
  1665. }
  1666. .start-recording-button {
  1667. width: 120px;
  1668. height: 120px;
  1669. border-radius: 50%;
  1670. background-color: #6c5ce7; /* 与开始面试按钮颜色保持一致 */
  1671. color: white;
  1672. font-size: 18px;
  1673. border: none;
  1674. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1675. display: flex;
  1676. justify-content: center;
  1677. align-items: center;
  1678. cursor: pointer;
  1679. animation: pulse 2s infinite; /* 使用相同的动画 */
  1680. }
  1681. /* 可以删除pulse-green动画,直接使用与开始面试按钮相同的pulse动画 */
  1682. </style>