identity-verify.js 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_config = require("../../common/config.js");
  4. const _sfc_main = {
  5. name: "IdentityVerify",
  6. data() {
  7. return {
  8. followUpQuestion: "",
  9. // 追问问题
  10. followUpAudioUrl: "",
  11. // 追问音频URL
  12. audioContext: null,
  13. // 音频上下文
  14. followUpQuestions: [],
  15. // 追问问题列表
  16. isWaitingForAnswer: false,
  17. // 添加新的状态来控制是否在等待用户回答
  18. currentFollowUpIndex: -1,
  19. // 当前追问问题索引
  20. showSubtitleText: false,
  21. // 是否显示字幕
  22. subtitleText: "",
  23. // 字幕文本
  24. parentQuestion: "",
  25. // 父问题文本
  26. isFollowUpQuestion: false,
  27. // 是否是追问问题
  28. loading: false,
  29. responses: [],
  30. processedResponses: [],
  31. assistantResponse: "",
  32. audioTranscript: "",
  33. videoPlaying: false,
  34. showDebugInfo: false,
  35. // 设置为true可以显示调试信息
  36. videoUrl: "",
  37. // 修改为空字符串,将通过API获取
  38. showReplayButton: false,
  39. cameraStream: null,
  40. // 存储摄像头流
  41. cameraError: null,
  42. // 存储摄像头错误信息
  43. useMiniProgramCameraComponent: false,
  44. // 添加小程序相机组件标志
  45. cameraContext: null,
  46. // 添加相机上下文
  47. currentSubtitle: "",
  48. currentTranslation: "",
  49. // 添加翻译文本
  50. subtitles: [],
  51. // 修改为空数组,将通过API获取
  52. // 移除硬编码的字幕数组
  53. // secondVideoSubtitles: [...],
  54. // thirdVideoSubtitles: [...],
  55. // 等等...
  56. showAnswerButton: false,
  57. // 控制答题按钮显示
  58. currentVideoIndex: 0,
  59. // 当前播放的视频索引
  60. videoList: [],
  61. // 修改为空数组,将通过API获取
  62. // 其他属性保持不变...
  63. questions: [],
  64. // 添加新属性存储API返回的问题数据
  65. introVideoUrl: (() => {
  66. const DEFAULT_VIDEO_URL = "https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4";
  67. try {
  68. const configStr = common_vendor.index.getStorageSync("configData");
  69. if (!configStr)
  70. return DEFAULT_VIDEO_URL;
  71. const config = JSON.parse(configStr);
  72. return config.digital_human_opening_video_url || DEFAULT_VIDEO_URL;
  73. } catch (error) {
  74. console.warn("解析配置数据失败:", error);
  75. return DEFAULT_VIDEO_URL;
  76. }
  77. })(),
  78. //'https://data.qicai321.com/minlong/ee4d9cce-c3d5-4350-8c6e-684283827897.mp4', // 保留介绍视频
  79. isRecording: false,
  80. recordingTimer: null,
  81. showStopRecordingButton: false,
  82. mediaRecorder: null,
  83. recordedChunks: [],
  84. recorder: null,
  85. lastUploadedVideoUrl: "",
  86. showStartRecordingButton: false,
  87. showRetryButton: false,
  88. // 控制重试按钮显示
  89. lastVideoToRetry: null,
  90. // 存储上次失败的视频URL,用于重试
  91. recordingStartTime: null,
  92. // 录制开始时间
  93. recordingTimerCount: 0,
  94. // 录制计时器计数
  95. recordingTimeDisplay: "00:00",
  96. // 格式化的录制时间显示
  97. // 添加上传队列相关数据
  98. uploadQueue: [],
  99. // 存储待上传的视频
  100. backgroundUploadQueue: [],
  101. // 存储后台上传的视频
  102. isUploading: false,
  103. // 标记是否正在上传
  104. uploadProgress: {},
  105. // 存储每个视频的上传进度
  106. uploadStatus: {},
  107. // 存储每个视频的上传状态
  108. showUploadStatus: false,
  109. // 是否显示上传状态指示器
  110. uploadStatusText: "",
  111. // 上传状态文本
  112. mediaRecorderTimeout: null,
  113. // 用于存储MediaRecorder的超时机制
  114. maxRecordingTime: 300,
  115. // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  116. remainingTime: 300,
  117. // 剩余录制时间(秒)- 从60秒改为300秒
  118. countdownValue: 10,
  119. // 倒计时数值
  120. showCountdown: false,
  121. // 是否显示倒计时蒙层
  122. countdownTimer: null,
  123. showGif: false,
  124. // 控制是否显示GIF
  125. gifUrl: "",
  126. // GIF图片的URL
  127. globalSocketTask: null,
  128. // 添加全局 WebSocket 连接对象
  129. lowScoreVideoUrl: (() => {
  130. var _a;
  131. const DEFAULT_VIDEO_URL = "https://data.qicai321.com/minlong/latentsync/0530e7f5-1957-422d-8f34-ba4a92608081_result.mp4";
  132. try {
  133. const configStr = common_vendor.index.getStorageSync("configData");
  134. if (!configStr)
  135. return DEFAULT_VIDEO_URL;
  136. const config = JSON.parse(configStr);
  137. return ((_a = config.digital_human) == null ? void 0 : _a.middle_video_url) || DEFAULT_VIDEO_URL;
  138. } catch (error) {
  139. console.warn("解析配置数据失败:", error);
  140. return DEFAULT_VIDEO_URL;
  141. }
  142. })(),
  143. //'https://data.qicai321.com/minlong/latentsync/0530e7f5-1957-422d-8f34-ba4a92608081_result.mp4', // 低分提示视频URL
  144. showRerecordButton: false,
  145. // 控制重新录制按钮显示
  146. isPlayingLowScoreVideo: false,
  147. // 标记是否正在播放低分提示视频
  148. lowScoreVideoSubtitles: [
  149. {
  150. startTime: 0,
  151. endTime: 10,
  152. // 假设视频长度为10秒,可根据实际情况调整
  153. text: "我未听清楚您在说什么,请您再说一遍!"
  154. }
  155. ],
  156. // 用于存储低分提示视频的字幕
  157. retryCount: 0,
  158. // 添加重试次数计数器
  159. maxRetryAttempts: 2,
  160. // 最大重试次数限制
  161. needPlayLowScoreVideo: false,
  162. // 是否需要播放低分视频
  163. finalRecordingDuration: 0,
  164. // 最终录制时长
  165. historyTime: 0,
  166. // 添加历史时间记录
  167. followUpQuestions: [],
  168. // 存储追问问题数据
  169. currentParentQuestionId: null,
  170. // 当前父问题ID
  171. hasPlayedFollowUp: {},
  172. // 记录已播放的追问,格式: {questionId: true}
  173. isFollowUpQuestion: false,
  174. // 标记当前是否正在播放追问问题
  175. currentFollowUpQuestion: null,
  176. // 添加当前追问问题的完整信息
  177. lastQuestionWasFollowUp: false,
  178. lastFollowUpQuestionId: null,
  179. // 追问次数控制 - 统一使用job_position_question_id作为key
  180. defaultFollowUpLimit: 1,
  181. // 默认每个主题目最多1次追问
  182. followUpLimitMap: {},
  183. // { job_position_question_id: limit }
  184. followUpAskedCountMap: {},
  185. // { job_position_question_id: askedCount }
  186. followUpRequestInFlight: {},
  187. // { job_position_question_id: boolean }
  188. lastUpdateTime: Date.now(),
  189. // 添加最后更新时间戳
  190. subtitleMap: {},
  191. // 用于存储字幕和翻译的映射
  192. progressPercent: 0,
  193. // 录制进度百分比
  194. progressColor: "#05dc8b",
  195. // 进度条颜色
  196. progressBgColor: "rgba(0, 0, 0,0.3)",
  197. // 进度条背景色
  198. parentQuestion: "",
  199. // 添加父问题存储
  200. screenCaptureCount: 0,
  201. // 添加截屏次数记录
  202. personDetectionSocket: null,
  203. // WebSocket对象
  204. personDetectionInterval: null,
  205. // 定时器对象
  206. showCameraWarning: false,
  207. // 添加新的数据属性
  208. showPageWarning: false,
  209. // 添加新的数据属性
  210. showPageDRWarning: false,
  211. //多人提示
  212. followUpQuestion: "",
  213. // 追问问题
  214. followUpAudioUrl: "",
  215. // 追问音频URL
  216. audioContext: null,
  217. // 音频上下文
  218. followUpQuestions: [],
  219. // 追问问题列表
  220. currentFollowUpIndex: -1,
  221. // 当前追问问题索引
  222. showSubtitleText: false,
  223. // 是否显示字幕
  224. subtitleText: "",
  225. // 字幕文本
  226. isAudioPlaying: false,
  227. isWaitingForAnswer: false,
  228. // 添加新的状态来控制是否在等待用户回答
  229. currentRecordingDuration: 0,
  230. // 添加当前录制时长
  231. parentJobPositionQuestionId: null,
  232. // 添加父问题的job_position_question_id
  233. isFollowUpMode: false,
  234. // 是否处于追问模式
  235. mainQuestionIndex: 0,
  236. // 当前主问题的索引
  237. isVideoSwitching: false,
  238. // 添加视频切换状态锁
  239. originalQuestionSubtitle: null,
  240. // 保存原始字幕信息
  241. isThinking: false,
  242. // 面试官思考中状态
  243. thinkingTimer: null,
  244. // 思考计时器
  245. questionRetryMap: {},
  246. // 用于跟踪每个问题的重试次数
  247. maxQuestionRetries: 2,
  248. // 每个问题最大重试次数
  249. statusBarHeight: 0
  250. };
  251. },
  252. onLoad() {
  253. this.statusBarHeight = common_vendor.index.getSystemInfoSync().statusBarHeight || 0;
  254. const systemInfo = common_vendor.index.getSystemInfoSync();
  255. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  256. if (isMiniProgram) {
  257. this.initPersonDetectionWebSocket();
  258. }
  259. },
  260. mounted() {
  261. this.fetchQuestions();
  262. this.checkAudioPermission();
  263. this.initCamera();
  264. this.checkIOSCameraRecordPermission();
  265. this.checkAndFixRenderingIssues();
  266. setTimeout(() => {
  267. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  268. this.testAudioInput();
  269. }
  270. }, 3e3);
  271. this.historyTime = 0;
  272. common_vendor.index.setKeepScreenOn({
  273. keepScreenOn: true
  274. });
  275. common_vendor.index.onUserCaptureScreen(() => {
  276. console.log("User captured screen");
  277. this.screenCaptureCount++;
  278. if (this.screenCaptureCount === 1) {
  279. common_vendor.index.showModal({
  280. title: "Warning",
  281. content: "检测到屏幕截图。如果你再次捕捉屏幕,你的面试将无效.",
  282. showCancel: false,
  283. confirmText: "OK"
  284. });
  285. } else if (this.screenCaptureCount >= 2) {
  286. common_vendor.index.showModal({
  287. title: "Interview Invalid",
  288. content: "由于多次屏幕截图,您的面试已无效。请联系招聘人员寻求帮助.",
  289. showCancel: false,
  290. confirmText: "OK",
  291. success: () => {
  292. this.invalidateInterview();
  293. }
  294. });
  295. }
  296. });
  297. },
  298. beforeDestroy() {
  299. common_vendor.index.offUserCaptureScreen();
  300. this.cleanupPersonDetectionWebSocket();
  301. },
  302. methods: {
  303. // ===== 追问次数控制:工具方法 =====
  304. getFollowUpLimit(jobPositionQuestionId) {
  305. if (!jobPositionQuestionId)
  306. return this.defaultFollowUpLimit;
  307. return this.followUpLimitMap[jobPositionQuestionId] || this.defaultFollowUpLimit;
  308. },
  309. setFollowUpLimit(jobPositionQuestionId, limit) {
  310. if (!jobPositionQuestionId)
  311. return;
  312. const parsed = parseInt(limit);
  313. this.followUpLimitMap[jobPositionQuestionId] = Number.isFinite(parsed) && parsed > 0 ? parsed : this.defaultFollowUpLimit;
  314. console.log(`设置问题 ${jobPositionQuestionId} 的追问上限为: ${this.followUpLimitMap[jobPositionQuestionId]}`);
  315. },
  316. resetFollowUpAskedCount(jobPositionQuestionId) {
  317. if (!jobPositionQuestionId)
  318. return;
  319. this.followUpAskedCountMap[jobPositionQuestionId] = 1;
  320. console.log(`重置问题 ${jobPositionQuestionId} 的追问次数为: 1`);
  321. },
  322. incrementFollowUpCount(jobPositionQuestionId) {
  323. if (!jobPositionQuestionId)
  324. return;
  325. const current = this.followUpAskedCountMap[jobPositionQuestionId] || 0;
  326. this.followUpAskedCountMap[jobPositionQuestionId] = current + 1;
  327. console.log(`问题 ${jobPositionQuestionId} 的追问次数增加到: ${this.followUpAskedCountMap[jobPositionQuestionId]}`);
  328. },
  329. canAskMoreFollowUps(jobPositionQuestionId) {
  330. if (!jobPositionQuestionId)
  331. return false;
  332. const asked = this.followUpAskedCountMap[jobPositionQuestionId] || 0;
  333. const limit = this.getFollowUpLimit(jobPositionQuestionId);
  334. const canAsk = asked < limit;
  335. console.log(`检查问题 ${jobPositionQuestionId} 是否可以继续追问: ${canAsk} (已问${asked}次/上限${limit}次)`);
  336. return canAsk;
  337. },
  338. async maybeRequestNextFollowUp(jobPositionQuestionId) {
  339. if (!jobPositionQuestionId) {
  340. console.log("缺少job_position_question_id,直接进入下一题");
  341. this.proceedToNextQuestion();
  342. return;
  343. }
  344. console.log(`检查是否需要继续追问,问题ID: ${jobPositionQuestionId}`);
  345. if (this.canAskMoreFollowUps(jobPositionQuestionId)) {
  346. if (this.followUpRequestInFlight[jobPositionQuestionId]) {
  347. console.log("已有追问请求进行中,跳过重复触发");
  348. return;
  349. }
  350. const ok = await this.callInterviewInteraction(jobPositionQuestionId);
  351. if (!ok) {
  352. console.log("未获取到下一条追问或调用失败,进入下一题");
  353. this.isFollowUpMode = false;
  354. this.isFollowUpQuestion = false;
  355. this.currentFollowUpQuestion = null;
  356. this.proceedToNextQuestion();
  357. }
  358. } else {
  359. console.log(`问题 ${jobPositionQuestionId} 追问次数已达上限,进入下一题`);
  360. this.isFollowUpMode = false;
  361. this.isFollowUpQuestion = false;
  362. this.currentFollowUpQuestion = null;
  363. this.proceedToNextQuestion();
  364. }
  365. },
  366. // 获取当前问题信息
  367. getCurrentQuestionByIndex(index) {
  368. return null;
  369. },
  370. // 处理音频播放完成
  371. handleAudioEnd() {
  372. console.log("音频播放完成");
  373. this.isWaitingForAnswer = true;
  374. this.showStartRecordingButton = true;
  375. setTimeout(() => {
  376. this.startCountdown();
  377. }, 100);
  378. },
  379. // 开始倒计时
  380. startCountdown() {
  381. this.showCountdown = true;
  382. this.countdownValue = 5;
  383. this.countdownTimer = setInterval(() => {
  384. this.countdownValue--;
  385. if (this.countdownValue <= 0) {
  386. this.clearCountdown();
  387. this.showStartRecordingButton = false;
  388. this.startRecordingAnswer();
  389. }
  390. }, 1e3);
  391. },
  392. // 清除倒计时
  393. clearCountdown() {
  394. if (this.countdownTimer) {
  395. clearInterval(this.countdownTimer);
  396. this.countdownTimer = null;
  397. }
  398. this.showCountdown = false;
  399. },
  400. // 处理追问问题
  401. async handleFollowUpQuestion(questionData) {
  402. console.log("处理追问:", questionData);
  403. this.enterFollowUpMode();
  404. console.log("处理追问问题数据:", questionData);
  405. if (!questionData || !questionData.follow_up_voice_url || !questionData.follow_up_question) {
  406. console.error("追问数据不完整:", questionData);
  407. common_vendor.index.showToast({
  408. title: "获取追问数据失败",
  409. icon: "none"
  410. });
  411. return;
  412. }
  413. this.parentQuestion = questionData.original_question || "";
  414. let audioUrl = "";
  415. if (questionData.follow_up_voice_url.direct_url) {
  416. audioUrl = questionData.follow_up_voice_url.direct_url;
  417. } else if (questionData.follow_up_voice_url.file_url) {
  418. audioUrl = questionData.follow_up_voice_url.file_url;
  419. }
  420. if (!audioUrl.startsWith("http")) {
  421. audioUrl = common_config.apiBaseUrl + audioUrl;
  422. }
  423. console.log("处理后的音频URL:", audioUrl);
  424. const followUpQuestionObj = {
  425. id: Date.now(),
  426. question: questionData.follow_up_question.trim(),
  427. // 去除可能的空白字符
  428. audioUrl,
  429. originalQuestion: questionData.original_question,
  430. sessionId: questionData.session_id
  431. };
  432. console.log("创建的追问问题对象:", followUpQuestionObj);
  433. this.followUpQuestions.push(followUpQuestionObj);
  434. this.currentFollowUpIndex = this.followUpQuestions.length - 1;
  435. this.followUpQuestion = followUpQuestionObj.question;
  436. this.followUpAudioUrl = followUpQuestionObj.audioUrl;
  437. this.isFollowUpQuestion = true;
  438. this.currentFollowUpQuestion = followUpQuestionObj;
  439. this.showSubtitle(followUpQuestionObj.question, true);
  440. try {
  441. await this.playFollowUpAudio();
  442. console.log("音频播放完成,准备录制回答");
  443. this.prepareToAnswer(true, followUpQuestionObj);
  444. } catch (error) {
  445. console.error("音频播放失败:", error);
  446. this.prepareToAnswer(true, followUpQuestionObj);
  447. }
  448. },
  449. // 准备回答问题
  450. prepareToAnswer(isFollowUp = false, questionData = null) {
  451. console.log("准备回答问题:", { isFollowUp, questionData });
  452. this.isRecording = false;
  453. this.recordedTime = 0;
  454. this.showStopRecordingButton = false;
  455. if (isFollowUp && questionData) {
  456. this.currentFollowUpQuestion = questionData;
  457. this.isFollowUpQuestion = true;
  458. }
  459. this.isWaitingForAnswer = true;
  460. this.handleAudioEnd();
  461. },
  462. // 调用面试互动接口
  463. async callInterviewInteraction(jobPositionQuestionId, retryCount = 0, maxRetries = 3) {
  464. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  465. const appId = common_vendor.index.getStorageSync("appId");
  466. const positionConfigId = JSON.parse(common_vendor.index.getStorageSync("configData")).id;
  467. try {
  468. this.showThinkingLoading();
  469. console.log("开始调用面试互动接口", { jobPositionQuestionId, appId });
  470. const res = await common_vendor.index.request({
  471. url: `${common_config.apiBaseUrl}/api/voice_interview_interaction/`,
  472. method: "POST",
  473. data: {
  474. tenant_id: common_vendor.index.getStorageSync("tenant_id") || userInfo.tenant_id || 1,
  475. question_id: jobPositionQuestionId,
  476. position_config_id: positionConfigId,
  477. application_id: appId
  478. },
  479. header: {
  480. "content-type": "application/json"
  481. }
  482. });
  483. console.log("面试互动接口返回数据:", res);
  484. if (res.statusCode === 400) {
  485. if (retryCount < maxRetries) {
  486. console.log(`视频转写未完成,${retryCount + 1}次重试中...`);
  487. await new Promise((resolve) => setTimeout(resolve, 3e3));
  488. return this.callInterviewInteraction(jobPositionQuestionId, retryCount + 1, maxRetries);
  489. } else {
  490. console.log("达到最大重试次数,视频转写仍未完成");
  491. common_vendor.index.showToast({
  492. title: "视频转写处理中,请稍后再试",
  493. icon: "none",
  494. duration: 2e3
  495. });
  496. return false;
  497. }
  498. }
  499. if (res.data.success) {
  500. if (jobPositionQuestionId) {
  501. this.incrementFollowUpCount(jobPositionQuestionId);
  502. }
  503. await this.handleFollowUpQuestion(res.data);
  504. this.hideThinkingLoading();
  505. return true;
  506. } else {
  507. console.error("面试互动接口返回错误:", res.data);
  508. this.hideThinkingLoading();
  509. return false;
  510. }
  511. } catch (error) {
  512. console.error("调用面试互动接口失败:", error);
  513. this.hideThinkingLoading();
  514. return false;
  515. }
  516. },
  517. // 播放追问音频
  518. async playFollowUpAudio() {
  519. return new Promise((resolve, reject) => {
  520. console.log("开始播放追问音频, URL:", this.followUpAudioUrl);
  521. if (!this.followUpAudioUrl) {
  522. console.error("没有音频URL");
  523. reject(new Error("没有音频URL"));
  524. return;
  525. }
  526. common_vendor.index.showLoading({
  527. title: "思考中..."
  528. });
  529. this.stopAndDestroyAudio();
  530. try {
  531. const innerAudioContext = common_vendor.index.createInnerAudioContext();
  532. this.audioContext = innerAudioContext;
  533. innerAudioContext.autoplay = true;
  534. innerAudioContext.obeyMuteSwitch = false;
  535. innerAudioContext.volume = 1;
  536. innerAudioContext.onCanplay(() => {
  537. console.log("音频可以播放");
  538. common_vendor.index.hideLoading();
  539. this.isAudioPlaying = true;
  540. });
  541. innerAudioContext.onEnded(() => {
  542. console.log("追问音频播放完成");
  543. this.isAudioPlaying = false;
  544. resolve();
  545. setTimeout(() => {
  546. this.stopAndDestroyAudio();
  547. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  548. }, 100);
  549. });
  550. innerAudioContext.onError((res) => {
  551. console.error("音频播放错误:", res);
  552. this.isAudioPlaying = false;
  553. common_vendor.index.hideLoading();
  554. reject(res);
  555. this.stopAndDestroyAudio();
  556. });
  557. console.log("设置音频源:", this.followUpAudioUrl);
  558. innerAudioContext.src = this.followUpAudioUrl;
  559. } catch (error) {
  560. console.error("创建或播放音频失败:", error);
  561. this.isAudioPlaying = false;
  562. common_vendor.index.hideLoading();
  563. this.stopAndDestroyAudio();
  564. reject(error);
  565. }
  566. });
  567. },
  568. // 停止并销毁音频
  569. stopAndDestroyAudio() {
  570. if (!this.audioContext) {
  571. return;
  572. }
  573. const ctx = this.audioContext;
  574. this.audioContext = null;
  575. try {
  576. if (this.isAudioPlaying) {
  577. ctx.stop();
  578. }
  579. } catch (error) {
  580. console.error("停止音频播放失败:", error);
  581. }
  582. try {
  583. ctx.destroy();
  584. } catch (error) {
  585. console.error("销毁音频实例失败:", error);
  586. }
  587. this.isAudioPlaying = false;
  588. },
  589. // 绑定音频事件
  590. bindAudioEvents(audioContext, resolve, reject) {
  591. audioContext.onCanplay(() => {
  592. console.log("音频可以播放");
  593. });
  594. audioContext.onTimeUpdate(() => {
  595. console.log("音频播放进度:", audioContext.currentTime);
  596. });
  597. audioContext.onEnded(() => {
  598. console.log("追问音频播放完成");
  599. this.cleanupAudioContext();
  600. resolve();
  601. });
  602. audioContext.onError((res) => {
  603. console.error("音频播放错误:", res);
  604. this.cleanupAudioContext();
  605. reject(res);
  606. });
  607. },
  608. // 修改准备回答方法
  609. prepareToAnswer(isFollowUp = false, questionData = null) {
  610. this.isRecording = false;
  611. this.recordedTime = 0;
  612. this.showStopRecordingButton = false;
  613. if (isFollowUp && questionData) {
  614. this.currentFollowUpQuestion = questionData;
  615. this.isFollowUpQuestion = true;
  616. }
  617. this.isWaitingForAnswer = true;
  618. this.handleAudioEnd();
  619. },
  620. // 初始化相机
  621. async initCamera() {
  622. const systemInfo = common_vendor.index.getSystemInfoSync();
  623. const isMiniProgram = systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay" || systemInfo.uniPlatform === "mp-baidu";
  624. if (isMiniProgram) {
  625. this.useMiniProgramCameraComponent = true;
  626. this.cameraContext = common_vendor.index.createCameraContext();
  627. common_vendor.index.getSetting({
  628. success: (res) => {
  629. if (!res.authSetting["scope.record"]) {
  630. common_vendor.index.authorize({
  631. scope: "scope.record",
  632. success: () => {
  633. console.log("录音权限已获取");
  634. },
  635. fail: (err) => {
  636. console.error("录音权限获取失败:", err);
  637. this.showPermissionDialog("录音");
  638. }
  639. });
  640. }
  641. if (!res.authSetting["scope.camera"]) {
  642. common_vendor.index.authorize({
  643. scope: "scope.camera",
  644. success: () => {
  645. console.log("相机权限已获取");
  646. },
  647. fail: (err) => {
  648. console.error("相机权限获取失败:", err);
  649. this.showPermissionDialog("相机");
  650. }
  651. });
  652. }
  653. const systemInfo2 = common_vendor.index.getSystemInfoSync();
  654. if (systemInfo2.platform === "ios") {
  655. if (!res.authSetting["scope.camera"] || !res.authSetting["scope.record"]) {
  656. console.log("iOS需要同时获取相机和录音权限");
  657. }
  658. }
  659. }
  660. });
  661. } else {
  662. try {
  663. const constraints = {
  664. audio: {
  665. echoCancellation: true,
  666. noiseSuppression: true,
  667. autoGainControl: true
  668. },
  669. video: {
  670. width: { ideal: 640, max: 1280 },
  671. // 控制视频宽度
  672. height: { ideal: 480, max: 720 },
  673. // 控制视频高度
  674. frameRate: { ideal: 15, max: 24 },
  675. // 控制帧率
  676. facingMode: "user"
  677. }
  678. };
  679. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  680. this.cameraStream = stream;
  681. const audioTracks = stream.getAudioTracks();
  682. console.log("音频轨道数量:", audioTracks.length);
  683. if (audioTracks.length > 0) {
  684. console.log("音频轨道已获取:", audioTracks[0].label);
  685. audioTracks[0].enabled = true;
  686. } else {
  687. console.warn("未检测到音频轨道,尝试单独获取音频");
  688. this.tryGetAudioOnly();
  689. }
  690. const videoElement = this.$refs.userCameraVideo;
  691. if (videoElement) {
  692. videoElement.srcObject = stream;
  693. videoElement.muted = true;
  694. }
  695. } catch (error) {
  696. console.error("获取摄像头失败:", error);
  697. this.cameraError = error.message || "无法访问摄像头";
  698. common_vendor.index.showToast({
  699. title: "无法访问摄像头,请检查权限设置",
  700. icon: "none"
  701. });
  702. }
  703. }
  704. },
  705. // 停止用户摄像头
  706. stopUserCamera() {
  707. if (this.cameraStream) {
  708. this.cameraStream.getTracks().forEach((track) => {
  709. track.stop();
  710. });
  711. this.cameraStream = null;
  712. }
  713. },
  714. async fetchData() {
  715. this.loading = true;
  716. this.assistantResponse = "";
  717. this.audioTranscript = "";
  718. this.processedResponses = [];
  719. common_vendor.index.showLoading({
  720. title: "面试官思考中...",
  721. mask: true
  722. // 添加遮罩,防止用户触摸界面
  723. });
  724. try {
  725. const requestTask = common_vendor.index.request({
  726. url: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
  727. method: "POST",
  728. header: {
  729. "Content-Type": "application/json",
  730. "Authorization": "Bearer sk-9e1ec73a7d97493b8613c63f06b6110c"
  731. },
  732. data: {
  733. "model": "qwen-omni-turbo",
  734. "messages": [
  735. {
  736. "role": "user",
  737. "content": [
  738. {
  739. "type": "input_audio",
  740. "input_audio": {
  741. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  742. "format": "wav"
  743. }
  744. },
  745. {
  746. "type": "text",
  747. "text": "这段音频在说什么"
  748. }
  749. ]
  750. }
  751. ],
  752. "stream": true,
  753. "stream_options": {
  754. "include_usage": true
  755. },
  756. "modalities": ["text", "audio"],
  757. "audio": { "voice": "Cherry", "format": "wav" }
  758. },
  759. success: (res) => {
  760. console.log("请求成功,响应数据:", res.data);
  761. if (typeof res.data === "string" && res.data.includes("data: {")) {
  762. const chunks = res.data.split("data: ").filter((chunk) => chunk.trim() !== "");
  763. chunks.forEach((chunk) => {
  764. this.handleStreamResponse(chunk);
  765. });
  766. } else {
  767. this.handleStreamResponse(res.data);
  768. }
  769. this.playDigitalHumanVideo();
  770. },
  771. fail: (err) => {
  772. console.error("请求失败:", err);
  773. common_vendor.index.showToast({
  774. title: "请求失败,请重试",
  775. icon: "none"
  776. });
  777. },
  778. complete: () => {
  779. this.loading = false;
  780. common_vendor.index.hideLoading();
  781. }
  782. });
  783. } catch (error) {
  784. console.error("获取数据失败:", error);
  785. this.loading = false;
  786. common_vendor.index.hideLoading();
  787. common_vendor.index.showToast({
  788. title: "系统错误,请重试",
  789. icon: "none"
  790. });
  791. }
  792. },
  793. handleStreamResponse(data) {
  794. if (typeof data === "string") {
  795. if (data === "[DONE]")
  796. return;
  797. try {
  798. const cleanData = data.trim();
  799. if (cleanData.startsWith("{") && cleanData.endsWith("}")) {
  800. const jsonData = JSON.parse(cleanData);
  801. this.processStreamChunk(jsonData);
  802. }
  803. } catch (e) {
  804. console.error("解析JSON失败:", e, "原始数据:", data);
  805. }
  806. } else {
  807. this.processStreamChunk(data);
  808. }
  809. },
  810. processStreamChunk(chunk) {
  811. if (chunk.choices && chunk.choices.length > 0) {
  812. const choice = chunk.choices[0];
  813. if (choice.delta && choice.delta.content) {
  814. this.assistantResponse += choice.delta.content;
  815. }
  816. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  817. this.audioTranscript += choice.delta.audio.transcript;
  818. }
  819. if (choice.delta) {
  820. const result = {};
  821. if (choice.delta.role) {
  822. result.role = choice.delta.role;
  823. }
  824. if (choice.delta.audio && choice.delta.audio.transcript) {
  825. result.transcript = choice.delta.audio.transcript;
  826. }
  827. if (Object.keys(result).length > 0) {
  828. this.processedResponses.push(result);
  829. }
  830. }
  831. }
  832. },
  833. processResponseData() {
  834. this.processedResponses = this.responses.map((item) => {
  835. const result = {};
  836. if (item.delta && item.delta.role) {
  837. result.role = item.delta.role;
  838. }
  839. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  840. result.transcript = item.delta.audio.transcript;
  841. }
  842. return result;
  843. }).filter((item) => Object.keys(item).length > 0);
  844. },
  845. // 播放数字人视频
  846. playDigitalHumanVideo() {
  847. if (this.videoList.length === 0) {
  848. this.fetchQuestions();
  849. return;
  850. }
  851. this.videoUrl = this.videoList[this.currentVideoIndex];
  852. this.videoPlaying = true;
  853. this.$nextTick(() => {
  854. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  855. if (videoContext) {
  856. videoContext.play();
  857. setTimeout(() => {
  858. if (this.videoPlaying && this.$refs.videoPlayer) {
  859. console.log("视频应该正在播放");
  860. } else {
  861. console.log("视频可能未成功播放,尝试替代方案");
  862. this.tryAlternativeVideoPath();
  863. }
  864. }, 1e3);
  865. } else {
  866. console.error("无法创建视频上下文");
  867. this.tryAlternativeVideoPath();
  868. }
  869. });
  870. },
  871. // 修改 tryAlternativeVideoPath 方法
  872. tryAlternativeVideoPath() {
  873. console.log("尝试使用替代路径");
  874. const alternativePaths = [
  875. "./static/demo.mp4",
  876. "../static/demo.mp4",
  877. "static/demo.mp4",
  878. "/static/demo.mp4",
  879. // 添加绝对路径
  880. `${window.location.origin}/static/demo.mp4`
  881. ];
  882. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  883. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  884. this.videoUrl = alternativePaths[nextPathIndex];
  885. console.log("尝试新路径:", this.videoUrl);
  886. this.$nextTick(() => {
  887. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  888. if (videoContext) {
  889. videoContext.stop();
  890. videoContext.play();
  891. setTimeout(() => {
  892. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  893. console.log("所有路径均失败,尝试使用uni.getVideoInfo检查视频");
  894. this.checkVideoWithAPI();
  895. }
  896. }, 1e3);
  897. }
  898. });
  899. },
  900. // 添加新方法:使用uni API检查视频
  901. checkVideoWithAPI() {
  902. common_vendor.index.getVideoInfo({
  903. src: "/static/demo.mp4",
  904. success: (res) => {
  905. console.log("视频信息获取成功:", res);
  906. this.videoUrl = "/static/demo.mp4";
  907. this.$nextTick(() => {
  908. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  909. if (videoContext) {
  910. videoContext.play();
  911. }
  912. });
  913. },
  914. fail: (err) => {
  915. console.error("视频信息获取失败:", err);
  916. this.fallbackToLocalVideo();
  917. }
  918. });
  919. },
  920. // 添加新方法:回退到本地视频
  921. fallbackToLocalVideo() {
  922. console.log("尝试使用本地视频资源");
  923. const platform = common_vendor.index.getSystemInfoSync().platform;
  924. if (platform === "android" || platform === "ios") {
  925. this.videoUrl = platform === "android" ? "android.resource://package_name/raw/demo" : "file:///assets/demo.mp4";
  926. this.$nextTick(() => {
  927. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  928. if (videoContext) {
  929. videoContext.play();
  930. }
  931. });
  932. } else {
  933. this.videoPlaying = false;
  934. common_vendor.index.showToast({
  935. title: "视频加载失败,显示静态图片",
  936. icon: "none"
  937. });
  938. }
  939. },
  940. // 修改 handleVideoError 方法
  941. handleVideoError(e) {
  942. console.error("视频加载错误:", e);
  943. if (e && e.detail) {
  944. console.error("详细错误信息:", e.detail);
  945. }
  946. common_vendor.index.getFileInfo({
  947. filePath: this.videoUrl.startsWith("/") ? this.videoUrl.substring(1) : this.videoUrl,
  948. success: (res) => {
  949. console.log("文件存在,大小:", res.size);
  950. this.tryDifferentFormat();
  951. },
  952. fail: (err) => {
  953. console.error("文件不存在或无法访问:", err);
  954. this.tryAlternativeVideoPath();
  955. }
  956. });
  957. common_vendor.index.showToast({
  958. title: "视频加载失败,请检查文件是否存在",
  959. icon: "none",
  960. duration: 2e3
  961. });
  962. },
  963. // 添加新方法:尝试不同格式
  964. tryDifferentFormat() {
  965. console.log("尝试不同的视频格式");
  966. const formats = [
  967. { ext: "mp4", mime: "video/mp4" },
  968. { ext: "webm", mime: "video/webm" },
  969. { ext: "ogg", mime: "video/ogg" },
  970. { ext: "mov", mime: "video/quicktime" }
  971. ];
  972. const currentPath = this.videoUrl;
  973. const basePath = currentPath.substring(0, currentPath.lastIndexOf(".")) || "/static/demo";
  974. let nextFormat = formats.find((f) => !currentPath.endsWith(f.ext));
  975. if (nextFormat) {
  976. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  977. console.log("尝试新格式:", this.videoUrl);
  978. this.$nextTick(() => {
  979. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  980. if (videoContext) {
  981. videoContext.stop();
  982. videoContext.play();
  983. }
  984. });
  985. } else {
  986. this.useBuiltInResource();
  987. }
  988. },
  989. // 添加新方法:使用内置资源
  990. useBuiltInResource() {
  991. console.log("尝试使用内置资源");
  992. const platform = common_vendor.index.getSystemInfoSync().platform;
  993. if (platform === "windows") {
  994. process.env.UNI_INPUT_DIR || "";
  995. this.videoUrl = `./static/demo.mp4`;
  996. console.log("Windows平台尝试路径:", this.videoUrl);
  997. } else if (platform === "android" || platform === "ios") {
  998. this.useNativeVideo();
  999. } else {
  1000. const baseUrl = window.location.origin;
  1001. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  1002. console.log("Web平台尝试URL:", this.videoUrl);
  1003. }
  1004. this.$nextTick(() => {
  1005. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1006. if (videoContext) {
  1007. videoContext.play();
  1008. }
  1009. });
  1010. },
  1011. // 添加新方法:使用原生视频能力
  1012. useNativeVideo() {
  1013. console.log("尝试使用原生视频能力");
  1014. common_vendor.index.chooseVideo({
  1015. sourceType: ["album"],
  1016. success: (res) => {
  1017. this.videoUrl = res.tempFilePath;
  1018. this.$nextTick(() => {
  1019. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1020. if (videoContext) {
  1021. videoContext.play();
  1022. }
  1023. });
  1024. },
  1025. fail: () => {
  1026. this.videoPlaying = false;
  1027. }
  1028. });
  1029. },
  1030. // 处理视频结束事件
  1031. handleVideoEnded() {
  1032. console.log("视频播放结束事件触发");
  1033. const videoElement = this.$refs.videoPlayer;
  1034. if (videoElement) {
  1035. const currentTime = videoElement.currentTime || 0;
  1036. const duration = videoElement.duration || 0;
  1037. if (duration > 0 && duration - currentTime > 0.5) {
  1038. console.log("检测到可能的误触发结束事件,继续播放", currentTime, duration);
  1039. setTimeout(() => {
  1040. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  1041. if (videoContext) {
  1042. if (duration - currentTime > 2) {
  1043. videoContext.seek(this.historyTime || 0);
  1044. } else {
  1045. videoContext.play();
  1046. }
  1047. }
  1048. }, 100);
  1049. return;
  1050. }
  1051. console.log("视频确认结束,当前时间:", currentTime, "总时长:", duration);
  1052. }
  1053. this.videoPlaying = false;
  1054. if (this.isPlayingLowScoreVideo) {
  1055. console.log("低分提示视频播放完成");
  1056. if (this.retryCount < this.maxRetryAttempts) {
  1057. console.log(`显示重新录制按钮 (重试次数: ${this.retryCount + 1}/${this.maxRetryAttempts})`);
  1058. this.showRerecordButton = true;
  1059. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1060. this.currentSubtitle = this.currentFollowUpQuestion.question || this.currentFollowUpQuestion.question;
  1061. } else if (this.originalQuestionSubtitle) {
  1062. this.currentSubtitle = this.originalQuestionSubtitle;
  1063. }
  1064. } else {
  1065. console.log("已达到最大重试次数,继续下一题");
  1066. this.retryCount = 0;
  1067. this.proceedToNextQuestion();
  1068. }
  1069. this.isPlayingLowScoreVideo = false;
  1070. this.originalQuestionSubtitle = null;
  1071. return;
  1072. }
  1073. if (this.isFollowUpQuestion) {
  1074. console.log("追问问题视频播放完成");
  1075. this.showStartRecordingButton = true;
  1076. this.startCountdown();
  1077. return;
  1078. }
  1079. if (this.currentVideoIndex >= 1) {
  1080. this.showStartRecordingButton = true;
  1081. this.startCountdown();
  1082. } else {
  1083. this.showAnswerButton = true;
  1084. }
  1085. if (this.isFollowUpQuestion) {
  1086. this.isFollowUpQuestion = false;
  1087. this.currentFollowUpQuestion = null;
  1088. }
  1089. },
  1090. // 添加新方法:开始倒计时
  1091. startCountdown() {
  1092. this.clearCountdown();
  1093. this.showCountdown = true;
  1094. this.countdownValue = 10;
  1095. this.countdownTimer = setInterval(() => {
  1096. if (!this.showStartRecordingButton) {
  1097. this.clearCountdown();
  1098. return;
  1099. }
  1100. this.countdownValue--;
  1101. if (this.countdownValue <= 0) {
  1102. this.clearCountdown();
  1103. this.showStartRecordingButton = false;
  1104. this.startRecordingAnswer();
  1105. }
  1106. }, 1e3);
  1107. },
  1108. // 添加新方法:清除倒计时
  1109. clearCountdown() {
  1110. if (this.countdownTimer) {
  1111. clearInterval(this.countdownTimer);
  1112. this.countdownTimer = null;
  1113. }
  1114. this.showCountdown = false;
  1115. },
  1116. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  1117. handleStartRecordingClick() {
  1118. this.showStartRecordingButton = false;
  1119. this.clearCountdown();
  1120. this.isWaitingForAnswer = false;
  1121. this.startRecordingAnswer();
  1122. },
  1123. // 修改 stopRecordingAnswer 方法,添加录制时长检查
  1124. stopRecordingAnswer() {
  1125. this.clearCountdown();
  1126. this.isWaitingForAnswer = false;
  1127. const recordingDuration = this.getRecordingDuration();
  1128. const minimumDuration = 3;
  1129. const lowScoreDuration = 7;
  1130. this.currentRecordingDuration = recordingDuration;
  1131. if (recordingDuration < minimumDuration) {
  1132. common_vendor.index.showToast({
  1133. title: "录制时间过短,请至少录制3秒",
  1134. icon: "none",
  1135. duration: 2e3
  1136. });
  1137. return;
  1138. }
  1139. if (recordingDuration < lowScoreDuration) {
  1140. console.log(`录制时间 ${recordingDuration} 秒,少于 ${lowScoreDuration} 秒,将播放低分提示视频`);
  1141. this.completeRecordingStop(false);
  1142. this.playLowScoreVideo();
  1143. } else {
  1144. this.completeRecordingStop(true);
  1145. this.needPlayLowScoreVideo = false;
  1146. this.recordingTimeDisplay = "00:00";
  1147. }
  1148. },
  1149. // 添加新方法:开始录制用户回答
  1150. startRecordingAnswer() {
  1151. console.log("开始录制用户回答");
  1152. this.isRecording = true;
  1153. this.recordingStartTime = Date.now();
  1154. this.recordingTimerCount = 0;
  1155. this.maxRecordingTime = this.getCurrentQuestionRecommendedDuration();
  1156. this.remainingTime = this.maxRecordingTime;
  1157. this.recordingTimer = setInterval(() => {
  1158. this.recordingTimerCount++;
  1159. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  1160. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1161. this.progressPercent = Math.min(this.recordingTimerCount / this.maxRecordingTime * 100, 100);
  1162. if (this.recordingTimerCount >= this.maxRecordingTime) {
  1163. console.log(`已达到最大录制时间(${this.maxRecordingTime}秒),自动停止录制`);
  1164. this.stopRecordingAnswer();
  1165. }
  1166. }, 1e3);
  1167. const systemInfo = common_vendor.index.getSystemInfoSync();
  1168. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1169. if (isMiniProgram) {
  1170. this.startMiniProgramRecording();
  1171. } else {
  1172. this.startBrowserRecording();
  1173. }
  1174. this.showStopRecordingButton = true;
  1175. },
  1176. // 添加一个新方法:重置相机组件
  1177. resetCamera() {
  1178. console.log("重置相机组件");
  1179. this.useMiniProgramCameraComponent = false;
  1180. if (this.cameraContext) {
  1181. this.cameraContext = null;
  1182. }
  1183. setTimeout(() => {
  1184. this.useMiniProgramCameraComponent = true;
  1185. setTimeout(() => {
  1186. this.cameraContext = common_vendor.index.createCameraContext();
  1187. console.log("相机组件已重置");
  1188. }, 500);
  1189. }, 500);
  1190. },
  1191. // 修改 startMiniProgramRecording 方法
  1192. startMiniProgramRecording() {
  1193. console.log("开始小程序录制方法");
  1194. const systemInfo = common_vendor.index.getSystemInfoSync();
  1195. const isIOS = systemInfo.platform === "ios";
  1196. if (isIOS) {
  1197. this.resetCamera();
  1198. setTimeout(() => {
  1199. this.actualStartRecording(isIOS);
  1200. }, 1e3);
  1201. } else {
  1202. this.actualStartRecording(isIOS);
  1203. }
  1204. },
  1205. // 添加新方法:实际开始录制
  1206. actualStartRecording(isIOS) {
  1207. if (!this.cameraContext) {
  1208. this.cameraContext = common_vendor.index.createCameraContext();
  1209. console.log("创建新的相机上下文");
  1210. }
  1211. common_vendor.index.getSetting({
  1212. success: (res) => {
  1213. const hasRecordAuth = res.authSetting["scope.record"];
  1214. const hasCameraAuth = res.authSetting["scope.camera"];
  1215. if (!hasRecordAuth || !hasCameraAuth) {
  1216. console.warn("缺少必要权限,请求权限");
  1217. this.requestMiniProgramPermissions();
  1218. return;
  1219. }
  1220. if (isIOS) {
  1221. console.log("iOS: 检查相机状态");
  1222. const maxDuration = this.getCurrentQuestionRecommendedDuration() * 1e3;
  1223. const options = {
  1224. timeout: maxDuration,
  1225. // 使用当前问题的推荐时长
  1226. quality: "low",
  1227. // 降低质量
  1228. compressed: true,
  1229. success: () => {
  1230. console.log("iOS录制开始成功");
  1231. },
  1232. fail: (err) => {
  1233. console.error("iOS录制失败:", err);
  1234. this.useAlternativeRecordingMethod();
  1235. }
  1236. };
  1237. try {
  1238. console.log("尝试开始录制");
  1239. this.recorder = this.cameraContext.startRecord(options);
  1240. } catch (e) {
  1241. console.error("开始录制异常:", e);
  1242. this.useAlternativeRecordingMethod();
  1243. }
  1244. } else {
  1245. const maxDuration = this.getCurrentQuestionRecommendedDuration() * 1e3;
  1246. const options = {
  1247. timeout: maxDuration,
  1248. // 使用当前问题的推荐时长
  1249. quality: "medium",
  1250. compressed: true,
  1251. success: () => {
  1252. console.log("Android录制开始成功");
  1253. },
  1254. fail: (err) => {
  1255. console.error("Android录制失败:", err);
  1256. common_vendor.index.showToast({
  1257. title: "录制失败,请检查相机权限",
  1258. icon: "none"
  1259. });
  1260. this.proceedToNextQuestion();
  1261. }
  1262. };
  1263. this.recorder = this.cameraContext.startRecord(options);
  1264. }
  1265. }
  1266. });
  1267. },
  1268. // 添加新方法:使用替代录制方法
  1269. useAlternativeRecordingMethod() {
  1270. console.log("使用替代录制方法");
  1271. common_vendor.index.showActionSheet({
  1272. itemList: ["使用相册中的视频", "跳过此问题"],
  1273. success: (res) => {
  1274. if (res.tapIndex === 0) {
  1275. common_vendor.index.chooseVideo({
  1276. sourceType: ["album"],
  1277. maxDuration: this.getCurrentQuestionRecommendedDuration(),
  1278. // 使用当前问题的推荐时长
  1279. camera: "front",
  1280. success: (res2) => {
  1281. console.log("选择视频成功:", res2.tempFilePath);
  1282. this.isRecording = false;
  1283. this.showStopRecordingButton = false;
  1284. this.uploadRecordedVideo(res2.tempFilePath);
  1285. },
  1286. fail: () => {
  1287. console.log("用户取消选择视频");
  1288. this.proceedToNextQuestion();
  1289. }
  1290. });
  1291. } else {
  1292. console.log("用户选择跳过问题");
  1293. this.proceedToNextQuestion();
  1294. }
  1295. },
  1296. fail: () => {
  1297. console.log("操作取消");
  1298. this.proceedToNextQuestion();
  1299. }
  1300. });
  1301. },
  1302. // 添加新方法:请求小程序权限
  1303. requestMiniProgramPermissions() {
  1304. common_vendor.index.authorize({
  1305. scope: "scope.record",
  1306. success: () => {
  1307. console.log("录音权限已获取");
  1308. common_vendor.index.authorize({
  1309. scope: "scope.camera",
  1310. success: () => {
  1311. console.log("相机权限已获取");
  1312. this.startMiniProgramRecording();
  1313. },
  1314. fail: (err) => {
  1315. console.error("相机权限获取失败:", err);
  1316. this.showPermissionDialog("相机");
  1317. }
  1318. });
  1319. },
  1320. fail: (err) => {
  1321. console.error("录音权限获取失败:", err);
  1322. this.showPermissionDialog("录音");
  1323. }
  1324. });
  1325. },
  1326. // 修改浏览器环境下的录制方法
  1327. startBrowserRecording() {
  1328. if (!this.cameraStream) {
  1329. console.error("没有可用的摄像头流");
  1330. this.proceedToNextQuestion();
  1331. return;
  1332. }
  1333. try {
  1334. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  1335. if (!hasAudio) {
  1336. console.warn("警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流");
  1337. navigator.mediaDevices.getUserMedia({
  1338. audio: {
  1339. echoCancellation: true,
  1340. noiseSuppression: true,
  1341. autoGainControl: true
  1342. },
  1343. video: true
  1344. }).then((newStream) => {
  1345. const audioTracks = newStream.getAudioTracks();
  1346. if (audioTracks.length > 0) {
  1347. console.log("成功获取音频轨道:", audioTracks[0].label);
  1348. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1349. const audioTrack = newStream.getAudioTracks()[0];
  1350. const combinedStream = new MediaStream();
  1351. if (videoTrack)
  1352. combinedStream.addTrack(videoTrack);
  1353. if (audioTrack)
  1354. combinedStream.addTrack(audioTrack);
  1355. this.cameraStream = combinedStream;
  1356. const videoElement = this.$refs.userCameraVideo;
  1357. if (videoElement) {
  1358. videoElement.srcObject = combinedStream;
  1359. videoElement.muted = true;
  1360. }
  1361. this.setupMediaRecorder(combinedStream);
  1362. } else {
  1363. console.warn("仍然无法获取音频轨道");
  1364. this.setupMediaRecorder(this.cameraStream);
  1365. }
  1366. }).catch((err) => {
  1367. console.error("获取音频失败:", err);
  1368. this.setupMediaRecorder(this.cameraStream);
  1369. });
  1370. } else {
  1371. console.log("检测到音频轨道,直接使用");
  1372. this.setupMediaRecorder(this.cameraStream);
  1373. }
  1374. } catch (error) {
  1375. console.error("浏览器录制失败:", error);
  1376. common_vendor.index.showToast({
  1377. title: "录制失败,浏览器可能不支持此功能",
  1378. icon: "none"
  1379. });
  1380. this.proceedToNextQuestion();
  1381. }
  1382. },
  1383. // 修改 setupMediaRecorder 方法
  1384. setupMediaRecorder(stream) {
  1385. const videoTracks = stream.getVideoTracks();
  1386. const audioTracks = stream.getAudioTracks();
  1387. console.log("设置MediaRecorder - 视频轨道:", videoTracks.length, "音频轨道:", audioTracks.length);
  1388. let mimeType = "";
  1389. const supportedTypes = [
  1390. "video/webm;codecs=vp9,opus",
  1391. "video/webm;codecs=vp8,opus",
  1392. "video/webm;codecs=h264,opus",
  1393. "video/mp4;codecs=h264,aac",
  1394. "video/webm",
  1395. "video/mp4"
  1396. ];
  1397. for (const type of supportedTypes) {
  1398. if (MediaRecorder.isTypeSupported(type)) {
  1399. mimeType = type;
  1400. console.log("使用支持的MIME类型:", mimeType);
  1401. break;
  1402. }
  1403. }
  1404. const options = {
  1405. mimeType: mimeType || "",
  1406. audioBitsPerSecond: 64e3,
  1407. // 降低音频比特率
  1408. videoBitsPerSecond: 1e6
  1409. // 降低视频比特率到1Mbps
  1410. };
  1411. try {
  1412. this.mediaRecorder = new MediaRecorder(stream, options);
  1413. console.log("MediaRecorder创建成功,使用选项:", options);
  1414. } catch (e) {
  1415. console.warn("使用指定选项创建MediaRecorder失败,尝试使用默认选项");
  1416. this.mediaRecorder = new MediaRecorder(stream);
  1417. }
  1418. this.recordedChunks = [];
  1419. this.mediaRecorder.ondataavailable = (event) => {
  1420. if (event.data && event.data.size > 0) {
  1421. this.recordedChunks.push(event.data);
  1422. console.log(`收到数据块: ${event.data.size} 字节`);
  1423. }
  1424. };
  1425. this.mediaRecorder.onstop = async () => {
  1426. console.log("MediaRecorder停止,数据块数量:", this.recordedChunks.length);
  1427. if (this.recordedChunks.length === 0) {
  1428. console.error("没有录制到数据");
  1429. this.proceedToNextQuestion();
  1430. return;
  1431. }
  1432. const mimeType2 = this.mediaRecorder.mimeType || "video/webm";
  1433. const blob = new Blob(this.recordedChunks, { type: mimeType2 });
  1434. console.log("创建Blob,原始大小:", blob.size, "类型:", mimeType2);
  1435. common_vendor.index.showLoading({
  1436. title: "正在处理视频...",
  1437. mask: true
  1438. });
  1439. try {
  1440. const compressedBlob = await this.compressVideo(blob);
  1441. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1442. const file = new File([compressedBlob], fileName, { type: mimeType2 });
  1443. common_vendor.index.hideLoading();
  1444. this.uploadRecordedVideo(file);
  1445. } catch (error) {
  1446. console.error("视频处理失败:", error);
  1447. common_vendor.index.hideLoading();
  1448. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1449. const file = new File([blob], fileName, { type: mimeType2 });
  1450. this.uploadRecordedVideo(file);
  1451. }
  1452. };
  1453. this.mediaRecorder.onerror = (event) => {
  1454. console.error("MediaRecorder错误:", event.error);
  1455. };
  1456. try {
  1457. this.mediaRecorder.start(1e3);
  1458. console.log("MediaRecorder开始录制");
  1459. this.mediaRecorderTimeout = setTimeout(() => {
  1460. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  1461. console.log("MediaRecorder备份超时机制触发,停止录制");
  1462. this.mediaRecorder.stop();
  1463. }
  1464. }, 3e5);
  1465. } catch (e) {
  1466. console.error("开始录制失败:", e);
  1467. }
  1468. },
  1469. // 添加新方法:获取录制时长
  1470. getRecordingDuration() {
  1471. if (this.recordingStartTime) {
  1472. return (Date.now() - this.recordingStartTime) / 1e3;
  1473. }
  1474. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1475. return this.$refs.userCameraVideo.currentTime || 0;
  1476. }
  1477. if (this.recordingTimerCount) {
  1478. return this.recordingTimerCount;
  1479. }
  1480. return 0;
  1481. },
  1482. // 添加新方法:重置录制
  1483. resetRecording() {
  1484. if (this.recordingTimer) {
  1485. clearTimeout(this.recordingTimer);
  1486. }
  1487. this.recordingStartTime = Date.now();
  1488. this.recordingTimerCount = 0;
  1489. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1490. this.mediaRecorder.stop();
  1491. this.recordedChunks = [];
  1492. setTimeout(() => {
  1493. this.startBrowserRecording();
  1494. }, 500);
  1495. } else if (this.cameraContext) {
  1496. this.cameraContext.stopRecord({
  1497. success: () => {
  1498. console.log("相机录制已停止");
  1499. },
  1500. fail: (err) => {
  1501. console.error("停止相机录制失败:", err);
  1502. }
  1503. });
  1504. }
  1505. },
  1506. // 修改 completeRecordingStop 方法,确保正确处理追问问题ID
  1507. completeRecordingStop(uploadVideo = true) {
  1508. console.log("完成录制停止");
  1509. if (this.recordingTimer) {
  1510. clearInterval(this.recordingTimer);
  1511. this.recordingTimer = null;
  1512. }
  1513. this.finalRecordingDuration = this.recordingTimerCount;
  1514. this.showStopRecordingButton = false;
  1515. this.isRecording = false;
  1516. const systemInfo = common_vendor.index.getSystemInfoSync();
  1517. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1518. if (isMiniProgram) {
  1519. if (uploadVideo) {
  1520. this.stopMiniProgramRecording();
  1521. } else {
  1522. if (this.cameraContext) {
  1523. this.cameraContext.stopRecord({
  1524. success: () => {
  1525. console.log("相机录制已停止,不上传视频");
  1526. },
  1527. fail: (err) => {
  1528. console.error("停止相机录制失败:", err);
  1529. }
  1530. });
  1531. }
  1532. }
  1533. } else {
  1534. if (uploadVideo) {
  1535. this.stopBrowserRecording();
  1536. } else {
  1537. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1538. this.mediaRecorder.stop();
  1539. this.recordedChunks = [];
  1540. }
  1541. }
  1542. }
  1543. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  1544. console.log("当前是追问问题,记录追问问题ID:", this.currentFollowUpQuestion.id);
  1545. this.lastQuestionWasFollowUp = true;
  1546. this.lastFollowUpQuestionId = this.currentFollowUpQuestion.id;
  1547. } else {
  1548. this.lastQuestionWasFollowUp = false;
  1549. this.lastFollowUpQuestionId = null;
  1550. }
  1551. },
  1552. // 修改 stopMiniProgramRecording 方法,确保正确传递追问问题ID
  1553. stopMiniProgramRecording() {
  1554. console.log("停止小程序录制");
  1555. if (!this.cameraContext) {
  1556. console.error("相机上下文不存在");
  1557. this.proceedToNextQuestion();
  1558. return;
  1559. }
  1560. this.cameraContext.stopRecord({
  1561. success: async (res) => {
  1562. console.log("小程序录制停止成功:", res);
  1563. const videoPath = res.tempVideoPath;
  1564. if (videoPath) {
  1565. await this.uploadRecordedVideo(videoPath);
  1566. } else {
  1567. console.error("未获取到录制视频路径");
  1568. this.proceedToNextQuestion();
  1569. }
  1570. },
  1571. fail: (err) => {
  1572. console.error("小程序录制停止失败:", err);
  1573. this.proceedToNextQuestion();
  1574. }
  1575. });
  1576. },
  1577. // 添加新方法:停止浏览器录制
  1578. stopBrowserRecording() {
  1579. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1580. this.mediaRecorder.stop();
  1581. console.log("浏览器录制停止成功");
  1582. } else {
  1583. console.error("MediaRecorder不存在或已经停止");
  1584. this.proceedToNextQuestion();
  1585. }
  1586. },
  1587. // 修改 uploadRecordedVideo 方法,添加记录父问题ID的逻辑
  1588. uploadRecordedVideo(fileOrPath) {
  1589. console.log("准备上传视频:", typeof fileOrPath === "string" ? fileOrPath : fileOrPath.name);
  1590. console.log("当前问题ID:", this.currentParentQuestionId);
  1591. console.log("当前问题类型:", this.isFollowUpQuestion);
  1592. console.log("当前追问问题:", this.currentFollowUpQuestion);
  1593. let questionId;
  1594. let isFollowUpQuestionUpload = false;
  1595. let questionText = "";
  1596. let questionForm = 0;
  1597. if (this.currentFollowUpQuestion && (this.currentFollowUpQuestion.question_form === 5 || this.isFollowUpQuestion)) {
  1598. questionId = this.currentFollowUpQuestion.id;
  1599. isFollowUpQuestionUpload = true;
  1600. questionText = this.currentFollowUpQuestion.question || "";
  1601. questionForm = 5;
  1602. console.log("正在上传追问问题的回答:", {
  1603. questionId,
  1604. questionText,
  1605. questionForm,
  1606. isFollowUp: true
  1607. });
  1608. } else {
  1609. this.showThinkingLoading();
  1610. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  1611. if (currentQuestion && currentQuestion.id) {
  1612. questionId = currentQuestion.id;
  1613. questionText = currentQuestion.question || "";
  1614. console.log("正在上传常规问题的回答:", {
  1615. questionId,
  1616. questionText,
  1617. questionForm: 0
  1618. });
  1619. } else {
  1620. questionId = this.getDefaultQuestionId(this.currentVideoIndex);
  1621. console.log("使用默认问题ID:", questionId);
  1622. }
  1623. }
  1624. const uploadTask = {
  1625. id: Date.now().toString(),
  1626. file: fileOrPath,
  1627. questionId,
  1628. isFollowUp: isFollowUpQuestionUpload,
  1629. questionText,
  1630. questionForm,
  1631. attempts: 0,
  1632. maxAttempts: 3,
  1633. parentQuestionId: this.currentParentQuestionId,
  1634. videoDuration: this.currentRecordingDuration || 0
  1635. // 添加视频时长
  1636. };
  1637. if (!isFollowUpQuestionUpload) {
  1638. this.currentParentQuestionId = questionId;
  1639. }
  1640. this.uploadQueue.push(uploadTask);
  1641. this.uploadProgress[uploadTask.id] = 0;
  1642. this.uploadStatus[uploadTask.id] = "pending";
  1643. this.updateUploadStatusText();
  1644. if (isFollowUpQuestionUpload) {
  1645. console.log("追问问题使用后台上传,不阻塞流程");
  1646. this.handlePostUploadActions(uploadTask);
  1647. this.startBackgroundUpload(uploadTask);
  1648. return Promise.resolve();
  1649. } else {
  1650. return new Promise((resolve) => {
  1651. const checkUploadStatus = () => {
  1652. if (!this.isUploading && this.uploadQueue.length === 0) {
  1653. this.handlePostUploadActions(uploadTask);
  1654. resolve();
  1655. } else {
  1656. setTimeout(checkUploadStatus, 100);
  1657. }
  1658. };
  1659. if (!this.isUploading) {
  1660. this.processUploadQueue();
  1661. }
  1662. checkUploadStatus();
  1663. });
  1664. }
  1665. },
  1666. // 添加新方法:启动后台上传
  1667. startBackgroundUpload(task) {
  1668. console.log("启动后台上传任务:", task.id);
  1669. task.isBackgroundUpload = true;
  1670. const taskIndex = this.uploadQueue.findIndex((t) => t.id === task.id);
  1671. if (taskIndex !== -1) {
  1672. this.uploadQueue.splice(taskIndex, 1);
  1673. }
  1674. if (!this.backgroundUploadQueue) {
  1675. this.backgroundUploadQueue = [];
  1676. }
  1677. this.backgroundUploadQueue.push(task);
  1678. this.processBackgroundUploadQueue();
  1679. },
  1680. // 添加新方法:处理后台上传队列
  1681. processBackgroundUploadQueue() {
  1682. if (!this.backgroundUploadQueue || this.backgroundUploadQueue.length === 0) {
  1683. return;
  1684. }
  1685. const task = this.backgroundUploadQueue[0];
  1686. console.log("开始后台上传:", task.id);
  1687. this.uploadStatus[task.id] = "uploading";
  1688. this.updateUploadStatusText();
  1689. task.attempts++;
  1690. if (typeof task.file === "string") {
  1691. this.uploadFileWithUniBackground(task);
  1692. } else {
  1693. this.uploadFileWithXHRBackground(task);
  1694. }
  1695. },
  1696. // 添加新方法:使用XMLHttpRequest后台上传文件
  1697. uploadFileWithXHRBackground(task) {
  1698. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1699. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1700. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1701. const formData = new FormData();
  1702. formData.append("file", task.file);
  1703. formData.append("openid", openid);
  1704. formData.append("tenant_id", tenant_id);
  1705. formData.append("application_id", common_vendor.index.getStorageSync("appId"));
  1706. formData.append("question_id", task.questionId);
  1707. formData.append("video_duration", task.videoDuration || 0);
  1708. formData.append("has_audio", "true");
  1709. const xhr = new XMLHttpRequest();
  1710. xhr.upload.onprogress = (event) => {
  1711. if (event.lengthComputable) {
  1712. const progress = Math.round(event.loaded / event.total * 100);
  1713. this.uploadProgress[task.id] = progress;
  1714. this.updateUploadStatusText();
  1715. }
  1716. };
  1717. xhr.onload = () => {
  1718. if (xhr.status === 200) {
  1719. try {
  1720. const res = JSON.parse(xhr.responseText);
  1721. console.log("后台上传响应:", res);
  1722. if (res.code === 2e3) {
  1723. const videoUrl = res.data.url || res.data.photoUrl || "";
  1724. if (videoUrl) {
  1725. this.uploadStatus[task.id] = "success";
  1726. this.updateUploadStatusText();
  1727. this.submitVideoToInterviewBackground(videoUrl, task);
  1728. } else {
  1729. this.handleBackgroundUploadFailure(task, "视频URL获取失败");
  1730. }
  1731. } else {
  1732. this.handleBackgroundUploadFailure(task, res.msg || "上传失败");
  1733. }
  1734. } catch (e) {
  1735. this.handleBackgroundUploadFailure(task, "解析响应失败");
  1736. }
  1737. } else {
  1738. this.handleBackgroundUploadFailure(task, "HTTP状态: " + xhr.status);
  1739. }
  1740. };
  1741. xhr.onerror = () => {
  1742. this.handleBackgroundUploadFailure(task, "网络错误");
  1743. };
  1744. xhr.ontimeout = () => {
  1745. this.handleBackgroundUploadFailure(task, "上传超时");
  1746. };
  1747. xhr.open("POST", `${common_config.apiBaseUrl}/api/upload/`);
  1748. xhr.send(formData);
  1749. },
  1750. // 添加新方法:使用uni.uploadFile后台上传文件
  1751. uploadFileWithUniBackground(task) {
  1752. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1753. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1754. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1755. const uploadTask = common_vendor.index.uploadFile({
  1756. url: `${common_config.apiBaseUrl}/api/upload/`,
  1757. filePath: task.file,
  1758. name: "file",
  1759. formData: {
  1760. openid,
  1761. tenant_id,
  1762. application_id: common_vendor.index.getStorageSync("appId"),
  1763. question_id: task.questionId,
  1764. video_duration: task.videoDuration || 0,
  1765. has_audio: "true"
  1766. },
  1767. success: (uploadRes) => {
  1768. try {
  1769. const res = JSON.parse(uploadRes.data);
  1770. console.log("后台上传响应:", res);
  1771. if (res.code === 2e3) {
  1772. const videoUrl = res.data.permanent_link || res.data.url || "";
  1773. if (videoUrl) {
  1774. this.uploadStatus[task.id] = "success";
  1775. this.updateUploadStatusText();
  1776. this.submitVideoToInterviewBackground(videoUrl, task);
  1777. } else {
  1778. this.handleBackgroundUploadFailure(task, "视频URL获取失败");
  1779. }
  1780. } else {
  1781. this.handleBackgroundUploadFailure(task, res.msg || "上传失败");
  1782. }
  1783. } catch (e) {
  1784. this.handleBackgroundUploadFailure(task, "解析响应失败");
  1785. }
  1786. },
  1787. fail: (err) => {
  1788. this.handleBackgroundUploadFailure(task, err.errMsg || "上传失败");
  1789. }
  1790. });
  1791. uploadTask.onProgressUpdate((res) => {
  1792. this.uploadProgress[task.id] = res.progress;
  1793. this.updateUploadStatusText();
  1794. });
  1795. },
  1796. // 添加新方法:处理后台上传失败
  1797. handleBackgroundUploadFailure(task, errorMsg) {
  1798. console.error("后台上传失败:", errorMsg);
  1799. this.uploadStatus[task.id] = "failed";
  1800. this.updateUploadStatusText();
  1801. if (task.attempts < task.maxAttempts) {
  1802. console.log(`将在5秒后重试后台上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1803. setTimeout(() => {
  1804. this.uploadProgress[task.id] = 0;
  1805. if (typeof task.file !== "string") {
  1806. this.uploadFileWithXHRBackground(task);
  1807. } else {
  1808. this.uploadFileWithUniBackground(task);
  1809. }
  1810. }, 5e3);
  1811. } else {
  1812. console.log("超过最大重试次数,放弃后台上传");
  1813. if (this.backgroundUploadQueue) {
  1814. const taskIndex = this.backgroundUploadQueue.findIndex((t) => t.id === task.id);
  1815. if (taskIndex !== -1) {
  1816. this.backgroundUploadQueue.splice(taskIndex, 1);
  1817. }
  1818. }
  1819. this.processBackgroundUploadQueue();
  1820. }
  1821. },
  1822. // 添加新方法:后台上传提交到面试接口
  1823. submitVideoToInterviewBackground(videoUrl, task) {
  1824. const followUpRequestData = {
  1825. application_id: common_vendor.index.getStorageSync("appId"),
  1826. tenant_id: common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1",
  1827. video_url: videoUrl,
  1828. original_question_id: this.parentJobPositionQuestionId,
  1829. // 使用保存的job_position_question_id
  1830. follow_up_question: task.questionText,
  1831. video_duration: task.videoDuration,
  1832. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || ""
  1833. };
  1834. console.log("后台上传提交追问视频:", followUpRequestData);
  1835. common_vendor.index.request({
  1836. url: `${common_config.apiBaseUrl}/voice_interview/upload_follow_up_video/`,
  1837. method: "POST",
  1838. data: followUpRequestData,
  1839. header: {
  1840. "content-type": "application/x-www-form-urlencoded"
  1841. },
  1842. success: (res) => {
  1843. if (res.data.code === 200 || res.data.code === 2e3) {
  1844. console.log("后台上传追问视频提交成功");
  1845. if (this.backgroundUploadQueue) {
  1846. const taskIndex = this.backgroundUploadQueue.findIndex((t) => t.id === task.id);
  1847. if (taskIndex !== -1) {
  1848. this.backgroundUploadQueue.splice(taskIndex, 1);
  1849. }
  1850. }
  1851. this.processBackgroundUploadQueue();
  1852. } else {
  1853. this.handleBackgroundSubmitFailure(task, "提交失败: " + (res.data.msg || "未知错误"));
  1854. }
  1855. },
  1856. fail: (err) => {
  1857. console.error("后台上传提交失败:", err);
  1858. this.handleBackgroundSubmitFailure(task, "提交失败: " + err.errMsg);
  1859. }
  1860. });
  1861. },
  1862. // 添加新方法:处理后台上传提交失败
  1863. handleBackgroundSubmitFailure(task, errorMsg) {
  1864. console.error("后台上传提交失败:", errorMsg);
  1865. if (task.attempts < task.maxAttempts) {
  1866. console.log(`将在5秒后重试后台上传提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1867. setTimeout(() => {
  1868. this.submitVideoToInterviewBackground(task.videoUrl, task);
  1869. }, 5e3);
  1870. } else {
  1871. console.log("超过最大重试次数,放弃后台上传提交");
  1872. if (this.backgroundUploadQueue) {
  1873. const taskIndex = this.backgroundUploadQueue.findIndex((t) => t.id === task.id);
  1874. if (taskIndex !== -1) {
  1875. this.backgroundUploadQueue.splice(taskIndex, 1);
  1876. }
  1877. }
  1878. this.processBackgroundUploadQueue();
  1879. }
  1880. },
  1881. // 添加新方法:处理上传后的逻辑
  1882. handlePostUploadActions(task) {
  1883. this.hideThinkingLoading();
  1884. if (this.needPlayLowScoreVideo && this.retryCount < 1) {
  1885. this.playLowScoreVideo();
  1886. this.needPlayLowScoreVideo = false;
  1887. } else {
  1888. if (task.isFollowUp) {
  1889. console.log("追问问题回答完成,检查是否继续追问");
  1890. this.retryCount = 0;
  1891. this.isFollowUpQuestion = false;
  1892. this.currentFollowUpQuestion = null;
  1893. this.videoUrl = "";
  1894. this.maybeRequestNextFollowUp(this.parentJobPositionQuestionId);
  1895. } else {
  1896. console.log("常规问题回答完成,准备检查追问,父问题ID:", this.parentJobPositionQuestionId);
  1897. if (this.parentJobPositionQuestionId) {
  1898. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  1899. if (currentQuestion && typeof currentQuestion.follow_up_count !== "undefined") {
  1900. this.setFollowUpLimit(this.parentJobPositionQuestionId, currentQuestion.follow_up_count);
  1901. } else if (typeof this.defaultFollowUpLimit !== "undefined") {
  1902. this.setFollowUpLimit(this.parentJobPositionQuestionId, this.defaultFollowUpLimit);
  1903. }
  1904. this.resetFollowUpAskedCount(this.parentJobPositionQuestionId);
  1905. this.checkAndPlayFollowUpQuestion(this.parentJobPositionQuestionId);
  1906. } else {
  1907. console.error("缺少parentJobPositionQuestionId,无法检查追问,直接进入下一题");
  1908. this.proceedToNextQuestion();
  1909. }
  1910. }
  1911. }
  1912. },
  1913. // 修改 processUploadQueue 方法
  1914. processUploadQueue() {
  1915. if (this.uploadQueue.length === 0) {
  1916. this.isUploading = false;
  1917. this.showUploadStatus = false;
  1918. this.hideThinkingLoading();
  1919. return;
  1920. }
  1921. this.isUploading = true;
  1922. this.showUploadStatus = true;
  1923. const task = this.uploadQueue[0];
  1924. this.uploadStatus[task.id] = "uploading";
  1925. this.updateUploadStatusText();
  1926. task.attempts++;
  1927. if (typeof task.file === "string") {
  1928. this.uploadFileWithUni(task);
  1929. } else {
  1930. this.uploadFileWithXHR(task);
  1931. }
  1932. },
  1933. // 添加新方法:使用XMLHttpRequest上传文件
  1934. uploadFileWithXHR(task) {
  1935. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1936. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1937. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1938. const formData = new FormData();
  1939. formData.append("file", task.file);
  1940. formData.append("openid", openid);
  1941. formData.append("tenant_id", tenant_id);
  1942. formData.append("application_id", common_vendor.index.getStorageSync("appId"));
  1943. formData.append("question_id", task.questionId);
  1944. formData.append("video_duration", task.videoDuration);
  1945. formData.append("has_audio", "true");
  1946. const xhr = new XMLHttpRequest();
  1947. xhr.open("POST", `${common_config.apiBaseUrl}/api/upload/`, true);
  1948. xhr.timeout = 12e4;
  1949. xhr.upload.onprogress = (event) => {
  1950. if (event.lengthComputable) {
  1951. const percentComplete = Math.round(event.loaded / event.total * 100);
  1952. this.uploadProgress[task.id] = percentComplete;
  1953. this.updateUploadStatusText();
  1954. }
  1955. };
  1956. xhr.onload = () => {
  1957. if (xhr.status === 200) {
  1958. try {
  1959. const res = JSON.parse(xhr.responseText);
  1960. console.log("上传响应:", res);
  1961. if (res.code === 2e3) {
  1962. const videoUrl = res.data.url || res.data.photoUrl || "";
  1963. if (videoUrl) {
  1964. this.uploadStatus[task.id] = "success";
  1965. this.updateUploadStatusText();
  1966. this.submitVideoToInterview(videoUrl, task);
  1967. } else {
  1968. this.handleUploadFailure(task, "视频URL获取失败");
  1969. }
  1970. } else {
  1971. this.handleUploadFailure(task, res.msg || "上传失败");
  1972. }
  1973. } catch (e) {
  1974. this.handleUploadFailure(task, "解析响应失败");
  1975. }
  1976. } else {
  1977. this.handleUploadFailure(task, "HTTP状态: " + xhr.status);
  1978. }
  1979. };
  1980. xhr.onerror = () => {
  1981. this.handleUploadFailure(task, "网络错误");
  1982. };
  1983. xhr.ontimeout = () => {
  1984. this.handleUploadFailure(task, "上传超时");
  1985. };
  1986. xhr.send(formData);
  1987. },
  1988. // 添加新方法:使用uni.uploadFile上传文件
  1989. uploadFileWithUni(task) {
  1990. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1991. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1992. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  1993. const uploadTask = common_vendor.index.uploadFile({
  1994. url: `${common_config.apiBaseUrl}/api/upload/`,
  1995. filePath: task.file,
  1996. name: "file",
  1997. formData: {
  1998. openid,
  1999. tenant_id,
  2000. application_id: common_vendor.index.getStorageSync("appId"),
  2001. question_id: task.questionId,
  2002. video_duration: 0,
  2003. has_audio: "true"
  2004. },
  2005. success: (uploadRes) => {
  2006. try {
  2007. const res = JSON.parse(uploadRes.data);
  2008. console.log("上传响应:", res);
  2009. if (res.code === 2e3) {
  2010. const videoUrl = res.data.permanent_link || res.data.url || "";
  2011. if (videoUrl) {
  2012. this.uploadStatus[task.id] = "success";
  2013. this.updateUploadStatusText();
  2014. this.submitVideoToInterview(videoUrl, task);
  2015. } else {
  2016. this.handleUploadFailure(task, "视频URL获取失败");
  2017. }
  2018. } else {
  2019. this.handleUploadFailure(task, res.msg || "上传失败");
  2020. }
  2021. } catch (e) {
  2022. this.handleUploadFailure(task, "解析响应失败");
  2023. }
  2024. },
  2025. fail: (err) => {
  2026. this.handleUploadFailure(task, err.errMsg || "上传失败");
  2027. }
  2028. });
  2029. uploadTask.onProgressUpdate((res) => {
  2030. this.uploadProgress[task.id] = res.progress;
  2031. this.updateUploadStatusText();
  2032. });
  2033. },
  2034. // 添加新方法:处理上传失败
  2035. handleUploadFailure(task, errorMsg) {
  2036. console.error("上传失败:", errorMsg);
  2037. this.uploadStatus[task.id] = "failed";
  2038. this.updateUploadStatusText();
  2039. if (task.attempts < task.maxAttempts) {
  2040. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  2041. setTimeout(() => {
  2042. this.uploadProgress[task.id] = 0;
  2043. if (typeof task.file !== "string") {
  2044. this.uploadFileWithXHR(task);
  2045. } else {
  2046. this.uploadFileWithUni(task);
  2047. }
  2048. }, 5e3);
  2049. } else {
  2050. console.log("超过最大重试次数,放弃上传");
  2051. this.uploadQueue.shift();
  2052. this.processUploadQueue();
  2053. }
  2054. },
  2055. // 修改 submitVideoToInterview 方法
  2056. submitVideoToInterview(videoUrl, task) {
  2057. const requestData = {
  2058. application_id: common_vendor.index.getStorageSync("appId"),
  2059. question_id: task.questionId,
  2060. video_url: videoUrl,
  2061. tenant_id: common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1",
  2062. is_follow_up: task.isFollowUp ? 1 : 0,
  2063. question_form: task.questionForm,
  2064. question_text: task.questionText
  2065. };
  2066. console.log("提交视频到面试接口", requestData);
  2067. if (task.isFollowUp) {
  2068. const followUpRequestData = {
  2069. application_id: common_vendor.index.getStorageSync("appId"),
  2070. tenant_id: common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1",
  2071. video_url: videoUrl,
  2072. original_question_id: this.parentJobPositionQuestionId,
  2073. // 使用保存的job_position_question_id
  2074. follow_up_question: task.questionText,
  2075. video_duration: task.videoDuration,
  2076. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || ""
  2077. };
  2078. console.log("准备提交追问视频:", followUpRequestData);
  2079. common_vendor.index.request({
  2080. url: `${common_config.apiBaseUrl}/voice_interview/upload_follow_up_video/`,
  2081. method: "POST",
  2082. data: followUpRequestData,
  2083. header: {
  2084. "content-type": "application/x-www-form-urlencoded"
  2085. },
  2086. success: async (res) => {
  2087. try {
  2088. if (res.data.code === 200 || res.data.code === 2e3) {
  2089. if (res.data.data && res.data.data.transcription_status === "pending") {
  2090. console.log("视频转写进行中,5秒后重试");
  2091. setTimeout(() => {
  2092. this.checkTranscriptionStatus(task, followUpRequestData);
  2093. }, 5e3);
  2094. return;
  2095. }
  2096. console.log("追问视频提交成功");
  2097. this.isFollowUpMode = false;
  2098. this.currentVideoIndex = this.mainQuestionIndex;
  2099. this.uploadQueue.shift();
  2100. this.processUploadQueue();
  2101. this.$nextTick(() => {
  2102. if (this.currentVideoIndex < this.videoList.length) {
  2103. this.videoUrl = this.videoList[this.currentVideoIndex];
  2104. this.videoPlaying = true;
  2105. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2106. if (videoContext) {
  2107. videoContext.play();
  2108. }
  2109. }
  2110. });
  2111. } else {
  2112. this.handleSubmitFailure(task, "提交失败: " + (res.data.msg || "未知错误"));
  2113. }
  2114. } catch (e) {
  2115. this.handleSubmitFailure(task, "解析响应失败: " + e.message);
  2116. }
  2117. },
  2118. fail: (err) => {
  2119. console.error("提交视频失败:", err);
  2120. this.handleSubmitFailure(task, "提交失败: " + err.errMsg);
  2121. }
  2122. });
  2123. } else {
  2124. common_vendor.index.request({
  2125. url: `${common_config.apiBaseUrl}/api/job/upload_video`,
  2126. method: "POST",
  2127. data: requestData,
  2128. header: {
  2129. "content-type": "application/x-www-form-urlencoded"
  2130. },
  2131. success: async (res) => {
  2132. if (res.data.code === 200 || res.data.code === 2e3) {
  2133. console.log("视频提交成功,准备调用面试互动接口");
  2134. try {
  2135. this.parentJobPositionQuestionId = res.data.data.job_position_question_id;
  2136. console.log("保存父问题ID:", this.parentJobPositionQuestionId);
  2137. const limitFromServer = res.data.data.follow_up_limit;
  2138. if (typeof limitFromServer !== "undefined") {
  2139. this.setFollowUpLimit(this.parentJobPositionQuestionId, limitFromServer);
  2140. } else {
  2141. this.setFollowUpLimit(this.parentJobPositionQuestionId, this.defaultFollowUpLimit);
  2142. }
  2143. this.resetFollowUpAskedCount(this.parentJobPositionQuestionId);
  2144. await this.callInterviewInteraction(res.data.data.job_position_question_id);
  2145. console.log("面试互动接口调用成功");
  2146. } catch (error) {
  2147. console.error("面试互动接口调用失败:", error);
  2148. this.proceedToNextQuestion();
  2149. }
  2150. this.uploadQueue.shift();
  2151. this.processUploadQueue();
  2152. } else {
  2153. this.handleSubmitFailure(task, "提交失败: " + (res.data.msg || "未知错误"));
  2154. }
  2155. },
  2156. fail: (err) => {
  2157. console.error("提交视频失败:", err);
  2158. this.handleSubmitFailure(task, "提交失败: " + err.errMsg);
  2159. }
  2160. });
  2161. }
  2162. },
  2163. // 添加新方法:处理提交失败
  2164. handleSubmitFailure(task, errorMsg) {
  2165. console.error("提交失败:", errorMsg);
  2166. this.uploadStatus[task.id] = "failed";
  2167. this.updateUploadStatusText();
  2168. if (task.attempts < task.maxAttempts) {
  2169. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  2170. setTimeout(() => {
  2171. this.submitVideoToInterview(task.videoUrl, task);
  2172. }, 5e3);
  2173. } else {
  2174. console.log("超过最大重试次数,放弃提交");
  2175. this.uploadQueue.shift();
  2176. this.processUploadQueue();
  2177. }
  2178. },
  2179. // 添加检查转写状态的方法
  2180. checkTranscriptionStatus(task, requestData, retryCount = 0) {
  2181. const maxRetries = 24;
  2182. const retryInterval = 5e3;
  2183. if (retryCount >= maxRetries) {
  2184. console.log("转写超时,继续下一个问题");
  2185. this.isFollowUpMode = false;
  2186. this.currentVideoIndex = this.mainQuestionIndex;
  2187. this.uploadQueue.shift();
  2188. this.processUploadQueue();
  2189. return;
  2190. }
  2191. common_vendor.index.request({
  2192. url: `${common_config.apiBaseUrl}/voice_interview/check_transcription_status/`,
  2193. method: "POST",
  2194. data: {
  2195. application_id: requestData.application_id,
  2196. original_question_id: requestData.original_question_id,
  2197. video_url: requestData.video_url
  2198. },
  2199. header: {
  2200. "content-type": "application/x-www-form-urlencoded"
  2201. },
  2202. success: (res) => {
  2203. if (res.data.code === 200 || res.data.code === 2e3) {
  2204. if (res.data.data && res.data.data.status === "completed") {
  2205. console.log("视频转写完成");
  2206. this.isFollowUpMode = false;
  2207. this.currentVideoIndex = this.mainQuestionIndex;
  2208. this.uploadQueue.shift();
  2209. this.processUploadQueue();
  2210. this.$nextTick(() => {
  2211. if (this.currentVideoIndex < this.videoList.length) {
  2212. this.videoUrl = this.videoList[this.currentVideoIndex];
  2213. this.videoPlaying = true;
  2214. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2215. if (videoContext) {
  2216. videoContext.play();
  2217. }
  2218. }
  2219. });
  2220. } else {
  2221. console.log(`转写进行中,${retryInterval / 1e3}秒后重试 (${retryCount + 1}/${maxRetries})`);
  2222. setTimeout(() => {
  2223. this.checkTranscriptionStatus(task, requestData, retryCount + 1);
  2224. }, retryInterval);
  2225. }
  2226. } else if (res.data.message === "面试视频尚未完成转写" || res.data.success === false) {
  2227. console.log(`转写未完成,${retryInterval / 1e3}秒后重试 (${retryCount + 1}/${maxRetries})`);
  2228. setTimeout(() => {
  2229. this.checkTranscriptionStatus(task, requestData, retryCount + 1);
  2230. }, retryInterval);
  2231. } else {
  2232. this.handleSubmitFailure(task, "检查转写状态失败: " + (res.data.msg || "未知错误"));
  2233. }
  2234. },
  2235. fail: (err) => {
  2236. console.error("检查转写状态失败:", err);
  2237. console.log(`网络请求失败,${retryInterval / 1e3}秒后重试 (${retryCount + 1}/${maxRetries})`);
  2238. setTimeout(() => {
  2239. this.checkTranscriptionStatus(task, requestData, retryCount + 1);
  2240. }, retryInterval);
  2241. }
  2242. });
  2243. },
  2244. // 添加新方法:更新上传状态文本
  2245. updateUploadStatusText() {
  2246. let mainQueueText = "";
  2247. if (this.uploadQueue.length > 0) {
  2248. const currentTask = this.uploadQueue[0];
  2249. const progress = this.uploadProgress[currentTask.id] || 0;
  2250. const status = this.uploadStatus[currentTask.id] || "pending";
  2251. let statusText = "";
  2252. switch (status) {
  2253. case "pending":
  2254. statusText = "等待上传";
  2255. break;
  2256. case "uploading":
  2257. statusText = `上传中 ${progress}%`;
  2258. break;
  2259. case "success":
  2260. statusText = "上传成功,提交中...";
  2261. break;
  2262. case "failed":
  2263. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? "即将重试" : "已放弃"}`;
  2264. break;
  2265. }
  2266. const questionTypeText = currentTask.isFollowUp ? "追问" : "问题";
  2267. const questionShortText = currentTask.questionText ? currentTask.questionText.length > 10 ? currentTask.questionText.substring(0, 10) + "..." : currentTask.questionText : `${questionTypeText}${currentTask.questionId}`;
  2268. mainQueueText = `${questionTypeText}「${questionShortText}」:${statusText}`;
  2269. if (this.uploadQueue.length > 1) {
  2270. mainQueueText += ` (${this.uploadQueue.length}个视频待处理)`;
  2271. }
  2272. }
  2273. let backgroundQueueText = "";
  2274. if (this.backgroundUploadQueue && this.backgroundUploadQueue.length > 0) {
  2275. const currentTask = this.backgroundUploadQueue[0];
  2276. const progress = this.uploadProgress[currentTask.id] || 0;
  2277. const status = this.uploadStatus[currentTask.id] || "pending";
  2278. let statusText = "";
  2279. switch (status) {
  2280. case "pending":
  2281. statusText = "等待后台上传";
  2282. break;
  2283. case "uploading":
  2284. statusText = `后台上传中 ${progress}%`;
  2285. break;
  2286. case "success":
  2287. statusText = "后台上传成功,提交中...";
  2288. break;
  2289. case "failed":
  2290. statusText = `后台上传失败,${currentTask.attempts < currentTask.maxAttempts ? "即将重试" : "已放弃"}`;
  2291. break;
  2292. }
  2293. const questionShortText = currentTask.questionText ? currentTask.questionText.length > 10 ? currentTask.questionText.substring(0, 10) + "..." : currentTask.questionText : `追问${currentTask.questionId}`;
  2294. backgroundQueueText = `追问「${questionShortText}」:${statusText}`;
  2295. if (this.backgroundUploadQueue.length > 1) {
  2296. backgroundQueueText += ` (${this.backgroundUploadQueue.length}个追问待后台上传)`;
  2297. }
  2298. }
  2299. if (mainQueueText && backgroundQueueText) {
  2300. this.uploadStatusText = `${mainQueueText} | ${backgroundQueueText}`;
  2301. } else if (mainQueueText) {
  2302. this.uploadStatusText = mainQueueText;
  2303. } else if (backgroundQueueText) {
  2304. this.uploadStatusText = backgroundQueueText;
  2305. } else {
  2306. this.uploadStatusText = "";
  2307. }
  2308. },
  2309. // 修改 proceedToNextQuestion 方法,确保在切换视频时重置历史时间
  2310. proceedToNextQuestion() {
  2311. if (this.isVideoSwitching) {
  2312. console.log("正在切换视频中,请等待...");
  2313. return;
  2314. }
  2315. console.log("继续下一个问题");
  2316. this.isVideoSwitching = true;
  2317. const currentVideo = common_vendor.index.createVideoContext("myVideo", this);
  2318. if (currentVideo) {
  2319. currentVideo.stop();
  2320. this.videoPlaying = false;
  2321. }
  2322. if (this.currentVideoIndex + 1 >= this.videoList.length) {
  2323. this.stopUserCamera();
  2324. this.clearCountdown();
  2325. const currentJobDetail = JSON.parse(common_vendor.index.getStorageSync("selectedJob"));
  2326. const jobId = currentJobDetail ? currentJobDetail.id : null;
  2327. this.handleVideoCompletion(jobId);
  2328. return;
  2329. }
  2330. this.currentVideoIndex++;
  2331. this.historyTime = 0;
  2332. if (this.currentVideoIndex < this.videoList.length) {
  2333. this.videoUrl = this.videoList[this.currentVideoIndex];
  2334. this.$nextTick(() => {
  2335. this.videoPlaying = true;
  2336. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2337. if (videoContext) {
  2338. videoContext.play();
  2339. }
  2340. this.isVideoSwitching = false;
  2341. });
  2342. } else {
  2343. console.log("所有视频已播放完毕");
  2344. this.stopUserCamera();
  2345. this.clearCountdown();
  2346. const currentJobDetail = JSON.parse(common_vendor.index.getStorageSync("selectedJob"));
  2347. console.log("当前职位ID:", currentJobDetail);
  2348. const jobId = currentJobDetail ? currentJobDetail.id : null;
  2349. console.log("当前职位ID:", jobId);
  2350. setTimeout(() => {
  2351. this.isVideoSwitching = false;
  2352. if (jobId === 9) {
  2353. common_vendor.index.navigateTo({
  2354. url: "/pages/interview-question/interview-question",
  2355. success: () => {
  2356. console.log("成功跳转到interview-question页面");
  2357. },
  2358. fail: (err) => {
  2359. console.error("跳转到interview-question页面失败:", err);
  2360. common_vendor.index.redirectTo({
  2361. url: "/pages/interview-question/interview-question",
  2362. fail: (redirectErr) => {
  2363. console.error("重定向到interview-question页面也失败:", redirectErr);
  2364. common_vendor.index.navigateBack({
  2365. delta: 1
  2366. });
  2367. }
  2368. });
  2369. }
  2370. });
  2371. } else {
  2372. common_vendor.index.navigateTo({
  2373. url: "/pages/camera/camera",
  2374. success: () => {
  2375. console.log("成功跳转到camera页面");
  2376. },
  2377. fail: (err) => {
  2378. console.error("跳转到camera页面失败:", err);
  2379. common_vendor.index.redirectTo({
  2380. url: "/pages/camera/camera",
  2381. fail: (redirectErr) => {
  2382. console.error("重定向到camera页面也失败:", redirectErr);
  2383. common_vendor.index.switchTab({
  2384. url: "/pages/camera/camera",
  2385. fail: (switchErr) => {
  2386. console.error("所有跳转方式都失败:", switchErr);
  2387. common_vendor.index.navigateBack({
  2388. delta: 1
  2389. });
  2390. }
  2391. });
  2392. }
  2393. });
  2394. }
  2395. });
  2396. }
  2397. }, 0);
  2398. }
  2399. },
  2400. // 修改 handleAnswerButtonClick 方法,确保在切换视频时重置历史时间
  2401. handleAnswerButtonClick() {
  2402. console.log("点击开始回答按钮");
  2403. this.showAnswerButton = false;
  2404. this.currentVideoIndex++;
  2405. this.historyTime = 0;
  2406. if (this.currentVideoIndex < this.videoList.length) {
  2407. this.videoUrl = this.videoList[this.currentVideoIndex];
  2408. this.videoPlaying = true;
  2409. this.$nextTick(() => {
  2410. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2411. if (videoContext) {
  2412. videoContext.play();
  2413. }
  2414. });
  2415. }
  2416. },
  2417. // 处理相机错误
  2418. handleCameraError(e) {
  2419. console.error("相机错误:", e);
  2420. const systemInfo = common_vendor.index.getSystemInfoSync();
  2421. const isIOS = systemInfo.platform === "ios";
  2422. if (isIOS) {
  2423. console.log("iOS相机错误,尝试重新初始化");
  2424. common_vendor.index.showToast({
  2425. title: "相机初始化中...",
  2426. icon: "loading",
  2427. duration: 2e3
  2428. });
  2429. this.resetCamera();
  2430. if (this.isRecording) {
  2431. this.isRecording = false;
  2432. this.showStopRecordingButton = false;
  2433. setTimeout(() => {
  2434. this.useAlternativeRecordingMethod();
  2435. }, 1e3);
  2436. }
  2437. } else {
  2438. common_vendor.index.showToast({
  2439. title: "相机初始化失败,请检查权限设置",
  2440. icon: "none"
  2441. });
  2442. this.tryFallbackOptions();
  2443. }
  2444. },
  2445. // 添加新方法:尝试备用选项
  2446. tryFallbackOptions() {
  2447. const systemInfo = common_vendor.index.getSystemInfoSync();
  2448. if (systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay") {
  2449. this.useMiniProgramCamera();
  2450. } else {
  2451. this.showStaticCameraPlaceholder();
  2452. }
  2453. },
  2454. // 添加新方法:使用小程序相机API
  2455. useMiniProgramCamera() {
  2456. console.log("尝试使用小程序相机组件");
  2457. this.useMiniProgramCameraComponent = true;
  2458. },
  2459. // 添加新方法:显示静态图像
  2460. showStaticCameraPlaceholder() {
  2461. console.log("显示静态摄像头占位图");
  2462. const img = document.createElement("img");
  2463. img.src = "/static/images/camera-placeholder.png";
  2464. img.className = "static-camera-image";
  2465. img.style.width = "100%";
  2466. img.style.height = "100%";
  2467. img.style.objectFit = "cover";
  2468. const container = this.$refs.userCameraVideo.parentNode;
  2469. container.appendChild(img);
  2470. },
  2471. // 修改 handleTimeUpdate 方法
  2472. handleTimeUpdate(e) {
  2473. const currentTime = e.detail.currentTime || e.target.currentTime;
  2474. if (this.isRecording && this.recordingTimerCount) {
  2475. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  2476. }
  2477. if (this.isPlayingLowScoreVideo) {
  2478. if (this.lowScoreVideoSubtitles && this.lowScoreVideoSubtitles.length > 0) {
  2479. const subtitle = this.lowScoreVideoSubtitles[0];
  2480. this.currentSubtitle = subtitle.text;
  2481. }
  2482. return;
  2483. }
  2484. let currentSubtitles = null;
  2485. if (this.isFollowUpQuestion && this.currentFollowUpQuestion) {
  2486. const subtitleKey = `followUpSubtitles_${this.currentFollowUpQuestion.id}`;
  2487. currentSubtitles = this[subtitleKey] || [{
  2488. startTime: 0,
  2489. endTime: 30,
  2490. text: this.currentFollowUpQuestion.question || this.currentFollowUpQuestion.question
  2491. }];
  2492. } else {
  2493. if (this.currentVideoIndex === 0) {
  2494. currentSubtitles = this.subtitles;
  2495. } else {
  2496. const subtitleArrayName = `question${this.currentVideoIndex}Subtitles`;
  2497. currentSubtitles = this[subtitleArrayName];
  2498. }
  2499. }
  2500. if (currentSubtitles && currentSubtitles.length > 0) {
  2501. const subtitle = currentSubtitles.find(
  2502. (sub) => currentTime >= sub.startTime && currentTime <= sub.endTime
  2503. ) || currentSubtitles[0];
  2504. if (subtitle) {
  2505. this.currentSubtitle = subtitle.text;
  2506. }
  2507. }
  2508. this.historyTime = currentTime;
  2509. this.lastUpdateTime = Date.now();
  2510. },
  2511. // Add a new method to handle the "Start Recording" button click
  2512. handleStartRecordingClick() {
  2513. this.showStartRecordingButton = false;
  2514. this.clearCountdown();
  2515. this.isWaitingForAnswer = false;
  2516. this.startRecordingAnswer();
  2517. },
  2518. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  2519. checkAudioPermission() {
  2520. const systemInfo = common_vendor.index.getSystemInfoSync();
  2521. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2522. if (isMiniProgram) {
  2523. common_vendor.index.getSetting({
  2524. success: (res) => {
  2525. if (!res.authSetting["scope.record"]) {
  2526. common_vendor.index.authorize({
  2527. scope: "scope.record",
  2528. success: () => {
  2529. console.log("录音权限已获取");
  2530. },
  2531. fail: (err) => {
  2532. console.error("录音权限获取失败:", err);
  2533. this.showPermissionDialog("录音");
  2534. }
  2535. });
  2536. }
  2537. if (!res.authSetting["scope.camera"]) {
  2538. common_vendor.index.authorize({
  2539. scope: "scope.camera",
  2540. success: () => {
  2541. console.log("相机权限已获取");
  2542. },
  2543. fail: (err) => {
  2544. console.error("相机权限获取失败:", err);
  2545. this.showPermissionDialog("相机");
  2546. }
  2547. });
  2548. }
  2549. }
  2550. });
  2551. }
  2552. },
  2553. // 添加新方法:测试音频输入
  2554. testAudioInput() {
  2555. if (!this.cameraStream) {
  2556. console.warn("没有可用的媒体流,无法测试音频");
  2557. return;
  2558. }
  2559. const audioTracks = this.cameraStream.getAudioTracks();
  2560. if (audioTracks.length === 0) {
  2561. console.warn("没有检测到音频轨道,尝试重新获取");
  2562. this.tryGetAudioOnly();
  2563. return;
  2564. }
  2565. console.log("音频轨道信息:", audioTracks[0].getSettings());
  2566. try {
  2567. const AudioContext = window.AudioContext || window.webkitAudioContext;
  2568. if (!AudioContext) {
  2569. console.warn("浏览器不支持AudioContext");
  2570. return;
  2571. }
  2572. const audioContext = new AudioContext();
  2573. const analyser = audioContext.createAnalyser();
  2574. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  2575. microphone.connect(analyser);
  2576. analyser.fftSize = 256;
  2577. const bufferLength = analyser.frequencyBinCount;
  2578. const dataArray = new Uint8Array(bufferLength);
  2579. let silenceCounter = 0;
  2580. const checkAudio = () => {
  2581. if (this.isRecording)
  2582. return;
  2583. analyser.getByteFrequencyData(dataArray);
  2584. let sum = 0;
  2585. for (let i = 0; i < bufferLength; i++) {
  2586. sum += dataArray[i];
  2587. }
  2588. const average = sum / bufferLength;
  2589. if (average > 10) {
  2590. console.log("检测到音频输入,音量:", average);
  2591. silenceCounter = 0;
  2592. } else {
  2593. silenceCounter++;
  2594. if (silenceCounter > 10) {
  2595. console.warn("持续检测不到音频输入,可能麦克风未正常工作");
  2596. silenceCounter = 0;
  2597. }
  2598. }
  2599. requestAnimationFrame(checkAudio);
  2600. };
  2601. checkAudio();
  2602. } catch (e) {
  2603. console.error("音频测试失败:", e);
  2604. }
  2605. },
  2606. // 添加新方法:尝试单独获取音频
  2607. tryGetAudioOnly() {
  2608. navigator.mediaDevices.getUserMedia({ audio: true }).then((audioStream) => {
  2609. if (this.cameraStream) {
  2610. const videoTrack = this.cameraStream.getVideoTracks()[0];
  2611. const audioTrack = audioStream.getAudioTracks()[0];
  2612. const combinedStream = new MediaStream();
  2613. if (videoTrack)
  2614. combinedStream.addTrack(videoTrack);
  2615. if (audioTrack)
  2616. combinedStream.addTrack(audioTrack);
  2617. this.cameraStream = combinedStream;
  2618. const videoElement = this.$refs.userCameraVideo;
  2619. if (videoElement) {
  2620. videoElement.srcObject = combinedStream;
  2621. videoElement.muted = true;
  2622. }
  2623. console.log("成功合并音频和视频轨道");
  2624. } else {
  2625. console.warn("没有视频流可合并");
  2626. }
  2627. }).catch((err) => {
  2628. console.error("单独获取音频失败:", err);
  2629. });
  2630. },
  2631. // 添加新方法:显示权限对话框
  2632. showPermissionDialog(permissionType) {
  2633. common_vendor.index.showModal({
  2634. title: "需要权限",
  2635. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  2636. confirmText: "去设置",
  2637. success: (res) => {
  2638. if (res.confirm) {
  2639. common_vendor.index.openSetting({
  2640. success: (settingRes) => {
  2641. console.log("设置页面打开成功", settingRes);
  2642. }
  2643. });
  2644. }
  2645. }
  2646. });
  2647. },
  2648. // 添加重试上传方法
  2649. retryVideoUpload() {
  2650. if (this.lastVideoToRetry) {
  2651. this.showRetryButton = false;
  2652. common_vendor.index.showLoading({
  2653. title: "正在重新提交...",
  2654. mask: true
  2655. });
  2656. this.submitVideoToInterview(this.lastVideoToRetry);
  2657. } else {
  2658. common_vendor.index.showToast({
  2659. title: "没有可重试的视频",
  2660. icon: "none"
  2661. });
  2662. }
  2663. },
  2664. // 添加一个新方法用于压缩视频
  2665. async compressVideo(videoBlob) {
  2666. if (videoBlob.size < 5 * 1024 * 1024) {
  2667. return videoBlob;
  2668. }
  2669. console.log("开始压缩视频,原始大小:", videoBlob.size);
  2670. const videoElement = document.createElement("video");
  2671. videoElement.muted = true;
  2672. videoElement.autoplay = false;
  2673. const canvas = document.createElement("canvas");
  2674. const ctx = canvas.getContext("2d");
  2675. videoElement.src = URL.createObjectURL(videoBlob);
  2676. return new Promise((resolve) => {
  2677. videoElement.onloadedmetadata = () => {
  2678. const width = Math.floor(videoElement.videoWidth / 2);
  2679. const height = Math.floor(videoElement.videoHeight / 2);
  2680. canvas.width = width;
  2681. canvas.height = height;
  2682. const stream = canvas.captureStream(15);
  2683. if (videoElement.captureStream) {
  2684. const originalStream = videoElement.captureStream();
  2685. const audioTracks = originalStream.getAudioTracks();
  2686. if (audioTracks.length > 0) {
  2687. stream.addTrack(audioTracks[0]);
  2688. }
  2689. }
  2690. const options = {
  2691. mimeType: "video/webm;codecs=vp8,opus",
  2692. audioBitsPerSecond: 64e3,
  2693. videoBitsPerSecond: 8e5
  2694. // 800kbps
  2695. };
  2696. const mediaRecorder = new MediaRecorder(stream, options);
  2697. const chunks = [];
  2698. mediaRecorder.ondataavailable = (e) => {
  2699. if (e.data.size > 0) {
  2700. chunks.push(e.data);
  2701. }
  2702. };
  2703. mediaRecorder.onstop = () => {
  2704. const compressedBlob = new Blob(chunks, { type: "video/webm" });
  2705. console.log("视频压缩完成,压缩后大小:", compressedBlob.size);
  2706. resolve(compressedBlob);
  2707. };
  2708. videoElement.onplay = () => {
  2709. mediaRecorder.start(10);
  2710. const drawFrame = () => {
  2711. if (videoElement.paused || videoElement.ended) {
  2712. mediaRecorder.stop();
  2713. return;
  2714. }
  2715. ctx.drawImage(videoElement, 0, 0, width, height);
  2716. requestAnimationFrame(drawFrame);
  2717. };
  2718. drawFrame();
  2719. };
  2720. videoElement.play();
  2721. };
  2722. });
  2723. },
  2724. // 修改 checkIOSCameraRecordPermission 方法
  2725. checkIOSCameraRecordPermission() {
  2726. const systemInfo = common_vendor.index.getSystemInfoSync();
  2727. if (systemInfo.platform !== "ios")
  2728. return;
  2729. common_vendor.index.getSetting({
  2730. success: (res) => {
  2731. if (!res.authSetting["scope.camera"]) {
  2732. common_vendor.index.authorize({
  2733. scope: "scope.camera",
  2734. success: () => {
  2735. console.log("iOS相机权限已获取");
  2736. },
  2737. fail: (err) => {
  2738. console.error("iOS相机权限获取失败:", err);
  2739. this.showPermissionDialog("相机");
  2740. }
  2741. });
  2742. }
  2743. if (!res.authSetting["scope.record"]) {
  2744. common_vendor.index.authorize({
  2745. scope: "scope.record",
  2746. success: () => {
  2747. console.log("iOS录音权限已获取");
  2748. },
  2749. fail: (err) => {
  2750. console.error("iOS录音权限获取失败:", err);
  2751. this.showPermissionDialog("录音");
  2752. }
  2753. });
  2754. }
  2755. }
  2756. });
  2757. },
  2758. // 添加新方法:检查并修复渲染问题
  2759. checkAndFixRenderingIssues() {
  2760. try {
  2761. if (typeof u !== "undefined" && u) {
  2762. if (!u.currentQuestion) {
  2763. console.log("修复: 创建缺失的currentQuestion对象");
  2764. u.currentQuestion = {};
  2765. }
  2766. if (u.currentQuestion && typeof u.currentQuestion.isImportant === "undefined") {
  2767. console.log("修复: 设置缺失的isImportant属性");
  2768. u.currentQuestion.isImportant = false;
  2769. }
  2770. }
  2771. } catch (e) {
  2772. console.log("防御性检查异常:", e);
  2773. }
  2774. },
  2775. // 添加格式化时间的辅助方法
  2776. formatTime(seconds) {
  2777. if (!seconds && seconds !== 0)
  2778. return "03:30";
  2779. const minutes = Math.floor(seconds / 60);
  2780. const remainingSeconds = seconds % 60;
  2781. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  2782. },
  2783. // 添加获取问题数据的方法
  2784. fetchQuestions() {
  2785. this.loading = true;
  2786. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  2787. let position_id = 1;
  2788. try {
  2789. const selectedJob = JSON.parse(common_vendor.index.getStorageSync("selectedJob") || "{}");
  2790. if (selectedJob && selectedJob.id) {
  2791. position_id = selectedJob.id;
  2792. console.log("使用选择的职位ID获取问题:", position_id);
  2793. } else {
  2794. console.warn("未找到选择的职位信息,使用默认职位ID:", position_id);
  2795. }
  2796. } catch (e) {
  2797. console.error("解析职位信息失败:", e);
  2798. }
  2799. const timestamp = Date.now();
  2800. const requestUrl = `${common_config.apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=0&_t=${timestamp}`;
  2801. console.log("完整的请求URL:", requestUrl);
  2802. common_vendor.index.request({
  2803. url: requestUrl,
  2804. method: "GET",
  2805. success: (res) => {
  2806. if (res.data && res.data.code === 2e3 && res.data.data && res.data.data.items) {
  2807. console.log("获取问题数据成功:", res.data.data.items);
  2808. this.questions = res.data.data.items;
  2809. this.processQuestionData();
  2810. this.playIntroVideo();
  2811. } else {
  2812. console.error("获取问题数据失败:", res.data);
  2813. common_vendor.index.showToast({
  2814. title: "获取问题数据失败",
  2815. icon: "none"
  2816. });
  2817. }
  2818. },
  2819. fail: (err) => {
  2820. console.error("请求问题数据失败:", err);
  2821. common_vendor.index.showToast({
  2822. title: "网络错误,请检查网络连接",
  2823. icon: "none"
  2824. });
  2825. },
  2826. complete: () => {
  2827. this.loading = false;
  2828. }
  2829. });
  2830. },
  2831. // 处理问题数据,提取视频URL和字幕
  2832. processQuestionData() {
  2833. this.videoList = [];
  2834. this.videoList.push(this.introVideoUrl);
  2835. const configStr = common_vendor.index.getStorageSync("configData");
  2836. let openingSpeech = [];
  2837. if (configStr) {
  2838. try {
  2839. const configData = JSON.parse(configStr);
  2840. if (configData && configData.digital_human_opening_speech) {
  2841. if (Array.isArray(configData.digital_human_opening_speech)) {
  2842. openingSpeech = configData.digital_human_opening_speech.map((item, index, arr) => {
  2843. const startTime = index === 0 ? 0 : arr[index - 1].end_time || index * 5;
  2844. const endTime = item.end_time || (index + 1) * 5;
  2845. return {
  2846. startTime,
  2847. endTime,
  2848. text: item.content
  2849. };
  2850. });
  2851. } else if (typeof configData.digital_human_opening_speech === "string") {
  2852. openingSpeech = this.convertStringToSubtitles(configData.digital_human_opening_speech);
  2853. }
  2854. }
  2855. } catch (error) {
  2856. console.error("解析configData失败:", error);
  2857. }
  2858. }
  2859. this.subtitles = openingSpeech.length > 0 ? openingSpeech : [
  2860. {
  2861. startTime: 0,
  2862. endTime: 5,
  2863. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  2864. },
  2865. {
  2866. startTime: 5,
  2867. endTime: 13,
  2868. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  2869. },
  2870. {
  2871. startTime: 13,
  2872. endTime: 20,
  2873. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  2874. }
  2875. ];
  2876. console.log("处理后的字幕数据:", this.subtitles);
  2877. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  2878. const questionsToUse = sortedQuestions.slice(0, 5);
  2879. questionsToUse.forEach((question, index) => {
  2880. if (question.digital_human_video_url) {
  2881. this.videoList.push(question.digital_human_video_url);
  2882. const subtitleArray = [{
  2883. startTime: 0,
  2884. endTime: 30,
  2885. // 延长字幕显示时间到30秒
  2886. text: question.question || question.question
  2887. }];
  2888. const videoIndex = this.videoList.length - 1;
  2889. this[`question${videoIndex}Subtitles`] = subtitleArray;
  2890. this.subtitleMap[question.digital_human_video_url] = subtitleArray;
  2891. }
  2892. });
  2893. if (this.videoList.length <= 1)
  2894. ;
  2895. else {
  2896. console.log("处理后的视频列表:", this.videoList);
  2897. console.log("处理后的字幕:", this.subtitles);
  2898. }
  2899. },
  2900. // 将字符串类型的文本转换为字幕格式
  2901. convertStringToSubtitles(text) {
  2902. if (!text || typeof text !== "string") {
  2903. return [];
  2904. }
  2905. const sentences = text.split(/([。!?!?])/).filter((part) => part.trim() !== "").reduce((acc, part, index, arr) => {
  2906. if (index % 2 === 0) {
  2907. const nextPart = arr[index + 1] || "";
  2908. acc.push(part + nextPart);
  2909. }
  2910. return acc;
  2911. }, []).filter((sentence) => sentence.trim().length > 0);
  2912. if (sentences.length <= 1) {
  2913. const maxLength = 30;
  2914. const textParts = [];
  2915. for (let i = 0; i < text.length; i += maxLength) {
  2916. textParts.push(text.substring(i, i + maxLength));
  2917. }
  2918. sentences.splice(0, sentences.length, ...textParts);
  2919. }
  2920. let currentStartTime = 0;
  2921. return sentences.map((sentence) => {
  2922. const duration = Math.max(3, Math.ceil(sentence.length / 6));
  2923. const startTime = currentStartTime;
  2924. const endTime = startTime + duration;
  2925. currentStartTime = endTime;
  2926. return {
  2927. startTime,
  2928. endTime,
  2929. text: sentence.trim()
  2930. };
  2931. });
  2932. },
  2933. // 使用默认视频和字幕(作为备用)
  2934. useDefaultVideosAndSubtitles() {
  2935. console.log("使用默认视频和字幕");
  2936. this.subtitles = [
  2937. {
  2938. startTime: 0,
  2939. endTime: 5,
  2940. text: "你好,我是本次面试的面试官,欢迎参加本公司的线上面试!"
  2941. },
  2942. {
  2943. startTime: 5,
  2944. endTime: 13,
  2945. text: "面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试"
  2946. },
  2947. {
  2948. startTime: 13,
  2949. endTime: 20,
  2950. text: "以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。"
  2951. }
  2952. ];
  2953. this.question1Subtitles = [{
  2954. startTime: 0,
  2955. endTime: 10,
  2956. text: "请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:"
  2957. }];
  2958. this.question2Subtitles = [{
  2959. startTime: 0,
  2960. endTime: 4,
  2961. text: "在工作中,你如何确保个人防护装备的正确使用?"
  2962. }];
  2963. this.question3Subtitles = [{
  2964. startTime: 0,
  2965. endTime: 4,
  2966. text: "描述一次你与团队合作改善生产流程的经历。"
  2967. }];
  2968. this.question4Subtitles = [{
  2969. startTime: 0,
  2970. endTime: 6,
  2971. text: "你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?"
  2972. }];
  2973. this.question5Subtitles = [{
  2974. startTime: 0,
  2975. endTime: 5,
  2976. text: "您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?"
  2977. }];
  2978. },
  2979. // 播放介绍视频
  2980. playIntroVideo() {
  2981. this.videoUrl = this.videoList[0];
  2982. this.videoPlaying = true;
  2983. this.$nextTick(() => {
  2984. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  2985. if (videoContext) {
  2986. videoContext.play();
  2987. }
  2988. });
  2989. },
  2990. // 添加新方法:获取默认问题ID
  2991. getDefaultQuestionId(videoIndex) {
  2992. switch (videoIndex) {
  2993. case 1:
  2994. return 10;
  2995. case 2:
  2996. return 11;
  2997. case 3:
  2998. return 12;
  2999. case 4:
  3000. return 13;
  3001. default:
  3002. return 10;
  3003. }
  3004. },
  3005. // 添加新方法:根据索引获取当前问题
  3006. getCurrentQuestionByIndex(videoIndex) {
  3007. const questionIndex = videoIndex - 1;
  3008. const sortedQuestions = [...this.questions].sort((a, b) => a.sequence_number - b.sequence_number);
  3009. if (questionIndex >= 0 && questionIndex < sortedQuestions.length) {
  3010. return sortedQuestions[questionIndex];
  3011. }
  3012. return null;
  3013. },
  3014. // 添加新方法:获取当前问题的推荐录制时长
  3015. getCurrentQuestionRecommendedDuration() {
  3016. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  3017. if (currentQuestion && currentQuestion.recommended_duration) {
  3018. const recommendedDuration = parseInt(currentQuestion.recommended_duration);
  3019. console.log(`使用问题推荐时长: ${recommendedDuration}秒`);
  3020. return recommendedDuration;
  3021. }
  3022. console.log("使用默认录制时长: 300秒");
  3023. return 300;
  3024. },
  3025. // 添加新方法:重置录制状态,准备重新回答
  3026. resetForRerecording() {
  3027. console.log("重置录制状态,准备重新回答");
  3028. this.isRecording = false;
  3029. this.showStopRecordingButton = false;
  3030. this.showStartRecordingButton = true;
  3031. this.clearCountdown();
  3032. if (this.recordingTimer) {
  3033. clearInterval(this.recordingTimer);
  3034. this.recordingTimer = null;
  3035. }
  3036. this.recordingTimerCount = 0;
  3037. this.recordingTimeDisplay = "00:00 ";
  3038. this.maxRecordingTime = this.getCurrentQuestionRecommendedDuration();
  3039. this.remainingTime = this.maxRecordingTime;
  3040. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  3041. this.mediaRecorder.stop();
  3042. this.recordedChunks = [];
  3043. }
  3044. if (this.cameraContext) {
  3045. this.cameraContext.stopRecord({
  3046. success: () => {
  3047. console.log("相机录制已停止");
  3048. },
  3049. fail: (err) => {
  3050. console.error("停止相机录制失败:", err);
  3051. }
  3052. });
  3053. }
  3054. this.questionRetryMap = {};
  3055. },
  3056. // 修改 playLowScoreVideo 方法,优化字幕显示并添加重试次数限制
  3057. playLowScoreVideo() {
  3058. var _a;
  3059. console.log("播放低分提示视频");
  3060. const currentQuestionId = (_a = this.getCurrentQuestionByIndex(this.currentVideoIndex)) == null ? void 0 : _a.id;
  3061. if (!currentQuestionId) {
  3062. console.error("无法获取当前问题ID");
  3063. return;
  3064. }
  3065. if (!this.questionRetryMap[currentQuestionId]) {
  3066. this.questionRetryMap[currentQuestionId] = 1;
  3067. } else {
  3068. this.questionRetryMap[currentQuestionId]++;
  3069. }
  3070. if (this.questionRetryMap[currentQuestionId] > this.maxQuestionRetries) {
  3071. console.log(`问题 ${currentQuestionId} 已超过最大重试次数,自动进入下一题`);
  3072. this.isPlayingLowScoreVideo = false;
  3073. this.isFollowUpQuestion = false;
  3074. this.currentFollowUpQuestion = null;
  3075. this.followUpQuestion = "";
  3076. this.parentQuestion = "";
  3077. this.currentVideoIndex++;
  3078. if (this.currentVideoIndex < this.videoList.length) {
  3079. const nextQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  3080. if (nextQuestion) {
  3081. this.currentSubtitle = nextQuestion.question || nextQuestion.question;
  3082. }
  3083. this.videoUrl = this.videoList[this.currentVideoIndex];
  3084. this.videoPlaying = true;
  3085. this.$nextTick(() => {
  3086. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  3087. if (videoContext) {
  3088. videoContext.play();
  3089. }
  3090. });
  3091. } else {
  3092. this.finishInterview();
  3093. }
  3094. return;
  3095. }
  3096. this.isPlayingLowScoreVideo = true;
  3097. this.showStartRecordingButton = false;
  3098. this.showStopRecordingButton = false;
  3099. this.showRerecordButton = false;
  3100. this.videoUrl = this.lowScoreVideoUrl;
  3101. this.videoPlaying = true;
  3102. const currentQuestion = this.getCurrentQuestionByIndex(this.currentVideoIndex);
  3103. const originalSubtitle = currentQuestion ? currentQuestion.question || currentQuestion.question : this.currentSubtitle;
  3104. this.currentSubtitle = "";
  3105. this.$nextTick(() => {
  3106. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  3107. if (videoContext) {
  3108. videoContext.play();
  3109. setTimeout(() => {
  3110. this.currentSubtitle = "我未听清楚您在说什么,请您再说一遍!";
  3111. this.lowScoreVideoSubtitles = [
  3112. {
  3113. startTime: 0,
  3114. endTime: 30,
  3115. text: "我未听清楚您在说什么,请您再说一遍!",
  3116. translation: "I didn't quite catch what you said, could you please repeat it?"
  3117. }
  3118. ];
  3119. }, 500);
  3120. }
  3121. });
  3122. this.originalQuestionSubtitle = originalSubtitle;
  3123. },
  3124. // 添加新方法:处理重新录制按钮点击
  3125. handleRerecordButtonClick() {
  3126. console.log("点击重新录制按钮");
  3127. this.retryCount++;
  3128. console.log(`当前重试次数: ${this.retryCount}/${this.maxRetryAttempts}`);
  3129. this.showRerecordButton = false;
  3130. this.handleStartRecordingClick();
  3131. },
  3132. // 页面卸载时关闭WebSocket连接
  3133. onUnload() {
  3134. console.log("页面卸载,关闭WebSocket连接");
  3135. this.cleanupPersonDetectionWebSocket();
  3136. this.stopUserCamera();
  3137. this.clearCountdown();
  3138. if (this.recordingTimer) {
  3139. clearInterval(this.recordingTimer);
  3140. this.recordingTimer = null;
  3141. }
  3142. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  3143. this.mediaRecorder.stop();
  3144. }
  3145. common_vendor.index.setKeepScreenOn({
  3146. keepScreenOn: false
  3147. });
  3148. },
  3149. // 页面隐藏时关闭WebSocket连接
  3150. onHide() {
  3151. console.log("页面隐藏,关闭WebSocket连接");
  3152. this.cleanupPersonDetectionWebSocket();
  3153. if (this.isRecording) {
  3154. console.log("页面隐藏时正在录制,录制将继续");
  3155. common_vendor.index.showToast({
  3156. title: "请不要离开面试页面",
  3157. icon: "none",
  3158. duration: 2e3
  3159. });
  3160. }
  3161. },
  3162. // 页面显示时重新初始化WebSocket连接
  3163. onShow() {
  3164. console.log("identity-verify页面onShow");
  3165. console.log("当前本地存储中的职位信息:", common_vendor.index.getStorageSync("selectedJob"));
  3166. this.fetchQuestions();
  3167. const systemInfo = common_vendor.index.getSystemInfoSync();
  3168. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  3169. if (isMiniProgram) {
  3170. if (!this.personDetectionSocket) {
  3171. console.log("重新初始化WebSocket连接");
  3172. this.initPersonDetectionWebSocket();
  3173. } else if (!this.personDetectionInterval) {
  3174. console.log("重新启动人脸检测定时器");
  3175. this.startPersonDetectionInterval();
  3176. }
  3177. }
  3178. common_vendor.index.hideHomeButton();
  3179. },
  3180. // 添加新方法:阻止视频控制
  3181. preventVideoControl(e) {
  3182. e.preventDefault();
  3183. e.stopPropagation();
  3184. console.log("阻止视频控制操作");
  3185. return false;
  3186. },
  3187. // 添加获取追问问题的方法
  3188. fetchFollowUpQuestions() {
  3189. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || "1";
  3190. let position_id = 1;
  3191. try {
  3192. const selectedJob = JSON.parse(common_vendor.index.getStorageSync("selectedJob") || "{}");
  3193. if (selectedJob && selectedJob.id) {
  3194. position_id = selectedJob.id;
  3195. console.log("使用选择的职位ID获取追问问题:", position_id);
  3196. } else {
  3197. console.warn("未找到选择的职位信息,使用默认职位ID:", position_id);
  3198. }
  3199. } catch (e) {
  3200. console.error("解析职位信息失败:", e);
  3201. }
  3202. const timestamp = Date.now();
  3203. const requestUrl = `${common_config.apiBaseUrl}/api/wechat/open_questions/?position_id=${position_id}&tenant_id=${tenant_id}&question_form=5&_t=${timestamp}`;
  3204. console.log("获取追问问题的请求URL:", requestUrl);
  3205. common_vendor.index.request({
  3206. url: requestUrl,
  3207. method: "GET",
  3208. success: (res) => {
  3209. if (res.data && res.data.code === 2e3 && res.data.data && res.data.data.items) {
  3210. console.log("获取追问问题数据成功:", res.data.data.items);
  3211. this.followUpQuestions = res.data.data.items.map((item) => ({
  3212. ...item,
  3213. question_form: 5
  3214. // 确保每个追问问题都有正确的question_form值
  3215. }));
  3216. this.processFollowUpQuestions();
  3217. } else {
  3218. console.error("获取追问问题数据失败:", res.data);
  3219. this.followUpQuestions = [];
  3220. }
  3221. },
  3222. fail: (err) => {
  3223. console.error("请求追问问题数据失败:", err);
  3224. this.followUpQuestions = [];
  3225. }
  3226. });
  3227. },
  3228. // 改进处理追问问题数据的方法
  3229. processFollowUpQuestions() {
  3230. if (!this.followUpQuestions || this.followUpQuestions.length === 0) {
  3231. console.log("没有追问问题数据");
  3232. this.followUpQuestions = [];
  3233. return;
  3234. }
  3235. console.log("处理追问问题数据,总数:", this.followUpQuestions.length);
  3236. this.followUpQuestions = this.followUpQuestions.filter((q) => q.parent_question_id);
  3237. const parentQuestionIds = /* @__PURE__ */ new Set();
  3238. this.followUpQuestions.forEach((question) => {
  3239. if (question.parent_question_id) {
  3240. parentQuestionIds.add(question.parent_question_id);
  3241. }
  3242. });
  3243. console.log("有追问的父问题ID:", Array.from(parentQuestionIds));
  3244. this.hasPlayedFollowUp = {};
  3245. },
  3246. // 修改检查并播放追问问题的方法
  3247. checkAndPlayFollowUpQuestion(parentQuestionId) {
  3248. console.log("检查是否有追问问题,父问题ID:", parentQuestionId);
  3249. if (this.isWaitingForAnswer) {
  3250. console.log("正在等待用户回答,暂不处理追问");
  3251. return;
  3252. }
  3253. if (!parentQuestionId) {
  3254. console.warn("没有父问题ID,无法检查追问问题");
  3255. this.proceedToNextQuestion();
  3256. return;
  3257. }
  3258. if (this.hasPlayedFollowUp[parentQuestionId]) {
  3259. console.log("已经播放过此问题的追问,直接进入下一个问题");
  3260. this.retryCount = 0;
  3261. this.proceedToNextQuestion();
  3262. return;
  3263. }
  3264. const followUpQuestion = this.followUpQuestions.find(
  3265. (q) => q.parent_question_id === parentQuestionId && q.digital_human_video_url && q.question_form === 5
  3266. );
  3267. if (followUpQuestion) {
  3268. console.log("找到追问问题:", followUpQuestion);
  3269. const recordingDuration = this.getRecordingDuration();
  3270. const lowScoreDuration = 7;
  3271. const minDuration = 3;
  3272. if (recordingDuration < lowScoreDuration) {
  3273. let message = recordingDuration < minDuration ? `录制时间 ${recordingDuration} 秒,少于最小时长 ${minDuration} 秒` : `录制时间 ${recordingDuration} 秒,少于标准时长 ${lowScoreDuration} 秒`;
  3274. console.log(message + ",将播放低分提示视频");
  3275. this.retryCount++;
  3276. this.needPlayLowScoreVideo = true;
  3277. this.playLowScoreVideo();
  3278. return;
  3279. }
  3280. this.retryCount = 0;
  3281. this.hasPlayedFollowUp[parentQuestionId] = true;
  3282. this.isFollowUpQuestion = true;
  3283. this.currentParentQuestionId = parentQuestionId;
  3284. this.currentFollowUpQuestion = followUpQuestion;
  3285. this.playFollowUpQuestionVideo(followUpQuestion);
  3286. } else {
  3287. console.log("没有找到对应的追问问题,继续下一个问题");
  3288. this.retryCount = 0;
  3289. this.proceedToNextQuestion();
  3290. }
  3291. },
  3292. // 修改播放追问问题视频的方法
  3293. playFollowUpQuestionVideo(followUpQuestion) {
  3294. console.log("播放追问问题视频:", followUpQuestion.digital_human_video_url);
  3295. this.parentQuestion = this.currentSubtitle;
  3296. this.videoUrl = followUpQuestion.digital_human_video_url;
  3297. this.videoPlaying = true;
  3298. this.isFollowUpQuestion = true;
  3299. this.currentFollowUpQuestion = followUpQuestion;
  3300. const followUpSubtitles = [{
  3301. startTime: 0,
  3302. endTime: 60,
  3303. // 延长显示时间到60秒
  3304. text: followUpQuestion.question || followUpQuestion.question
  3305. }];
  3306. const subtitleKey = `followUpSubtitles_${followUpQuestion.id}`;
  3307. this[subtitleKey] = followUpSubtitles;
  3308. this.$nextTick(() => {
  3309. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  3310. if (videoContext) {
  3311. videoContext.play();
  3312. this.currentSubtitle = followUpSubtitles[0].text;
  3313. }
  3314. });
  3315. },
  3316. // 添加作废面试的方法
  3317. invalidateInterview() {
  3318. if (this.isRecording) {
  3319. this.stopRecordingAnswer();
  3320. }
  3321. if (this.recordingTimer) {
  3322. clearInterval(this.recordingTimer);
  3323. }
  3324. this.isRecording = false;
  3325. this.showStopRecordingButton = false;
  3326. this.showStartRecordingButton = false;
  3327. setTimeout(() => {
  3328. common_vendor.index.switchTab({
  3329. url: "/pages/index/index"
  3330. });
  3331. }, 500);
  3332. },
  3333. initPersonDetectionWebSocket() {
  3334. if (this.personDetectionSocket) {
  3335. this.cleanupPersonDetectionWebSocket();
  3336. }
  3337. console.log("YOLO分析开始");
  3338. try {
  3339. this.personDetectionSocket = common_vendor.index.connectSocket({
  3340. url: `${common_config.personDetectionWsUrl}/ws/interview-room/room_${common_vendor.index.getStorageSync("appId")}/${common_vendor.index.getStorageSync("appId")}/`,
  3341. success: () => {
  3342. console.log("WebSocket connection initiated");
  3343. },
  3344. fail: (error) => {
  3345. console.error("WebSocket connection failed:", error);
  3346. }
  3347. });
  3348. this.personDetectionSocket.onOpen(() => {
  3349. console.log("WebSocket connection opened");
  3350. this.startPersonDetectionInterval();
  3351. });
  3352. this.personDetectionSocket.onError((error) => {
  3353. console.error("WebSocket error:", error);
  3354. this.cleanupPersonDetectionWebSocket();
  3355. });
  3356. this.personDetectionSocket.onClose(() => {
  3357. console.log("WebSocket connection closed");
  3358. this.cleanupPersonDetectionWebSocket();
  3359. });
  3360. this.personDetectionSocket.onMessage((res) => {
  3361. try {
  3362. const data = JSON.parse(res.data);
  3363. if (data.type === "person_detection_result") {
  3364. this.handlePersonDetectionResult(data);
  3365. }
  3366. } catch (error) {
  3367. }
  3368. });
  3369. } catch (error) {
  3370. console.error("Error initializing WebSocket:", error);
  3371. this.cleanupPersonDetectionWebSocket();
  3372. }
  3373. },
  3374. startPersonDetectionInterval() {
  3375. if (this.personDetectionInterval) {
  3376. clearInterval(this.personDetectionInterval);
  3377. }
  3378. this.personDetectionInterval = setInterval(() => {
  3379. try {
  3380. if (!this.personDetectionSocket || !this.cameraContext) {
  3381. console.warn("人脸检测:相机上下文或WebSocket连接未就绪");
  3382. return;
  3383. }
  3384. this.cameraContext.takePhoto({
  3385. quality: "low",
  3386. success: (res) => {
  3387. try {
  3388. const tempFilePath = res.tempImagePath;
  3389. if (!tempFilePath) {
  3390. console.warn("人脸检测:未获取到有效的图片路径");
  3391. }
  3392. common_vendor.index.getFileSystemManager().readFile({
  3393. filePath: tempFilePath,
  3394. encoding: "base64",
  3395. success: (res2) => {
  3396. try {
  3397. const base64Image = res2.data;
  3398. if (!this.personDetectionSocket || this.personDetectionSocket.readyState !== 1) {
  3399. console.warn("人脸检测:WebSocket连接已断开或未就绪");
  3400. return;
  3401. }
  3402. this.personDetectionSocket.send({
  3403. data: JSON.stringify({
  3404. type: "person_detection",
  3405. image_data: base64Image
  3406. })
  3407. });
  3408. } catch (wsError) {
  3409. console.error("人脸检测:发送WebSocket数据时出错:", wsError);
  3410. }
  3411. },
  3412. fail: (error) => {
  3413. console.error("人脸检测:读取图片文件失败:", error);
  3414. }
  3415. });
  3416. } catch (fileError) {
  3417. console.error("人脸检测:处理图片文件时出错:", fileError);
  3418. }
  3419. },
  3420. fail: (error) => {
  3421. console.error("人脸检测:拍照失败:", error);
  3422. }
  3423. });
  3424. } catch (mainError) {
  3425. console.error("人脸检测:主流程执行出错:", mainError);
  3426. }
  3427. }, 5e3);
  3428. },
  3429. cleanupPersonDetectionWebSocket() {
  3430. if (this.personDetectionInterval) {
  3431. clearInterval(this.personDetectionInterval);
  3432. this.personDetectionInterval = null;
  3433. }
  3434. if (this.personDetectionSocket) {
  3435. try {
  3436. this.personDetectionSocket.close();
  3437. } catch (error) {
  3438. console.error("Error closing WebSocket:", error);
  3439. }
  3440. this.personDetectionSocket = null;
  3441. }
  3442. },
  3443. handlePersonDetectionResult(data) {
  3444. if (data.data.identity.status == "no_face") {
  3445. this.showPageWarning = true;
  3446. common_vendor.index.showToast({
  3447. /* data.data.identity.message */
  3448. title: "请保持面部完整出现在镜头内",
  3449. icon: "none",
  3450. duration: 3e3
  3451. });
  3452. common_vendor.index.vibrateLong({
  3453. success: function() {
  3454. console.log("Vibration successful");
  3455. },
  3456. fail: function(err) {
  3457. console.error("Vibration failed:", err);
  3458. }
  3459. });
  3460. setTimeout(() => {
  3461. this.showPageWarning = false;
  3462. }, 3e3);
  3463. } else if (data.data.identity.status == "multiple_faces") {
  3464. this.showPageDRWarning = true;
  3465. common_vendor.index.showToast({
  3466. /* data.data.identity.message */
  3467. title: "请保持独立面试环境",
  3468. icon: "none",
  3469. duration: 3e3
  3470. });
  3471. common_vendor.index.vibrateLong({
  3472. success: function() {
  3473. console.log("Vibration successful");
  3474. },
  3475. fail: function(err) {
  3476. console.error("Vibration failed:", err);
  3477. }
  3478. });
  3479. setTimeout(() => {
  3480. this.showPageDRWarning = false;
  3481. }, 3e3);
  3482. }
  3483. },
  3484. // 进入追问模式
  3485. enterFollowUpMode() {
  3486. this.isFollowUpMode = true;
  3487. this.mainQuestionIndex = this.currentVideoIndex;
  3488. console.log("进入追问模式,保存主问题索引:", this.mainQuestionIndex);
  3489. },
  3490. // 修改 handleFollowUpQuestion 方法
  3491. async handleFollowUpQuestion(questionData) {
  3492. console.log("处理追问:", questionData);
  3493. this.enterFollowUpMode();
  3494. console.log("处理追问问题数据:", questionData);
  3495. if (!questionData || !questionData.follow_up_voice_url || !questionData.follow_up_question) {
  3496. console.error("追问数据不完整:", questionData);
  3497. common_vendor.index.showToast({
  3498. title: "获取追问数据失败",
  3499. icon: "none"
  3500. });
  3501. return;
  3502. }
  3503. this.parentQuestion = questionData.original_question || "";
  3504. let audioUrl = "";
  3505. if (questionData.follow_up_voice_url.direct_url) {
  3506. audioUrl = questionData.follow_up_voice_url.direct_url;
  3507. } else if (questionData.follow_up_voice_url.file_url) {
  3508. audioUrl = questionData.follow_up_voice_url.file_url;
  3509. }
  3510. if (!audioUrl.startsWith("http")) {
  3511. audioUrl = common_config.apiBaseUrl + audioUrl;
  3512. }
  3513. console.log("处理后的音频URL:", audioUrl);
  3514. const followUpQuestionObj = {
  3515. id: Date.now(),
  3516. question: questionData.follow_up_question.trim(),
  3517. // 去除可能的空白字符
  3518. audioUrl,
  3519. originalQuestion: questionData.original_question,
  3520. sessionId: questionData.session_id
  3521. };
  3522. console.log("创建的追问问题对象:", followUpQuestionObj);
  3523. this.followUpQuestions.push(followUpQuestionObj);
  3524. this.currentFollowUpIndex = this.followUpQuestions.length - 1;
  3525. this.followUpQuestion = followUpQuestionObj.question;
  3526. this.followUpAudioUrl = followUpQuestionObj.audioUrl;
  3527. this.isFollowUpQuestion = true;
  3528. this.currentFollowUpQuestion = followUpQuestionObj;
  3529. this.showSubtitle(followUpQuestionObj.question, true);
  3530. try {
  3531. await this.playFollowUpAudio();
  3532. console.log("音频播放完成,准备录制回答");
  3533. this.prepareToAnswer(true, followUpQuestionObj);
  3534. } catch (error) {
  3535. console.error("音频播放失败:", error);
  3536. this.prepareToAnswer(true, followUpQuestionObj);
  3537. }
  3538. },
  3539. // 播放追问音频
  3540. async playFollowUpAudio() {
  3541. return new Promise((resolve, reject) => {
  3542. console.log("开始播放追问音频, URL:", this.followUpAudioUrl);
  3543. if (!this.followUpAudioUrl) {
  3544. console.error("没有音频URL");
  3545. reject(new Error("没有音频URL"));
  3546. return;
  3547. }
  3548. common_vendor.index.showLoading({
  3549. title: "思考中..."
  3550. });
  3551. this.stopAndDestroyAudio();
  3552. try {
  3553. const innerAudioContext = common_vendor.index.createInnerAudioContext();
  3554. this.audioContext = innerAudioContext;
  3555. innerAudioContext.autoplay = true;
  3556. innerAudioContext.obeyMuteSwitch = false;
  3557. innerAudioContext.volume = 1;
  3558. innerAudioContext.onCanplay(() => {
  3559. console.log("音频可以播放");
  3560. common_vendor.index.hideLoading();
  3561. this.isAudioPlaying = true;
  3562. });
  3563. innerAudioContext.onEnded(() => {
  3564. console.log("追问音频播放完成");
  3565. this.isAudioPlaying = false;
  3566. resolve();
  3567. setTimeout(() => {
  3568. this.stopAndDestroyAudio();
  3569. this.prepareToAnswer(true, this.currentFollowUpQuestion);
  3570. }, 100);
  3571. });
  3572. innerAudioContext.onError((res) => {
  3573. console.error("音频播放错误:", res);
  3574. this.isAudioPlaying = false;
  3575. common_vendor.index.hideLoading();
  3576. common_vendor.index.showToast({
  3577. title: "音频播放失败",
  3578. icon: "none"
  3579. });
  3580. reject(res);
  3581. this.stopAndDestroyAudio();
  3582. });
  3583. console.log("设置音频源:", this.followUpAudioUrl);
  3584. innerAudioContext.src = this.followUpAudioUrl;
  3585. } catch (error) {
  3586. console.error("创建或播放音频失败:", error);
  3587. this.isAudioPlaying = false;
  3588. common_vendor.index.hideLoading();
  3589. common_vendor.index.showToast({
  3590. title: "音频播放失败",
  3591. icon: "none"
  3592. });
  3593. this.stopAndDestroyAudio();
  3594. reject(error);
  3595. }
  3596. });
  3597. },
  3598. // 停止并销毁音频
  3599. stopAndDestroyAudio() {
  3600. if (!this.audioContext) {
  3601. return;
  3602. }
  3603. const ctx = this.audioContext;
  3604. this.audioContext = null;
  3605. try {
  3606. if (this.isAudioPlaying) {
  3607. ctx.stop();
  3608. }
  3609. } catch (error) {
  3610. console.error("停止音频播放失败:", error);
  3611. }
  3612. try {
  3613. ctx.destroy();
  3614. } catch (error) {
  3615. console.error("销毁音频实例失败:", error);
  3616. }
  3617. this.isAudioPlaying = false;
  3618. },
  3619. // 显示字幕
  3620. showSubtitle(text, isFollowUp = true) {
  3621. console.log("显示字幕:", text);
  3622. this.subtitleText = text;
  3623. this.showSubtitleText = true;
  3624. this.isFollowUpQuestion = isFollowUp;
  3625. this.$nextTick(() => {
  3626. setTimeout(() => {
  3627. this.showSubtitleText = false;
  3628. }, 8e3);
  3629. });
  3630. },
  3631. // 显示问题对话框
  3632. showQuestionDialog() {
  3633. if (this.followUpQuestion) {
  3634. common_vendor.index.showModal({
  3635. title: "追问问题",
  3636. content: this.followUpQuestion,
  3637. showCancel: false,
  3638. success: () => {
  3639. this.prepareToAnswer(true, this.followUpQuestions[this.currentFollowUpIndex]);
  3640. }
  3641. });
  3642. }
  3643. },
  3644. // 显示面试官思考中loading
  3645. showThinkingLoading() {
  3646. this.isThinking = true;
  3647. common_vendor.index.showLoading({
  3648. title: "面试官正在思考中",
  3649. mask: false
  3650. });
  3651. },
  3652. // 隐藏面试官思考中loading
  3653. hideThinkingLoading() {
  3654. this.isThinking = false;
  3655. common_vendor.index.hideLoading();
  3656. if (this.thinkingTimer) {
  3657. clearTimeout(this.thinkingTimer);
  3658. this.thinkingTimer = null;
  3659. }
  3660. },
  3661. // 处理视频完成
  3662. handleVideoCompletion(jobId) {
  3663. this.isVideoSwitching = false;
  3664. if (jobId === 9) {
  3665. common_vendor.index.navigateTo({
  3666. url: "/pages/interview-question/interview-question",
  3667. success: () => {
  3668. console.log("成功跳转到interview-question页面");
  3669. },
  3670. fail: (err) => {
  3671. console.error("跳转到interview-question页面失败:", err);
  3672. common_vendor.index.redirectTo({
  3673. url: "/pages/interview-question/interview-question",
  3674. fail: (redirectErr) => {
  3675. console.error("重定向到interview-question页面也失败:", redirectErr);
  3676. common_vendor.index.navigateBack({
  3677. delta: 1
  3678. });
  3679. }
  3680. });
  3681. }
  3682. });
  3683. } else {
  3684. let configData = {};
  3685. try {
  3686. const configStr = common_vendor.index.getStorageSync("configData");
  3687. if (configStr && configStr.trim()) {
  3688. configData = JSON.parse(configStr);
  3689. }
  3690. } catch (error) {
  3691. console.error("解析configData失败:", error);
  3692. configData = {};
  3693. }
  3694. console.log("获取到的配置数据:", configData);
  3695. const questionFormSwitches = (configData == null ? void 0 : configData.question_form_switches) || {};
  3696. const hasChoiceQuestions = questionFormSwitches.enable_fill_blank || questionFormSwitches.enable_image_choice || questionFormSwitches.enable_multiple_choice || questionFormSwitches.enable_single_choice;
  3697. const hasScoringQuestions = questionFormSwitches.enable_scoring_questions;
  3698. const hasPostureDetection = configData == null ? void 0 : configData.enable_posture_check;
  3699. const hasCandidateQuestions = questionFormSwitches.enable_candidate_questions;
  3700. let targetUrl = "/pages/camera/camera";
  3701. let pageName = "camera页面";
  3702. console.log("当前配置数据:", hasScoringQuestions);
  3703. if (hasChoiceQuestions || hasScoringQuestions) {
  3704. targetUrl = "/pages/camera/camera";
  3705. pageName = "camera页面";
  3706. } else if (hasPostureDetection) {
  3707. targetUrl = "/pages/posture-guide/posture-guide";
  3708. pageName = "posture-guide页面";
  3709. } else if (hasCandidateQuestions) {
  3710. targetUrl = "/pages/interview-question/interview-question";
  3711. pageName = "interview-question页面";
  3712. } else {
  3713. targetUrl = "/pages/success/success";
  3714. pageName = "camera页面";
  3715. }
  3716. console.log("根据配置跳转到:", targetUrl, "配置项:", questionFormSwitches);
  3717. common_vendor.index.navigateTo({
  3718. url: targetUrl,
  3719. success: () => {
  3720. console.log(`成功跳转到${pageName}`);
  3721. },
  3722. fail: (err) => {
  3723. console.error(`跳转到${pageName}失败:`, err);
  3724. common_vendor.index.redirectTo({
  3725. url: targetUrl,
  3726. fail: (redirectErr) => {
  3727. console.error(`重定向到${pageName}也失败:`, redirectErr);
  3728. common_vendor.index.switchTab({
  3729. url: targetUrl,
  3730. fail: (switchErr) => {
  3731. console.error("所有跳转方式都失败:", switchErr);
  3732. common_vendor.index.navigateBack({
  3733. delta: 1
  3734. });
  3735. }
  3736. });
  3737. }
  3738. });
  3739. }
  3740. });
  3741. }
  3742. }
  3743. },
  3744. computed: {
  3745. // 计算进度比例
  3746. progressRatio() {
  3747. return this.recordingTimerCount / this.maxRecordingTime;
  3748. },
  3749. // 右半圆旋转角度
  3750. half1Turn() {
  3751. if (this.progressRatio <= 0.5) {
  3752. return this.progressRatio * 2 * 180;
  3753. } else {
  3754. return 180;
  3755. }
  3756. },
  3757. // 左半圆旋转角度
  3758. half2Turn() {
  3759. if (this.progressRatio <= 0.5) {
  3760. return 0;
  3761. } else {
  3762. return (this.progressRatio - 0.5) * 2 * 180;
  3763. }
  3764. },
  3765. // 修改进度百分比的计算方式
  3766. progressPercent() {
  3767. return Math.min(Math.round(this.recordingTimerCount / this.maxRecordingTime * 100), 100);
  3768. },
  3769. // 添加总题数计算属性
  3770. totalQuestions() {
  3771. return this.questions.length || 5;
  3772. }
  3773. }
  3774. };
  3775. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  3776. return common_vendor.e({
  3777. a: $data.statusBarHeight + "px",
  3778. b: $data.videoUrl,
  3779. c: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  3780. d: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  3781. e: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  3782. f: common_vendor.o((...args) => $options.preventVideoControl && $options.preventVideoControl(...args)),
  3783. g: common_vendor.o((...args) => $options.preventVideoControl && $options.preventVideoControl(...args)),
  3784. h: $data.showAnswerButton
  3785. }, $data.showAnswerButton ? {
  3786. i: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  3787. } : {}, {
  3788. j: $data.currentSubtitle || $data.showSubtitleText && $data.subtitleText
  3789. }, $data.currentSubtitle || $data.showSubtitleText && $data.subtitleText ? common_vendor.e({
  3790. k: $data.currentVideoIndex > 0
  3791. }, $data.currentVideoIndex > 0 ? {
  3792. l: common_vendor.t($data.currentVideoIndex),
  3793. m: common_vendor.t($options.totalQuestions)
  3794. } : {}, {
  3795. n: !$data.isFollowUpQuestion
  3796. }, !$data.isFollowUpQuestion ? {
  3797. o: common_vendor.t($data.currentSubtitle)
  3798. } : {
  3799. p: common_vendor.t($data.parentQuestion),
  3800. q: common_vendor.t($data.subtitleText || $data.currentSubtitle)
  3801. }) : {}, {
  3802. r: $data.useMiniProgramCameraComponent
  3803. }, $data.useMiniProgramCameraComponent ? {
  3804. s: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  3805. } : {}, {
  3806. t: $data.showPageWarning ? 1 : "",
  3807. v: $data.loading
  3808. }, $data.loading ? {} : {}, {
  3809. w: $data.showDebugInfo
  3810. }, $data.showDebugInfo ? common_vendor.e({
  3811. x: $data.assistantResponse
  3812. }, $data.assistantResponse ? {
  3813. y: common_vendor.t($data.assistantResponse)
  3814. } : {}, {
  3815. z: $data.audioTranscript
  3816. }, $data.audioTranscript ? {
  3817. A: common_vendor.t($data.audioTranscript)
  3818. } : {}, {
  3819. B: common_vendor.f($data.processedResponses, (item, index, i0) => {
  3820. return common_vendor.e({
  3821. a: item.role
  3822. }, item.role ? {
  3823. b: common_vendor.t(item.role)
  3824. } : {}, {
  3825. c: item.transcript
  3826. }, item.transcript ? {
  3827. d: common_vendor.t(item.transcript)
  3828. } : {}, {
  3829. e: index
  3830. });
  3831. })
  3832. }) : {}, {
  3833. C: $data.showStopRecordingButton
  3834. }, $data.showStopRecordingButton ? {
  3835. D: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  3836. } : {}, {
  3837. E: $data.isRecording
  3838. }, $data.isRecording ? {
  3839. F: common_vendor.t($data.recordingTimeDisplay),
  3840. G: `conic-gradient(${$data.progressColor} ${$options.progressPercent}%, ${$data.progressBgColor} 0%)`
  3841. } : {}, {
  3842. H: $data.showStartRecordingButton
  3843. }, $data.showStartRecordingButton ? {
  3844. I: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  3845. } : {}, {
  3846. J: $data.showRetryButton
  3847. }, $data.showRetryButton ? {
  3848. K: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  3849. } : {}, {
  3850. L: $data.showCountdown
  3851. }, $data.showCountdown ? {
  3852. M: common_vendor.t($data.countdownValue)
  3853. } : {}, {
  3854. N: $data.showRerecordButton
  3855. }, $data.showRerecordButton ? {
  3856. O: common_vendor.o((...args) => $options.handleRerecordButtonClick && $options.handleRerecordButtonClick(...args))
  3857. } : {}, {
  3858. P: $data.showPageWarning ? 1 : ""
  3859. });
  3860. }
  3861. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-464e78c6"]]);
  3862. wx.createPage(MiniProgramPage);