ChatBotPage.tsx 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079
  1. import { useCallback, useEffect, useMemo, useRef, useState, type KeyboardEvent } from 'react';
  2. import { useNavigate, useSearchParams, useLocation } from 'react-router-dom';
  3. import { message as antMessage } from 'antd';
  4. import { v4 as uuidv4 } from 'uuid';
  5. import { useAppStore } from '@/store';
  6. import { AudioRecorder, type FrequencyFrame } from '@/utils/audioRecorder';
  7. import { AudioPlayer } from '@/utils/audioPlayer';
  8. import { WebSocketManager } from '@/utils/websocket';
  9. import { prepareMobileAudio, requestVolumeControl, isMobileDevice } from '@/utils/audioHelper';
  10. import {
  11. setStorage,
  12. getStorage,
  13. getUserLevel,
  14. getHasSharedAvatar,
  15. STORAGE_KEYS,
  16. markRouteVisited,
  17. getUserId,
  18. getVisitedRoutes,
  19. setUserLevel,
  20. getCurrentBackgroundImageUrl,
  21. getCurrentBackgroundImageId,
  22. getCurrentBackgroundImageOpacity,
  23. syncBackgroundImageToStorage,
  24. setUserName,
  25. } from '@/utils/storage';
  26. import { onScreenResize, calculateLayout, type CalculatedLayout, updateTooSmallFlag } from '@/utils/screenSize';
  27. import { performanceLogger } from '@/utils/logger';
  28. import { getUserInfo, updateUserUIState, getUserUIState, getAvatarGifs, type AvatarGifSet } from '@/utils/api';
  29. import { px2vw, px2vh, px2vmin } from '@/utils/viewport';
  30. import type { Message, EnergyStoneSpawnMessage } from '@/types';
  31. import '@/styles/chatbot-fix.css';
  32. import { getOssUrl } from '@/utils/oss';
  33. import { AudioWave, type AudioWaveHandle } from '@/components/AudioWave';
  34. import { ProgressRingCanvas, type ProgressRingCanvasHandle } from '@/components/ProgressRingCanvas';
  35. const avatarC = getOssUrl('Avatar-C.svg');
  36. const avatarGift = getOssUrl('Gift.gif');
  37. const audioButton = getOssUrl('audio-button.png');
  38. const avatarHint = getOssUrl('Avatar-hint.png');
  39. const microphoneIcon = getOssUrl('microphone-icon.svg');
  40. const progressRing = getOssUrl('progress-ring.svg');
  41. const tapToChat = getOssUrl('TapToChat.svg');
  42. const cancelIcon = getOssUrl('cancel-icon.svg');
  43. const sendIcon = getOssUrl('send-icon.svg');
  44. const tooltipsCancel = getOssUrl('tooltips-cancel.svg');
  45. const tooltipsSend = getOssUrl('tooltips-send.svg');
  46. const tooltipsProgressBar = getOssUrl('tooltips-progress-bar.svg');
  47. const missionEnergyStone = getOssUrl('Mission-Energy-Stone.png');
  48. const conversationEnergyStone = getOssUrl('Conversation-Energy-Stone.png');
  49. const missionEnergyStoneHint = getOssUrl('Mission-Energy-Stone-hint.png');
  50. const conversationEnergyStoneHint = getOssUrl('Conversation-Energy-Stone-hint.png');
  51. import { useEnergyStones, type EnergyStoneEventPayload } from '@/hooks/useEnergyStones';
  52. import { MissionEnergyStoneModal } from '@/components/MissionEnergyStoneModal';
  53. const MAX_RECORDING_DURATION_MS = 60_000;
  54. const AVATAR_C_UNLOCK_THRESHOLD_MS = 60_000;
  55. const COUNTDOWN_THRESHOLD_MS = 10_000;
  56. const WS_URL = import.meta.env.VITE_WS_URL || 'ws://localhost:8000/ws';
  57. // GIF URL 兜底常量(基础形象)
  58. const FALLBACK_DEFAULT_AVATAR_GIF = getOssUrl('01-Default.gif');
  59. const FALLBACK_POSITIVE_EMOTION_GIF = getOssUrl('04-Positive-emotion.gif');
  60. const FALLBACK_NEGATIVE_EMOTION_GIF = getOssUrl('05-Negative-emotion.gif');
  61. const FALLBACK_TALKING_GIF = getOssUrl('06-talking.gif');
  62. // GIF URL 兜底常量(注册赠送形象:带帽子)
  63. const FALLBACK_REGISTERED_DEFAULT_AVATAR_GIF = getOssUrl('01-Default-Registered.gif');
  64. const FALLBACK_REGISTERED_POSITIVE_EMOTION_GIF = getOssUrl('04-Positive-emotion-Registered.gif');
  65. const FALLBACK_REGISTERED_NEGATIVE_EMOTION_GIF = getOssUrl('05-Negative-emotion-Registered.gif');
  66. const FALLBACK_REGISTERED_TALKING_GIF = getOssUrl('06-talking-Registered.gif');
  67. // 形象 ID 常量(与后端/迁移脚本一致)
  68. const AVATAR_ID_DEFAULT = 'avatar_default';
  69. const AVATAR_ID_REGISTERED = 'avatar_registered_cap';
  70. type AvatarState = 'default' | 'positive' | 'negative' | 'talking';
  71. // 情绪分类函数
  72. const getEmotionCategory = (emotion: string): 'positive' | 'negative' | 'neutral' => {
  73. // 正向情绪
  74. const positiveEmotions = ['happy', 'excited', 'encouraging', 'satisfied', 'friendly'];
  75. // 反向情绪
  76. const negativeEmotions = ['sad', 'angry', 'worried', 'confused', 'sorry'];
  77. if (positiveEmotions.includes(emotion)) {
  78. return 'positive';
  79. }
  80. if (negativeEmotions.includes(emotion)) {
  81. return 'negative';
  82. }
  83. // neutral, surprised, thoughtful 等归为中性
  84. return 'neutral';
  85. };
  86. // 获取情绪 GIF 播放时长(毫秒)
  87. // 注册的正向情绪GIF 2秒,注册的负向情绪 5秒
  88. // 未注册的正向情绪GIF 2秒,未注册的负向情绪 4秒
  89. const getEmotionGifDuration = (avatarId: string | null, emotionCategory: 'positive' | 'negative'): number => {
  90. const isRegistered = avatarId === AVATAR_ID_REGISTERED;
  91. if (emotionCategory === 'positive') {
  92. // 正向情绪:注册和未注册都是 2秒
  93. return 2000;
  94. } else if (emotionCategory === 'negative') {
  95. // 负向情绪:注册 5秒,未注册 4秒
  96. return isRegistered ? 5000 : 4000;
  97. }
  98. return 0;
  99. };
  100. export const ChatBotPage = () => {
  101. const navigate = useNavigate();
  102. const [searchParams] = useSearchParams();
  103. const location = useLocation();
  104. const {
  105. currentState,
  106. currentRequestId,
  107. conversationProgress,
  108. progressLoopCount,
  109. setState,
  110. setRequestId,
  111. setEmotion,
  112. setRecognizedText,
  113. setError,
  114. setConnected,
  115. updateProgressByDuration,
  116. setProgress,
  117. reset,
  118. isConnected,
  119. } = useAppStore();
  120. const recorderRef = useRef<AudioRecorder | null>(null);
  121. const playerRef = useRef<AudioPlayer | null>(null);
  122. const wsRef = useRef<WebSocketManager | null>(null);
  123. const chunkIndexRef = useRef(0);
  124. const connectionLostRef = useRef(false);
  125. const activeRequestIdRef = useRef<string | null>(null);
  126. const cancelledRequestIdsRef = useRef<Set<string>>(new Set());
  127. const mobileAudioPreparedRef = useRef(false);
  128. const sendTimeRef = useRef<number>(0);
  129. const audioWaveRef = useRef<AudioWaveHandle | null>(null);
  130. const progressRingCanvasRef = useRef<ProgressRingCanvasHandle | null>(null);
  131. const isMountedRef = useRef(true);
  132. const [userLevelValue, setUserLevelValue] = useState(() => getUserLevel());
  133. const levelRedirectRef = useRef(false);
  134. const [hasSharedAvatar, setHasSharedAvatarState] = useState(() => getHasSharedAvatar());
  135. // 背景图片 URL
  136. const [backgroundImageUrl, setBackgroundImageUrl] = useState<string | null>(() => {
  137. // 初始化时从 localStorage 获取背景图片 URL
  138. const url = getCurrentBackgroundImageUrl();
  139. return url || null;
  140. });
  141. // 背景图片 ID(只保留 setter,因为变量本身未使用)
  142. const [, setBackgroundImageId] = useState<string | null>(() => {
  143. return getCurrentBackgroundImageId();
  144. });
  145. // 初始化时检查 localStorage,确保第一次打开时立即显示提示
  146. const [showTapToChat, setShowTapToChat] = useState(() => {
  147. const hasShown = getStorage(STORAGE_KEYS.HAS_SHOWN_TAP_TO_CHAT);
  148. // 如果已经显示过,直接返回 false
  149. if (hasShown) {
  150. return false;
  151. }
  152. // 第一次打开,返回 true(但不立即保存到 localStorage,等 WebSocket 连接成功后再保存)
  153. return true;
  154. });
  155. const sendingTimeoutRef = useRef<number | null>(null);
  156. const updateRequestId = useCallback(
  157. (id: string | null) => {
  158. activeRequestIdRef.current = id;
  159. setRequestId(id);
  160. },
  161. [setRequestId]
  162. );
  163. useEffect(() => {
  164. activeRequestIdRef.current = currentRequestId;
  165. }, [currentRequestId]);
  166. useEffect(() => {
  167. const isLoggedIn = getStorage(STORAGE_KEYS.IS_LOGGED_IN) === 'true';
  168. if (!isLoggedIn && userLevelValue >= 5 && !levelRedirectRef.current) {
  169. levelRedirectRef.current = true;
  170. // 等待音频播放完成后再跳转
  171. const performRedirect = async () => {
  172. // 检查是否正在等待AI回复(sending状态)或正在播放音频(playing状态)
  173. const isWaitingOrPlaying = currentState === 'sending' || currentState === 'playing';
  174. const hasActiveRequest = currentRequestId !== null;
  175. const isAudioPlaying = playerRef.current?.isPlaying() || false;
  176. if (isWaitingOrPlaying || hasActiveRequest || isAudioPlaying) {
  177. console.log('[ChatBotPage] 检测到正在等待AI回复或播放音频,等待完成后再跳转...', {
  178. state: currentState,
  179. hasRequest: hasActiveRequest,
  180. isPlaying: isAudioPlaying
  181. });
  182. // 如果正在等待回复(sending状态),等待状态变为playing或idle
  183. if (currentState === 'sending' || hasActiveRequest) {
  184. await new Promise<void>((resolve) => {
  185. // 设置超时保护,最多等待30秒
  186. const timeoutId = setTimeout(() => {
  187. console.warn('[ChatBotPage] 等待AI回复超时,继续执行跳转');
  188. resolve();
  189. }, 30000);
  190. // 使用轮询检查状态,直到状态变为idle且没有活跃请求
  191. const checkInterval = setInterval(() => {
  192. const appState = useAppStore.getState();
  193. // 如果状态变为idle且没有活跃请求,说明对话已完成
  194. if (appState.currentState === 'idle' && appState.currentRequestId === null) {
  195. clearInterval(checkInterval);
  196. clearTimeout(timeoutId);
  197. resolve();
  198. }
  199. }, 100);
  200. });
  201. }
  202. // 如果音频正在播放或状态为playing,等待播放完成
  203. if (currentState === 'playing' || isAudioPlaying || playerRef.current?.isPlaying()) {
  204. if (playerRef.current) {
  205. try {
  206. await playerRef.current.waitForPlaybackComplete();
  207. console.log('[ChatBotPage] 音频播放完成');
  208. } catch (error) {
  209. console.warn('[ChatBotPage] 等待音频播放完成时出错:', error);
  210. }
  211. }
  212. }
  213. console.log('[ChatBotPage] 所有等待完成,执行跳转');
  214. }
  215. navigate('/level5-welcome');
  216. };
  217. performRedirect();
  218. }
  219. }, [userLevelValue, navigate, currentState, currentRequestId]);
  220. const [showCancelTooltip, setShowCancelTooltip] = useState(false);
  221. const [showSendTooltip, setShowSendTooltip] = useState(false);
  222. const [showProgressBarTooltip, setShowProgressBarTooltip] = useState(false);
  223. const [showMissionStoneModal, setShowMissionStoneModal] = useState(false);
  224. const [energyStoneModalType, setEnergyStoneModalType] = useState<'mission' | 'conversation'>('mission');
  225. const [hasClickedMissionStone, setHasClickedMissionStone] = useState(() => {
  226. return getStorage(STORAGE_KEYS.HAS_CLICKED_MISSION_STONE) === 'true';
  227. });
  228. const [hasClickedConversationStone, setHasClickedConversationStone] = useState(() => {
  229. return getStorage(STORAGE_KEYS.HAS_CLICKED_CONVERSATION_STONE) === 'true';
  230. });
  231. const [hasShownProgressBarTooltip, setHasShownProgressBarTooltip] = useState(() => {
  232. const stored = getStorage(STORAGE_KEYS.HAS_SHOWN_PROGRESS_BAR_TOOLTIP);
  233. return stored === 'true';
  234. });
  235. const progressBarTooltipTimerRef = useRef<number | null>(null);
  236. const progressBarTooltipRef = useRef<HTMLImageElement | null>(null);
  237. const pendingStoneIdRef = useRef<string | null>(null);
  238. const [isAvatarCUnlocked, setIsAvatarCUnlocked] = useState(() => {
  239. const stored = getStorage(STORAGE_KEYS.AVATAR_C_UNLOCKED);
  240. return stored === 'true';
  241. });
  242. const avatarCUnlockedRef = useRef(isAvatarCUnlocked);
  243. useEffect(() => {
  244. avatarCUnlockedRef.current = isAvatarCUnlocked;
  245. }, [isAvatarCUnlocked]);
  246. const totalSpeechDurationRef = useRef<number>(
  247. isAvatarCUnlocked ? AVATAR_C_UNLOCK_THRESHOLD_MS : 0
  248. );
  249. // 控制声纹容器高度(延迟降低,等待衰减动画完成)
  250. const [shouldExpandWaveform, setShouldExpandWaveform] = useState(false);
  251. const waveformCollapseTimerRef = useRef<number | null>(null);
  252. const WAVEFORM_HEIGHT_PX = 40;
  253. const waveformHeightStyle = '40PX'; // 始终保持 40PX,不随屏幕尺寸变化
  254. const waveformHeightPx = WAVEFORM_HEIGHT_PX;
  255. // 用于动画的显示进度(平滑过渡)
  256. const [displayProgress, setDisplayProgress] = useState(0);
  257. const animationFrameRef = useRef<number>();
  258. const totalProgressRef = useRef(0);
  259. // 默认置为 true,避免组件初次挂载或路由返回时尚未完成数据同步就触发历史进度动画。
  260. const shouldSkipProgressAnimationRef = useRef(true);
  261. // 保存上一次的进度状态,用于计算动画起始值
  262. const prevProgressStateRef = useRef<{ progress: number; loopCount: number }>({
  263. progress: 0,
  264. loopCount: 0,
  265. });
  266. const recordingTimerRef = useRef<number | null>(null);
  267. const recordingDeadlineRef = useRef<number>(0);
  268. const handleSendRef = useRef<() => void>();
  269. const [remainingTimeMs, setRemainingTimeMs] = useState<number | null>(null);
  270. const pushFrequencyFrame = useCallback((frame: FrequencyFrame) => {
  271. audioWaveRef.current?.pushFrequencyFrame(frame.frequency);
  272. }, []);
  273. const resetFrequencyVisualization = useCallback(() => {
  274. audioWaveRef.current?.pushFrequencyFrame(null);
  275. }, []);
  276. // V2: 完全计算式布局系统
  277. const [layout, setLayout] = useState<CalculatedLayout>(() => {
  278. const initialLayout = calculateLayout();
  279. return initialLayout;
  280. });
  281. // GIF 切换定时器
  282. const emotionGifTimerRef = useRef<number | null>(null);
  283. const handleAvatarClick = useCallback(async () => {
  284. if (!isAvatarCUnlocked) {
  285. return;
  286. }
  287. const isLoggedIn = getStorage(STORAGE_KEYS.IS_LOGGED_IN) === 'true';
  288. if (isLoggedIn) {
  289. // 已登录:跳转到 profile 界面
  290. navigate('/profile');
  291. return;
  292. }
  293. // 未登录:根据用户等级跳转
  294. if (userLevelValue >= 5) {
  295. levelRedirectRef.current = true;
  296. // 等待音频播放完成后再跳转
  297. if (playerRef.current?.isPlaying()) {
  298. console.log('[ChatBotPage] 检测到音频正在播放,等待播放完成后再跳转...');
  299. try {
  300. await playerRef.current.waitForPlaybackComplete();
  301. console.log('[ChatBotPage] 音频播放完成,执行跳转');
  302. } catch (error) {
  303. console.warn('[ChatBotPage] 等待音频播放完成时出错:', error);
  304. }
  305. }
  306. navigate('/level5-welcome');
  307. return;
  308. }
  309. navigate('/level34-welcome');
  310. }, [isAvatarCUnlocked, navigate, userLevelValue]);
  311. const handleAvatarKeyDown = useCallback(
  312. (event: KeyboardEvent<HTMLDivElement>) => {
  313. if (event.key === 'Enter' || event.key === ' ') {
  314. event.preventDefault();
  315. handleAvatarClick();
  316. }
  317. },
  318. [handleAvatarClick],
  319. );
  320. const timingRef = useRef({
  321. recordStart: 0,
  322. recordEnd: 0,
  323. firstAudioReceived: 0,
  324. emotionReceived: 0,
  325. });
  326. // 使用 Web Animations API 驱动飞行动画,避免部分端 CSS 动画被覆盖
  327. const runStoneAnimation = useCallback(
  328. (container: HTMLElement | null, dx: number, dy: number) => {
  329. if (!container) return;
  330. const img = container.querySelector('.energy-stone') as HTMLElement | null;
  331. const keyframes: Keyframe[] = [
  332. {
  333. transform: 'translate(-50%, -50%) translate3d(0, 0, 0)',
  334. opacity: 1,
  335. offset: 0,
  336. },
  337. {
  338. transform: `translate(-50%, -50%) translate3d(${dx}px, ${dy}px, 0)`,
  339. opacity: 1,
  340. offset: 0.85,
  341. },
  342. {
  343. transform: `translate(-50%, -50%) translate3d(${dx}px, ${dy}px, 0)`,
  344. opacity: 0,
  345. offset: 1,
  346. },
  347. ];
  348. const timing: KeyframeAnimationOptions = {
  349. duration: 1400,
  350. easing: 'ease-in-out',
  351. fill: 'forwards',
  352. };
  353. try {
  354. container.animate(keyframes, timing);
  355. if (img) {
  356. img.animate(
  357. [
  358. { transform: 'translate(-50%, -50%)', opacity: 1, offset: 0 },
  359. { transform: 'translate(-50%, -50%)', opacity: 1, offset: 0.85 },
  360. { transform: 'translate(-50%, -50%)', opacity: 0, offset: 1 },
  361. ],
  362. timing
  363. );
  364. }
  365. } catch (error) {
  366. // 如果 WAAPI 不可用,忽略,保留 CSS 动画兜底
  367. console.warn('[EnergyStone] WAAPI animate failed, fallback to CSS animation', error);
  368. }
  369. },
  370. []
  371. );
  372. // 当前形象ID(支持后端返回/本地缓存)
  373. const [currentAvatarId, setCurrentAvatarId] = useState<string | null>(() => {
  374. return getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  375. });
  376. const [avatarGifSets, setAvatarGifSets] = useState<Record<string, AvatarGifSet>>({
  377. [AVATAR_ID_DEFAULT]: {
  378. default: FALLBACK_DEFAULT_AVATAR_GIF,
  379. positive: FALLBACK_POSITIVE_EMOTION_GIF,
  380. negative: FALLBACK_NEGATIVE_EMOTION_GIF,
  381. talking: FALLBACK_TALKING_GIF,
  382. },
  383. [AVATAR_ID_REGISTERED]: {
  384. default: FALLBACK_REGISTERED_DEFAULT_AVATAR_GIF,
  385. positive: FALLBACK_REGISTERED_POSITIVE_EMOTION_GIF,
  386. negative: FALLBACK_REGISTERED_NEGATIVE_EMOTION_GIF,
  387. talking: FALLBACK_REGISTERED_TALKING_GIF,
  388. },
  389. });
  390. const resolveAvatarGifSet = useCallback(
  391. (avatarId: string | null | undefined): AvatarGifSet => {
  392. const fallback = avatarGifSets[AVATAR_ID_DEFAULT];
  393. if (!avatarId) {
  394. console.log(`[resolveAvatarGifSet] avatarId 为 null,返回默认形象`);
  395. return fallback;
  396. }
  397. const set = avatarGifSets[avatarId];
  398. const result = {
  399. default: set?.default || fallback.default,
  400. positive: set?.positive || fallback.positive,
  401. negative: set?.negative || fallback.negative,
  402. talking: set?.talking || fallback.talking,
  403. };
  404. console.log(`[resolveAvatarGifSet] avatarId=${avatarId}, set存在=${!!set}, result=`, result);
  405. return result;
  406. },
  407. [avatarGifSets],
  408. );
  409. const avatarGifSet = useMemo(() => {
  410. const gifSet = resolveAvatarGifSet(currentAvatarId);
  411. console.log(`[avatarGifSet] currentAvatarId=${currentAvatarId}, gifSet=`, gifSet);
  412. return gifSet;
  413. }, [currentAvatarId, resolveAvatarGifSet]);
  414. const [currentAvatarGif, setCurrentAvatarGif] = useState<string>(() => {
  415. const storedId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  416. return resolveAvatarGifSet(storedId).default;
  417. });
  418. const setGifByState = useCallback(
  419. (state: AvatarState) => {
  420. // 实时获取最新的 avatarGifSet,避免闭包问题
  421. const latestAvatarId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  422. // 使用最新的 avatarGifSets 状态来计算 GIF
  423. const fallback = avatarGifSets[AVATAR_ID_DEFAULT];
  424. let latestGifSet: AvatarGifSet;
  425. if (!latestAvatarId) {
  426. latestGifSet = fallback;
  427. } else {
  428. const set = avatarGifSets[latestAvatarId];
  429. latestGifSet = {
  430. default: set?.default || fallback.default,
  431. positive: set?.positive || fallback.positive,
  432. negative: set?.negative || fallback.negative,
  433. talking: set?.talking || fallback.talking,
  434. };
  435. }
  436. const nextGif = latestGifSet[state] || latestGifSet.default;
  437. console.log(`[setGifByState] state=${state}, currentAvatarId=${currentAvatarId}, latestAvatarId=${latestAvatarId}, nextGif=${nextGif}`);
  438. setCurrentAvatarGif(nextGif);
  439. },
  440. [avatarGifSet, currentAvatarId, avatarGifSets],
  441. );
  442. const showDefaultGif = useCallback(() => setGifByState('default'), [setGifByState]);
  443. const showPositiveGif = useCallback(() => setGifByState('positive'), [setGifByState]);
  444. const showNegativeGif = useCallback(() => setGifByState('negative'), [setGifByState]);
  445. const showTalkingGif = useCallback(() => setGifByState('talking'), [setGifByState]);
  446. // 预加载动画形象的 GIF
  447. const preloadAvatarGifs = useCallback(
  448. (avatarId: string | null) => {
  449. const gifSet = resolveAvatarGifSet(avatarId);
  450. const gifUrls = [gifSet.default, gifSet.positive, gifSet.negative, gifSet.talking];
  451. // 使用 Image 对象预加载所有 GIF
  452. gifUrls.forEach((url) => {
  453. if (url) {
  454. const img = new Image();
  455. img.src = url;
  456. // 静默处理加载错误,避免影响主流程
  457. img.onerror = () => {
  458. console.warn(`[预加载] GIF 加载失败: ${url}`);
  459. };
  460. }
  461. });
  462. console.log(`[预加载] 已开始预加载形象 ${avatarId || 'default'} 的所有 GIF`);
  463. },
  464. [resolveAvatarGifSet]
  465. );
  466. // 拉取头像 GIF 配置
  467. useEffect(() => {
  468. const fetchAvatarGifs = async () => {
  469. try {
  470. const res = await getAvatarGifs();
  471. if (!res.success || !res.data) {
  472. return;
  473. }
  474. setAvatarGifSets((prev) => {
  475. const next = { ...prev };
  476. const applyPartial = (key: string, partial?: Partial<AvatarGifSet>) => {
  477. if (!partial) return;
  478. const base = next[key] || prev[AVATAR_ID_DEFAULT];
  479. next[key] = {
  480. default: partial.default || base.default,
  481. positive: partial.positive || base.positive,
  482. negative: partial.negative || base.negative,
  483. talking: partial.talking || base.talking,
  484. };
  485. };
  486. applyPartial(AVATAR_ID_DEFAULT, res.data.avatar_default);
  487. applyPartial(AVATAR_ID_REGISTERED, res.data.avatar_registered_cap);
  488. return next;
  489. });
  490. } catch (error) {
  491. // 静默降级使用兜底
  492. console.warn('获取头像 GIF 配置失败,使用兜底资源', error);
  493. }
  494. };
  495. fetchAvatarGifs();
  496. }, []);
  497. // 当 avatarGifSets 更新或 currentAvatarId 变化时,预加载当前形象的 GIF
  498. useEffect(() => {
  499. if (Object.keys(avatarGifSets).length > 0) {
  500. // 如果 currentAvatarId 为 null,预加载默认形象
  501. const avatarIdToPreload = currentAvatarId || AVATAR_ID_DEFAULT;
  502. preloadAvatarGifs(avatarIdToPreload);
  503. }
  504. }, [avatarGifSets, currentAvatarId, preloadAvatarGifs]);
  505. // 监听 storage(跨标签页同步当前形象)
  506. useEffect(() => {
  507. const handleStorage = (event: StorageEvent) => {
  508. if (event.key === STORAGE_KEYS.CURRENT_AVATAR_ID) {
  509. setCurrentAvatarId(event.newValue);
  510. }
  511. if (event.key === STORAGE_KEYS.IS_LOGGED_IN && event.newValue === 'false') {
  512. // 游客回退默认形象
  513. setCurrentAvatarId(AVATAR_ID_DEFAULT);
  514. }
  515. };
  516. window.addEventListener('storage', handleStorage);
  517. return () => {
  518. window.removeEventListener('storage', handleStorage);
  519. };
  520. }, []);
  521. // 监听路由变化,同步 localStorage 中的 currentAvatarId(处理从其他页面返回的情况)
  522. useEffect(() => {
  523. if (location.pathname === '/chatbot') {
  524. const storedAvatarId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  525. // 只有当 localStorage 中的值与当前状态不一致时才更新
  526. if (storedAvatarId !== currentAvatarId) {
  527. console.log(`[ChatBotPage] 检测到 localStorage 中的形象ID变化: ${currentAvatarId} -> ${storedAvatarId}`);
  528. setCurrentAvatarId(storedAvatarId);
  529. // 立即更新显示的 GIF,使用最新的 avatarGifSets
  530. const newGifSet = resolveAvatarGifSet(storedAvatarId);
  531. console.log(`[ChatBotPage] 更新显示的 GIF: ${newGifSet.default}`);
  532. setCurrentAvatarGif(newGifSet.default);
  533. }
  534. }
  535. // eslint-disable-next-line react-hooks/exhaustive-deps
  536. }, [location.pathname]);
  537. // 当形象变化时立即更新显示 GIF
  538. useEffect(() => {
  539. showDefaultGif();
  540. }, [avatarGifSet, showDefaultGif]);
  541. const syncUserLevel = useCallback((level: number) => {
  542. setUserLevel(level);
  543. setUserLevelValue(level);
  544. }, []);
  545. const unlockAvatarC = useCallback(() => {
  546. if (avatarCUnlockedRef.current) {
  547. return;
  548. }
  549. avatarCUnlockedRef.current = true;
  550. setIsAvatarCUnlocked(true);
  551. setStorage(STORAGE_KEYS.AVATAR_C_UNLOCKED, 'true');
  552. const userId = getUserId();
  553. if (userId) {
  554. updateUserUIState(userId, {
  555. avatar_c_unlocked: true,
  556. }).catch(() => {
  557. // 同步失败不影响主流程
  558. });
  559. }
  560. }, []);
  561. const registerSpeechDuration = useCallback(
  562. (durationMs: number) => {
  563. if (durationMs <= 0) {
  564. return;
  565. }
  566. totalSpeechDurationRef.current += durationMs;
  567. if (
  568. !avatarCUnlockedRef.current &&
  569. totalSpeechDurationRef.current >= AVATAR_C_UNLOCK_THRESHOLD_MS
  570. ) {
  571. unlockAvatarC();
  572. }
  573. },
  574. [unlockAvatarC]
  575. );
  576. const emitStoneEvent = useCallback(
  577. (stoneId: string, event: 'click' | 'timeout', position: EnergyStoneEventPayload) => {
  578. if (!wsRef.current) {
  579. return;
  580. }
  581. wsRef.current.send({
  582. type: 'energy_stone_event',
  583. requestId: stoneId,
  584. stoneId,
  585. event,
  586. clientTs: Date.now(),
  587. position,
  588. });
  589. },
  590. []
  591. );
  592. const resolveStoneSize = useCallback(
  593. (_stoneType: 'mission' | 'conversation') => {
  594. // 设计稿统一尺寸 48px,基于 vw/vh 最小值
  595. return 48;
  596. },
  597. []
  598. );
  599. // 创建一个隐藏的容器元素用于 useEnergyStones hook 计算位置
  600. const hiddenEnergyStoneZoneRef = useRef<HTMLDivElement>(null);
  601. const { stones: energyStones, spawnStone, handleStoneClick } = useEnergyStones(hiddenEnergyStoneZoneRef, {
  602. onEvent: emitStoneEvent,
  603. resolveStoneSize,
  604. });
  605. // 当能量石开始消失时,设置动画的起点和终点(处理超时自动消失的情况)
  606. useEffect(() => {
  607. const timers: number[] = [];
  608. const resetAnimation = (element?: HTMLElement | null) => {
  609. if (!element) {
  610. return;
  611. }
  612. element.style.animation = 'none';
  613. // 强制重排,确保重新读取起点坐标
  614. void element.offsetWidth;
  615. element.style.animation = '';
  616. };
  617. energyStones.forEach((stone) => {
  618. const container = document.querySelector(
  619. `[data-stone-id="${stone.stoneId}"]`
  620. ) as HTMLElement | null;
  621. // 非消失状态,移除就绪标记,避免错误启动动画
  622. if (!stone.fading) {
  623. container?.removeAttribute('data-anim-ready');
  624. return;
  625. }
  626. // 使用 setTimeout 确保 DOM 已更新
  627. const timer = window.setTimeout(() => {
  628. const target = document.querySelector(
  629. `[data-stone-id="${stone.stoneId}"]`
  630. ) as HTMLElement | null;
  631. if (!target) {
  632. return;
  633. }
  634. const rect = target.getBoundingClientRect();
  635. const startLeft = rect.left + rect.width / 2; // 中心点 x 坐标(像素)
  636. const startTop = rect.top + rect.height / 2; // 中心点 y 坐标(像素)
  637. // 计算终点位置(动画形象中心,距离顶部60%位置)
  638. const viewportWidth = window.innerWidth;
  639. const viewportHeight = window.innerHeight;
  640. // 计算终点 top 位置(像素)
  641. const endTopPx = Math.min(
  642. (452 * Math.min(viewportWidth, 600) / 375),
  643. (452 / 812) * viewportHeight
  644. );
  645. const endLeft = viewportWidth / 2; // 50% 位置(像素)
  646. const dx = endLeft - startLeft;
  647. const dy = endTopPx - startTop;
  648. // 设置 CSS 变量,用于动画(使用像素值)
  649. target.style.setProperty('--animation-start-left', `${startLeft}px`);
  650. target.style.setProperty('--animation-start-top', `${startTop}px`);
  651. target.style.setProperty('--animation-end-left', `${endLeft}px`);
  652. target.style.setProperty('--animation-end-top', `${endTopPx}px`);
  653. target.style.setProperty('--animation-dx', `${dx}px`);
  654. target.style.setProperty('--animation-dy', `${dy}px`);
  655. // 强制写入动画样式,避免某些终端上样式覆盖/未生效
  656. target.style.left = `${startLeft}px`;
  657. target.style.top = `${startTop}px`;
  658. target.style.transform = 'translate(-50%, -50%) translate3d(0, 0, 0)';
  659. target.style.animation = 'energyStoneMoveToAvatar 1s ease-in-out forwards';
  660. target.style.willChange = 'transform, opacity';
  661. target.style.position = 'fixed';
  662. target.style.pointerEvents = 'none';
  663. runStoneAnimation(target, dx, dy);
  664. // 标记动画就绪,允许 CSS 启动
  665. target.setAttribute('data-anim-ready', 'true');
  666. // 重新启动动画,确保使用更新后的起点
  667. resetAnimation(target);
  668. resetAnimation(target.querySelector('.energy-stone') as HTMLElement | null);
  669. resetAnimation(target.querySelector('.energy-stone-hint') as HTMLElement | null);
  670. }, 0);
  671. timers.push(timer);
  672. });
  673. return () => {
  674. timers.forEach((timer) => {
  675. window.clearTimeout(timer);
  676. });
  677. };
  678. }, [energyStones, runStoneAnimation]);
  679. const startStoneAnimationAndRemove = useCallback(
  680. (stoneId: string) => {
  681. const container = document.querySelector(
  682. `[data-stone-id="${stoneId}"]`
  683. ) as HTMLElement | null;
  684. if (container) {
  685. const rect = container.getBoundingClientRect();
  686. const startLeft = rect.left + rect.width / 2;
  687. const startTop = rect.top + rect.height / 2;
  688. const viewportWidth = window.innerWidth;
  689. const viewportHeight = window.innerHeight;
  690. const endTopPx = Math.min(
  691. (452 * Math.min(viewportWidth, 600) / 375),
  692. (452 / 812) * viewportHeight
  693. );
  694. const endLeft = viewportWidth / 2;
  695. const dx = endLeft - startLeft;
  696. const dy = endTopPx - startTop;
  697. container.style.setProperty('--animation-start-left', `${startLeft}px`);
  698. container.style.setProperty('--animation-start-top', `${startTop}px`);
  699. container.style.setProperty('--animation-end-left', `${endLeft}px`);
  700. container.style.setProperty('--animation-end-top', `${endTopPx}px`);
  701. container.style.setProperty('--animation-dx', `${dx}px`);
  702. container.style.setProperty('--animation-dy', `${dy}px`);
  703. container.style.left = `${startLeft}px`;
  704. container.style.top = `${startTop}px`;
  705. container.style.transform = 'translate(-50%, -50%) translate3d(0, 0, 0)';
  706. container.style.animation = 'energyStoneMoveToAvatar 1s ease-in-out forwards';
  707. container.style.willChange = 'transform, opacity';
  708. container.style.position = 'fixed';
  709. container.style.pointerEvents = 'none';
  710. runStoneAnimation(container, dx, dy);
  711. }
  712. handleStoneClick(stoneId);
  713. },
  714. [handleStoneClick, runStoneAnimation]
  715. );
  716. const handleMissionStoneModalClose = useCallback(() => {
  717. setShowMissionStoneModal(false);
  718. const pendingStoneId = pendingStoneIdRef.current;
  719. if (pendingStoneId) {
  720. pendingStoneIdRef.current = null;
  721. startStoneAnimationAndRemove(pendingStoneId);
  722. }
  723. }, [startStoneAnimationAndRemove]);
  724. // 能量石展示区域:宽高 & 位置按设计稿 375×812 固定,以 px2vw/px2vh/px2vmin 适配
  725. const stopRecordingTimer = useCallback((options?: { preserveCountdown?: boolean }) => {
  726. if (recordingTimerRef.current) {
  727. clearInterval(recordingTimerRef.current);
  728. recordingTimerRef.current = null;
  729. }
  730. recordingDeadlineRef.current = 0;
  731. if (!options?.preserveCountdown) {
  732. setRemainingTimeMs(null);
  733. }
  734. }, []);
  735. const startRecordingTimer = useCallback(() => {
  736. stopRecordingTimer();
  737. recordingDeadlineRef.current = Date.now() + MAX_RECORDING_DURATION_MS;
  738. setRemainingTimeMs(MAX_RECORDING_DURATION_MS);
  739. recordingTimerRef.current = window.setInterval(() => {
  740. const remaining = recordingDeadlineRef.current - Date.now();
  741. if (remaining <= 0) {
  742. setRemainingTimeMs(0);
  743. stopRecordingTimer({ preserveCountdown: true });
  744. if (useAppStore.getState().currentState === 'recording') {
  745. handleSendRef.current?.();
  746. }
  747. return;
  748. }
  749. setRemainingTimeMs(remaining);
  750. }, 250);
  751. }, [stopRecordingTimer]);
  752. useEffect(() => {
  753. return () => {
  754. stopRecordingTimer();
  755. };
  756. }, [stopRecordingTimer]);
  757. useEffect(() => {
  758. if (currentState !== 'recording') {
  759. stopRecordingTimer();
  760. }
  761. }, [currentState, stopRecordingTimer]);
  762. const formatCountdown = (ms: number) => {
  763. const totalSeconds = Math.max(0, Math.ceil(ms / 1000));
  764. const minutes = Math.floor(totalSeconds / 60);
  765. const seconds = totalSeconds % 60;
  766. return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
  767. };
  768. // 进度动画效果(支持多圈连续动画)
  769. useEffect(() => {
  770. const targetTotal = progressLoopCount * 100 + conversationProgress;
  771. // 🔍 日志:记录 useEffect 触发时的状态
  772. console.log('[Progress] [useEffect] 触发:', {
  773. conversationProgress,
  774. progressLoopCount,
  775. targetTotal,
  776. prevProgressStateRef: { ...prevProgressStateRef.current },
  777. totalProgressRef: totalProgressRef.current,
  778. displayProgress,
  779. shouldSkipAnimation: shouldSkipProgressAnimationRef.current,
  780. hasActiveAnimation: !!animationFrameRef.current,
  781. });
  782. // 🔧 如果 shouldSkipProgressAnimation 为 true,说明处于初始化/同步阶段
  783. // 直接同步 ref 并返回,避免播放历史动画。标志由后续用户动作(如 handleSend)再置为 false。
  784. if (shouldSkipProgressAnimationRef.current) {
  785. console.log('[Progress] [useEffect] 初始化/同步阶段,跳过动画并同步 ref');
  786. if (animationFrameRef.current) {
  787. cancelAnimationFrame(animationFrameRef.current);
  788. animationFrameRef.current = undefined;
  789. }
  790. prevProgressStateRef.current = {
  791. progress: conversationProgress,
  792. loopCount: progressLoopCount,
  793. };
  794. totalProgressRef.current = targetTotal;
  795. const normalized = ((targetTotal % 100) + 100) % 100;
  796. setDisplayProgress(normalized);
  797. return;
  798. }
  799. // 使用上一次保存的状态值作为起始值(确保动画起始值正确)
  800. // 这样可以避免 displayProgress 异步更新导致的时序问题
  801. let prevTotal = prevProgressStateRef.current.loopCount * 100 + prevProgressStateRef.current.progress;
  802. // 立即保存当前状态到 prevProgressStateRef(供下次动画使用)
  803. // 这样即使本次动画还没完成,新的状态更新也能正确工作
  804. const currentState = {
  805. progress: conversationProgress,
  806. loopCount: progressLoopCount,
  807. };
  808. // 如果 prevTotal 是初始值 0,但目标值已经大于 0,说明是首次使用或状态被重置
  809. // 此时应该使用 totalProgressRef.current 或 displayProgress 作为起始值
  810. // 但如果 shouldSkipProgressAnimation 为 true,说明是初始化,应该直接使用目标值作为起始值
  811. if (prevTotal === 0 && targetTotal > 0) {
  812. if (shouldSkipProgressAnimationRef.current) {
  813. // 初始化场景:直接使用目标值作为起始值,避免播放动画
  814. console.log('[Progress] [useEffect] 初始化场景,使用目标值作为起始值,跳过动画');
  815. prevTotal = targetTotal;
  816. prevProgressStateRef.current = currentState;
  817. totalProgressRef.current = targetTotal;
  818. } else {
  819. console.log('[Progress] [useEffect] 检测到初始值情况,使用 displayProgress 或 totalProgressRef');
  820. // 如果 totalProgressRef 有值,使用它;否则使用 displayProgress
  821. if (totalProgressRef.current > 0) {
  822. prevTotal = totalProgressRef.current;
  823. console.log('[Progress] [useEffect] 使用 totalProgressRef:', totalProgressRef.current);
  824. } else {
  825. prevTotal = displayProgress;
  826. console.log('[Progress] [useEffect] 使用 displayProgress:', displayProgress);
  827. }
  828. // 更新 prevProgressStateRef 为当前显示的状态
  829. prevProgressStateRef.current = {
  830. progress: displayProgress,
  831. loopCount: Math.floor(totalProgressRef.current / 100),
  832. };
  833. console.log('[Progress] [useEffect] 更新 prevProgressStateRef 为:', prevProgressStateRef.current);
  834. }
  835. }
  836. // 始终使用上一次保存的状态作为起始值,而不是 totalProgressRef.current
  837. // 因为 totalProgressRef.current 可能在动画过程中,不准确
  838. let startTotal = prevTotal;
  839. // 如果 totalProgressRef 与上一次状态不一致,同步它
  840. // 这样可以确保 totalProgressRef 与起始值一致,避免动画跳变
  841. if (!shouldSkipProgressAnimationRef.current) {
  842. const diffFromPrev = Math.abs(totalProgressRef.current - prevTotal);
  843. console.log('[Progress] [useEffect] 检查 totalProgressRef 同步:', {
  844. diffFromPrev,
  845. totalProgressRef: totalProgressRef.current,
  846. prevTotal,
  847. needSync: diffFromPrev > 1,
  848. });
  849. // 如果差异较大(超过1%),说明可能有动画在进行,需要取消它并同步起始值
  850. if (diffFromPrev > 1) {
  851. console.log('[Progress] [useEffect] 取消旧动画并同步 totalProgressRef');
  852. // 取消可能正在进行的动画
  853. if (animationFrameRef.current) {
  854. cancelAnimationFrame(animationFrameRef.current);
  855. animationFrameRef.current = undefined;
  856. }
  857. // 同步 totalProgressRef 到起始值
  858. totalProgressRef.current = startTotal;
  859. }
  860. }
  861. // 立即更新 prevProgressStateRef 为当前状态(供下次动画使用)
  862. // 注意:这里保存的是本次动画的目标值,下次动画会使用这个值作为起始值
  863. prevProgressStateRef.current = currentState;
  864. const diff = targetTotal - startTotal;
  865. console.log('[Progress] [useEffect] 计算动画参数:', {
  866. startTotal,
  867. targetTotal,
  868. diff,
  869. willAnimate: Math.abs(diff) >= 0.01 && !shouldSkipProgressAnimationRef.current,
  870. });
  871. // 如果 diff 为 0 或非常小,直接同步,不播放动画
  872. if (Math.abs(diff) < 0.01) {
  873. console.log('[Progress] [useEffect] 跳过动画(diff 太小):', {
  874. diff,
  875. startTotal,
  876. targetTotal,
  877. });
  878. totalProgressRef.current = targetTotal;
  879. const normalized = ((targetTotal % 100) + 100) % 100;
  880. setDisplayProgress(normalized);
  881. // prevProgressStateRef 已经在上面更新了,这里不需要再次更新
  882. return;
  883. }
  884. // 处理整数圈:如果 diff 接近 0 且是整数圈,播放完整一圈动画
  885. let actualDiff = diff;
  886. if (Math.abs(diff) < 0.1) {
  887. // 判断是否是整数圈(目标值是 100 的整数倍)
  888. const isIntegerLoop = Math.abs(targetTotal % 100) < 0.1;
  889. if (isIntegerLoop && targetTotal > startTotal) {
  890. // 整数圈增长,播放完整一圈动画
  891. actualDiff = 100;
  892. } else if (isIntegerLoop && targetTotal < startTotal) {
  893. // 整数圈减少,播放完整一圈反向动画
  894. actualDiff = -100;
  895. } else {
  896. // 非整数圈的微小变化,直接同步
  897. totalProgressRef.current = targetTotal;
  898. const normalized = ((targetTotal % 100) + 100) % 100;
  899. setDisplayProgress(normalized);
  900. // prevProgressStateRef 已经在上面更新了,这里不需要再次更新
  901. return;
  902. }
  903. }
  904. // 每 1% 的进度约 8ms,限制在 0.4s~2.4s 之间
  905. const durationPerPercent = 8;
  906. const duration = Math.max(400, Math.min(2400, Math.abs(actualDiff) * durationPerPercent));
  907. const startTime = performance.now();
  908. console.log('[Progress] [useEffect] 开始动画:', {
  909. startTotal,
  910. targetTotal,
  911. actualDiff,
  912. duration,
  913. startTime,
  914. });
  915. if (animationFrameRef.current) {
  916. console.log('[Progress] [useEffect] 取消旧动画');
  917. cancelAnimationFrame(animationFrameRef.current);
  918. }
  919. let animationFrameCount = 0;
  920. const animate = (currentTime: number) => {
  921. animationFrameCount++;
  922. const elapsed = currentTime - startTime;
  923. const progress = Math.min(elapsed / duration, 1);
  924. // easeOutCubic 缓动
  925. const easeProgress = 1 - Math.pow(1 - progress, 3);
  926. const currentTotal = startTotal + actualDiff * easeProgress;
  927. totalProgressRef.current = currentTotal;
  928. const normalized = ((currentTotal % 100) + 100) % 100;
  929. setDisplayProgress(normalized);
  930. // 🔍 日志:每10帧记录一次动画进度
  931. if (animationFrameCount % 10 === 0 || progress >= 1) {
  932. console.log('[Progress] [animate] 动画进度:', {
  933. frame: animationFrameCount,
  934. progress: (progress * 100).toFixed(2) + '%',
  935. currentTotal: currentTotal.toFixed(2),
  936. normalized: normalized.toFixed(2),
  937. elapsed: elapsed.toFixed(2) + 'ms',
  938. });
  939. }
  940. if (progress < 1) {
  941. animationFrameRef.current = requestAnimationFrame(animate);
  942. } else {
  943. // 动画完成后,同步到实际的目标值
  944. console.log('[Progress] [animate] 动画完成:', {
  945. finalTotal: targetTotal,
  946. finalNormalized: ((targetTotal % 100) + 100) % 100,
  947. totalFrames: animationFrameCount,
  948. });
  949. totalProgressRef.current = targetTotal;
  950. const finalNormalized = ((targetTotal % 100) + 100) % 100;
  951. setDisplayProgress(finalNormalized);
  952. // prevProgressStateRef 已经在 useEffect 开始时更新了,这里不需要再次更新
  953. }
  954. };
  955. animationFrameRef.current = requestAnimationFrame(animate);
  956. return () => {
  957. if (animationFrameRef.current) {
  958. cancelAnimationFrame(animationFrameRef.current);
  959. }
  960. };
  961. }, [conversationProgress, progressLoopCount]);
  962. // 控制声纹容器高度变化
  963. useEffect(() => {
  964. // 清除之前的定时器
  965. if (waveformCollapseTimerRef.current) {
  966. clearTimeout(waveformCollapseTimerRef.current);
  967. waveformCollapseTimerRef.current = null;
  968. }
  969. if (currentState === 'recording' || currentState === 'playing') {
  970. // 录音或播放时,立即展开
  971. setShouldExpandWaveform(true);
  972. } else {
  973. // 其他状态(sending/idle 等),延迟 3 秒后再收起(等待衰减动画完成)
  974. waveformCollapseTimerRef.current = window.setTimeout(() => {
  975. setShouldExpandWaveform(false);
  976. }, 3000); // 3秒足够让所有竖条衰减为静态高度
  977. }
  978. return () => {
  979. if (waveformCollapseTimerRef.current) {
  980. clearTimeout(waveformCollapseTimerRef.current);
  981. }
  982. };
  983. }, [currentState]);
  984. // 监听录音状态,首次显示取消提示(3秒后自动隐藏)
  985. useEffect(() => {
  986. if (currentState === 'recording') {
  987. const hasShownCancel = getStorage(STORAGE_KEYS.HAS_SHOWN_CANCEL_TOOLTIP);
  988. if (!hasShownCancel) {
  989. setShowCancelTooltip(true);
  990. setStorage(STORAGE_KEYS.HAS_SHOWN_CANCEL_TOOLTIP, 'true');
  991. const userId = getUserId();
  992. if (userId) {
  993. updateUserUIState(userId, {
  994. has_shown_cancel_tooltip: true,
  995. }).catch(() => {
  996. // 同步失败不影响主流程
  997. });
  998. }
  999. // 🔧 调试模式:注释掉自动隐藏,方便调试
  1000. // 3秒后自动隐藏
  1001. // const timer = setTimeout(() => {
  1002. // setShowCancelTooltip(false);
  1003. // }, 3000);
  1004. // return () => clearTimeout(timer);
  1005. } else {
  1006. // 已显示过,确保隐藏
  1007. setShowCancelTooltip(false);
  1008. }
  1009. } else {
  1010. // 非录音状态,隐藏提示
  1011. setShowCancelTooltip(false);
  1012. }
  1013. }, [currentState]);
  1014. // 监听录音状态,首次显示发送提示(3秒后自动隐藏)
  1015. useEffect(() => {
  1016. if (currentState === 'recording') {
  1017. const hasShownSend = getStorage(STORAGE_KEYS.HAS_SHOWN_SEND_TOOLTIP);
  1018. if (!hasShownSend) {
  1019. setShowSendTooltip(true);
  1020. setStorage(STORAGE_KEYS.HAS_SHOWN_SEND_TOOLTIP, 'true');
  1021. const userId = getUserId();
  1022. if (userId) {
  1023. updateUserUIState(userId, {
  1024. has_shown_send_tooltip: true,
  1025. }).catch(() => {
  1026. // 同步失败不影响主流程
  1027. });
  1028. }
  1029. // 🔧 调试模式:注释掉自动隐藏,方便调试
  1030. // 3秒后自动隐藏
  1031. // const timer = setTimeout(() => {
  1032. // setShowSendTooltip(false);
  1033. // }, 3000);
  1034. // return () => clearTimeout(timer);
  1035. } else {
  1036. // 已显示过,确保隐藏
  1037. setShowSendTooltip(false);
  1038. }
  1039. } else {
  1040. // 非录音状态,隐藏提示
  1041. setShowSendTooltip(false);
  1042. }
  1043. }, [currentState]);
  1044. const dismissProgressBarTooltip = useCallback(() => {
  1045. if (progressBarTooltipTimerRef.current) {
  1046. clearTimeout(progressBarTooltipTimerRef.current);
  1047. progressBarTooltipTimerRef.current = null;
  1048. }
  1049. if (progressBarTooltipRef.current) {
  1050. progressBarTooltipRef.current.style.opacity = '0';
  1051. progressBarTooltipRef.current.style.pointerEvents = 'none';
  1052. progressBarTooltipRef.current.style.display = 'none';
  1053. }
  1054. setShowProgressBarTooltip(false);
  1055. }, []);
  1056. useEffect(() => {
  1057. if (showProgressBarTooltip && progressBarTooltipRef.current) {
  1058. progressBarTooltipRef.current.style.display = 'block';
  1059. progressBarTooltipRef.current.style.opacity = '1';
  1060. progressBarTooltipRef.current.style.pointerEvents = 'none';
  1061. }
  1062. }, [showProgressBarTooltip]);
  1063. // 监听进度条变化,首次有进度时显示进度条提示(只出现一次)
  1064. useEffect(() => {
  1065. if (
  1066. displayProgress > 0 &&
  1067. !hasShownProgressBarTooltip &&
  1068. isConnected &&
  1069. currentState !== 'recording'
  1070. ) {
  1071. setShowProgressBarTooltip(true);
  1072. setStorage(STORAGE_KEYS.HAS_SHOWN_PROGRESS_BAR_TOOLTIP, 'true');
  1073. setHasShownProgressBarTooltip(true);
  1074. const userId = getUserId();
  1075. if (userId) {
  1076. updateUserUIState(userId, {
  1077. has_shown_progress_bar_tooltip: true,
  1078. }).catch(() => {
  1079. // 同步失败不影响主流程
  1080. });
  1081. }
  1082. }
  1083. }, [displayProgress, hasShownProgressBarTooltip, isConnected, currentState]);
  1084. // 进度条提示显示后自动隐藏(2 秒)
  1085. useEffect(() => {
  1086. if (!showProgressBarTooltip) {
  1087. if (progressBarTooltipTimerRef.current) {
  1088. clearTimeout(progressBarTooltipTimerRef.current);
  1089. progressBarTooltipTimerRef.current = null;
  1090. }
  1091. return;
  1092. }
  1093. progressBarTooltipTimerRef.current = window.setTimeout(() => {
  1094. setShowProgressBarTooltip(false);
  1095. progressBarTooltipTimerRef.current = null;
  1096. }, 2000);
  1097. return () => {
  1098. if (progressBarTooltipTimerRef.current) {
  1099. clearTimeout(progressBarTooltipTimerRef.current);
  1100. progressBarTooltipTimerRef.current = null;
  1101. }
  1102. };
  1103. }, [showProgressBarTooltip]);
  1104. // 录音状态下强制隐藏进度条提示,避免在用户开始录音后残留
  1105. useEffect(() => {
  1106. if (currentState === 'recording') {
  1107. dismissProgressBarTooltip();
  1108. }
  1109. }, [currentState, dismissProgressBarTooltip]);
  1110. // 监听屏幕尺寸变化
  1111. useEffect(() => {
  1112. // 初始化时更新标记
  1113. updateTooSmallFlag();
  1114. const cleanup = onScreenResize(() => {
  1115. const newLayout = calculateLayout();
  1116. setLayout(newLayout); // V2: 更新完全计算式布局
  1117. // 更新 data-too-small 标记
  1118. updateTooSmallFlag();
  1119. });
  1120. return cleanup;
  1121. }, []);
  1122. // 同步背景图片状态的函数
  1123. const syncBackgroundImageState = useCallback(() => {
  1124. const bgUrl = getCurrentBackgroundImageUrl();
  1125. const bgId = getCurrentBackgroundImageId();
  1126. // 直接更新状态,React 会自动处理重复更新
  1127. setBackgroundImageUrl(bgUrl);
  1128. setBackgroundImageId(bgId);
  1129. }, []);
  1130. // 初始化背景图片(从 localStorage 读取)
  1131. useEffect(() => {
  1132. const bgUrl = getCurrentBackgroundImageUrl();
  1133. const bgId = getCurrentBackgroundImageId();
  1134. if (bgUrl) {
  1135. setBackgroundImageUrl(bgUrl);
  1136. }
  1137. if (bgId) {
  1138. setBackgroundImageId(bgId);
  1139. }
  1140. if (!bgUrl || !bgId) {
  1141. // 如果没有背景图片,尝试从用户信息同步
  1142. const user_id = getUserId();
  1143. if (user_id) {
  1144. getUserInfo(user_id).then((userInfo) => {
  1145. if (userInfo.success && userInfo.data?.current_background_image_id) {
  1146. syncBackgroundImageToStorage(
  1147. userInfo.data.current_background_image_id,
  1148. userInfo.data.current_background_image_metadata
  1149. );
  1150. const newBgUrl = getCurrentBackgroundImageUrl();
  1151. const newBgId = getCurrentBackgroundImageId();
  1152. if (newBgUrl) {
  1153. setBackgroundImageUrl(newBgUrl);
  1154. }
  1155. if (newBgId) {
  1156. setBackgroundImageId(newBgId);
  1157. }
  1158. }
  1159. }).catch(() => {
  1160. // 静默处理错误
  1161. });
  1162. }
  1163. }
  1164. }, []);
  1165. // 能量石点击后直接消失,不再需要监听 fading 状态
  1166. // 监听 localStorage 中背景图片的变化
  1167. useEffect(() => {
  1168. // 监听 storage 事件(其他标签页/窗口的变化)
  1169. const handleStorageChange = (e: StorageEvent) => {
  1170. if (e.key === STORAGE_KEYS.CURRENT_BACKGROUND_IMAGE_ID ||
  1171. e.key === STORAGE_KEYS.CURRENT_BACKGROUND_IMAGE_URL) {
  1172. syncBackgroundImageState();
  1173. }
  1174. };
  1175. // 监听页面可见性变化(当页面重新获得焦点时检查)
  1176. const handleVisibilityChange = () => {
  1177. if (!document.hidden) {
  1178. syncBackgroundImageState();
  1179. }
  1180. };
  1181. // 定期检查背景图片变化(作为后备机制,每5秒检查一次)
  1182. const intervalId = setInterval(() => {
  1183. syncBackgroundImageState();
  1184. }, 5000);
  1185. window.addEventListener('storage', handleStorageChange);
  1186. document.addEventListener('visibilitychange', handleVisibilityChange);
  1187. return () => {
  1188. window.removeEventListener('storage', handleStorageChange);
  1189. document.removeEventListener('visibilitychange', handleVisibilityChange);
  1190. clearInterval(intervalId);
  1191. };
  1192. }, [syncBackgroundImageState]);
  1193. // 处理OAuth登录回调(QQ登录成功后直接跳转到chatbot)
  1194. useEffect(() => {
  1195. const oauth = searchParams.get('oauth');
  1196. const userId = searchParams.get('user_id');
  1197. const usernameParam = searchParams.get('username');
  1198. if (oauth === 'qq' && userId && usernameParam) {
  1199. // QQ登录成功,保存用户信息
  1200. setStorage(STORAGE_KEYS.USER_ID, userId);
  1201. setStorage(STORAGE_KEYS.USER_NAME, decodeURIComponent(usernameParam));
  1202. antMessage.success('QQ登录成功');
  1203. // 清除URL参数
  1204. navigate('/chatbot', { replace: true });
  1205. }
  1206. }, [searchParams, navigate]);
  1207. // 初始化
  1208. useEffect(() => {
  1209. // 🔧 立即设置 shouldSkipProgressAnimation = true,避免在异步加载完成前触发动画
  1210. // 这可以防止从其他页面返回时播放历史动画
  1211. shouldSkipProgressAnimationRef.current = true;
  1212. console.log('[Progress] [初始化] 组件挂载,设置 shouldSkipProgressAnimation = true');
  1213. // 记录用户当前所在页面
  1214. setStorage(STORAGE_KEYS.CURRENT_PAGE, 'chatbot');
  1215. // 记录路由访问
  1216. markRouteVisited('/chatbot');
  1217. const visited = getVisitedRoutes();
  1218. const userId = getUserId();
  1219. if (userId) {
  1220. // 从数据库加载所有用户信息到 localStorage
  1221. // 1. 先加载用户基本信息(getUserInfo)
  1222. // 2. 再加载用户 UI 状态(getUserUIState)
  1223. Promise.all([
  1224. getUserInfo(userId).catch(() => null),
  1225. getUserUIState(userId).catch(() => null),
  1226. ]).then(([userInfoResult, uiStateResult]) => {
  1227. // 加载用户基本信息到 localStorage
  1228. if (userInfoResult?.success && userInfoResult.data) {
  1229. const userInfo = userInfoResult.data;
  1230. // Lyrria_user_name
  1231. if (userInfo.username) {
  1232. setUserName(userInfo.username);
  1233. }
  1234. // Lyrria_lumi_name
  1235. if (userInfo.bot_name) {
  1236. setStorage(STORAGE_KEYS.LUMI_NAME, userInfo.bot_name);
  1237. }
  1238. // Lyrria_user_level
  1239. if (typeof userInfo.user_level === 'number') {
  1240. syncUserLevel(userInfo.user_level);
  1241. }
  1242. // Lyrria_is_logged_in (根据 user_type 判断:registered 为已登录)
  1243. if (userInfo.user_type === 'registered') {
  1244. setStorage(STORAGE_KEYS.IS_LOGGED_IN, 'true');
  1245. } else {
  1246. setStorage(STORAGE_KEYS.IS_LOGGED_IN, 'false');
  1247. }
  1248. // Lyrria_current_background_image_id, Lyrria_current_background_image_url, Lyrria_current_background_image_opacity
  1249. if (userInfo.current_background_image_id) {
  1250. syncBackgroundImageToStorage(
  1251. userInfo.current_background_image_id,
  1252. userInfo.current_background_image_metadata
  1253. );
  1254. const bgUrl = getCurrentBackgroundImageUrl();
  1255. const bgId = getCurrentBackgroundImageId();
  1256. if (bgUrl) {
  1257. setBackgroundImageUrl(bgUrl);
  1258. }
  1259. if (bgId) {
  1260. setBackgroundImageId(bgId);
  1261. }
  1262. }
  1263. // 初始化进度条(基于 total_speech_duration_ms)
  1264. const totalDurationMs = userInfo.total_speech_duration_ms ?? 0;
  1265. if (totalDurationMs > 0) {
  1266. const PROGRESS_FULL_DURATION_MS = 30 * 1000; // 30000ms
  1267. totalSpeechDurationRef.current = Math.max(
  1268. totalDurationMs,
  1269. avatarCUnlockedRef.current ? AVATAR_C_UNLOCK_THRESHOLD_MS : 0
  1270. );
  1271. if (totalDurationMs >= AVATAR_C_UNLOCK_THRESHOLD_MS) {
  1272. unlockAvatarC();
  1273. }
  1274. const totalProgressPercent = (totalDurationMs / PROGRESS_FULL_DURATION_MS) * 100;
  1275. const initialProgress = totalProgressPercent % 100;
  1276. const initialLoopCount = Math.floor(totalProgressPercent / 100);
  1277. console.log('[Progress] [初始化] 设置 shouldSkipProgressAnimation = true (Promise.all)', {
  1278. totalDurationMs,
  1279. totalProgressPercent,
  1280. initialProgress,
  1281. initialLoopCount,
  1282. });
  1283. // 在设置进度之前,先同步 prevProgressStateRef 和 totalProgressRef,避免动画计算错误
  1284. prevProgressStateRef.current = {
  1285. progress: initialProgress,
  1286. loopCount: initialLoopCount,
  1287. };
  1288. totalProgressRef.current = initialLoopCount * 100 + initialProgress;
  1289. shouldSkipProgressAnimationRef.current = true;
  1290. // 设置正确的圈数,而不是重置为 0
  1291. setProgress(initialProgress, initialLoopCount);
  1292. }
  1293. }
  1294. // 加载用户 UI 状态到 localStorage
  1295. if (uiStateResult?.success && uiStateResult.data) {
  1296. const state = uiStateResult.data;
  1297. // Lyrria_current_avatar_id
  1298. if (state.current_avatar_id) {
  1299. setCurrentAvatarId(state.current_avatar_id);
  1300. setStorage(STORAGE_KEYS.CURRENT_AVATAR_ID, state.current_avatar_id);
  1301. }
  1302. // Lyrria_current_page
  1303. if (state.current_page) {
  1304. setStorage(STORAGE_KEYS.CURRENT_PAGE, state.current_page);
  1305. }
  1306. // Lyrria_visited_routes (无论是否为空数组,都从数据库同步)
  1307. if (Array.isArray(state.visited_routes)) {
  1308. setStorage(STORAGE_KEYS.VISITED_ROUTES, JSON.stringify(state.visited_routes));
  1309. } else if (state.visited_routes === null || state.visited_routes === undefined) {
  1310. // 如果数据库中是 null 或 undefined,设置为空数组
  1311. setStorage(STORAGE_KEYS.VISITED_ROUTES, JSON.stringify([]));
  1312. }
  1313. // Lyrria_has_shown_tap_to_chat
  1314. if (state.has_shown_tap_to_chat) {
  1315. setStorage(STORAGE_KEYS.HAS_SHOWN_TAP_TO_CHAT, 'true');
  1316. if (isConnected) {
  1317. setShowTapToChat(false);
  1318. }
  1319. }
  1320. // Lyrria_has_shown_cancel_tooltip
  1321. if (state.has_shown_cancel_tooltip) {
  1322. setStorage(STORAGE_KEYS.HAS_SHOWN_CANCEL_TOOLTIP, 'true');
  1323. setShowCancelTooltip(false);
  1324. }
  1325. // Lyrria_has_shown_send_tooltip
  1326. if (state.has_shown_send_tooltip) {
  1327. setStorage(STORAGE_KEYS.HAS_SHOWN_SEND_TOOLTIP, 'true');
  1328. setShowSendTooltip(false);
  1329. }
  1330. // Lyrria_has_shown_progress_bar_tooltip
  1331. if (state.has_shown_progress_bar_tooltip) {
  1332. setStorage(STORAGE_KEYS.HAS_SHOWN_PROGRESS_BAR_TOOLTIP, 'true');
  1333. setHasShownProgressBarTooltip(true);
  1334. }
  1335. // Lyrria_avatar_c_unlocked
  1336. if (state.avatar_c_unlocked && !avatarCUnlockedRef.current) {
  1337. avatarCUnlockedRef.current = true;
  1338. setIsAvatarCUnlocked(true);
  1339. setStorage(STORAGE_KEYS.AVATAR_C_UNLOCKED, 'true');
  1340. }
  1341. // Lyrria_has_shared_avatar (无论 true/false,都从数据库同步)
  1342. setStorage(STORAGE_KEYS.HAS_SHARED_AVATAR, state.has_shared_avatar ? 'true' : 'false');
  1343. setHasSharedAvatarState(state.has_shared_avatar);
  1344. // Lyrria_has_shown_mission_stone_modal
  1345. if (state.has_shown_mission_stone_modal) {
  1346. setStorage(STORAGE_KEYS.HAS_SHOWN_MISSION_STONE_MODAL, 'true');
  1347. }
  1348. // Lyrria_has_shown_conversation_stone_modal
  1349. if (state.has_shown_conversation_stone_modal) {
  1350. setStorage(STORAGE_KEYS.HAS_SHOWN_CONVERSATION_STONE_MODAL, 'true');
  1351. }
  1352. // Lyrria_has_clicked_mission_stone
  1353. if (state.has_clicked_mission_stone) {
  1354. setStorage(STORAGE_KEYS.HAS_CLICKED_MISSION_STONE, 'true');
  1355. setHasClickedMissionStone(true);
  1356. }
  1357. // Lyrria_has_clicked_conversation_stone
  1358. if (state.has_clicked_conversation_stone) {
  1359. setStorage(STORAGE_KEYS.HAS_CLICKED_CONVERSATION_STONE, 'true');
  1360. setHasClickedConversationStone(true);
  1361. }
  1362. // 补偿同步:如果本地记录为 true 但后端为 false,尝试补偿同步
  1363. const localMissionShown = getStorage(STORAGE_KEYS.HAS_SHOWN_MISSION_STONE_MODAL) === 'true';
  1364. if (localMissionShown && !state.has_shown_mission_stone_modal) {
  1365. updateUserUIState(userId, {
  1366. has_shown_mission_stone_modal: true,
  1367. }).catch(() => {
  1368. // 静默处理
  1369. });
  1370. }
  1371. const localConversationShown =
  1372. getStorage(STORAGE_KEYS.HAS_SHOWN_CONVERSATION_STONE_MODAL) === 'true';
  1373. if (localConversationShown && !state.has_shown_conversation_stone_modal) {
  1374. updateUserUIState(userId, {
  1375. has_shown_conversation_stone_modal: true,
  1376. }).catch(() => {
  1377. // 静默处理
  1378. });
  1379. }
  1380. const localMissionClicked = getStorage(STORAGE_KEYS.HAS_CLICKED_MISSION_STONE) === 'true';
  1381. if (localMissionClicked && !state.has_clicked_mission_stone) {
  1382. updateUserUIState(userId, {
  1383. has_clicked_mission_stone: true,
  1384. }).catch(() => {
  1385. // 静默处理
  1386. });
  1387. }
  1388. const localConversationClicked = getStorage(STORAGE_KEYS.HAS_CLICKED_CONVERSATION_STONE) === 'true';
  1389. if (localConversationClicked && !state.has_clicked_conversation_stone) {
  1390. updateUserUIState(userId, {
  1391. has_clicked_conversation_stone: true,
  1392. }).catch(() => {
  1393. // 静默处理
  1394. });
  1395. }
  1396. }
  1397. }).catch(() => {
  1398. // 加载失败不影响主流程
  1399. console.warn('[ChatBotPage] 从数据库加载用户信息失败');
  1400. });
  1401. // 同步当前页面与已访问路由
  1402. updateUserUIState(userId, {
  1403. current_page: 'chatbot',
  1404. visited_routes: visited,
  1405. }).catch(() => {
  1406. // 同步失败不影响主流程
  1407. });
  1408. }
  1409. const init = async () => {
  1410. try {
  1411. // 获取用户信息并初始化进度条(已在上面 Promise.all 中处理,这里保留作为兜底)
  1412. const user_id = getUserId();
  1413. if (user_id) {
  1414. try {
  1415. const userInfo = await getUserInfo(user_id);
  1416. if (userInfo.success && userInfo.data) {
  1417. // 同步用户名到 localStorage
  1418. if (userInfo.data.username) {
  1419. setUserName(userInfo.data.username);
  1420. }
  1421. // 同步机器人名字到 localStorage
  1422. if (userInfo.data.bot_name) {
  1423. setStorage(STORAGE_KEYS.LUMI_NAME, userInfo.data.bot_name);
  1424. }
  1425. // 同步用户等级
  1426. const totalDurationMs = userInfo.data.total_speech_duration_ms ?? 0;
  1427. if (typeof userInfo.data.user_level === 'number') {
  1428. syncUserLevel(userInfo.data.user_level);
  1429. }
  1430. // 同步背景图片
  1431. if (userInfo.data.current_background_image_id) {
  1432. syncBackgroundImageToStorage(
  1433. userInfo.data.current_background_image_id,
  1434. userInfo.data.current_background_image_metadata
  1435. );
  1436. const bgUrl = getCurrentBackgroundImageUrl();
  1437. const bgId = getCurrentBackgroundImageId();
  1438. if (bgUrl) {
  1439. setBackgroundImageUrl(bgUrl);
  1440. }
  1441. if (bgId) {
  1442. setBackgroundImageId(bgId);
  1443. }
  1444. }
  1445. if (totalDurationMs > 0) {
  1446. // 根据总说话时长计算初始进度(30秒=100%)
  1447. const PROGRESS_FULL_DURATION_MS = 30 * 1000; // 30000ms
  1448. totalSpeechDurationRef.current = Math.max(
  1449. totalDurationMs,
  1450. avatarCUnlockedRef.current ? AVATAR_C_UNLOCK_THRESHOLD_MS : 0
  1451. );
  1452. if (totalDurationMs >= AVATAR_C_UNLOCK_THRESHOLD_MS) {
  1453. unlockAvatarC();
  1454. }
  1455. // 计算总进度百分比和圈数
  1456. const totalProgressPercent = (totalDurationMs / PROGRESS_FULL_DURATION_MS) * 100;
  1457. const initialProgress = totalProgressPercent % 100;
  1458. const initialLoopCount = Math.floor(totalProgressPercent / 100);
  1459. console.log('[Progress] [初始化] 设置 shouldSkipProgressAnimation = true (init)', {
  1460. totalDurationMs,
  1461. totalProgressPercent,
  1462. initialProgress,
  1463. initialLoopCount,
  1464. });
  1465. // 在设置进度之前,先同步 prevProgressStateRef 和 totalProgressRef,避免动画计算错误
  1466. prevProgressStateRef.current = {
  1467. progress: initialProgress,
  1468. loopCount: initialLoopCount,
  1469. };
  1470. totalProgressRef.current = initialLoopCount * 100 + initialProgress;
  1471. shouldSkipProgressAnimationRef.current = true;
  1472. // 设置正确的圈数,而不是重置为 0
  1473. setProgress(initialProgress, initialLoopCount);
  1474. }
  1475. }
  1476. } catch (error) {
  1477. // 获取用户信息失败不影响主流程,静默处理
  1478. console.warn('获取用户信息失败:', error);
  1479. }
  1480. }
  1481. // 初始化录音器
  1482. const recorder = new AudioRecorder();
  1483. await recorder.initialize();
  1484. recorderRef.current = recorder;
  1485. // 初始化播放器
  1486. const player = new AudioPlayer();
  1487. await player.initialize();
  1488. player.onEnd(() => {
  1489. const requestId = useAppStore.getState().currentRequestId;
  1490. if (requestId) {
  1491. performanceLogger.recordPlaybackEnd(requestId);
  1492. }
  1493. // 进度条已基于用户说话时长更新,无需在播放完成后再次更新
  1494. // 清理情绪 GIF 切换定时器
  1495. if (emotionGifTimerRef.current) {
  1496. clearTimeout(emotionGifTimerRef.current);
  1497. emotionGifTimerRef.current = null;
  1498. }
  1499. // 恢复默认头像 GIF
  1500. showDefaultGif();
  1501. setState('idle');
  1502. reset();
  1503. });
  1504. playerRef.current = player;
  1505. // 初始化WebSocket(添加user_id查询参数)
  1506. const ws = new WebSocketManager();
  1507. const wsUrl = user_id ? `${WS_URL}?user_id=${encodeURIComponent(user_id)}` : WS_URL;
  1508. ws.connect(wsUrl, (connected) => {
  1509. // 如果组件已卸载,不执行任何操作
  1510. if (!isMountedRef.current) {
  1511. return;
  1512. }
  1513. const appState = useAppStore.getState();
  1514. const wasConnected = appState.isConnected;
  1515. setConnected(connected);
  1516. if (connected) {
  1517. // 记录WebSocket连接建立时间
  1518. performanceLogger.recordWebSocketConnected();
  1519. // 如果这是首次连接成功,处理 TapToChat 提示
  1520. if (!connectionLostRef.current) {
  1521. const hasShown = getStorage(STORAGE_KEYS.HAS_SHOWN_TAP_TO_CHAT);
  1522. if (!hasShown && showTapToChat) {
  1523. // 第一次连接成功,保存标记
  1524. setStorage(STORAGE_KEYS.HAS_SHOWN_TAP_TO_CHAT, 'true');
  1525. // 同步到后端 UI 状态(如果已有 user_id)
  1526. const userId = getUserId();
  1527. if (userId) {
  1528. updateUserUIState(userId, {
  1529. has_shown_tap_to_chat: true,
  1530. }).catch(() => {
  1531. // 同步失败不影响前端提示展示
  1532. });
  1533. }
  1534. } else if (hasShown) {
  1535. // 如果已经标记为已显示(可能是从后端同步的),现在 WebSocket 已连接,可以安全隐藏
  1536. setShowTapToChat(false);
  1537. }
  1538. }
  1539. if (connectionLostRef.current) {
  1540. antMessage.success('语音服务已恢复,可以继续对话');
  1541. connectionLostRef.current = false;
  1542. recorderRef.current?.stop();
  1543. resetFrequencyVisualization();
  1544. playerRef.current?.stop();
  1545. // 清理情绪 GIF 切换定时器
  1546. if (emotionGifTimerRef.current) {
  1547. clearTimeout(emotionGifTimerRef.current);
  1548. emotionGifTimerRef.current = null;
  1549. }
  1550. // 恢复默认头像 GIF
  1551. showDefaultGif();
  1552. reset();
  1553. setError(null);
  1554. }
  1555. return;
  1556. }
  1557. if (!wasConnected) {
  1558. // 初次连接失败,静默等待自动重连
  1559. return;
  1560. }
  1561. connectionLostRef.current = true;
  1562. if (appState.currentRequestId) {
  1563. performanceLogger.clearRequest(appState.currentRequestId, 'websocket_disconnected');
  1564. }
  1565. recorderRef.current?.stop();
  1566. resetFrequencyVisualization();
  1567. playerRef.current?.stop();
  1568. // 清理情绪 GIF 切换定时器
  1569. if (emotionGifTimerRef.current) {
  1570. clearTimeout(emotionGifTimerRef.current);
  1571. emotionGifTimerRef.current = null;
  1572. }
  1573. // 恢复默认头像 GIF
  1574. showDefaultGif();
  1575. antMessage.error('语音服务连接断开,请稍后重试');
  1576. setError('语音服务连接断开,请稍后重试');
  1577. });
  1578. ws.onMessage('app', handleMessage);
  1579. wsRef.current = ws;
  1580. } catch (error) {
  1581. antMessage.error('初始化失败,请检查麦克风权限');
  1582. setError('初始化失败');
  1583. }
  1584. };
  1585. init();
  1586. return () => {
  1587. // 标记组件已卸载,防止 WebSocket 回调显示错误消息
  1588. isMountedRef.current = false;
  1589. recorderRef.current?.release();
  1590. playerRef.current?.release();
  1591. wsRef.current?.disconnect();
  1592. resetFrequencyVisualization();
  1593. if (sendingTimeoutRef.current) {
  1594. clearTimeout(sendingTimeoutRef.current);
  1595. }
  1596. if (animationFrameRef.current) {
  1597. cancelAnimationFrame(animationFrameRef.current);
  1598. }
  1599. if (progressBarTooltipTimerRef.current) {
  1600. clearTimeout(progressBarTooltipTimerRef.current);
  1601. }
  1602. if (emotionGifTimerRef.current) {
  1603. clearTimeout(emotionGifTimerRef.current);
  1604. }
  1605. };
  1606. }, [resetFrequencyVisualization]);
  1607. // 处理WebSocket消息
  1608. const handleMessage = (message: Message) => {
  1609. const requestId = message.requestId || currentRequestId;
  1610. if (!requestId) {
  1611. console.warn('[ChatBotPage] 收到缺少 requestId 的消息,已忽略', message.type);
  1612. return;
  1613. }
  1614. const activeRequestId = activeRequestIdRef.current;
  1615. const isCancelledRequest = cancelledRequestIdsRef.current.has(requestId);
  1616. if (isCancelledRequest) {
  1617. if (message.type === 'audio_response' && message.isLast) {
  1618. cancelledRequestIdsRef.current.delete(requestId);
  1619. }
  1620. console.log(`[${message.type}] [${requestId.substring(0, 8)}] 已取消的请求,忽略响应`);
  1621. return;
  1622. }
  1623. switch (message.type) {
  1624. case 'text_result':
  1625. // 如果当前状态是 recording,且 requestId 不匹配当前请求,说明这是旧请求的返回,直接丢弃
  1626. // 如果 activeRequestId 为 null,说明是新请求的开始,允许处理
  1627. if (activeRequestId !== null && requestId !== activeRequestId) {
  1628. console.log(`[文本结果] [${requestId.substring(0, 8)}] 请求ID不匹配,丢弃旧请求的文本结果`);
  1629. break;
  1630. }
  1631. if (message.isFinal) {
  1632. performanceLogger.recordFinalText(requestId, message.text, message.timestamp);
  1633. } else if (!performanceLogger.hasFirstText(requestId)) {
  1634. performanceLogger.recordFirstText(requestId, message.text, message.timestamp);
  1635. }
  1636. setRecognizedText(message.text);
  1637. break;
  1638. case 'user_level':
  1639. if (typeof message.level === 'number') {
  1640. syncUserLevel(message.level);
  1641. }
  1642. // 🔥 使用后端返回的 totalDurationMs 同步进度条,确保与数据库一致
  1643. if (typeof message.totalDurationMs === 'number' && message.totalDurationMs > 0) {
  1644. const PROGRESS_FULL_DURATION_MS = 30 * 1000; // 30000ms
  1645. // 更新本地累加器,确保与数据库一致
  1646. totalSpeechDurationRef.current = Math.max(
  1647. message.totalDurationMs,
  1648. avatarCUnlockedRef.current ? AVATAR_C_UNLOCK_THRESHOLD_MS : 0
  1649. );
  1650. // 根据总时长计算进度条位置(只显示非整数圈的进度)
  1651. const totalProgressPercent = (message.totalDurationMs / PROGRESS_FULL_DURATION_MS) * 100;
  1652. const progress = totalProgressPercent % 100;
  1653. // 🔍 检查当前状态,如果用户刚刚更新了进度,不要覆盖用户的更新
  1654. const currentState = useAppStore.getState();
  1655. const currentTotal = currentState.progressLoopCount * 100 + currentState.conversationProgress;
  1656. const newTotal = Math.floor(totalProgressPercent / 100) * 100 + progress;
  1657. console.log('[Progress] [user_level] 收到后端同步请求', {
  1658. totalDurationMs: message.totalDurationMs,
  1659. progress,
  1660. currentTotal,
  1661. newTotal,
  1662. requestId: requestId?.substring(0, 8),
  1663. shouldSync: Math.abs(currentTotal - newTotal) > 0.1, // 只有差异较大时才同步
  1664. });
  1665. // 只有在差异较大时才同步(避免覆盖用户刚刚的更新)
  1666. // 并且只在初始化时跳过动画,用户主动更新后不应该跳过动画
  1667. if (Math.abs(currentTotal - newTotal) > 0.1) {
  1668. // 计算应该保持的圈数(基于总时长)
  1669. const targetLoopCount = Math.floor(totalProgressPercent / 100);
  1670. // 只在初始化时跳过动画(如果当前进度接近0)
  1671. const isInitialSync = currentTotal < 1;
  1672. if (isInitialSync) {
  1673. console.log('[Progress] [user_level] 初始化同步,跳过动画');
  1674. shouldSkipProgressAnimationRef.current = true;
  1675. } else {
  1676. console.log('[Progress] [user_level] 同步进度,保持圈数:', targetLoopCount);
  1677. // 保持当前的圈数,不重置为0
  1678. setProgress(progress, targetLoopCount);
  1679. }
  1680. } else {
  1681. console.log('[Progress] [user_level] 差异太小,跳过同步');
  1682. }
  1683. }
  1684. break;
  1685. case 'emotion':
  1686. // 如果当前状态是 recording,且 requestId 不匹配当前请求,说明这是旧请求的返回,直接丢弃
  1687. // 如果 activeRequestId 为 null,说明是新请求的开始,允许处理
  1688. if (activeRequestId !== null && requestId !== activeRequestId) {
  1689. console.log(`[情绪] [${requestId.substring(0, 8)}] 请求ID不匹配,丢弃旧请求的情绪`);
  1690. break;
  1691. }
  1692. performanceLogger.recordEmotion(requestId, message.emotion, message.timestamp);
  1693. setEmotion(message.emotion);
  1694. // 获取最新状态,确保能正确判断是否正在播放
  1695. const appState = useAppStore.getState();
  1696. // 如果正在播放或发送,根据新收到的情绪更新 GIF
  1697. if (appState.currentState === 'playing' || appState.currentState === 'sending') {
  1698. const emotionCategory = getEmotionCategory(message.emotion);
  1699. if (emotionCategory === 'positive') {
  1700. // 正向情绪:先显示正向情绪 GIF,播放一次后切换到 talking GIF
  1701. showPositiveGif();
  1702. if (emotionGifTimerRef.current) {
  1703. clearTimeout(emotionGifTimerRef.current);
  1704. }
  1705. // 获取当前形象ID,根据形象类型设置对应的播放时长
  1706. const currentAvatarId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  1707. const duration = getEmotionGifDuration(currentAvatarId, emotionCategory);
  1708. emotionGifTimerRef.current = window.setTimeout(() => {
  1709. showTalkingGif();
  1710. emotionGifTimerRef.current = null;
  1711. }, duration);
  1712. } else if (emotionCategory === 'negative') {
  1713. // 负向情绪:先显示负向情绪 GIF,播放一次后切换到 talking GIF
  1714. showNegativeGif();
  1715. if (emotionGifTimerRef.current) {
  1716. clearTimeout(emotionGifTimerRef.current);
  1717. }
  1718. // 获取当前形象ID,根据形象类型设置对应的播放时长
  1719. const currentAvatarId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  1720. const duration = getEmotionGifDuration(currentAvatarId, emotionCategory);
  1721. emotionGifTimerRef.current = window.setTimeout(() => {
  1722. showTalkingGif();
  1723. emotionGifTimerRef.current = null;
  1724. }, duration);
  1725. } else {
  1726. // 中性情绪:直接显示 talking GIF
  1727. showTalkingGif();
  1728. }
  1729. }
  1730. break;
  1731. case 'audio_response':
  1732. // 如果当前状态是 recording,说明用户已经点击了录音按钮,忽略后续的音频响应
  1733. if (currentState === 'recording') {
  1734. console.log(`[音频响应] [${requestId.substring(0, 8)}] 用户已开始录音,忽略音频响应`);
  1735. break;
  1736. }
  1737. // 如果 currentRequestId 不为 null,且 requestId 不匹配当前请求,说明这是旧请求的返回,直接丢弃
  1738. // 如果 activeRequestId 为 null,说明是新请求的开始,允许处理
  1739. if (activeRequestId !== null && requestId !== activeRequestId) {
  1740. console.log(`[音频响应] [${requestId.substring(0, 8)}] 请求ID不匹配,丢弃旧请求的音频响应`);
  1741. break;
  1742. }
  1743. // 清除发送超时
  1744. if (sendingTimeoutRef.current) {
  1745. clearTimeout(sendingTimeoutRef.current);
  1746. sendingTimeoutRef.current = null;
  1747. }
  1748. if (currentState !== 'playing') {
  1749. performanceLogger.recordFirstAudio(requestId, message.timestamp);
  1750. performanceLogger.recordPlaybackStart(requestId);
  1751. setState('playing');
  1752. // 获取最新的情绪状态(可能情绪消息已经到达)
  1753. const latestEmotion = useAppStore.getState().currentEmotion;
  1754. // 根据情绪类型设置 GIF
  1755. const emotionCategory = getEmotionCategory(latestEmotion);
  1756. if (emotionCategory === 'positive') {
  1757. // 正向情绪:先显示正向情绪 GIF,播放一次后切换到 talking GIF
  1758. showPositiveGif();
  1759. if (emotionGifTimerRef.current) {
  1760. clearTimeout(emotionGifTimerRef.current);
  1761. }
  1762. // 获取当前形象ID,根据形象类型设置对应的播放时长
  1763. const currentAvatarId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  1764. const duration = getEmotionGifDuration(currentAvatarId, emotionCategory);
  1765. emotionGifTimerRef.current = window.setTimeout(() => {
  1766. showTalkingGif();
  1767. emotionGifTimerRef.current = null;
  1768. }, duration);
  1769. } else if (emotionCategory === 'negative') {
  1770. // 负向情绪:先显示负向情绪 GIF,播放一次后切换到 talking GIF
  1771. showNegativeGif();
  1772. if (emotionGifTimerRef.current) {
  1773. clearTimeout(emotionGifTimerRef.current);
  1774. }
  1775. // 获取当前形象ID,根据形象类型设置对应的播放时长
  1776. const currentAvatarId = getStorage(STORAGE_KEYS.CURRENT_AVATAR_ID);
  1777. const duration = getEmotionGifDuration(currentAvatarId, emotionCategory);
  1778. emotionGifTimerRef.current = window.setTimeout(() => {
  1779. showTalkingGif();
  1780. emotionGifTimerRef.current = null;
  1781. }, duration);
  1782. } else {
  1783. // 中性情绪或无情绪:直接显示 talking GIF
  1784. showTalkingGif();
  1785. }
  1786. }
  1787. if (message.data) {
  1788. const audioData = atob(message.data);
  1789. const audioBytes = new Uint8Array(audioData.length);
  1790. for (let i = 0; i < audioData.length; i++) {
  1791. audioBytes[i] = audioData.charCodeAt(i);
  1792. }
  1793. playerRef.current?.addChunk(audioBytes, message.sampleRate || 24000);
  1794. }
  1795. if (message.isLast) {
  1796. performanceLogger.recordLastAudio(requestId);
  1797. playerRef.current?.markStreamEnd();
  1798. // 进度已在发送时增加,这里不再重复增加
  1799. }
  1800. break;
  1801. case 'energy_stone_spawn':
  1802. handleEnergyStoneSpawn(message);
  1803. break;
  1804. case 'error':
  1805. console.error(`[错误] [${requestId?.substring(0, 8) || ''}]`, message.message, message.code);
  1806. antMessage.error(message.message);
  1807. setError(message.message);
  1808. // 清理情绪 GIF 切换定时器
  1809. if (emotionGifTimerRef.current) {
  1810. clearTimeout(emotionGifTimerRef.current);
  1811. emotionGifTimerRef.current = null;
  1812. }
  1813. // 恢复默认头像 GIF
  1814. showDefaultGif();
  1815. performanceLogger.clearRequest(requestId, `${message.code}: ${message.message}`);
  1816. break;
  1817. case 'pong':
  1818. break;
  1819. default:
  1820. console.warn(`[未知消息] [${requestId?.substring(0, 8) || ''}]`, message.type);
  1821. break;
  1822. }
  1823. };
  1824. // 开始录音
  1825. const handleStartRecord = async () => {
  1826. // 如果正在录音,忽略
  1827. if (currentState === 'recording') {
  1828. return;
  1829. }
  1830. if (!isConnected) {
  1831. antMessage.warning('语音服务正在重连,请稍后再试');
  1832. return;
  1833. }
  1834. if (!wsRef.current?.isReady()) {
  1835. antMessage.warning('语音服务正在恢复,请稍后重试');
  1836. return;
  1837. }
  1838. // 保存旧请求ID(用于取消和过滤旧消息)
  1839. const oldRequestId = currentRequestId;
  1840. const wasPlaying = currentState === 'playing' || currentState === 'sending';
  1841. // 如果正在播放,先停止播放并重置状态
  1842. if (wasPlaying) {
  1843. // 停止音频播放
  1844. playerRef.current?.stop();
  1845. // 清理情绪 GIF 切换定时器
  1846. if (emotionGifTimerRef.current) {
  1847. clearTimeout(emotionGifTimerRef.current);
  1848. emotionGifTimerRef.current = null;
  1849. }
  1850. // 立即恢复默认头像 GIF
  1851. showDefaultGif();
  1852. // 取消当前的请求(如果有),丢弃上一次请求的返回
  1853. if (oldRequestId) {
  1854. wsRef.current?.sendCancel(oldRequestId);
  1855. cancelledRequestIdsRef.current.add(oldRequestId);
  1856. performanceLogger.clearRequest(oldRequestId, '用户中断播放开始新录音');
  1857. }
  1858. // 清理旧请求的状态(丢弃上一次请求的返回)
  1859. setRecognizedText('');
  1860. setEmotion('neutral');
  1861. // 清理发送超时
  1862. if (sendingTimeoutRef.current) {
  1863. clearTimeout(sendingTimeoutRef.current);
  1864. sendingTimeoutRef.current = null;
  1865. }
  1866. }
  1867. // 其他状态(idle/error/sending)都允许开始录音
  1868. // 首次点击录音按钮,隐藏 TapToChat 提示并保存标记
  1869. if (showTapToChat) {
  1870. setShowTapToChat(false);
  1871. // 确保标记已保存(防止 WebSocket 连接较慢时未保存)
  1872. const hasShown = getStorage(STORAGE_KEYS.HAS_SHOWN_TAP_TO_CHAT);
  1873. if (!hasShown) {
  1874. setStorage(STORAGE_KEYS.HAS_SHOWN_TAP_TO_CHAT, 'true');
  1875. // 同步到后端 UI 状态(如果已有 user_id)
  1876. const userId = getUserId();
  1877. if (userId) {
  1878. updateUserUIState(userId, {
  1879. has_shown_tap_to_chat: true,
  1880. }).catch(() => {
  1881. // 同步失败不影响主流程
  1882. });
  1883. }
  1884. }
  1885. }
  1886. // 点击录音按钮时,隐藏进度条提示
  1887. dismissProgressBarTooltip();
  1888. // 🔥 清理播放器状态,确保上一次对话的残留状态被重置
  1889. playerRef.current?.stop();
  1890. // 生成新的请求ID并立即设置,确保后续消息能正确匹配
  1891. const requestId = uuidv4();
  1892. performanceLogger.startRequest(requestId);
  1893. updateRequestId(requestId);
  1894. setState('recording');
  1895. chunkIndexRef.current = 0;
  1896. setRecognizedText('');
  1897. // 准备移动端音频(在设置状态之后,避免阻塞状态更新)
  1898. if (isMobileDevice() && !mobileAudioPreparedRef.current) {
  1899. // 异步准备,不阻塞录音开始
  1900. prepareMobileAudio()
  1901. .then(() => {
  1902. requestVolumeControl();
  1903. mobileAudioPreparedRef.current = true;
  1904. antMessage.info('请确保设备音量已调高');
  1905. })
  1906. .catch(() => {
  1907. // 准备移动端音频失败,静默处理
  1908. });
  1909. }
  1910. timingRef.current = {
  1911. recordStart: Date.now(),
  1912. recordEnd: 0,
  1913. firstAudioReceived: 0,
  1914. emotionReceived: 0,
  1915. };
  1916. // 立即启动录音,确保第一次点击就能开始
  1917. try {
  1918. await recorderRef.current?.start((pcmData) => {
  1919. // 检查当前状态,如果已经不是recording,说明用户可能又点击了,忽略
  1920. const currentStateCheck = useAppStore.getState().currentState;
  1921. if (currentStateCheck !== 'recording') {
  1922. return;
  1923. }
  1924. if (chunkIndexRef.current === 0) {
  1925. performanceLogger.recordFirstChunkSent(requestId);
  1926. }
  1927. const uint8Array = new Uint8Array(pcmData.buffer);
  1928. const base64 = btoa(String.fromCharCode(...Array.from(uint8Array)));
  1929. wsRef.current?.sendAudioChunk(requestId, base64, chunkIndexRef.current);
  1930. chunkIndexRef.current++;
  1931. }, pushFrequencyFrame);
  1932. // 录音启动成功,开始计时
  1933. startRecordingTimer();
  1934. } catch (error) {
  1935. console.error(`[录音] [${requestId.substring(0, 8)}] 启动失败`, error);
  1936. antMessage.error('麦克风启动失败,请检查权限或重试');
  1937. performanceLogger.clearRequest(requestId, 'recorder_start_failed');
  1938. resetFrequencyVisualization();
  1939. // 清理情绪 GIF 切换定时器
  1940. if (emotionGifTimerRef.current) {
  1941. clearTimeout(emotionGifTimerRef.current);
  1942. emotionGifTimerRef.current = null;
  1943. }
  1944. // 恢复默认头像 GIF
  1945. showDefaultGif();
  1946. setState('idle');
  1947. reset();
  1948. return;
  1949. }
  1950. };
  1951. // 发送录音
  1952. const handleSend = () => {
  1953. if (currentState !== 'recording' || !currentRequestId) {
  1954. return;
  1955. }
  1956. stopRecordingTimer();
  1957. if (!isConnected) {
  1958. antMessage.error('语音服务未连接,请重新录音发送');
  1959. recorderRef.current?.stop();
  1960. resetFrequencyVisualization();
  1961. // 清理情绪 GIF 切换定时器
  1962. if (emotionGifTimerRef.current) {
  1963. clearTimeout(emotionGifTimerRef.current);
  1964. emotionGifTimerRef.current = null;
  1965. }
  1966. // 恢复默认头像 GIF
  1967. showDefaultGif();
  1968. performanceLogger.clearRequest(currentRequestId, 'websocket_disconnected_before_send');
  1969. reset();
  1970. return;
  1971. }
  1972. if (!wsRef.current?.isReady()) {
  1973. antMessage.error('语音服务尚未准备完成,请重新录音发送');
  1974. recorderRef.current?.stop();
  1975. resetFrequencyVisualization();
  1976. // 清理情绪 GIF 切换定时器
  1977. if (emotionGifTimerRef.current) {
  1978. clearTimeout(emotionGifTimerRef.current);
  1979. emotionGifTimerRef.current = null;
  1980. }
  1981. // 恢复默认头像 GIF
  1982. showDefaultGif();
  1983. performanceLogger.clearRequest(currentRequestId, 'websocket_unready_before_send');
  1984. reset();
  1985. return;
  1986. }
  1987. timingRef.current.recordEnd = Date.now();
  1988. sendTimeRef.current = Date.now();
  1989. performanceLogger.recordEnd(currentRequestId, chunkIndexRef.current);
  1990. // 计算说话时长(毫秒)
  1991. const speechDuration = timingRef.current.recordEnd - timingRef.current.recordStart;
  1992. // 更新进度条(基于说话时长,30秒=100%)
  1993. if (speechDuration > 0) {
  1994. // 🔍 日志:记录更新前的状态
  1995. const beforeState = useAppStore.getState();
  1996. console.log('[Progress] [handleSend] 准备更新进度:', {
  1997. speechDurationMs: speechDuration,
  1998. beforeProgress: beforeState.conversationProgress,
  1999. beforeLoopCount: beforeState.progressLoopCount,
  2000. beforeTotal: beforeState.progressLoopCount * 100 + beforeState.conversationProgress,
  2001. prevProgressStateRef: { ...prevProgressStateRef.current },
  2002. totalProgressRef: totalProgressRef.current,
  2003. displayProgress: displayProgress,
  2004. shouldSkipAnimation: shouldSkipProgressAnimationRef.current,
  2005. });
  2006. // 🔧 确保 shouldSkipProgressAnimation 在用户主动更新时是 false
  2007. // 初始化完成后,用户主动更新进度时应该播放动画
  2008. if (shouldSkipProgressAnimationRef.current) {
  2009. console.log('[Progress] [handleSend] 检测到 shouldSkipProgressAnimation 为 true,重置为 false');
  2010. shouldSkipProgressAnimationRef.current = false;
  2011. }
  2012. updateProgressByDuration(speechDuration);
  2013. registerSpeechDuration(speechDuration);
  2014. // 🔍 日志:记录更新后的状态
  2015. const afterState = useAppStore.getState();
  2016. console.log('[Progress] [handleSend] 更新后状态:', {
  2017. afterProgress: afterState.conversationProgress,
  2018. afterLoopCount: afterState.progressLoopCount,
  2019. afterTotal: afterState.progressLoopCount * 100 + afterState.conversationProgress,
  2020. expectedIncrement: (speechDuration / 30000) * 100,
  2021. });
  2022. }
  2023. recorderRef.current?.stop();
  2024. resetFrequencyVisualization();
  2025. // 发送音频结束消息,包含说话时长信息
  2026. wsRef.current?.sendAudioEnd(currentRequestId, chunkIndexRef.current, speechDuration);
  2027. setState('sending');
  2028. // 设置超时保护:10秒后如果还在 sending 或 playing 状态,自动重置
  2029. if (sendingTimeoutRef.current) {
  2030. clearTimeout(sendingTimeoutRef.current);
  2031. }
  2032. sendingTimeoutRef.current = window.setTimeout(() => {
  2033. const state = useAppStore.getState().currentState;
  2034. if (state === 'sending' || state === 'playing') {
  2035. const shortId = currentRequestId ? currentRequestId.substring(0, 8) : '';
  2036. console.warn(`[超时] [${shortId}] 响应超时,自动重置`);
  2037. antMessage.warning('响应超时,请重试');
  2038. // 清理情绪 GIF 切换定时器
  2039. if (emotionGifTimerRef.current) {
  2040. clearTimeout(emotionGifTimerRef.current);
  2041. emotionGifTimerRef.current = null;
  2042. }
  2043. // 恢复默认头像 GIF
  2044. showDefaultGif();
  2045. setState('idle');
  2046. reset();
  2047. performanceLogger.clearRequest(currentRequestId, '响应超时');
  2048. }
  2049. }, 10000);
  2050. };
  2051. handleSendRef.current = handleSend;
  2052. // 取消录音
  2053. const handleCancel = () => {
  2054. if (currentState !== 'recording' || !currentRequestId) return;
  2055. const shortId = currentRequestId.substring(0, 8);
  2056. console.log(`[取消] [${shortId}] 取消录音`);
  2057. recorderRef.current?.stop();
  2058. resetFrequencyVisualization();
  2059. stopRecordingTimer();
  2060. // 清理情绪 GIF 切换定时器
  2061. if (emotionGifTimerRef.current) {
  2062. clearTimeout(emotionGifTimerRef.current);
  2063. emotionGifTimerRef.current = null;
  2064. }
  2065. // 恢复默认头像 GIF
  2066. showDefaultGif();
  2067. wsRef.current?.sendCancel(currentRequestId);
  2068. cancelledRequestIdsRef.current.add(currentRequestId);
  2069. performanceLogger.clearRequest(currentRequestId);
  2070. reset();
  2071. };
  2072. const avatarHintSize = Math.round(layout.avatarCSize * 1.8);
  2073. const currentAvatarAlt = currentState === 'playing' ? 'AnimaZone Avatar Animated' : 'AnimaZone Avatar';
  2074. const energyStoneAssets: Record<'mission' | 'conversation', string> = {
  2075. mission: missionEnergyStone,
  2076. conversation: conversationEnergyStone,
  2077. };
  2078. const energyStoneHintAssets: Record<'mission' | 'conversation', string> = {
  2079. mission: missionEnergyStoneHint,
  2080. conversation: conversationEnergyStoneHint,
  2081. };
  2082. const handleEnergyStoneSpawn = useCallback(
  2083. (payload: EnergyStoneSpawnMessage) => {
  2084. spawnStone(payload);
  2085. },
  2086. [spawnStone]
  2087. );
  2088. // 获取用户名的首字母(英文大写,汉字保持原样)
  2089. const getUserInitial = (): string => {
  2090. const userName = getStorage(STORAGE_KEYS.USER_NAME);
  2091. if (!userName || userName.length === 0) {
  2092. return '';
  2093. }
  2094. const firstChar = userName[0];
  2095. // 判断是否为英文字母
  2096. if (/[a-zA-Z]/.test(firstChar)) {
  2097. return firstChar.toUpperCase();
  2098. }
  2099. // 汉字或其他字符保持原样
  2100. return firstChar;
  2101. };
  2102. // 根据背景图片获取对应的树木装饰图片URL
  2103. const getTreesImage = (): string | null => {
  2104. // 实时从 localStorage 读取最新的背景图片URL,使用背景图片作为树木装饰
  2105. const currentBgUrl = getCurrentBackgroundImageUrl() || backgroundImageUrl;
  2106. return currentBgUrl;
  2107. };
  2108. // 根据背景图片ID获取树木装饰图片的样式配置
  2109. const getTreesImageStyle = (): React.CSSProperties => {
  2110. const baseStyle: React.CSSProperties = {
  2111. width: '100%',
  2112. height: '100%',
  2113. top: 0,
  2114. left: 0,
  2115. objectFit: 'cover',
  2116. zIndex: 1,
  2117. };
  2118. // 从 localStorage 读取透明度配置(从数据库metadata中获取)
  2119. const opacity = getCurrentBackgroundImageOpacity();
  2120. return { ...baseStyle, opacity };
  2121. };
  2122. const userInitial = getUserInitial();
  2123. // 计算 Avatar-C 头像的显示逻辑
  2124. const isLoggedIn = getStorage(STORAGE_KEYS.IS_LOGGED_IN) === 'true';
  2125. // 图片选择:未登录或已登录且已分享 → avatarC,已登录且未分享 → avatarGift
  2126. const avatarSrc = (!isLoggedIn || hasSharedAvatar) ? avatarC : avatarGift;
  2127. // 首字母显示:未登录或已登录且已分享 → 显示,已登录且未分享 → 不显示
  2128. const shouldShowInitial = userInitial && (!isLoggedIn || hasSharedAvatar);
  2129. return (
  2130. <div
  2131. style={{
  2132. position: 'relative',
  2133. width: '100%',
  2134. minHeight: '100dvh',
  2135. }}
  2136. >
  2137. {/* 树木装饰 - 根据背景图片调整图片和样式 */}
  2138. {getTreesImage() && (
  2139. <img
  2140. src={getTreesImage() || undefined}
  2141. alt="Trees"
  2142. className="chatbot-trees absolute pointer-events-none"
  2143. style={getTreesImageStyle()}
  2144. />
  2145. )}
  2146. {/* Avatar-C 头像 - 设计稿:top 63px, right 33px, 尺寸 40×40,使用 px2vmin 等比缩放 */}
  2147. <div
  2148. className="chatbot-avatar-c absolute"
  2149. data-avatar-state={isAvatarCUnlocked ? 'unlocked' : 'locked'}
  2150. role={isAvatarCUnlocked ? 'button' : undefined}
  2151. tabIndex={isAvatarCUnlocked ? 0 : -1}
  2152. aria-label={isAvatarCUnlocked ? 'Avatar C' : undefined}
  2153. onClick={isAvatarCUnlocked ? handleAvatarClick : undefined}
  2154. onKeyDown={isAvatarCUnlocked ? handleAvatarKeyDown : undefined}
  2155. style={{
  2156. width: px2vmin(40),
  2157. height: px2vmin(40),
  2158. top: px2vmin(63),
  2159. right: px2vmin(33),
  2160. zIndex: hasSharedAvatar ? 5 : 30,
  2161. pointerEvents: isAvatarCUnlocked ? 'auto' : 'none',
  2162. cursor: isAvatarCUnlocked ? 'pointer' : 'default',
  2163. }}
  2164. >
  2165. {isAvatarCUnlocked && (
  2166. <img
  2167. src={avatarHint}
  2168. alt="Avatar Hint"
  2169. className="chatbot-avatar-hint"
  2170. style={{
  2171. width: `${avatarHintSize}px`,
  2172. height: `${avatarHintSize}px`,
  2173. }}
  2174. />
  2175. )}
  2176. <img
  2177. src={avatarSrc}
  2178. alt="Avatar C"
  2179. className="rounded-full"
  2180. style={{
  2181. width: '100%',
  2182. height: '100%',
  2183. opacity: isAvatarCUnlocked ? 1 : 0.2,
  2184. transition: 'opacity 0.4s ease',
  2185. position: 'relative',
  2186. zIndex: hasSharedAvatar ? 1 : 30,
  2187. }}
  2188. />
  2189. {shouldShowInitial && (
  2190. <div
  2191. style={{
  2192. position: 'absolute',
  2193. top: '50%',
  2194. left: '50%',
  2195. transform: 'translate(-50%, -50%)',
  2196. backgroundImage: 'linear-gradient(180deg, #25232E 0%, #2B2A2F 100%)',
  2197. WebkitBackgroundClip: 'text',
  2198. backgroundClip: 'text',
  2199. WebkitTextFillColor: 'transparent',
  2200. color: 'transparent',
  2201. fontSize: px2vmin(16),
  2202. fontWeight: 600,
  2203. opacity: 0.9,
  2204. pointerEvents: 'none',
  2205. zIndex: 31,
  2206. }}
  2207. >
  2208. {userInitial}
  2209. </div>
  2210. )}
  2211. </div>
  2212. {/* 隐藏的能量石容器 - 用于 useEnergyStones hook 计算位置 */}
  2213. <div
  2214. ref={hiddenEnergyStoneZoneRef}
  2215. style={{
  2216. position: 'absolute',
  2217. width: px2vmin(343), // 设计稿 343px,基于 vw/vh 最小值
  2218. height: px2vmin(172), // 设计稿 172px,基于 vw/vh 最小值
  2219. top: px2vmin(126), // 设计稿 top 126px,基于 vw/vh 最小值
  2220. left: '50%',
  2221. transform: 'translateX(-50%)',
  2222. visibility: 'hidden',
  2223. pointerEvents: 'none',
  2224. zIndex: -1,
  2225. }}
  2226. />
  2227. {/* 能量石展示区域 - 已废弃,能量石现在独立定位,不再嵌套在此容器中 */}
  2228. {/* <div
  2229. className="chatbot-energy-zone absolute"
  2230. style={{
  2231. width: px2vmin(343), // 设计稿 343px,基于 vw/vh 最小值
  2232. height: px2vmin(172), // 设计稿 172px,基于 vw/vh 最小值
  2233. top: px2vmin(126), // 设计稿 top 126px,基于 vw/vh 最小值
  2234. left: '50%',
  2235. transform: 'translateX(-50%)',
  2236. zIndex: 4,
  2237. pointerEvents: 'none',
  2238. }}
  2239. >
  2240. <div className="energy-stone-layer" />
  2241. </div> */}
  2242. {/* 正式能量石 - 独立绝对定位,限制在能量石区域内 */}
  2243. {energyStones.map((stone) => {
  2244. const asset = energyStoneAssets[stone.stoneType];
  2245. const hintAsset = energyStoneHintAssets[stone.stoneType];
  2246. const stoneSize = 48; // 设计稿 48px,基于 vw/vh 最小值
  2247. const hintSize = 92; // 设计稿 92px,基于 vw/vh 最小值
  2248. const shouldShowHint = stone.stoneType === 'mission'
  2249. ? !hasClickedMissionStone
  2250. : !hasClickedConversationStone;
  2251. // 计算绝对位置:基于能量石区域的边界
  2252. // 区域:left 50% translateX(-50%), top: px2vmin(126), width: px2vmin(343), height: px2vmin(172)
  2253. // 能量石在区域内的百分比位置转换为绝对位置
  2254. // left: 50% + (xRatio - 0.5) * zoneWidth
  2255. // top: zoneTop + yRatio * zoneHeight
  2256. const zoneTop = px2vmin(126);
  2257. const zoneWidth = px2vmin(343);
  2258. const zoneHeight = px2vmin(172);
  2259. const absoluteLeft = `calc(50% + (${stone.position.xRatio} - 0.5) * ${zoneWidth})`;
  2260. const absoluteTop = `calc(${zoneTop} + ${stone.position.yRatio} * ${zoneHeight})`;
  2261. return (
  2262. <div
  2263. key={stone.stoneId}
  2264. data-stone-id={stone.stoneId}
  2265. data-fading={stone.fading ? 'true' : 'false'}
  2266. className={`energy-stone-container ${stone.stoneType === 'mission' ? 'energy-stone-mission' : ''}`}
  2267. style={{
  2268. position: 'absolute',
  2269. left: absoluteLeft,
  2270. top: absoluteTop,
  2271. transform: 'translate(-50%, -50%)',
  2272. width: px2vmin(stoneSize),
  2273. height: px2vmin(stoneSize),
  2274. pointerEvents: 'auto',
  2275. cursor: 'pointer',
  2276. zIndex: 5,
  2277. }}
  2278. >
  2279. {/* 光晕层 - 只在未点击时显示,但在动画过程中也显示以跟随能量石 */}
  2280. {shouldShowHint && (
  2281. <img
  2282. src={hintAsset}
  2283. alt=""
  2284. className="energy-stone-hint"
  2285. style={{
  2286. position: 'absolute',
  2287. top: '50%',
  2288. left: '50%',
  2289. transform: 'translate(-50%, -50%)',
  2290. transformOrigin: 'center center',
  2291. width: px2vmin(hintSize),
  2292. height: px2vmin(hintSize),
  2293. pointerEvents: 'none',
  2294. zIndex: 0,
  2295. opacity: stone.fading ? 0 : undefined, // 动画过程中逐渐消失
  2296. maxWidth: 'none',
  2297. maxHeight: 'none',
  2298. objectFit: 'contain',
  2299. }}
  2300. />
  2301. )}
  2302. {/* 能量石按钮 */}
  2303. <button
  2304. type="button"
  2305. className="energy-stone"
  2306. data-type={stone.stoneType}
  2307. data-fading={stone.fading ? 'true' : 'false'}
  2308. style={{
  2309. position: 'absolute',
  2310. top: '50%',
  2311. left: '50%',
  2312. transform: 'translate(-50%, -50%)',
  2313. transformOrigin: 'center center',
  2314. width: px2vmin(stoneSize),
  2315. height: px2vmin(stoneSize),
  2316. zIndex: stone.fading ? 100 : 1,
  2317. pointerEvents: stone.fading ? 'none' : 'auto',
  2318. }}
  2319. onClick={async () => {
  2320. // 更新点击状态(控制光晕显示)
  2321. const clickedStorageKey = stone.stoneType === 'mission'
  2322. ? STORAGE_KEYS.HAS_CLICKED_MISSION_STONE
  2323. : STORAGE_KEYS.HAS_CLICKED_CONVERSATION_STONE;
  2324. const hasClicked = getStorage(clickedStorageKey);
  2325. const isFirstClick = hasClicked !== 'true';
  2326. // 检查是否是第一次点击,决定是否显示弹窗
  2327. const storageKey = stone.stoneType === 'mission'
  2328. ? STORAGE_KEYS.HAS_SHOWN_MISSION_STONE_MODAL
  2329. : STORAGE_KEYS.HAS_SHOWN_CONVERSATION_STONE_MODAL;
  2330. const hasShown = getStorage(storageKey);
  2331. const isFirstShowModal = hasShown !== 'true';
  2332. // 更新点击状态(控制光晕显示)
  2333. if (isFirstClick) {
  2334. setStorage(clickedStorageKey, 'true');
  2335. if (stone.stoneType === 'mission') {
  2336. setHasClickedMissionStone(true);
  2337. } else {
  2338. setHasClickedConversationStone(true);
  2339. }
  2340. }
  2341. // 准备需要同步到数据库的字段
  2342. const userId = getUserId();
  2343. const updatePayload: Record<string, boolean> = {};
  2344. // 如果是第一次点击,需要更新点击状态
  2345. if (isFirstClick) {
  2346. if (stone.stoneType === 'mission') {
  2347. updatePayload.has_clicked_mission_stone = true;
  2348. } else {
  2349. updatePayload.has_clicked_conversation_stone = true;
  2350. }
  2351. }
  2352. // 如果是第一次显示弹窗,需要更新弹窗状态
  2353. if (isFirstShowModal) {
  2354. setEnergyStoneModalType(stone.stoneType);
  2355. setShowMissionStoneModal(true);
  2356. pendingStoneIdRef.current = stone.stoneId;
  2357. if (stone.stoneType === 'mission') {
  2358. updatePayload.has_shown_mission_stone_modal = true;
  2359. } else {
  2360. updatePayload.has_shown_conversation_stone_modal = true;
  2361. }
  2362. }
  2363. // 非首次弹窗,直接播放动画并移除能量石
  2364. if (!isFirstShowModal) {
  2365. startStoneAnimationAndRemove(stone.stoneId);
  2366. }
  2367. // 合并同步到数据库(一次性更新所有需要更新的字段)
  2368. if (userId && Object.keys(updatePayload).length > 0) {
  2369. try {
  2370. await updateUserUIState(userId, updatePayload);
  2371. console.log('[能量石] 能量石状态已同步到数据库:', updatePayload);
  2372. // 同步成功后更新 localStorage
  2373. if (isFirstShowModal) {
  2374. setStorage(storageKey, 'true');
  2375. }
  2376. } catch (error) {
  2377. console.error('同步能量石状态到数据库失败:', error);
  2378. // 同步失败时,如果只是点击状态失败,不影响弹窗显示
  2379. // 但如果是弹窗状态失败,需要回滚弹窗显示
  2380. if (isFirstShowModal) {
  2381. // 弹窗已经显示,但同步失败,记录错误以便后续补偿
  2382. console.warn('[能量石] 弹窗状态同步失败,但弹窗已显示');
  2383. }
  2384. }
  2385. } else if (!userId) {
  2386. // 没有 userId(游客态),仅更新本地
  2387. if (isFirstShowModal) {
  2388. setStorage(storageKey, 'true');
  2389. }
  2390. console.log('[能量石] 游客状态,仅更新本地,等待后续补偿同步');
  2391. } else {
  2392. // 没有需要更新的字段,直接更新 localStorage(如果显示弹窗)
  2393. if (isFirstShowModal) {
  2394. setStorage(storageKey, 'true');
  2395. }
  2396. }
  2397. }}
  2398. aria-label={stone.stoneType === 'mission' ? '任务能量石' : '对话能量石'}
  2399. >
  2400. <img src={asset} alt="" aria-hidden="true" />
  2401. </button>
  2402. </div>
  2403. );
  2404. })}
  2405. {/* AnimaZone 头像 - 使用设计稿 375×812 基准 + px2vmin 适配 */}
  2406. <div
  2407. className="chatbot-anima-zone-avatar absolute"
  2408. style={{
  2409. // 设计稿宽 343px,高 340px,基于 vw/vh 的最小值等比缩放
  2410. width: px2vmin(343),
  2411. height: px2vmin(340),
  2412. // 设计稿 top 248px,同样基于 vw/vh 的最小值
  2413. top: px2vmin(248),
  2414. left: '50%',
  2415. transform: 'translateX(-50%)',
  2416. opacity: 1,
  2417. zIndex: 2,
  2418. }}
  2419. >
  2420. <img
  2421. src={currentAvatarGif}
  2422. alt={currentAvatarAlt}
  2423. loading="eager"
  2424. decoding="sync"
  2425. style={{
  2426. width: '100%',
  2427. height: '100%',
  2428. objectFit: 'contain',
  2429. pointerEvents: 'none',
  2430. }}
  2431. />
  2432. </div>
  2433. {/* 音频波形 - 宽度 100% 自适应 */}
  2434. <div
  2435. className="chatbot-audio-wave overflow-hidden"
  2436. style={{
  2437. // 相对于父容器定位,贴底
  2438. position: 'absolute',
  2439. width: '100%',
  2440. // 录音/播放时展开波形高度
  2441. height: waveformHeightStyle,
  2442. bottom: 0,
  2443. left: 0,
  2444. transition: 'height 0.3s ease-in-out',
  2445. zIndex: 10,
  2446. }}
  2447. data-expanded={shouldExpandWaveform ? 'true' : 'false'}
  2448. data-waveform-bottom={0}
  2449. >
  2450. <div
  2451. style={{
  2452. position: 'absolute',
  2453. bottom: 0,
  2454. left: 0,
  2455. width: '100%',
  2456. height: waveformHeightStyle, // 设计稿高度根据状态切换
  2457. }}
  2458. >
  2459. <AudioWave
  2460. ref={audioWaveRef}
  2461. isAnimating={currentState === 'recording'}
  2462. width={typeof window !== 'undefined' ? window.innerWidth : 375}
  2463. height={waveformHeightPx}
  2464. />
  2465. </div>
  2466. </div>
  2467. {/* 进度条+录音按钮容器 - 设计稿:top 614px,尺寸 114×114, 左右居中,使用 px2vmin 适配(基于 vw/vh 最小值) */}
  2468. {/* 容器在非录音状态显示进度环,在录音状态显示按钮 */}
  2469. <div
  2470. className="chatbot-progress-container absolute"
  2471. style={{
  2472. width: px2vmin(114), // 设计稿 114px,基于 vw/vh 最小值
  2473. height: px2vmin(114), // 设计稿 114px,基于 vw/vh 最小值
  2474. top: px2vh(614), // 设计稿 top 614px,基于 vh
  2475. left: '50%',
  2476. transform: 'translateX(-50%)',
  2477. zIndex: 20,
  2478. }}
  2479. >
  2480. {/* 进度环 - 背景层 - 只在非录音状态显示 */}
  2481. {currentState !== 'recording' && (
  2482. <div
  2483. className="chatbot-progress-ring absolute"
  2484. style={{
  2485. width: '100%',
  2486. height: '100%',
  2487. top: '1px',
  2488. left: 0,
  2489. }}
  2490. >
  2491. {/* 空状态背景 */}
  2492. <img
  2493. src={progressRing}
  2494. alt="Progress Ring"
  2495. className="pointer-events-none"
  2496. style={{
  2497. position: 'absolute',
  2498. top: px2vmin(0), // 设计稿 top 0px,基于 vw/vh 最小值
  2499. left: '-0.5PX', // 设计稿 left -0.5px,使用大写 PX 避免转换
  2500. width: '100%',
  2501. height: '100%',
  2502. opacity: 1,
  2503. zIndex: 1,
  2504. }}
  2505. />
  2506. {/* 进度填充层 - 使用 Canvas 绘制(类似 arrow-process.html) */}
  2507. <ProgressRingCanvas
  2508. ref={progressRingCanvasRef}
  2509. progress={displayProgress}
  2510. width={118}
  2511. height={118}
  2512. />
  2513. {/* 进度条提示 - 相对于进度环定位 - 首次有进度时显示 */}
  2514. {showProgressBarTooltip && isConnected && (
  2515. <img
  2516. ref={progressBarTooltipRef}
  2517. src={tooltipsProgressBar}
  2518. alt="Progress Bar Tooltip"
  2519. className="chatbot-tooltips absolute pointer-events-none"
  2520. style={{
  2521. width: px2vmin(167), // 设计稿 width 167px,基于 vw/vh 最小值
  2522. height: px2vmin(130), // 设计稿 height 130px,基于 vw/vh 最小值
  2523. top: '-100%',
  2524. left: '-100%',
  2525. opacity: 1,
  2526. zIndex: 10,
  2527. maxWidth: 'none', // 确保不被限制
  2528. maxHeight: 'none', // 确保不被限制
  2529. }}
  2530. />
  2531. )}
  2532. </div>
  2533. )}
  2534. {/* 录音按钮 - 设计稿:尺寸 96×96,top 623px,左右居中 - 只在非录音状态显示 */}
  2535. {currentState !== 'recording' && (
  2536. <div
  2537. className={`chatbot-record-button absolute touch-manipulation ${isConnected ? 'cursor-pointer' : 'cursor-not-allowed'}`}
  2538. onClick={handleStartRecord}
  2539. style={{
  2540. width: px2vmin(96), // 设计稿 96px,基于 vw/vh 最小值
  2541. height: px2vmin(96), // 设计稿 96px,基于 vw/vh 最小值
  2542. top: '50%', // 在容器内垂直居中
  2543. left: '50%',
  2544. transform: 'translate(-50%, -50%)', // 水平和垂直居中
  2545. borderRadius: px2vw(100),
  2546. opacity: isConnected ? 1 : 0.55,
  2547. display: 'flex',
  2548. alignItems: 'center',
  2549. justifyContent: 'center',
  2550. userSelect: 'none',
  2551. WebkitUserSelect: 'none',
  2552. caretColor: 'transparent',
  2553. outline: 'none',
  2554. filter: isConnected ? 'none' : 'grayscale(0.25)',
  2555. overflow: 'visible', // 允许 tooltips 超出按钮边界
  2556. }}
  2557. contentEditable={false}
  2558. tabIndex={-1}
  2559. aria-disabled={!isConnected}
  2560. >
  2561. {/* 录音按钮背景 - PNG 图片 */}
  2562. <img
  2563. src={audioButton}
  2564. alt="Audio Button Background"
  2565. className="pointer-events-none"
  2566. style={{
  2567. width: '100%',
  2568. height: '100%',
  2569. position: 'absolute',
  2570. top: 0,
  2571. left: 0,
  2572. }}
  2573. />
  2574. {/* 麦克风图标 - 居中 - 响应式尺寸 */}
  2575. <img
  2576. src={microphoneIcon}
  2577. alt="Microphone"
  2578. className="pointer-events-none"
  2579. style={{
  2580. width: px2vmin(32),
  2581. height: px2vmin(32),
  2582. opacity: 1,
  2583. position: 'relative',
  2584. zIndex: 1,
  2585. }}
  2586. />
  2587. {/* Tap to Chat 提示 - 相对于录音按钮定位 - 只显示一次 - 在按钮内部使用绝对定位 */}
  2588. {showTapToChat && isConnected && (
  2589. <img
  2590. src={tapToChat}
  2591. alt="Tap to Chat"
  2592. className="chatbot-tooltips absolute pointer-events-none"
  2593. style={{
  2594. width: px2vmin(128), // 设计稿 width 128px,基于 vw/vh 最小值
  2595. height: px2vmin(100), // 设计稿 height 100px,基于 vw/vh 最小值
  2596. top: '-100%',
  2597. right: '-100%',
  2598. borderRadius: `${Math.round(8 * layout.avatarScale)}px`,
  2599. opacity: 1,
  2600. zIndex: 10,
  2601. maxWidth: 'none', // 确保不被限制
  2602. maxHeight: 'none', // 确保不被限制
  2603. }}
  2604. />
  2605. )}
  2606. </div>
  2607. )}
  2608. {/* 取消语音发送按钮 - 录音状态显示,与录音按钮同高度,左侧对齐 */}
  2609. {currentState === 'recording' && (
  2610. <div
  2611. className="chatbot-action-buttons absolute flex items-center justify-center cursor-pointer touch-manipulation"
  2612. onClick={handleCancel}
  2613. style={{
  2614. width: px2vmin(56), // 设计稿 width 56px,基于 vw/vh 最小值
  2615. height: px2vmin(56), // 设计稿 height 56px,基于 vw/vh 最小值
  2616. top: '50%',
  2617. left: `calc(50% - ${px2vmin(56)} / 2 - ${px2vmin(50)} / 2)`, // 按钮宽度 56px,间距 50px,基于 vw/vh 最小值
  2618. transform: 'translate(-50%, -50%)',
  2619. borderRadius: px2vw(100),
  2620. opacity: 1,
  2621. background: 'rgba(255, 255, 255, 0.1)',
  2622. backdropFilter: 'blur(15px)',
  2623. zIndex: 20,
  2624. userSelect: 'none',
  2625. WebkitUserSelect: 'none',
  2626. caretColor: 'transparent',
  2627. outline: 'none',
  2628. overflow: 'visible', // 允许 tooltip 超出按钮边界
  2629. }}
  2630. contentEditable={false}
  2631. tabIndex={-1}
  2632. >
  2633. {/* 按钮背景装饰图片 */}
  2634. <img
  2635. src={audioButton}
  2636. alt="Button Background"
  2637. className="pointer-events-none"
  2638. style={{
  2639. width: 'calc(100% + 1px)',
  2640. height: 'calc(100% + 1px)',
  2641. position: 'absolute',
  2642. top: '0',
  2643. left: '0',
  2644. }}
  2645. />
  2646. {/* Cancel 图标 - 居中 - 响应式尺寸 */}
  2647. <img
  2648. src={cancelIcon}
  2649. alt="Cancel"
  2650. className="pointer-events-none"
  2651. style={{
  2652. width: px2vmin(24),
  2653. height: px2vmin(24),
  2654. position: 'relative',
  2655. zIndex: 1,
  2656. }}
  2657. />
  2658. {/* 取消语音发送提示 - 相对于取消按钮定位 - 只在首次录音时显示 */}
  2659. {showCancelTooltip && (
  2660. <img
  2661. src={tooltipsCancel}
  2662. alt="Cancel Tooltip"
  2663. className="chatbot-tooltips absolute pointer-events-none"
  2664. style={{
  2665. width: px2vmin(109), // 设计稿 width 109px,基于 vw/vh 最小值
  2666. height: px2vmin(91), // 设计稿 height 91px,基于 vw/vh 最小值
  2667. bottom: '110%',
  2668. left: '-150%',
  2669. borderRadius: px2vmin(8),
  2670. opacity: 1,
  2671. zIndex: 10,
  2672. maxWidth: 'none', // 确保不被限制
  2673. maxHeight: 'none', // 确保不被限制
  2674. }}
  2675. />
  2676. )}
  2677. </div>
  2678. )}
  2679. {/* 语音发送按钮 - 录音状态显示,与录音按钮同高度,右侧对齐 */}
  2680. {currentState === 'recording' && (
  2681. <div
  2682. className="chatbot-action-buttons absolute flex items-center justify-center cursor-pointer touch-manipulation"
  2683. onClick={handleSend}
  2684. style={{
  2685. width: px2vmin(56), // 设计稿 width 56px,基于 vw/vh 最小值
  2686. height: px2vmin(56), // 设计稿 height 56px,基于 vw/vh 最小值
  2687. top: '50%',
  2688. left: `calc(50% + ${px2vmin(56)} / 2 + ${px2vmin(50)} / 2)`, // 按钮宽度 56px,间距 50px,基于 vw/vh 最小值
  2689. transform: 'translate(-50%, -50%)',
  2690. borderRadius: px2vw(100),
  2691. opacity: 1,
  2692. background: 'rgba(255, 255, 255, 0.1)',
  2693. backdropFilter: 'blur(15px)',
  2694. zIndex: 20,
  2695. userSelect: 'none',
  2696. WebkitUserSelect: 'none',
  2697. caretColor: 'transparent',
  2698. outline: 'none',
  2699. overflow: 'visible', // 允许 tooltip 超出按钮边界
  2700. }}
  2701. contentEditable={false}
  2702. tabIndex={-1}
  2703. >
  2704. {/* 按钮背景装饰图片 */}
  2705. <img
  2706. src={audioButton}
  2707. alt="Button Background"
  2708. className="pointer-events-none"
  2709. style={{
  2710. width: 'calc(100% + 1px)',
  2711. height: 'calc(100% + 1px)',
  2712. position: 'absolute',
  2713. top: '0',
  2714. left: '0',
  2715. }}
  2716. />
  2717. {/* Send 图标 - 居中 - 响应式尺寸 */}
  2718. <img
  2719. src={sendIcon}
  2720. alt="Send"
  2721. className="pointer-events-none"
  2722. style={{
  2723. width: px2vmin(24),
  2724. height: px2vmin(24),
  2725. position: 'relative',
  2726. zIndex: 1,
  2727. }}
  2728. />
  2729. {/* 语音发送提示 - 相对于发送按钮定位 - 只在首次录音时显示 */}
  2730. {showSendTooltip && (
  2731. <img
  2732. src={tooltipsSend}
  2733. alt="Send Tooltip"
  2734. className="chatbot-tooltips absolute pointer-events-none"
  2735. style={{
  2736. width: px2vmin(111),
  2737. height: 'auto',
  2738. bottom: '110%',
  2739. right: '-150%',
  2740. borderRadius: px2vmin(8),
  2741. opacity: 1,
  2742. zIndex: 10,
  2743. maxWidth: 'none', // 确保不被限制
  2744. maxHeight: 'none', // 确保不被限制
  2745. }}
  2746. />
  2747. )}
  2748. </div>
  2749. )}
  2750. {/* 倒计时组件 - 只在录音50秒后(最后10秒)显示 - 与按钮上下居中,位于界面最中间 */}
  2751. {currentState === 'recording' &&
  2752. remainingTimeMs !== null &&
  2753. remainingTimeMs <= COUNTDOWN_THRESHOLD_MS && (
  2754. <div
  2755. className="chatbot-countdown absolute font-semibold"
  2756. style={{
  2757. width: px2vmin(50), // 设计稿 width 50px,基于 vw/vh 最小值
  2758. height: px2vmin(20), // 设计稿 height 20px,基于 vw/vh 最小值
  2759. top: '50%', // 在容器内垂直居中,与按钮同一水平线
  2760. left: '50%', // 在容器内水平居中
  2761. transform: 'translate(-50%, -50%)', // 水平和垂直居中
  2762. zIndex: 21,
  2763. fontSize: px2vmin(12), // 设计稿 font-size 12px,基于 vw/vh 最小值
  2764. color: 'rgba(245, 243, 240, 1)',
  2765. textShadow: '0 2px 8px rgba(0,0,0,0.55)',
  2766. letterSpacing: '0.08em',
  2767. display: 'flex',
  2768. alignItems: 'center',
  2769. justifyContent: 'center',
  2770. }}
  2771. >
  2772. {formatCountdown(remainingTimeMs)}
  2773. </div>
  2774. )}
  2775. </div>
  2776. {!isConnected && currentState !== 'recording' && (
  2777. <div
  2778. className="absolute text-white/80 text-center"
  2779. style={{
  2780. top: px2vh(580), // 设计稿 top 580px,基于 vh
  2781. left: '50%',
  2782. transform: 'translateX(-50%)',
  2783. zIndex: 25,
  2784. fontSize: px2vmin(14),
  2785. textShadow: '0 2px 6px rgba(0,0,0,0.35)',
  2786. }}
  2787. >
  2788. 正在重连语音服务...
  2789. </div>
  2790. )}
  2791. {/* 能量石弹窗 */}
  2792. <MissionEnergyStoneModal
  2793. visible={showMissionStoneModal}
  2794. onClose={handleMissionStoneModalClose}
  2795. stoneType={energyStoneModalType}
  2796. />
  2797. </div>
  2798. );
  2799. };