identity-verify.vue 93 KB

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