followUp.vue 103 KB

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