identity-verify.vue 61 KB

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