identity-verify.vue 93 KB

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