identity-verify.vue 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. </camera>
  41. <!-- 在H5/App环境中使用video元素 -->
  42. <video v-else
  43. id="userCamera"
  44. ref="userCameraVideo"
  45. autoplay
  46. playsinline
  47. muted
  48. class="user-camera-video"
  49. :controls="false">
  50. </video>
  51. </div>
  52. <!-- 字幕/文本覆盖区域 -->
  53. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  54. {{ assistantResponse }}
  55. </div> -->
  56. </div>
  57. <!-- 加载状态 -->
  58. <div v-if="loading" class="loading">加载中...</div>
  59. <!-- 控制面板(可选,可以隐藏) -->
  60. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  61. <div v-if="showDebugInfo" class="response-container">
  62. <div v-if="assistantResponse" class="response-item">
  63. <div class="response-content">
  64. <span>助手回复: {{ assistantResponse }}</span>
  65. </div>
  66. </div>
  67. <div v-if="audioTranscript" class="response-item">
  68. <div class="response-content">
  69. <span>音频转写: {{ audioTranscript }}</span>
  70. </div>
  71. </div>
  72. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  73. <div class="response-content">
  74. <span v-if="item.role">角色: {{ item.role }}</span>
  75. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  76. </div>
  77. </div>
  78. </div>
  79. </div>
  80. </template>
  81. <script>
  82. export default {
  83. name: 'IdentityVerify',
  84. data() {
  85. return {
  86. loading: false,
  87. responses: [],
  88. processedResponses: [],
  89. assistantResponse: '',
  90. audioTranscript: '',
  91. videoPlaying: false,
  92. showDebugInfo: false, // 设置为true可以显示调试信息
  93. 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
  94. showReplayButton: false,
  95. cameraStream: null, // 存储摄像头流
  96. cameraError: null, // 存储摄像头错误信息
  97. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  98. cameraContext: null, // 添加相机上下文
  99. currentSubtitle: '',
  100. subtitles: [
  101. {
  102. startTime: 0, // 开始时间(秒)
  103. endTime: 5, // 结束时间(秒)
  104. text: '你好,我是本次面试的面试官,欢迎参加本公司的线上面试!'
  105. },
  106. {
  107. startTime: 5,
  108. endTime: 13,
  109. text: '面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试'
  110. },
  111. {
  112. startTime: 13,
  113. endTime: 20,
  114. text: '以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。'
  115. }
  116. ],
  117. secondVideoSubtitles: [
  118. {
  119. startTime: 0,
  120. endTime: 10,
  121. text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  122. }
  123. ],
  124. thirdVideoSubtitles: [
  125. {
  126. startTime: 0,
  127. endTime: 3,
  128. text: '在工作中,你如何确保个人防护装备的正确使用?'
  129. }
  130. ],
  131. fourthVideoSubtitles: [
  132. {
  133. startTime: 0,
  134. endTime: 3,
  135. text: '描述一次你与团队合作改善生产流程的经历。'
  136. }
  137. ],
  138. fifthVideoSubtitles: [
  139. {
  140. startTime: 0,
  141. endTime: 6,
  142. text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  143. }
  144. ],
  145. sixthVideoSubtitles: [
  146. {
  147. startTime: 0,
  148. endTime: 5,
  149. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  150. }
  151. ],
  152. showAnswerButton: false, // 控制答题按钮显示
  153. currentVideoIndex: 0, // 当前播放的视频索引
  154. videoList: [
  155. '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', // 第一段视频
  156. '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', // 第二段视频
  157. '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', // 第三段视频
  158. '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',
  159. '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',
  160. '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'//结束
  161. ],
  162. }
  163. },
  164. mounted() {
  165. /* this.fetchData() */
  166. this.playDigitalHumanVideo();
  167. this.initCamera();
  168. },
  169. beforeDestroy() {
  170. // 组件销毁前停止摄像头
  171. this.stopUserCamera();
  172. },
  173. methods: {
  174. // 初始化相机
  175. initCamera() {
  176. // 检查平台
  177. const systemInfo = uni.getSystemInfoSync();
  178. // 判断是否在小程序环境中
  179. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  180. systemInfo.uniPlatform === 'mp-alipay' ||
  181. systemInfo.uniPlatform === 'mp-baidu' ||
  182. systemInfo.uniPlatform === 'mp-toutiao';
  183. // 设置标志,控制使用哪种相机组件
  184. this.useMiniProgramCameraComponent = isMiniProgram;
  185. if (isMiniProgram) {
  186. // 在小程序环境中使用camera组件
  187. this.$nextTick(() => {
  188. // 创建相机上下文
  189. this.cameraContext = uni.createCameraContext();
  190. });
  191. } else {
  192. // 只在非小程序环境(H5/App)中尝试使用getUserMedia
  193. this.startUserCamera();
  194. }
  195. },
  196. // 启动用户摄像头
  197. async startUserCamera() {
  198. try {
  199. // 首先检查是否在小程序环境中
  200. const systemInfo = uni.getSystemInfoSync();
  201. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  202. if (isMiniProgram) {
  203. // 小程序环境不应该调用这个方法,但如果调用了,直接返回
  204. console.log('小程序环境不支持 getUserMedia API');
  205. return;
  206. }
  207. // 检查是否支持getUserMedia (仅在H5/App环境)
  208. if (!navigator || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
  209. throw new Error('您的浏览器不支持摄像头访问');
  210. }
  211. // 请求摄像头权限并获取媒体流
  212. const stream = await navigator.mediaDevices.getUserMedia({
  213. video: {
  214. width: { ideal: 200 },
  215. height: { ideal: 150 },
  216. facingMode: 'user' // 使用前置摄像头
  217. },
  218. audio: true // 不需要音频
  219. });
  220. this.cameraStream = stream;
  221. // 将摄像头流设置到视频元素
  222. this.$nextTick(() => {
  223. if (this.$refs.userCameraVideo) {
  224. this.$refs.userCameraVideo.srcObject = stream;
  225. }
  226. });
  227. } catch (error) {
  228. console.error('无法访问摄像头:', error);
  229. this.cameraError = error.message;
  230. // 根据错误类型显示不同的错误提示
  231. let errorMessage = '无法访问摄像头,请检查权限设置';
  232. if (error.name === 'NotAllowedError' || error.name === 'PermissionDeniedError') {
  233. errorMessage = '摄像头访问被拒绝,请在浏览器设置中允许摄像头访问';
  234. } else if (error.name === 'NotFoundError' || error.name === 'DevicesNotFoundError') {
  235. errorMessage = '未检测到摄像头设备';
  236. } else if (error.name === 'NotReadableError' || error.name === 'TrackStartError') {
  237. errorMessage = '摄像头可能被其他应用占用';
  238. } else if (error.name === 'OverconstrainedError') {
  239. errorMessage = '摄像头不满足指定的要求';
  240. } else if (error.name === 'TypeError' || error.message.includes('SSL')) {
  241. errorMessage = '请确保在HTTPS环境下访问';
  242. }
  243. // 显示错误提示
  244. uni.showToast({
  245. title: errorMessage,
  246. icon: 'none',
  247. duration: 3000
  248. });
  249. // 添加摄像头错误处理
  250. this.handleCameraError(errorMessage);
  251. }
  252. },
  253. // 停止用户摄像头
  254. stopUserCamera() {
  255. if (this.cameraStream) {
  256. // 停止所有轨道
  257. this.cameraStream.getTracks().forEach(track => {
  258. track.stop();
  259. });
  260. this.cameraStream = null;
  261. }
  262. },
  263. async fetchData() {
  264. this.loading = true
  265. this.assistantResponse = ''
  266. this.audioTranscript = ''
  267. this.processedResponses = []
  268. try {
  269. // 使用uni.request代替fetch
  270. const requestTask = uni.request({
  271. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  272. method: 'POST',
  273. header: {
  274. 'Content-Type': 'application/json',
  275. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  276. },
  277. data: {
  278. "model": "qwen-omni-turbo",
  279. "messages": [
  280. {
  281. "role": "user",
  282. "content": [
  283. {
  284. "type": "input_audio",
  285. "input_audio": {
  286. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  287. "format": "wav"
  288. }
  289. },
  290. {
  291. "type": "text",
  292. "text": "这段音频在说什么"
  293. }
  294. ]
  295. }
  296. ],
  297. "stream":true,
  298. "stream_options":{
  299. "include_usage":true
  300. },
  301. "modalities":["text","audio"],
  302. "audio":{"voice":"Cherry","format":"wav"}
  303. },
  304. success: (res) => {
  305. console.log('请求成功,响应数据:', res.data);
  306. // 检查响应数据是否包含多个JSON对象
  307. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  308. // 处理包含多个JSON对象的情况
  309. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  310. chunks.forEach(chunk => {
  311. this.handleStreamResponse(chunk);
  312. });
  313. } else {
  314. // 处理单个响应对象的情况
  315. this.handleStreamResponse(res.data);
  316. }
  317. // 模拟获取到数字人视频并播放
  318. this.playDigitalHumanVideo();
  319. },
  320. fail: (err) => {
  321. console.error('请求失败:', err);
  322. },
  323. complete: () => {
  324. this.loading = false;
  325. }
  326. });
  327. } catch (error) {
  328. console.error('获取数据失败:', error);
  329. this.loading = false;
  330. }
  331. },
  332. handleStreamResponse(data) {
  333. // 处理流式响应数据
  334. if (typeof data === 'string') {
  335. // 处理字符串格式的响应
  336. if (data === '[DONE]') return;
  337. try {
  338. // 移除可能存在的换行符和多余空格
  339. const cleanData = data.trim();
  340. // 检查是否是有效的JSON字符串
  341. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  342. const jsonData = JSON.parse(cleanData);
  343. this.processStreamChunk(jsonData);
  344. }
  345. } catch (e) {
  346. console.error('解析JSON失败:', e, '原始数据:', data);
  347. }
  348. } else {
  349. // 处理对象格式的响应
  350. this.processStreamChunk(data);
  351. }
  352. },
  353. processStreamChunk(chunk) {
  354. if (chunk.choices && chunk.choices.length > 0) {
  355. const choice = chunk.choices[0];
  356. // 处理助手回复内容
  357. if (choice.delta && choice.delta.content) {
  358. this.assistantResponse += choice.delta.content;
  359. }
  360. // 处理音频转写内容
  361. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  362. this.audioTranscript += choice.delta.audio.transcript;
  363. }
  364. // 处理角色和音频转写
  365. if (choice.delta) {
  366. const result = {};
  367. if (choice.delta.role) {
  368. result.role = choice.delta.role;
  369. }
  370. if (choice.delta.audio && choice.delta.audio.transcript) {
  371. result.transcript = choice.delta.audio.transcript;
  372. }
  373. if (Object.keys(result).length > 0) {
  374. this.processedResponses.push(result);
  375. }
  376. }
  377. }
  378. },
  379. processResponseData() {
  380. // 处理返回的数据
  381. this.processedResponses = this.responses.map(item => {
  382. const result = {}
  383. // 处理角色信息
  384. if (item.delta && item.delta.role) {
  385. result.role = item.delta.role
  386. }
  387. // 处理音频转写文本
  388. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  389. result.transcript = item.delta.audio.transcript
  390. }
  391. return result
  392. }).filter(item => Object.keys(item).length > 0)
  393. },
  394. // 播放数字人视频
  395. playDigitalHumanVideo() {
  396. // 设置第一个视频
  397. this.videoUrl = this.videoList[this.currentVideoIndex];
  398. this.videoPlaying = true;
  399. // 使用 uni.createVideoContext 来控制视频
  400. this.$nextTick(() => {
  401. const videoContext = uni.createVideoContext('myVideo', this);
  402. if (videoContext) {
  403. videoContext.play();
  404. // 设置超时检查,确认视频是否真的在播放
  405. setTimeout(() => {
  406. if (this.videoPlaying && this.$refs.videoPlayer) {
  407. console.log('视频应该正在播放');
  408. } else {
  409. console.log('视频可能未成功播放,尝试替代方案');
  410. this.tryAlternativeVideoPath();
  411. }
  412. }, 1000);
  413. } else {
  414. console.error('无法创建视频上下文');
  415. this.tryAlternativeVideoPath();
  416. }
  417. });
  418. },
  419. // 修改 tryAlternativeVideoPath 方法
  420. tryAlternativeVideoPath() {
  421. console.log('尝试使用替代路径');
  422. // 尝试不同的路径格式
  423. const alternativePaths = [
  424. './static/demo.mp4',
  425. '../static/demo.mp4',
  426. 'static/demo.mp4',
  427. '/static/demo.mp4',
  428. // 添加绝对路径
  429. `${window.location.origin}/static/demo.mp4`
  430. ];
  431. // 获取当前路径索引
  432. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  433. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  434. // 设置下一个路径
  435. this.videoUrl = alternativePaths[nextPathIndex];
  436. console.log('尝试新路径:', this.videoUrl);
  437. this.$nextTick(() => {
  438. const videoContext = uni.createVideoContext('myVideo', this);
  439. if (videoContext) {
  440. videoContext.stop();
  441. videoContext.play();
  442. // 检查是否成功播放
  443. setTimeout(() => {
  444. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  445. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  446. this.checkVideoWithAPI();
  447. }
  448. }, 1000);
  449. }
  450. });
  451. },
  452. // 添加新方法:使用uni API检查视频
  453. checkVideoWithAPI() {
  454. // 尝试使用uni.getVideoInfo API检查视频是否可用
  455. uni.getVideoInfo({
  456. src: '/static/demo.mp4',
  457. success: (res) => {
  458. console.log('视频信息获取成功:', res);
  459. // 如果能获取到视频信息,再次尝试播放
  460. this.videoUrl = '/static/demo.mp4';
  461. this.$nextTick(() => {
  462. const videoContext = uni.createVideoContext('myVideo', this);
  463. if (videoContext) {
  464. videoContext.play();
  465. }
  466. });
  467. },
  468. fail: (err) => {
  469. console.error('视频信息获取失败:', err);
  470. // 最后尝试使用uni.chooseVideo API
  471. this.fallbackToLocalVideo();
  472. }
  473. });
  474. },
  475. // 添加新方法:回退到本地视频
  476. fallbackToLocalVideo() {
  477. console.log('尝试使用本地视频资源');
  478. // 检查平台
  479. const platform = uni.getSystemInfoSync().platform;
  480. if (platform === 'android' || platform === 'ios') {
  481. // 移动端可以尝试使用本地资源
  482. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  483. this.$nextTick(() => {
  484. const videoContext = uni.createVideoContext('myVideo', this);
  485. if (videoContext) {
  486. videoContext.play();
  487. }
  488. });
  489. } else {
  490. // 最终回退到静态图片
  491. this.videoPlaying = false;
  492. uni.showToast({
  493. title: '视频加载失败,显示静态图片',
  494. icon: 'none'
  495. });
  496. }
  497. },
  498. // 修改 handleVideoError 方法
  499. handleVideoError(e) {
  500. console.error('视频加载错误:', e);
  501. // 记录更详细的错误信息
  502. if (e && e.detail) {
  503. console.error('详细错误信息:', e.detail);
  504. }
  505. // 检查视频文件是否存在
  506. uni.getFileInfo({
  507. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  508. success: (res) => {
  509. console.log('文件存在,大小:', res.size);
  510. // 文件存在但播放失败,可能是格式问题
  511. this.tryDifferentFormat();
  512. },
  513. fail: (err) => {
  514. console.error('文件不存在或无法访问:', err);
  515. // 尝试不同路径
  516. this.tryAlternativeVideoPath();
  517. }
  518. });
  519. // 如果多次尝试后仍然失败,显示错误信息
  520. uni.showToast({
  521. title: '视频加载失败,请检查文件是否存在',
  522. icon: 'none',
  523. duration: 2000
  524. });
  525. },
  526. // 添加新方法:尝试不同格式
  527. tryDifferentFormat() {
  528. console.log('尝试不同的视频格式');
  529. // 尝试不同的视频格式
  530. const formats = [
  531. { ext: 'mp4', mime: 'video/mp4' },
  532. { ext: 'webm', mime: 'video/webm' },
  533. { ext: 'ogg', mime: 'video/ogg' },
  534. { ext: 'mov', mime: 'video/quicktime' }
  535. ];
  536. // 获取当前文件名(不含扩展名)
  537. const currentPath = this.videoUrl;
  538. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  539. // 尝试下一个格式
  540. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  541. if (nextFormat) {
  542. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  543. console.log('尝试新格式:', this.videoUrl);
  544. this.$nextTick(() => {
  545. const videoContext = uni.createVideoContext('myVideo', this);
  546. if (videoContext) {
  547. videoContext.stop();
  548. videoContext.play();
  549. }
  550. });
  551. } else {
  552. // 所有格式都尝试过了,使用内置资源
  553. this.useBuiltInResource();
  554. }
  555. },
  556. // 添加新方法:使用内置资源
  557. useBuiltInResource() {
  558. console.log('尝试使用内置资源');
  559. // 检查平台
  560. const platform = uni.getSystemInfoSync().platform;
  561. // 根据平台选择合适的视频源
  562. if (platform === 'windows') {
  563. // Windows平台特定处理
  564. // 尝试使用相对于应用根目录的路径
  565. const appRoot = process.env.UNI_INPUT_DIR || '';
  566. this.videoUrl = `./static/demo.mp4`;
  567. // 或者尝试使用file://协议
  568. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  569. console.log('Windows平台尝试路径:', this.videoUrl);
  570. } else if (platform === 'android' || platform === 'ios') {
  571. // 移动端
  572. this.useNativeVideo();
  573. } else {
  574. // Web平台
  575. // 尝试使用完整URL
  576. const baseUrl = window.location.origin;
  577. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  578. console.log('Web平台尝试URL:', this.videoUrl);
  579. }
  580. this.$nextTick(() => {
  581. const videoContext = uni.createVideoContext('myVideo', this);
  582. if (videoContext) {
  583. videoContext.play();
  584. }
  585. });
  586. },
  587. // 添加新方法:使用原生视频能力
  588. useNativeVideo() {
  589. console.log('尝试使用原生视频能力');
  590. // 在移动端,可以尝试使用原生视频播放器
  591. uni.chooseVideo({
  592. sourceType: ['album'],
  593. success: (res) => {
  594. this.videoUrl = res.tempFilePath;
  595. this.$nextTick(() => {
  596. const videoContext = uni.createVideoContext('myVideo', this);
  597. if (videoContext) {
  598. videoContext.play();
  599. }
  600. });
  601. },
  602. fail: () => {
  603. // 如果用户取消选择,回退到静态图片
  604. this.videoPlaying = false;
  605. uni.showToast({
  606. title: '无法加载视频,显示静态图片',
  607. icon: 'none'
  608. });
  609. }
  610. });
  611. },
  612. // 处理视频结束事件
  613. handleVideoEnded() {
  614. console.log('视频播放结束');
  615. this.videoPlaying = false;
  616. // 显示答题按钮
  617. this.showAnswerButton = true;
  618. },
  619. // 处理答题按钮点击
  620. handleAnswerButtonClick() {
  621. // 隐藏答题按钮
  622. this.showAnswerButton = false;
  623. // 切换到下一个视频
  624. this.currentVideoIndex++;
  625. if (this.currentVideoIndex < this.videoList.length) {
  626. // 还有下一段视频,播放它
  627. this.videoUrl = this.videoList[this.currentVideoIndex];
  628. this.videoPlaying = true;
  629. // 重置当前字幕
  630. this.currentSubtitle = '';
  631. // 使用 nextTick 确保 DOM 更新后再播放视频
  632. this.$nextTick(() => {
  633. const videoContext = uni.createVideoContext('myVideo', this);
  634. if (videoContext) {
  635. videoContext.play();
  636. }
  637. });
  638. } else {
  639. // 所有视频都播放完毕,可以进行下一步操作
  640. uni.showToast({
  641. title: '面试完成',
  642. icon: 'success',
  643. duration: 2000
  644. });
  645. // 可以在这里添加面试完成后的逻辑,比如跳转到下一个页面
  646. setTimeout(() => {
  647. uni.navigateTo({
  648. url: '/pages/interview-result/interview-result'
  649. });
  650. }, 2000);
  651. }
  652. },
  653. // 处理相机错误
  654. handleCameraError(e) {
  655. console.error('相机错误:', e);
  656. // 显示错误提示
  657. uni.showToast({
  658. title: '相机初始化失败,请检查权限设置',
  659. icon: 'none'
  660. });
  661. // 尝试备用选项
  662. this.tryFallbackOptions();
  663. },
  664. // 添加新方法:尝试备用选项
  665. tryFallbackOptions() {
  666. // 检查环境
  667. const systemInfo = uni.getSystemInfoSync();
  668. // 在小程序环境中使用小程序API
  669. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  670. this.useMiniProgramCamera();
  671. }
  672. // 在H5环境中显示静态图像
  673. else {
  674. this.showStaticCameraPlaceholder();
  675. }
  676. },
  677. // 添加新方法:使用小程序相机API
  678. useMiniProgramCamera() {
  679. console.log('尝试使用小程序相机组件');
  680. // 这里需要在模板中添加小程序相机组件
  681. // 并设置一个标志来控制显示
  682. this.useMiniProgramCameraComponent = true;
  683. },
  684. // 添加新方法:显示静态图像
  685. showStaticCameraPlaceholder() {
  686. console.log('显示静态摄像头占位图');
  687. // 创建一个图像元素
  688. const img = document.createElement('img');
  689. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  690. img.className = 'static-camera-image';
  691. img.style.width = '100%';
  692. img.style.height = '100%';
  693. img.style.objectFit = 'cover';
  694. // 获取容器并添加图像
  695. const container = this.$refs.userCameraVideo.parentNode;
  696. container.appendChild(img);
  697. },
  698. // 处理视频时间更新事件
  699. handleTimeUpdate(e) {
  700. // 获取当前视频播放时间
  701. const currentTime = e.target.currentTime;
  702. // 根据当前播放的视频索引选择对应的字幕数组
  703. let currentSubtitles;
  704. if (this.currentVideoIndex === 0) {
  705. currentSubtitles = this.subtitles;
  706. } else if (this.currentVideoIndex === 1) {
  707. currentSubtitles = this.secondVideoSubtitles;
  708. }else if (this.currentVideoIndex === 2) {
  709. currentSubtitles = this.thirdVideoSubtitles;
  710. }else if (this.currentVideoIndex === 3) {
  711. currentSubtitles = this.fourthVideoSubtitles;
  712. }else if (this.currentVideoIndex === 4) {
  713. currentSubtitles = this.fifthVideoSubtitles;
  714. }else if (this.currentVideoIndex === 5) {
  715. currentSubtitles = this.sixthVideoSubtitles;
  716. }else {
  717. // 如果有更多视频,可以继续添加条件
  718. currentSubtitles = [];
  719. }
  720. // 查找当前时间应该显示的字幕
  721. const subtitle = currentSubtitles.find(
  722. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  723. );
  724. // 更新当前字幕
  725. this.currentSubtitle = subtitle ? subtitle.text : '';
  726. },
  727. }
  728. }
  729. </script>
  730. <style scoped>
  731. .identity-verify-container {
  732. padding: 0;
  733. max-width: 100%;
  734. margin: 0 auto;
  735. height: 100vh;
  736. display: flex;
  737. flex-direction: column;
  738. background-color: #f5f5f5;
  739. }
  740. .digital-human-container {
  741. position: relative;
  742. width: 100%;
  743. height: 100vh;
  744. overflow: hidden;
  745. background-color: #f0f0f0;
  746. }
  747. .digital-human-video {
  748. width: 100%;
  749. height: 100%;
  750. display: flex;
  751. justify-content: center;
  752. align-items: center;
  753. }
  754. /* 用户摄像头容器样式 */
  755. .user-camera-container {
  756. position: absolute;
  757. top: 20px;
  758. right: 20px;
  759. width: 100px;
  760. height: 150px;
  761. border-radius: 8px;
  762. overflow: hidden;
  763. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  764. z-index: 20;
  765. border: 1px solid #fff;
  766. }
  767. /* 用户摄像头视频样式 */
  768. .user-camera-video {
  769. width: 100%;
  770. height: 100%;
  771. object-fit: cover;
  772. background-color: #333;
  773. }
  774. .video-player {
  775. width: 100%;
  776. height: 100%;
  777. object-fit: cover;
  778. outline: none; /* 移除视频获得焦点时的轮廓 */
  779. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  780. }
  781. /* 隐藏视频控制条 */
  782. video::-webkit-media-controls {
  783. display: none !important;
  784. }
  785. video::-webkit-media-controls-enclosure {
  786. display: none !important;
  787. }
  788. video::-webkit-media-controls-panel {
  789. display: none !important;
  790. }
  791. video::-webkit-media-controls-play-button {
  792. display: none !important;
  793. }
  794. video::-webkit-media-controls-timeline {
  795. display: none !important;
  796. }
  797. video::-webkit-media-controls-current-time-display {
  798. display: none !important;
  799. }
  800. video::-webkit-media-controls-time-remaining-display {
  801. display: none !important;
  802. }
  803. video::-webkit-media-controls-mute-button {
  804. display: none !important;
  805. }
  806. video::-webkit-media-controls-volume-slider {
  807. display: none !important;
  808. }
  809. video::-webkit-media-controls-fullscreen-button {
  810. display: none !important;
  811. }
  812. .subtitle-overlay {
  813. position: absolute;
  814. bottom: 50px;
  815. left: 0;
  816. width: 100%;
  817. padding: 15px;
  818. background-color: rgba(0, 0, 0, 0.7);
  819. color: white;
  820. text-align: center;
  821. font-size: 16px;
  822. line-height: 1.5;
  823. z-index: 10;
  824. border-radius: 4px;
  825. max-width: 90%;
  826. margin: 0 auto;
  827. left: 5%;
  828. right: 5%;
  829. }
  830. .control-panel {
  831. padding: 15px;
  832. display: flex;
  833. justify-content: center;
  834. }
  835. .control-button {
  836. padding: 10px 20px;
  837. background-color: #4CAF50;
  838. color: white;
  839. border: none;
  840. border-radius: 4px;
  841. cursor: pointer;
  842. }
  843. .loading {
  844. text-align: center;
  845. margin: 20px 0;
  846. font-size: 16px;
  847. }
  848. .response-container {
  849. margin-top: 20px;
  850. padding: 0 20px;
  851. display: none; /* 默认隐藏调试信息 */
  852. }
  853. /* 当showDebugInfo为true时显示 */
  854. .showDebugInfo .response-container {
  855. display: block;
  856. }
  857. .response-item {
  858. padding: 10px;
  859. border: 1px solid #eee;
  860. border-radius: 4px;
  861. margin-bottom: 10px;
  862. background-color: #f9f9f9;
  863. }
  864. .response-content {
  865. display: flex;
  866. flex-direction: column;
  867. }
  868. .answer-button-container {
  869. position: absolute;
  870. top: 75%;
  871. left: 50%;
  872. transform: translate(-50%, -50%);
  873. z-index: 20;
  874. }
  875. .answer-button {
  876. width: 120px;
  877. height: 120px;
  878. border-radius: 50%;
  879. background-color: #6c5ce7; /* 绿色背景,与图片中的颜色类似 */
  880. color: white;
  881. font-size: 18px;
  882. border: none;
  883. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  884. display: flex;
  885. justify-content: center;
  886. align-items: center;
  887. cursor: pointer;
  888. animation: pulse 2s infinite;
  889. }
  890. @keyframes pulse {
  891. 0% {
  892. transform: scale(1);
  893. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  894. }
  895. 50% {
  896. transform: scale(1.05);
  897. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  898. }
  899. 100% {
  900. transform: scale(1);
  901. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  902. }
  903. }
  904. </style>