identity-verify.vue 93 KB

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