identity-verify.vue 92 KB

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