identity-verify.vue 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  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. // 如果是第二段视频播放完成,自动开始倒计时
  757. // if (this.currentVideoIndex >= 1) {
  758. // }
  759. } else {
  760. // 对于介绍视频(第一个视频),显示"开始面试"按钮
  761. this.showAnswerButton = true;
  762. }
  763. },
  764. // 添加新方法:开始倒计时
  765. startCountdown() {
  766. // 显示倒计时蒙层
  767. this.showCountdown = true;
  768. this.countdownValue = 10;
  769. // 开始倒计时
  770. this.countdownTimer = setInterval(() => {
  771. this.countdownValue--;
  772. if (this.countdownValue <= 0) {
  773. // 倒计时结束,清除定时器
  774. this.clearCountdown();
  775. // 隐藏"开始回答"按钮
  776. this.showStartRecordingButton = false;
  777. // 开始录制
  778. this.startRecordingAnswer();
  779. }
  780. }, 1000);
  781. },
  782. // 添加新方法:清除倒计时
  783. clearCountdown() {
  784. if (this.countdownTimer) {
  785. clearInterval(this.countdownTimer);
  786. this.countdownTimer = null;
  787. }
  788. this.showCountdown = false;
  789. },
  790. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  791. handleStartRecordingClick() {
  792. // 隐藏"开始回答"按钮
  793. this.showStartRecordingButton = false;
  794. // 如果倒计时正在进行,清除倒计时
  795. this.clearCountdown();
  796. // 直接开始录制
  797. this.startRecordingAnswer();
  798. },
  799. // 修改 stopRecordingAnswer 方法,确保清除倒计时
  800. stopRecordingAnswer() {
  801. // 如果倒计时正在进行,先清除倒计时
  802. this.clearCountdown();
  803. // 检查录制时长
  804. const recordingDuration = this.getRecordingDuration();
  805. const minimumDuration = 5; // 最小录制时长(秒)
  806. if (recordingDuration < minimumDuration) {
  807. // 录制时间过短,显示提示
  808. uni.showToast({
  809. title: '录制时间过短,请至少录制5秒',
  810. icon: 'none',
  811. duration: 2000
  812. });
  813. // 不执行停止录制逻辑,继续录制
  814. return;
  815. }
  816. // 录制时长足够,直接停止
  817. this.completeRecordingStop();
  818. },
  819. // 添加新方法:开始录制用户回答
  820. startRecordingAnswer() {
  821. console.log('开始录制用户回答');
  822. this.isRecording = true;
  823. // 记录录制开始时间
  824. this.recordingStartTime = Date.now();
  825. this.recordingTimerCount = 0;
  826. this.remainingTime = this.maxRecordingTime;
  827. // 启动计时器,每秒更新计时
  828. this.recordingTimer = setInterval(() => {
  829. this.recordingTimerCount++;
  830. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  831. // 更新UI显示录制时长和剩余时间
  832. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount) +
  833. ' / ' + this.formatTime(this.maxRecordingTime);
  834. // 检查是否达到最大录制时间
  835. if (this.recordingTimerCount >= this.maxRecordingTime) {
  836. console.log('已达到最大录制时间(60秒),自动停止录制');
  837. this.stopRecordingAnswer();
  838. }
  839. }, 1000);
  840. // 显示录制中的提示
  841. // uni.showLoading({
  842. // title: '正在录制...',
  843. // mask: true
  844. // });
  845. // 根据平台选择不同的录制方法
  846. const systemInfo = uni.getSystemInfoSync();
  847. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  848. if (isMiniProgram) {
  849. // 小程序环境使用小程序的录制API
  850. this.startMiniProgramRecording();
  851. } else {
  852. // H5/App环境使用MediaRecorder API
  853. this.startBrowserRecording();
  854. }
  855. // 显示停止录制按钮
  856. this.showStopRecordingButton = true;
  857. },
  858. // 添加一个新方法:重置相机组件
  859. resetCamera() {
  860. console.log('重置相机组件');
  861. // 先完全移除相机组件
  862. this.useMiniProgramCameraComponent = false;
  863. // 释放相机上下文
  864. if (this.cameraContext) {
  865. this.cameraContext = null;
  866. }
  867. // 延迟后重新创建相机组件
  868. setTimeout(() => {
  869. this.useMiniProgramCameraComponent = true;
  870. // 再次延迟创建相机上下文,确保组件已完全渲染
  871. setTimeout(() => {
  872. this.cameraContext = uni.createCameraContext();
  873. console.log('相机组件已重置');
  874. }, 500);
  875. }, 500);
  876. },
  877. // 修改 startMiniProgramRecording 方法
  878. startMiniProgramRecording() {
  879. console.log('开始小程序录制方法');
  880. // 获取平台信息
  881. const systemInfo = uni.getSystemInfoSync();
  882. const isIOS = systemInfo.platform === 'ios';
  883. // 在iOS上,先重置相机
  884. if (isIOS) {
  885. // 先重置相机组件
  886. this.resetCamera();
  887. // 延迟执行录制,给相机组件足够的初始化时间
  888. setTimeout(() => {
  889. this.actualStartRecording(isIOS);
  890. }, 1000);
  891. } else {
  892. // Android直接开始
  893. this.actualStartRecording(isIOS);
  894. }
  895. },
  896. // 添加新方法:实际开始录制
  897. actualStartRecording(isIOS) {
  898. if (!this.cameraContext) {
  899. this.cameraContext = uni.createCameraContext();
  900. console.log('创建新的相机上下文');
  901. }
  902. // 确保有录音权限
  903. uni.getSetting({
  904. success: (res) => {
  905. const hasRecordAuth = res.authSetting['scope.record'];
  906. const hasCameraAuth = res.authSetting['scope.camera'];
  907. if (!hasRecordAuth || !hasCameraAuth) {
  908. console.warn('缺少必要权限,请求权限');
  909. this.requestMiniProgramPermissions();
  910. return;
  911. }
  912. // 在iOS上,先检查相机状态
  913. if (isIOS) {
  914. console.log('iOS: 检查相机状态');
  915. // 使用最简单的选项,设置最大录制时间为5分钟
  916. const options = {
  917. timeout: 300000, // 300秒超时 (5分钟)
  918. quality: 'low', // 降低质量
  919. compressed: true,
  920. success: () => {
  921. console.log('iOS录制开始成功');
  922. },
  923. fail: (err) => {
  924. console.error('iOS录制失败:', err);
  925. // 如果失败,尝试使用不同的方法
  926. this.useAlternativeRecordingMethod();
  927. }
  928. };
  929. try {
  930. console.log('尝试开始录制');
  931. this.recorder = this.cameraContext.startRecord(options);
  932. } catch (e) {
  933. console.error('开始录制异常:', e);
  934. this.useAlternativeRecordingMethod();
  935. }
  936. } else {
  937. // Android使用标准选项,设置最大录制时间为5分钟
  938. const options = {
  939. timeout: 300000, // 300秒超时 (5分钟)
  940. quality: 'medium',
  941. compressed: true,
  942. success: () => {
  943. console.log('Android录制开始成功');
  944. },
  945. fail: (err) => {
  946. console.error('Android录制失败:', err);
  947. uni.showToast({
  948. title: '录制失败,请检查相机权限',
  949. icon: 'none'
  950. });
  951. this.proceedToNextQuestion();
  952. }
  953. };
  954. this.recorder = this.cameraContext.startRecord(options);
  955. }
  956. }
  957. });
  958. },
  959. // 添加新方法:使用替代录制方法
  960. useAlternativeRecordingMethod() {
  961. console.log('使用替代录制方法');
  962. // 在iOS上,可以尝试使用chooseVideo API作为备选
  963. uni.showActionSheet({
  964. itemList: ['使用相册中的视频', '跳过此问题'],
  965. success: (res) => {
  966. if (res.tapIndex === 0) {
  967. // 选择相册中的视频
  968. uni.chooseVideo({
  969. sourceType: ['album'],
  970. maxDuration: 300, // 从60秒改为300秒
  971. camera: 'front',
  972. success: (res) => {
  973. console.log('选择视频成功:', res.tempFilePath);
  974. // 停止录制状态
  975. this.isRecording = false;
  976. this.showStopRecordingButton = false;
  977. // 上传选择的视频
  978. this.uploadRecordedVideo(res.tempFilePath);
  979. },
  980. fail: () => {
  981. console.log('用户取消选择视频');
  982. this.proceedToNextQuestion();
  983. }
  984. });
  985. } else {
  986. // 跳过此问题
  987. console.log('用户选择跳过问题');
  988. this.proceedToNextQuestion();
  989. }
  990. },
  991. fail: () => {
  992. console.log('操作取消');
  993. this.proceedToNextQuestion();
  994. }
  995. });
  996. },
  997. // 添加新方法:请求小程序权限
  998. requestMiniProgramPermissions() {
  999. // 请求录音权限
  1000. uni.authorize({
  1001. scope: 'scope.record',
  1002. success: () => {
  1003. console.log('录音权限已获取');
  1004. // 请求相机权限
  1005. uni.authorize({
  1006. scope: 'scope.camera',
  1007. success: () => {
  1008. console.log('相机权限已获取');
  1009. // 权限都获取到后,开始录制
  1010. this.startMiniProgramRecording();
  1011. },
  1012. fail: (err) => {
  1013. console.error('相机权限获取失败:', err);
  1014. this.showPermissionDialog('相机');
  1015. }
  1016. });
  1017. },
  1018. fail: (err) => {
  1019. console.error('录音权限获取失败:', err);
  1020. this.showPermissionDialog('录音');
  1021. }
  1022. });
  1023. },
  1024. // 修改浏览器环境下的录制方法
  1025. startBrowserRecording() {
  1026. if (!this.cameraStream) {
  1027. console.error('没有可用的摄像头流');
  1028. uni.showToast({
  1029. title: '录制失败,摄像头未就绪',
  1030. icon: 'none'
  1031. });
  1032. this.proceedToNextQuestion();
  1033. return;
  1034. }
  1035. try {
  1036. // 检查流中是否包含音频轨道
  1037. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  1038. if (!hasAudio) {
  1039. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  1040. // 尝试重新获取带音频的媒体流
  1041. navigator.mediaDevices.getUserMedia({
  1042. audio: {
  1043. echoCancellation: true,
  1044. noiseSuppression: true,
  1045. autoGainControl: true
  1046. },
  1047. video: true
  1048. })
  1049. .then(newStream => {
  1050. // 检查新流是否包含音频轨道
  1051. const audioTracks = newStream.getAudioTracks();
  1052. if (audioTracks.length > 0) {
  1053. console.log('成功获取音频轨道:', audioTracks[0].label);
  1054. // 合并视频和音频轨道
  1055. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1056. const audioTrack = newStream.getAudioTracks()[0];
  1057. // 创建新的媒体流,包含视频和音频轨道
  1058. const combinedStream = new MediaStream();
  1059. if (videoTrack) combinedStream.addTrack(videoTrack);
  1060. if (audioTrack) combinedStream.addTrack(audioTrack);
  1061. this.cameraStream = combinedStream;
  1062. // 更新视频元素的源
  1063. const videoElement = this.$refs.userCameraVideo;
  1064. if (videoElement) {
  1065. videoElement.srcObject = combinedStream;
  1066. videoElement.muted = true; // 避免回声,但仍然录制声音
  1067. }
  1068. // 继续录制过程
  1069. this.setupMediaRecorder(combinedStream);
  1070. } else {
  1071. console.warn('仍然无法获取音频轨道');
  1072. // 使用现有流继续
  1073. this.setupMediaRecorder(this.cameraStream);
  1074. }
  1075. })
  1076. .catch(err => {
  1077. console.error('获取音频失败:', err);
  1078. // 使用现有流继续
  1079. this.setupMediaRecorder(this.cameraStream);
  1080. });
  1081. } else {
  1082. console.log('检测到音频轨道,直接使用');
  1083. // 已有音频轨道,直接使用
  1084. this.setupMediaRecorder(this.cameraStream);
  1085. }
  1086. } catch (error) {
  1087. console.error('浏览器录制失败:', error);
  1088. uni.showToast({
  1089. title: '录制失败,浏览器可能不支持此功能',
  1090. icon: 'none'
  1091. });
  1092. this.proceedToNextQuestion();
  1093. }
  1094. },
  1095. // 修改 setupMediaRecorder 方法
  1096. setupMediaRecorder(stream) {
  1097. // 检查流中的轨道
  1098. const videoTracks = stream.getVideoTracks();
  1099. const audioTracks = stream.getAudioTracks();
  1100. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  1101. // 尝试使用支持度更广的编码格式
  1102. let mimeType = '';
  1103. const supportedTypes = [
  1104. 'video/webm;codecs=vp9,opus',
  1105. 'video/webm;codecs=vp8,opus',
  1106. 'video/webm;codecs=h264,opus',
  1107. 'video/mp4;codecs=h264,aac',
  1108. 'video/webm',
  1109. 'video/mp4'
  1110. ];
  1111. for (const type of supportedTypes) {
  1112. if (MediaRecorder.isTypeSupported(type)) {
  1113. mimeType = type;
  1114. console.log('使用支持的MIME类型:', mimeType);
  1115. break;
  1116. }
  1117. }
  1118. // 创建MediaRecorder实例,设置较低的比特率以减小文件大小
  1119. const options = {
  1120. mimeType: mimeType || '',
  1121. audioBitsPerSecond: 64000, // 降低音频比特率
  1122. videoBitsPerSecond: 1000000, // 降低视频比特率到1Mbps
  1123. };
  1124. try {
  1125. this.mediaRecorder = new MediaRecorder(stream, options);
  1126. console.log('MediaRecorder创建成功,使用选项:', options);
  1127. } catch (e) {
  1128. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  1129. this.mediaRecorder = new MediaRecorder(stream);
  1130. }
  1131. // 存储录制的数据块
  1132. this.recordedChunks = [];
  1133. // 监听数据可用事件
  1134. this.mediaRecorder.ondataavailable = (event) => {
  1135. if (event.data && event.data.size > 0) {
  1136. this.recordedChunks.push(event.data);
  1137. console.log(`收到数据块: ${event.data.size} 字节`);
  1138. }
  1139. };
  1140. // 监听录制停止事件
  1141. this.mediaRecorder.onstop = async () => {
  1142. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  1143. if (this.recordedChunks.length === 0) {
  1144. console.error('没有录制到数据');
  1145. uni.showToast({
  1146. title: '录制失败,未捕获到数据',
  1147. icon: 'none'
  1148. });
  1149. this.proceedToNextQuestion();
  1150. return;
  1151. }
  1152. // 创建Blob对象
  1153. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  1154. const blob = new Blob(this.recordedChunks, { type: mimeType });
  1155. console.log('创建Blob,原始大小:', blob.size, '类型:', mimeType);
  1156. // 显示压缩中提示
  1157. uni.showLoading({
  1158. title: '正在处理视频...',
  1159. mask: true
  1160. });
  1161. try {
  1162. // 压缩视频
  1163. const compressedBlob = await this.compressVideo(blob);
  1164. // 将Blob转换为File对象
  1165. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1166. const file = new File([compressedBlob], fileName, { type: mimeType });
  1167. // 隐藏压缩提示
  1168. uni.hideLoading();
  1169. // 上传文件
  1170. this.uploadRecordedVideo(file);
  1171. } catch (error) {
  1172. console.error('视频处理失败:', error);
  1173. uni.hideLoading();
  1174. // 如果压缩失败,使用原始视频
  1175. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1176. const file = new File([blob], fileName, { type: mimeType });
  1177. this.uploadRecordedVideo(file);
  1178. }
  1179. };
  1180. // 监听错误
  1181. this.mediaRecorder.onerror = (event) => {
  1182. console.error('MediaRecorder错误:', event.error);
  1183. };
  1184. // 开始录制
  1185. try {
  1186. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  1187. console.log('MediaRecorder开始录制');
  1188. // 设置最大录制时间为60秒
  1189. // 注意:我们已经在startRecordingAnswer方法中设置了全局计时器
  1190. // 这里作为备份机制,确保即使全局计时器失效,也能停止录制
  1191. this.mediaRecorderTimeout = setTimeout(() => {
  1192. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  1193. console.log('MediaRecorder备份超时机制触发,停止录制');
  1194. this.mediaRecorder.stop();
  1195. }
  1196. }, 300000); // 300秒 (5分钟)
  1197. } catch (e) {
  1198. console.error('开始录制失败:', e);
  1199. }
  1200. },
  1201. // 添加新方法:停止录制用户回答
  1202. stopRecordingAnswer() {
  1203. console.log('停止录制用户回答');
  1204. // 如果倒计时正在进行,先清除倒计时
  1205. if (this.countdownTimer) {
  1206. clearInterval(this.countdownTimer);
  1207. this.countdownTimer = null;
  1208. this.showCountdown = false;
  1209. }
  1210. // 检查录制时长
  1211. const recordingDuration = this.getRecordingDuration();
  1212. const minimumDuration = 5; // 最小录制时长(秒),从3秒改为5秒
  1213. if (recordingDuration < minimumDuration) {
  1214. // 录制时间过短,显示提示
  1215. uni.showToast({
  1216. title: '录制时间过短,请至少录制5秒',
  1217. icon: 'none',
  1218. duration: 2000
  1219. });
  1220. // 不执行停止录制逻辑,继续录制
  1221. return;
  1222. }
  1223. // 录制时长足够,直接停止
  1224. this.completeRecordingStop();
  1225. },
  1226. // 添加新方法:获取录制时长
  1227. getRecordingDuration() {
  1228. // 如果有明确的录制开始时间,计算实际录制时长
  1229. if (this.recordingStartTime) {
  1230. return (Date.now() - this.recordingStartTime) / 1000;
  1231. }
  1232. // 如果没有明确记录开始时间,尝试从视频元素获取
  1233. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1234. return this.$refs.userCameraVideo.currentTime || 0;
  1235. }
  1236. // 如果无法获取准确时长,返回估计值(如果有计时器)
  1237. if (this.recordingTimerCount) {
  1238. return this.recordingTimerCount;
  1239. }
  1240. // 默认返回0
  1241. return 0;
  1242. },
  1243. // 添加新方法:重置录制
  1244. resetRecording() {
  1245. // 保持录制状态,但重置计时器
  1246. if (this.recordingTimer) {
  1247. clearTimeout(this.recordingTimer);
  1248. }
  1249. // 重置录制开始时间
  1250. this.recordingStartTime = Date.now();
  1251. this.recordingTimerCount = 0;
  1252. // 如果是浏览器环境,需要重置MediaRecorder
  1253. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1254. // 停止当前录制
  1255. this.mediaRecorder.stop();
  1256. // 清空已录制的数据块
  1257. this.recordedChunks = [];
  1258. // 短暂延迟后重新开始录制
  1259. setTimeout(() => {
  1260. this.startBrowserRecording();
  1261. }, 500);
  1262. }
  1263. // 如果是小程序环境,需要重置相机录制
  1264. else if (this.cameraContext) {
  1265. // 停止当前录制
  1266. this.cameraContext.stopRecord({
  1267. success: () => {
  1268. console.log('重置录制:停止当前录制成功');
  1269. // 短暂延迟后重新开始录制
  1270. setTimeout(() => {
  1271. this.startMiniProgramRecording();
  1272. }, 500);
  1273. },
  1274. fail: (err) => {
  1275. console.error('重置录制:停止当前录制失败', err);
  1276. // 尝试直接重新开始录制
  1277. this.startMiniProgramRecording();
  1278. }
  1279. });
  1280. }
  1281. // 显示提示
  1282. uni.showToast({
  1283. title: '请重新开始回答',
  1284. icon: 'none',
  1285. duration: 2000
  1286. });
  1287. },
  1288. // 添加新方法:完成录制停止流程
  1289. completeRecordingStop() {
  1290. this.isRecording = false;
  1291. // 清除定时器
  1292. if (this.recordingTimer) {
  1293. clearInterval(this.recordingTimer);
  1294. this.recordingTimer = null;
  1295. }
  1296. // 清除MediaRecorder超时定时器
  1297. if (this.mediaRecorderTimeout) {
  1298. clearTimeout(this.mediaRecorderTimeout);
  1299. this.mediaRecorderTimeout = null;
  1300. }
  1301. // 隐藏录制中提示
  1302. uni.hideLoading();
  1303. // 隐藏停止录制按钮
  1304. this.showStopRecordingButton = false;
  1305. // 根据平台选择不同的停止录制方法
  1306. const systemInfo = uni.getSystemInfoSync();
  1307. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1308. if (isMiniProgram) {
  1309. // 小程序环境停止录制
  1310. this.stopMiniProgramRecording();
  1311. } else {
  1312. // H5/App环境停止录制
  1313. this.stopBrowserRecording();
  1314. }
  1315. },
  1316. // 修改 stopMiniProgramRecording 方法
  1317. stopMiniProgramRecording() {
  1318. if (!this.cameraContext) {
  1319. console.error('相机上下文不存在');
  1320. this.proceedToNextQuestion();
  1321. return;
  1322. }
  1323. const systemInfo = uni.getSystemInfoSync();
  1324. const isIOS = systemInfo.platform === 'ios';
  1325. // 在iOS上添加额外的错误处理
  1326. const stopOptions = {
  1327. success: (res) => {
  1328. console.log('小程序录像停止成功:', res);
  1329. // 获取临时文件路径
  1330. const tempFilePath = res.tempVideoPath;
  1331. if (!tempFilePath) {
  1332. console.error('未获取到视频文件路径');
  1333. uni.showToast({
  1334. title: '录制失败,未获取到视频文件',
  1335. icon: 'none'
  1336. });
  1337. this.proceedToNextQuestion();
  1338. return;
  1339. }
  1340. // 在iOS上,检查文件是否存在
  1341. if (isIOS) {
  1342. uni.getFileInfo({
  1343. filePath: tempFilePath,
  1344. success: () => {
  1345. // 文件存在,上传视频
  1346. this.uploadRecordedVideo(tempFilePath);
  1347. },
  1348. fail: (err) => {
  1349. console.error('视频文件不存在:', err);
  1350. uni.showToast({
  1351. title: '录制失败,视频文件不存在',
  1352. icon: 'none'
  1353. });
  1354. this.proceedToNextQuestion();
  1355. }
  1356. });
  1357. } else {
  1358. // Android直接上传
  1359. this.uploadRecordedVideo(tempFilePath);
  1360. }
  1361. },
  1362. fail: (err) => {
  1363. console.error('小程序录像停止失败:', err);
  1364. uni.showToast({
  1365. title: '录制失败',
  1366. icon: 'none'
  1367. });
  1368. this.proceedToNextQuestion();
  1369. }
  1370. };
  1371. // 停止录制
  1372. this.cameraContext.stopRecord(stopOptions);
  1373. },
  1374. // 添加新方法:停止浏览器录制
  1375. stopBrowserRecording() {
  1376. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1377. this.mediaRecorder.stop();
  1378. console.log('浏览器录制停止成功');
  1379. } else {
  1380. console.error('MediaRecorder不存在或已经停止');
  1381. this.proceedToNextQuestion();
  1382. }
  1383. },
  1384. // 修改上传录制的视频方法
  1385. uploadRecordedVideo(fileOrPath) {
  1386. console.log('准备上传视频:', typeof fileOrPath === 'string' ? fileOrPath : fileOrPath.name);
  1387. // 根据当前视频索引映射到正确的问题ID
  1388. let questionId;
  1389. switch(this.currentVideoIndex) {
  1390. case 1: // 第一个问题
  1391. questionId = 10;
  1392. break;
  1393. case 2: // 第二个问题
  1394. questionId = 11;
  1395. break;
  1396. case 3: // 第三个问题
  1397. questionId = 12;
  1398. break;
  1399. case 4: // 第四个问题
  1400. questionId = 13;
  1401. break;
  1402. default:
  1403. questionId = 10; // 默认值
  1404. }
  1405. // 创建上传任务对象
  1406. const uploadTask = {
  1407. id: Date.now().toString(), // 生成唯一ID
  1408. file: fileOrPath,
  1409. questionId: questionId,
  1410. attempts: 0, // 上传尝试次数
  1411. maxAttempts: 3, // 最大尝试次数
  1412. };
  1413. // 添加到上传队列
  1414. this.uploadQueue.push(uploadTask);
  1415. // 初始化上传进度和状态
  1416. this.uploadProgress[uploadTask.id] = 0;
  1417. this.uploadStatus[uploadTask.id] = 'pending'; // pending, uploading, success, failed
  1418. // 显示简短的上传状态提示
  1419. uni.showToast({
  1420. title: '已完成回答',
  1421. icon: 'none',
  1422. duration: 1500
  1423. });
  1424. // 更新上传状态文本
  1425. this.updateUploadStatusText();
  1426. // 如果当前没有上传任务在进行,开始处理队列
  1427. if (!this.isUploading) {
  1428. this.processUploadQueue();
  1429. }
  1430. // 立即进入下一个问题,不等待上传完成
  1431. this.proceedToNextQuestion();
  1432. },
  1433. // 添加新方法:处理上传队列
  1434. processUploadQueue() {
  1435. // 如果队列为空,结束处理
  1436. if (this.uploadQueue.length === 0) {
  1437. this.isUploading = false;
  1438. this.showUploadStatus = false;
  1439. return;
  1440. }
  1441. // 标记为正在上传
  1442. this.isUploading = true;
  1443. this.showUploadStatus = true;
  1444. // 获取队列中的第一个任务
  1445. const task = this.uploadQueue[0];
  1446. // 更新任务状态
  1447. this.uploadStatus[task.id] = 'uploading';
  1448. this.updateUploadStatusText();
  1449. // 增加尝试次数
  1450. task.attempts++;
  1451. // 根据文件类型选择上传方法
  1452. if (typeof task.file !== 'string') {
  1453. // 浏览器环境,使用XMLHttpRequest上传
  1454. this.uploadFileWithXHR(task);
  1455. } else {
  1456. // 小程序环境,使用uni.uploadFile上传
  1457. this.uploadFileWithUni(task);
  1458. }
  1459. },
  1460. // 添加新方法:使用XMLHttpRequest上传文件
  1461. uploadFileWithXHR(task) {
  1462. // 获取用户信息
  1463. const userInfo = uni.getStorageSync('userInfo');
  1464. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1465. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1466. // 创建FormData
  1467. const formData = new FormData();
  1468. formData.append('file', task.file);
  1469. formData.append('openid', openid);
  1470. formData.append('tenant_id', tenant_id);
  1471. formData.append('application_id', uni.getStorageSync('appId'));
  1472. formData.append('question_id', task.questionId);
  1473. formData.append('video_duration', 0);
  1474. formData.append('has_audio', 'true');
  1475. // 创建XMLHttpRequest
  1476. const xhr = new XMLHttpRequest();
  1477. xhr.open('POST', `${apiBaseUrl}/api/upload/`, true);
  1478. xhr.timeout = 120000; // 2分钟超时
  1479. // 监听上传进度
  1480. xhr.upload.onprogress = (event) => {
  1481. if (event.lengthComputable) {
  1482. const percentComplete = Math.round((event.loaded / event.total) * 100);
  1483. this.uploadProgress[task.id] = percentComplete;
  1484. this.updateUploadStatusText();
  1485. }
  1486. };
  1487. // 监听请求完成
  1488. xhr.onload = () => {
  1489. if (xhr.status === 200) {
  1490. try {
  1491. const res = JSON.parse(xhr.responseText);
  1492. console.log('上传响应:', res);
  1493. if (res.code === 2000) {
  1494. // 获取上传后的视频URL
  1495. const videoUrl = res.data.url || res.data.photoUrl || '';
  1496. if (videoUrl) {
  1497. // 上传成功,更新状态
  1498. this.uploadStatus[task.id] = 'success';
  1499. this.updateUploadStatusText();
  1500. // 提交到面试接口
  1501. this.submitVideoToInterview(videoUrl, task);
  1502. } else {
  1503. this.handleUploadFailure(task, '视频URL获取失败');
  1504. }
  1505. } else {
  1506. this.handleUploadFailure(task, res.msg || '上传失败');
  1507. }
  1508. } catch (e) {
  1509. this.handleUploadFailure(task, '解析响应失败');
  1510. }
  1511. } else {
  1512. this.handleUploadFailure(task, 'HTTP状态: ' + xhr.status);
  1513. }
  1514. };
  1515. // 监听错误
  1516. xhr.onerror = () => {
  1517. this.handleUploadFailure(task, '网络错误');
  1518. };
  1519. // 监听超时
  1520. xhr.ontimeout = () => {
  1521. this.handleUploadFailure(task, '上传超时');
  1522. };
  1523. // 发送请求
  1524. xhr.send(formData);
  1525. },
  1526. // 添加新方法:使用uni.uploadFile上传文件
  1527. uploadFileWithUni(task) {
  1528. // 获取用户信息
  1529. const userInfo = uni.getStorageSync('userInfo');
  1530. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1531. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1532. // 创建上传任务
  1533. const uploadTask = uni.uploadFile({
  1534. url: `${apiBaseUrl}/api/upload/`,
  1535. filePath: task.file,
  1536. name: 'file',
  1537. formData: {
  1538. openid: openid,
  1539. tenant_id: tenant_id,
  1540. application_id: uni.getStorageSync('appId'),
  1541. question_id: task.questionId,
  1542. video_duration: 0,
  1543. has_audio: 'true'
  1544. },
  1545. success: (uploadRes) => {
  1546. try {
  1547. const res = JSON.parse(uploadRes.data);
  1548. console.log('上传响应:', res);
  1549. if (res.code === 2000) {
  1550. // 获取上传后的视频URL
  1551. const videoUrl = res.data.permanent_link || res.data.url || '';
  1552. if (videoUrl) {
  1553. // 上传成功,更新状态
  1554. this.uploadStatus[task.id] = 'success';
  1555. this.updateUploadStatusText();
  1556. // 提交到面试接口
  1557. this.submitVideoToInterview(videoUrl, task);
  1558. } else {
  1559. this.handleUploadFailure(task, '视频URL获取失败');
  1560. }
  1561. } else {
  1562. this.handleUploadFailure(task, res.msg || '上传失败');
  1563. }
  1564. } catch (e) {
  1565. this.handleUploadFailure(task, '解析响应失败');
  1566. }
  1567. },
  1568. fail: (err) => {
  1569. this.handleUploadFailure(task, err.errMsg || '上传失败');
  1570. }
  1571. });
  1572. // 监听上传进度
  1573. uploadTask.onProgressUpdate((res) => {
  1574. this.uploadProgress[task.id] = res.progress;
  1575. this.updateUploadStatusText();
  1576. });
  1577. },
  1578. // 添加新方法:处理上传失败
  1579. handleUploadFailure(task, errorMsg) {
  1580. console.error('上传失败:', errorMsg);
  1581. // 更新任务状态
  1582. this.uploadStatus[task.id] = 'failed';
  1583. this.updateUploadStatusText();
  1584. // 检查是否还有重试机会
  1585. if (task.attempts < task.maxAttempts) {
  1586. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1587. // 5秒后重试
  1588. setTimeout(() => {
  1589. // 重置进度
  1590. this.uploadProgress[task.id] = 0;
  1591. // 重新开始上传
  1592. if (typeof task.file !== 'string') {
  1593. this.uploadFileWithXHR(task);
  1594. } else {
  1595. this.uploadFileWithUni(task);
  1596. }
  1597. }, 5000);
  1598. } else {
  1599. // 超过最大重试次数,移除任务并继续处理队列
  1600. console.log('超过最大重试次数,放弃上传');
  1601. // 显示错误提示
  1602. uni.showToast({
  1603. title: '视频上传失败,请稍后重试',
  1604. icon: 'none',
  1605. duration: 2000
  1606. });
  1607. // 从队列中移除当前任务
  1608. this.uploadQueue.shift();
  1609. // 继续处理队列中的下一个任务
  1610. this.processUploadQueue();
  1611. }
  1612. },
  1613. // 修改 submitVideoToInterview 方法
  1614. submitVideoToInterview(videoUrl, task = null) {
  1615. console.log('提交视频URL到面试接口:', videoUrl);
  1616. // 确定问题ID
  1617. let questionId;
  1618. if (task) {
  1619. questionId = task.questionId;
  1620. } else {
  1621. // 根据当前视频索引映射到正确的问题ID(兼容旧代码)
  1622. switch(this.currentVideoIndex) {
  1623. case 1:
  1624. questionId = 10;
  1625. break;
  1626. case 2:
  1627. questionId = 11;
  1628. break;
  1629. case 3:
  1630. questionId = 12;
  1631. break;
  1632. case 4:
  1633. questionId = 13;
  1634. break;
  1635. default:
  1636. questionId = 10;
  1637. }
  1638. }
  1639. // 准备请求参数
  1640. const requestData = {
  1641. application_id: uni.getStorageSync('appId'),
  1642. question_id: questionId,
  1643. video_url: videoUrl,
  1644. tenant_id: uni.getStorageSync('tenant_id') || '1'
  1645. };
  1646. // 发送请求到面试接口
  1647. uni.request({
  1648. url: `${apiBaseUrl}/api/job/upload_video`,
  1649. method: 'POST',
  1650. data: requestData,
  1651. header: {
  1652. 'content-type': 'application/x-www-form-urlencoded'
  1653. },
  1654. success: (res) => {
  1655. console.log('面试接口提交成功:', res);
  1656. if (res.data.code === 0 || res.data.code === 2000) {
  1657. // 提交成功
  1658. if (task) {
  1659. // 从队列中移除当前任务
  1660. this.uploadQueue.shift();
  1661. // 显示简短的成功提示
  1662. // uni.showToast({
  1663. // title: '视频提交成功',
  1664. // icon: 'success',
  1665. // duration: 1500
  1666. // });
  1667. // 继续处理队列中的下一个任务
  1668. this.processUploadQueue();
  1669. } else {
  1670. // 兼容旧代码的处理逻辑
  1671. uni.showToast({
  1672. title: '回答已提交',
  1673. icon: 'success'
  1674. });
  1675. // 保存最后上传的视频URL
  1676. this.lastUploadedVideoUrl = videoUrl;
  1677. // 隐藏重试按钮(如果之前显示了)
  1678. this.showRetryButton = false;
  1679. this.lastVideoToRetry = null;
  1680. }
  1681. } else {
  1682. // 提交失败
  1683. if (task) {
  1684. this.handleSubmitFailure(task, res.data.msg || '提交失败');
  1685. } else {
  1686. // 兼容旧代码的处理逻辑
  1687. uni.showToast({
  1688. title: res.data.msg || '提交失败,请重试',
  1689. icon: 'none'
  1690. });
  1691. // 保存失败的视频URL,用于重试
  1692. this.lastVideoToRetry = videoUrl;
  1693. // 显示重试按钮
  1694. this.showRetryButton = true;
  1695. }
  1696. }
  1697. },
  1698. fail: (err) => {
  1699. console.error('面试接口提交失败:', err);
  1700. if (task) {
  1701. this.handleSubmitFailure(task, err.errMsg || '网络错误');
  1702. } else {
  1703. // 兼容旧代码的处理逻辑
  1704. uni.hideLoading();
  1705. uni.showToast({
  1706. title: '网络错误,请重试',
  1707. icon: 'none'
  1708. });
  1709. // 保存失败的视频URL,用于重试
  1710. this.lastVideoToRetry = videoUrl;
  1711. // 显示重试按钮
  1712. this.showRetryButton = true;
  1713. }
  1714. }
  1715. });
  1716. },
  1717. // 添加新方法:处理提交失败
  1718. handleSubmitFailure(task, errorMsg) {
  1719. console.error('提交失败:', errorMsg);
  1720. // 更新任务状态
  1721. this.uploadStatus[task.id] = 'failed';
  1722. this.updateUploadStatusText();
  1723. // 检查是否还有重试机会
  1724. if (task.attempts < task.maxAttempts) {
  1725. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1726. // 5秒后重试
  1727. setTimeout(() => {
  1728. // 重新提交
  1729. this.submitVideoToInterview(task.videoUrl, task);
  1730. }, 5000);
  1731. } else {
  1732. // 超过最大重试次数,移除任务并继续处理队列
  1733. console.log('超过最大重试次数,放弃提交');
  1734. // 显示错误提示
  1735. uni.showToast({
  1736. title: '视频提交失败,请稍后重试',
  1737. icon: 'none',
  1738. duration: 2000
  1739. });
  1740. // 从队列中移除当前任务
  1741. this.uploadQueue.shift();
  1742. // 继续处理队列中的下一个任务
  1743. this.processUploadQueue();
  1744. }
  1745. },
  1746. // 添加新方法:更新上传状态文本
  1747. updateUploadStatusText() {
  1748. if (this.uploadQueue.length === 0) {
  1749. this.uploadStatusText = '';
  1750. return;
  1751. }
  1752. const currentTask = this.uploadQueue[0];
  1753. const progress = this.uploadProgress[currentTask.id] || 0;
  1754. const status = this.uploadStatus[currentTask.id] || 'pending';
  1755. let statusText = '';
  1756. switch (status) {
  1757. case 'pending':
  1758. statusText = '等待上传';
  1759. break;
  1760. case 'uploading':
  1761. statusText = `上传中 ${progress}%`;
  1762. break;
  1763. case 'success':
  1764. statusText = '上传成功,提交中...';
  1765. break;
  1766. case 'failed':
  1767. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? '即将重试' : '已放弃'}`;
  1768. break;
  1769. }
  1770. this.uploadStatusText = `问题${currentTask.questionId - 9}:${statusText}`;
  1771. // 如果队列中有多个任务,显示总数
  1772. if (this.uploadQueue.length > 1) {
  1773. this.uploadStatusText += ` (${this.uploadQueue.length}个视频待处理)`;
  1774. }
  1775. },
  1776. // 修改 proceedToNextQuestion 方法,不再等待上传完成
  1777. proceedToNextQuestion() {
  1778. // 检查是否完成了第五个视频问题(索引为4)
  1779. if (this.currentVideoIndex === 4) {
  1780. // 完成第五个问题后,跳转到相机页面
  1781. uni.navigateTo({
  1782. url: '/pages/camera/camera'
  1783. });
  1784. return;
  1785. }
  1786. // 切换到下一个视频
  1787. this.currentVideoIndex++;
  1788. if (this.currentVideoIndex < this.videoList.length) {
  1789. // 还有下一段视频,播放它
  1790. this.videoUrl = this.videoList[this.currentVideoIndex];
  1791. this.videoPlaying = true;
  1792. // 重置当前字幕
  1793. this.currentSubtitle = '';
  1794. // 使用 nextTick 确保 DOM 更新后再播放视频
  1795. this.$nextTick(() => {
  1796. const videoContext = uni.createVideoContext('myVideo', this);
  1797. if (videoContext) {
  1798. videoContext.play();
  1799. }
  1800. });
  1801. } else {
  1802. // 所有视频都播放完毕,可以进行下一步操作
  1803. uni.showToast({
  1804. title: '面试完成',
  1805. icon: 'success',
  1806. duration: 2000
  1807. });
  1808. // 可以在这里添加面试完成后的逻辑,比如跳转到下一个页面
  1809. setTimeout(() => {
  1810. uni.navigateTo({
  1811. url: '/pages/interview-result/interview-result'
  1812. });
  1813. }, 2000);
  1814. }
  1815. },
  1816. // 修改 handleAnswerButtonClick 方法
  1817. handleAnswerButtonClick() {
  1818. // 隐藏答题按钮
  1819. this.showAnswerButton = false;
  1820. // 直接进入下一个问题
  1821. this.proceedToNextQuestion();
  1822. },
  1823. // 处理相机错误
  1824. handleCameraError(e) {
  1825. console.error('相机错误:', e);
  1826. // 获取平台信息
  1827. const systemInfo = uni.getSystemInfoSync();
  1828. const isIOS = systemInfo.platform === 'ios';
  1829. if (isIOS) {
  1830. console.log('iOS相机错误,尝试重新初始化');
  1831. // 显示提示
  1832. uni.showToast({
  1833. title: '相机初始化中...',
  1834. icon: 'loading',
  1835. duration: 2000
  1836. });
  1837. // 重置相机
  1838. this.resetCamera();
  1839. // 如果正在录制,停止录制
  1840. if (this.isRecording) {
  1841. this.isRecording = false;
  1842. this.showStopRecordingButton = false;
  1843. // 提供替代选项
  1844. setTimeout(() => {
  1845. this.useAlternativeRecordingMethod();
  1846. }, 1000);
  1847. }
  1848. } else {
  1849. // 显示错误提示
  1850. uni.showToast({
  1851. title: '相机初始化失败,请检查权限设置',
  1852. icon: 'none'
  1853. });
  1854. // 尝试备用选项
  1855. this.tryFallbackOptions();
  1856. }
  1857. },
  1858. // 添加新方法:尝试备用选项
  1859. tryFallbackOptions() {
  1860. // 检查环境
  1861. const systemInfo = uni.getSystemInfoSync();
  1862. // 在小程序环境中使用小程序API
  1863. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  1864. this.useMiniProgramCamera();
  1865. }
  1866. // 在H5环境中显示静态图像
  1867. else {
  1868. this.showStaticCameraPlaceholder();
  1869. }
  1870. },
  1871. // 添加新方法:使用小程序相机API
  1872. useMiniProgramCamera() {
  1873. console.log('尝试使用小程序相机组件');
  1874. // 这里需要在模板中添加小程序相机组件
  1875. // 并设置一个标志来控制显示
  1876. this.useMiniProgramCameraComponent = true;
  1877. },
  1878. // 添加新方法:显示静态图像
  1879. showStaticCameraPlaceholder() {
  1880. console.log('显示静态摄像头占位图');
  1881. // 创建一个图像元素
  1882. const img = document.createElement('img');
  1883. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  1884. img.className = 'static-camera-image';
  1885. img.style.width = '100%';
  1886. img.style.height = '100%';
  1887. img.style.objectFit = 'cover';
  1888. // 获取容器并添加图像
  1889. const container = this.$refs.userCameraVideo.parentNode;
  1890. container.appendChild(img);
  1891. },
  1892. // 处理视频时间更新事件
  1893. handleTimeUpdate(e) {
  1894. // 获取当前视频播放时间
  1895. const currentTime = e.target.currentTime;
  1896. // 如果正在录制,更新录制时间显示
  1897. if (this.isRecording && this.recordingTimerCount) {
  1898. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1899. }
  1900. // 根据当前播放的视频索引选择对应的字幕数组
  1901. let currentSubtitles;
  1902. if (this.currentVideoIndex === 0) {
  1903. currentSubtitles = this.subtitles;
  1904. } else if (this.currentVideoIndex === 1) {
  1905. currentSubtitles = this.secondVideoSubtitles;
  1906. } else if (this.currentVideoIndex === 2) {
  1907. currentSubtitles = this.thirdVideoSubtitles;
  1908. } else if (this.currentVideoIndex === 3) {
  1909. currentSubtitles = this.fourthVideoSubtitles;
  1910. } else if (this.currentVideoIndex === 4) {
  1911. currentSubtitles = this.fifthVideoSubtitles;
  1912. } else if (this.currentVideoIndex === 5) {
  1913. currentSubtitles = this.sixthVideoSubtitles;
  1914. } else {
  1915. // 如果有更多视频,可以继续添加条件
  1916. currentSubtitles = [];
  1917. }
  1918. // 查找当前时间应该显示的字幕
  1919. const subtitle = currentSubtitles.find(
  1920. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  1921. );
  1922. // 更新当前字幕
  1923. this.currentSubtitle = subtitle ? subtitle.text : '';
  1924. },
  1925. // Add a new method to handle the "Start Recording" button click
  1926. handleStartRecordingClick() {
  1927. // 隐藏"开始回答"按钮
  1928. this.showStartRecordingButton = false;
  1929. // 如果倒计时正在进行,清除倒计时
  1930. this.clearCountdown();
  1931. // 直接开始录制
  1932. this.startRecordingAnswer();
  1933. },
  1934. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1935. checkAudioPermission() {
  1936. // 在小程序环境中
  1937. const systemInfo = uni.getSystemInfoSync();
  1938. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1939. if (isMiniProgram) {
  1940. // 小程序环境下的权限请求
  1941. uni.getSetting({
  1942. success: (res) => {
  1943. if (!res.authSetting['scope.record']) {
  1944. uni.authorize({
  1945. scope: 'scope.record',
  1946. success: () => {
  1947. console.log('录音权限已获取');
  1948. },
  1949. fail: (err) => {
  1950. console.error('录音权限获取失败:', err);
  1951. this.showPermissionDialog('录音');
  1952. }
  1953. });
  1954. }
  1955. if (!res.authSetting['scope.camera']) {
  1956. uni.authorize({
  1957. scope: 'scope.camera',
  1958. success: () => {
  1959. console.log('相机权限已获取');
  1960. },
  1961. fail: (err) => {
  1962. console.error('相机权限获取失败:', err);
  1963. this.showPermissionDialog('相机');
  1964. }
  1965. });
  1966. }
  1967. }
  1968. });
  1969. } else {
  1970. // 浏览器环境下的权限请求
  1971. // ... 保持原有代码不变
  1972. }
  1973. },
  1974. // 添加新方法:测试音频输入
  1975. testAudioInput() {
  1976. if (!this.cameraStream) {
  1977. console.warn('没有可用的媒体流,无法测试音频');
  1978. return;
  1979. }
  1980. const audioTracks = this.cameraStream.getAudioTracks();
  1981. if (audioTracks.length === 0) {
  1982. console.warn('没有检测到音频轨道,尝试重新获取');
  1983. this.tryGetAudioOnly();
  1984. return;
  1985. }
  1986. console.log('音频轨道信息:', audioTracks[0].getSettings());
  1987. // 创建音频上下文和分析器
  1988. try {
  1989. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1990. if (!AudioContext) {
  1991. console.warn('浏览器不支持AudioContext');
  1992. return;
  1993. }
  1994. const audioContext = new AudioContext();
  1995. const analyser = audioContext.createAnalyser();
  1996. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1997. microphone.connect(analyser);
  1998. // 设置分析器
  1999. analyser.fftSize = 256;
  2000. const bufferLength = analyser.frequencyBinCount;
  2001. const dataArray = new Uint8Array(bufferLength);
  2002. // 检测音频输入
  2003. let silenceCounter = 0;
  2004. const checkAudio = () => {
  2005. if (this.isRecording) return; // 如果正在录制,停止检测
  2006. analyser.getByteFrequencyData(dataArray);
  2007. // 计算平均音量
  2008. let sum = 0;
  2009. for (let i = 0; i < bufferLength; i++) {
  2010. sum += dataArray[i];
  2011. }
  2012. const average = sum / bufferLength;
  2013. if (average > 10) {
  2014. console.log('检测到音频输入,音量:', average);
  2015. silenceCounter = 0;
  2016. } else {
  2017. silenceCounter++;
  2018. if (silenceCounter > 10) {
  2019. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  2020. silenceCounter = 0;
  2021. }
  2022. }
  2023. // 继续检测
  2024. requestAnimationFrame(checkAudio);
  2025. };
  2026. // 开始检测
  2027. checkAudio();
  2028. } catch (e) {
  2029. console.error('音频测试失败:', e);
  2030. }
  2031. },
  2032. // 添加新方法:尝试单独获取音频
  2033. tryGetAudioOnly() {
  2034. navigator.mediaDevices.getUserMedia({ audio: true })
  2035. .then(audioStream => {
  2036. // 如果已有视频流,合并音频和视频轨道
  2037. if (this.cameraStream) {
  2038. const videoTrack = this.cameraStream.getVideoTracks()[0];
  2039. const audioTrack = audioStream.getAudioTracks()[0];
  2040. // 创建新的媒体流,包含视频和音频轨道
  2041. const combinedStream = new MediaStream();
  2042. if (videoTrack) combinedStream.addTrack(videoTrack);
  2043. if (audioTrack) combinedStream.addTrack(audioTrack);
  2044. // 更新摄像头流
  2045. this.cameraStream = combinedStream;
  2046. // 更新视频元素的源
  2047. const videoElement = this.$refs.userCameraVideo;
  2048. if (videoElement) {
  2049. videoElement.srcObject = combinedStream;
  2050. videoElement.muted = true; // 避免回声,但仍然录制声音
  2051. }
  2052. console.log('成功合并音频和视频轨道');
  2053. } else {
  2054. console.warn('没有视频流可合并');
  2055. }
  2056. })
  2057. .catch(err => {
  2058. console.error('单独获取音频失败:', err);
  2059. });
  2060. },
  2061. // 添加新方法:显示权限对话框
  2062. showPermissionDialog(permissionType) {
  2063. uni.showModal({
  2064. title: '需要权限',
  2065. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  2066. confirmText: '去设置',
  2067. success: (res) => {
  2068. if (res.confirm) {
  2069. uni.openSetting({
  2070. success: (settingRes) => {
  2071. console.log('设置页面打开成功', settingRes);
  2072. }
  2073. });
  2074. }
  2075. }
  2076. });
  2077. },
  2078. // 添加重试上传方法
  2079. retryVideoUpload() {
  2080. if (this.lastVideoToRetry) {
  2081. // 隐藏重试按钮
  2082. this.showRetryButton = false;
  2083. // 显示加载提示
  2084. uni.showLoading({
  2085. title: '正在重新提交...',
  2086. mask: true
  2087. });
  2088. // 重新提交视频
  2089. this.submitVideoToInterview(this.lastVideoToRetry);
  2090. } else {
  2091. uni.showToast({
  2092. title: '没有可重试的视频',
  2093. icon: 'none'
  2094. });
  2095. }
  2096. },
  2097. // 添加一个新方法用于压缩视频
  2098. async compressVideo(videoBlob) {
  2099. // 如果视频大小小于某个阈值,直接返回原视频
  2100. if (videoBlob.size < 5 * 1024 * 1024) { // 小于5MB
  2101. return videoBlob;
  2102. }
  2103. console.log('开始压缩视频,原始大小:', videoBlob.size);
  2104. // 创建一个视频元素来加载视频
  2105. const videoElement = document.createElement('video');
  2106. videoElement.muted = true;
  2107. videoElement.autoplay = false;
  2108. // 创建一个canvas元素用于绘制视频帧
  2109. const canvas = document.createElement('canvas');
  2110. const ctx = canvas.getContext('2d');
  2111. // 设置视频源
  2112. videoElement.src = URL.createObjectURL(videoBlob);
  2113. return new Promise((resolve) => {
  2114. videoElement.onloadedmetadata = () => {
  2115. // 设置canvas尺寸为视频的一半
  2116. const width = Math.floor(videoElement.videoWidth / 2);
  2117. const height = Math.floor(videoElement.videoHeight / 2);
  2118. canvas.width = width;
  2119. canvas.height = height;
  2120. // 创建MediaRecorder来录制canvas
  2121. const stream = canvas.captureStream(15); // 15fps
  2122. // 如果原视频有音轨,添加到新流中
  2123. if (videoElement.captureStream) {
  2124. const originalStream = videoElement.captureStream();
  2125. const audioTracks = originalStream.getAudioTracks();
  2126. if (audioTracks.length > 0) {
  2127. stream.addTrack(audioTracks[0]);
  2128. }
  2129. }
  2130. // 设置较低的比特率
  2131. const options = {
  2132. mimeType: 'video/webm;codecs=vp8,opus',
  2133. audioBitsPerSecond: 64000,
  2134. videoBitsPerSecond: 800000 // 800kbps
  2135. };
  2136. const mediaRecorder = new MediaRecorder(stream, options);
  2137. const chunks = [];
  2138. mediaRecorder.ondataavailable = (e) => {
  2139. if (e.data.size > 0) {
  2140. chunks.push(e.data);
  2141. }
  2142. };
  2143. mediaRecorder.onstop = () => {
  2144. const compressedBlob = new Blob(chunks, { type: 'video/webm' });
  2145. console.log('视频压缩完成,压缩后大小:', compressedBlob.size);
  2146. resolve(compressedBlob);
  2147. };
  2148. // 开始播放视频并录制
  2149. videoElement.onplay = () => {
  2150. mediaRecorder.start(100);
  2151. // 绘制视频帧到canvas
  2152. const drawFrame = () => {
  2153. if (videoElement.paused || videoElement.ended) {
  2154. mediaRecorder.stop();
  2155. return;
  2156. }
  2157. ctx.drawImage(videoElement, 0, 0, width, height);
  2158. requestAnimationFrame(drawFrame);
  2159. };
  2160. drawFrame();
  2161. };
  2162. videoElement.play();
  2163. };
  2164. });
  2165. },
  2166. // 修改 checkIOSCameraRecordPermission 方法
  2167. checkIOSCameraRecordPermission() {
  2168. const systemInfo = uni.getSystemInfoSync();
  2169. if (systemInfo.platform !== 'ios') return;
  2170. // 在iOS上,需要同时请求相机和录音权限
  2171. uni.getSetting({
  2172. success: (res) => {
  2173. // 检查相机权限
  2174. if (!res.authSetting['scope.camera']) {
  2175. uni.authorize({
  2176. scope: 'scope.camera',
  2177. success: () => {
  2178. console.log('iOS相机权限已获取');
  2179. },
  2180. fail: (err) => {
  2181. console.error('iOS相机权限获取失败:', err);
  2182. this.showPermissionDialog('相机');
  2183. }
  2184. });
  2185. }
  2186. // 检查录音权限
  2187. if (!res.authSetting['scope.record']) {
  2188. uni.authorize({
  2189. scope: 'scope.record',
  2190. success: () => {
  2191. console.log('iOS录音权限已获取');
  2192. },
  2193. fail: (err) => {
  2194. console.error('iOS录音权限获取失败:', err);
  2195. this.showPermissionDialog('录音');
  2196. }
  2197. });
  2198. }
  2199. }
  2200. });
  2201. },
  2202. // 添加新方法:检查并修复渲染问题
  2203. checkAndFixRenderingIssues() {
  2204. // 检查全局对象,防止渲染错误
  2205. try {
  2206. // 检查是否有全局变量u
  2207. if (typeof u !== 'undefined' && u) {
  2208. // 检查currentQuestion属性
  2209. if (!u.currentQuestion) {
  2210. console.log('修复: 创建缺失的currentQuestion对象');
  2211. u.currentQuestion = {};
  2212. }
  2213. // 确保isImportant属性存在
  2214. if (u.currentQuestion && typeof u.currentQuestion.isImportant === 'undefined') {
  2215. console.log('修复: 设置缺失的isImportant属性');
  2216. u.currentQuestion.isImportant = false;
  2217. }
  2218. }
  2219. } catch (e) {
  2220. console.log('防御性检查异常:', e);
  2221. }
  2222. },
  2223. // 添加格式化时间的辅助方法
  2224. formatTime(seconds) {
  2225. if (!seconds && seconds !== 0) return '03:30'; // 默认显示03:30
  2226. const minutes = Math.floor(seconds / 60);
  2227. const remainingSeconds = seconds % 60;
  2228. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  2229. },
  2230. }
  2231. }
  2232. </script>
  2233. <style scoped>
  2234. .identity-verify-container {
  2235. padding: 0;
  2236. max-width: 100%;
  2237. margin: 0 auto;
  2238. height: 100vh;
  2239. display: flex;
  2240. flex-direction: column;
  2241. background-color: #f5f5f5;
  2242. }
  2243. .digital-human-container {
  2244. position: relative;
  2245. width: 100%;
  2246. height: 100vh;
  2247. overflow: hidden;
  2248. background-color: #f0f0f0;
  2249. }
  2250. .digital-human-video {
  2251. width: 100%;
  2252. height: 100%;
  2253. display: flex;
  2254. justify-content: center;
  2255. align-items: center;
  2256. }
  2257. /* 用户摄像头容器样式 */
  2258. .user-camera-container {
  2259. position: absolute;
  2260. top: 50px;
  2261. right: 5px;
  2262. width: 110px; /* 稍微增加宽度 */
  2263. height: 160px; /* 稍微增加高度 */
  2264. border-radius: 4px; /* 减小圆角 */
  2265. overflow: hidden;
  2266. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  2267. z-index: 20;
  2268. }
  2269. /* 用户摄像头视频样式 */
  2270. .user-camera-video {
  2271. width: 100%;
  2272. height: 100%;
  2273. object-fit: cover;
  2274. background-color: #333;
  2275. }
  2276. .video-player {
  2277. width: 100%;
  2278. height: 100%;
  2279. object-fit: cover;
  2280. outline: none; /* 移除视频获得焦点时的轮廓 */
  2281. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  2282. }
  2283. /* 隐藏视频控制条 */
  2284. video::-webkit-media-controls {
  2285. display: none !important;
  2286. }
  2287. video::-webkit-media-controls-enclosure {
  2288. display: none !important;
  2289. }
  2290. video::-webkit-media-controls-panel {
  2291. display: none !important;
  2292. }
  2293. video::-webkit-media-controls-play-button {
  2294. display: none !important;
  2295. }
  2296. video::-webkit-media-controls-timeline {
  2297. display: none !important;
  2298. }
  2299. video::-webkit-media-controls-current-time-display {
  2300. display: none !important;
  2301. }
  2302. video::-webkit-media-controls-time-remaining-display {
  2303. display: none !important;
  2304. }
  2305. video::-webkit-media-controls-mute-button {
  2306. display: none !important;
  2307. }
  2308. video::-webkit-media-controls-volume-slider {
  2309. display: none !important;
  2310. }
  2311. video::-webkit-media-controls-fullscreen-button {
  2312. display: none !important;
  2313. }
  2314. /* 修改字幕覆盖层样式,使其与图片中的样式一致 */
  2315. .subtitle-overlay {
  2316. position: absolute;
  2317. bottom: 180px; /* 调整位置,使其更靠近底部 */
  2318. left: 5%; /* 从左侧留出5%的空间 */
  2319. width: 80%; /* 宽度设为90%,两侧各留5%实现居中 */
  2320. padding: 15px 20px; /* 增加左右内边距 */
  2321. border-radius: 15px;
  2322. background-color: rgba(255, 255, 255, 0.9); /* 白色半透明背景 */
  2323. color: #333; /* 文字颜色为深色 */
  2324. text-align: left; /* 文字左对齐 */
  2325. font-size: 16px;
  2326. line-height: 1.5;
  2327. z-index: 10;
  2328. margin: 0 auto; /* 添加自动边距实现居中 */
  2329. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 添加轻微阴影 */
  2330. }
  2331. /* 添加计时器样式,右侧显示橙色圆点和时间 */
  2332. .subtitle-overlay::after {
  2333. content: attr(data-time);
  2334. position: absolute;
  2335. right: 20px;
  2336. bottom: 15px;
  2337. color: #333;
  2338. font-size: 14px;
  2339. }
  2340. .control-panel {
  2341. padding: 15px;
  2342. display: flex;
  2343. justify-content: center;
  2344. }
  2345. .control-button {
  2346. padding: 10px 20px;
  2347. background-color: #4CAF50;
  2348. color: white;
  2349. border: none;
  2350. border-radius: 4px;
  2351. cursor: pointer;
  2352. }
  2353. .loading {
  2354. text-align: center;
  2355. margin: 20px 0;
  2356. font-size: 16px;
  2357. }
  2358. .response-container {
  2359. margin-top: 20px;
  2360. padding: 0 20px;
  2361. display: none; /* 默认隐藏调试信息 */
  2362. }
  2363. /* 当showDebugInfo为true时显示 */
  2364. .showDebugInfo .response-container {
  2365. display: block;
  2366. }
  2367. .response-item {
  2368. padding: 10px;
  2369. border: 1px solid #eee;
  2370. border-radius: 4px;
  2371. margin-bottom: 10px;
  2372. background-color: #f9f9f9;
  2373. }
  2374. .response-content {
  2375. display: flex;
  2376. flex-direction: column;
  2377. }
  2378. .answer-button-container {
  2379. position: absolute;
  2380. bottom: 50px; /* 将按钮放在底部而不是75%的位置 */
  2381. left: 50%;
  2382. transform: translateX(-50%); /* 只在X轴上平移,保持水平居中 */
  2383. z-index: 20;
  2384. }
  2385. /* 修改回答按钮样式 */
  2386. .answer-button {
  2387. width: 120px;
  2388. height: 40px; /* 改为矩形按钮 */
  2389. border-radius: 20px; /* 圆角矩形 */
  2390. background-color: #ffffff; /* 白色背景 */
  2391. color: #333; /* 深色文字 */
  2392. font-size: 16px;
  2393. border: none;
  2394. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  2395. display: flex;
  2396. justify-content: center;
  2397. align-items: center;
  2398. cursor: pointer;
  2399. animation: none; /* 移除脉动动画 */
  2400. }
  2401. .stop-recording-button-container {
  2402. position: absolute;
  2403. bottom: 50px; /* 统一与其他按钮的位置 */
  2404. left: 50%;
  2405. transform: translateX(-50%);
  2406. z-index: 20;
  2407. }
  2408. .stop-recording-button {
  2409. width: 120px;
  2410. height: 40px; /* 改为矩形按钮 */
  2411. border-radius: 20px; /* 圆角矩形 */
  2412. background-color: #e74c3c; /* 白色背景 */
  2413. color: #333; /* 深色文字 */
  2414. font-size: 16px;
  2415. border: none;
  2416. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  2417. display: flex;
  2418. justify-content: center;
  2419. align-items: center;
  2420. cursor: pointer;
  2421. animation: none; /* 移除脉动动画 */
  2422. }
  2423. .recording-indicator {
  2424. position: absolute;
  2425. top: 20px;
  2426. left: 20px;
  2427. display: flex;
  2428. align-items: center;
  2429. background-color: rgba(255, 255, 255, 0.9); /* 改为白色半透明背景 */
  2430. padding: 5px 10px;
  2431. border-radius: 15px;
  2432. z-index: 20;
  2433. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  2434. }
  2435. .recording-dot {
  2436. width: 12px;
  2437. height: 12px;
  2438. background-color: #ff6b00; /* 改为橙色,与图片中的颜色一致 */
  2439. border-radius: 50%;
  2440. margin-right: 8px;
  2441. animation: blink 1s infinite;
  2442. }
  2443. .recording-text {
  2444. color: #333; /* 改为深色文字 */
  2445. font-size: 14px;
  2446. }
  2447. .timer-text {
  2448. color: #333; /* 改为深色文字 */
  2449. font-size: 14px;
  2450. margin-left: 8px;
  2451. }
  2452. .start-recording-button-container {
  2453. position: absolute;
  2454. bottom: 50px; /* 统一与其他按钮的位置 */
  2455. left: 50%;
  2456. transform: translateX(-50%);
  2457. z-index: 101;
  2458. }
  2459. .start-recording-button {
  2460. width: 120px;
  2461. height: 40px; /* 改为矩形按钮 */
  2462. border-radius: 20px; /* 圆角矩形 */
  2463. background-color: #ffffff; /* 白色背景 */
  2464. color: #333; /* 深色文字 */
  2465. font-size: 16px;
  2466. border: none;
  2467. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  2468. display: flex;
  2469. justify-content: center;
  2470. align-items: center;
  2471. cursor: pointer;
  2472. animation: none; /* 移除脉动动画 */
  2473. }
  2474. .retry-button-container {
  2475. position: absolute;
  2476. bottom: 30px; /* 统一与其他按钮的位置 */
  2477. left: 50%;
  2478. transform: translateX(-50%);
  2479. z-index: 20;
  2480. }
  2481. .retry-button {
  2482. padding: 10px 20px;
  2483. background-color: #ffffff; /* 白色背景 */
  2484. color: #333; /* 深色文字 */
  2485. font-size: 16px;
  2486. border: none;
  2487. border-radius: 20px; /* 圆角矩形 */
  2488. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  2489. cursor: pointer;
  2490. }
  2491. .retry-button:hover {
  2492. background-color: #2980b9;
  2493. }
  2494. /* 录制时长显示 */
  2495. .recording-timer {
  2496. position: absolute;
  2497. top: 20px;
  2498. left: 130px; /* 放在录制指示器旁边 */
  2499. background-color: rgba(0, 0, 0, 0.6);
  2500. padding: 5px 10px;
  2501. border-radius: 15px;
  2502. z-index: 20;
  2503. display: flex;
  2504. flex-direction: column;
  2505. }
  2506. .timer-text {
  2507. color: #000;
  2508. font-size: 14px;
  2509. font-family: monospace; /* 使用等宽字体,使时间显示更稳定 */
  2510. }
  2511. .remaining-time {
  2512. color: white;
  2513. font-size: 12px;
  2514. margin-top: 2px;
  2515. }
  2516. .remaining-time.warning {
  2517. color: #ff6b6b; /* 剩余时间少时显示红色 */
  2518. animation: blink 1s infinite; /* 使用闪烁动画提醒用户 */
  2519. }
  2520. /* 添加上传状态指示器 */
  2521. .upload-status-indicator {
  2522. position: absolute;
  2523. top: 20px;
  2524. left: 130px; /* 放在录制指示器旁边 */
  2525. background-color: rgba(0, 0, 0, 0.6);
  2526. padding: 5px 10px;
  2527. border-radius: 15px;
  2528. z-index: 20;
  2529. }
  2530. .upload-status-content {
  2531. display: flex;
  2532. align-items: center;
  2533. }
  2534. .upload-status-icon {
  2535. width: 12px;
  2536. height: 12px;
  2537. border-radius: 50%;
  2538. margin-right: 8px;
  2539. }
  2540. .upload-status-text {
  2541. color: white;
  2542. font-size: 14px;
  2543. }
  2544. .uploading {
  2545. background-color: #e74c3c;
  2546. }
  2547. /* 添加倒计时蒙层样式 */
  2548. .countdown-overlay {
  2549. position: fixed;
  2550. top: 0;
  2551. left: 0;
  2552. width: 100%;
  2553. height: 100%;
  2554. background-color: rgba(0, 0, 0, 0.7);
  2555. z-index: 100;
  2556. display: flex;
  2557. justify-content: center;
  2558. align-items: center;
  2559. }
  2560. .countdown-content {
  2561. display: flex;
  2562. flex-direction: column;
  2563. align-items: center;
  2564. }
  2565. .countdown-number {
  2566. font-size: 80px;
  2567. color: #ffffff;
  2568. font-weight: bold;
  2569. margin-bottom: 20px;
  2570. }
  2571. .countdown-text {
  2572. font-size: 24px;
  2573. color: #ffffff;
  2574. }
  2575. </style>