interview-question.vue 101 KB

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