FirstLoginRewardPage.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. import { useEffect, useRef, useState, type TouchEvent } from 'react';
  2. import { useNavigate } from 'react-router-dom';
  3. import { message } from 'antd';
  4. import {
  5. setStorage,
  6. STORAGE_KEYS,
  7. getUserId,
  8. getVisitedRoutes,
  9. setCurrentBackgroundImageId,
  10. setCurrentBackgroundImageUrl,
  11. syncBackgroundImageToStorage,
  12. setHasSharedAvatar,
  13. } from '@/utils/storage';
  14. import { updateUserUIState, claimShareReward } from '@/utils/api';
  15. import { px2vmin, px2vw } from '@/utils/viewport';
  16. import { getOssUrl } from '@/utils/oss';
  17. const backButton = getOssUrl('backbutn.png');
  18. // const shareProgressBar = getOssUrl('share-progress-bar.png'); // Unused
  19. const avatarAnimaZone = getOssUrl('Lumi-cap.png');
  20. const shareBtn = getOssUrl('share-btn.png');
  21. const closeBtn = getOssUrl('close-btn.png');
  22. // const wechatIcon = getOssUrl('wechat.png'); // 已注释,未使用
  23. // const qqIcon = getOssUrl('QQ.png'); // 已注释,未使用
  24. // const saveIcon = getOssUrl('save.png'); // 已注释,未使用
  25. // const saveBtnImage = getOssUrl('share-done-claim-gift.png'); // Unused
  26. const claimGiftBtnImage = getOssUrl('claim-the-gift.png');
  27. const chevronLeftImage = getOssUrl('Chevron-L.png');
  28. const chevronRightImage = getOssUrl('Chevron-R.png');
  29. import { shareImages as fallbackShareImages } from '@/data/shareImages';
  30. import { getShareImages, type ShareImageItem } from '@/utils/api';
  31. export const FirstLoginRewardPage = () => {
  32. const navigate = useNavigate();
  33. const [showShareModal, setShowShareModal] = useState(false);
  34. const [shareImages, setShareImages] = useState<ShareImageItem[]>([]);
  35. const [activeShareIndex, setActiveShareIndex] = useState(0);
  36. const touchStartXRef = useRef<number | null>(null);
  37. useEffect(() => {
  38. // 记录用户当前所在页面
  39. setStorage(STORAGE_KEYS.CURRENT_PAGE, 'first-login-reward');
  40. const visited = getVisitedRoutes();
  41. const userId = getUserId();
  42. if (userId) {
  43. updateUserUIState(userId, {
  44. current_page: 'first-login-reward',
  45. visited_routes: visited,
  46. }).catch(() => {
  47. // 同步失败不影响前端流程
  48. });
  49. }
  50. }, []);
  51. useEffect(() => {
  52. const fetchShareImages = async () => {
  53. try {
  54. const res = await getShareImages();
  55. if (res.success && res.data?.length) {
  56. setShareImages(res.data);
  57. setActiveShareIndex(0);
  58. return;
  59. }
  60. } catch (error) {
  61. // fallback below
  62. }
  63. setShareImages(fallbackShareImages);
  64. setActiveShareIndex(0);
  65. };
  66. fetchShareImages();
  67. }, []);
  68. // 预加载分享图片
  69. useEffect(() => {
  70. if (shareImages.length === 0) {
  71. return;
  72. }
  73. const preloadImages = () => {
  74. shareImages.forEach((item) => {
  75. if (item.url) {
  76. const img = new Image();
  77. img.src = item.url;
  78. }
  79. });
  80. };
  81. preloadImages();
  82. }, [shareImages]);
  83. const handleBack = () => {
  84. navigate('/chatbot');
  85. };
  86. const handleShareClick = () => {
  87. if (!shareImages.length) {
  88. return;
  89. }
  90. setShowShareModal(true);
  91. };
  92. const handleShareClose = () => {
  93. setShowShareModal(false);
  94. setActiveShareIndex(0);
  95. };
  96. // 领取分享奖励
  97. const handleSaveImage = async () => {
  98. try {
  99. const userId = getUserId();
  100. if (!userId) {
  101. message.error('用户未登录,请先登录');
  102. return;
  103. }
  104. message.loading('正在领取分享奖励...', 0);
  105. // 调用分享奖励接口
  106. const result = await claimShareReward(userId);
  107. message.destroy();
  108. if (result.success && result.data) {
  109. // 显式更新 localStorage 中的背景图片相关变量
  110. const backgroundImageId = result.data.background_image_id || 'happynewyear';
  111. const backgroundImageMetadata = result.data.background_image_metadata || { opacity: 0.5 };
  112. // 同步背景图片到 localStorage(包括 ID、URL 和 metadata)
  113. syncBackgroundImageToStorage(backgroundImageId, backgroundImageMetadata);
  114. // 确保背景图片 ID 和 URL 都已更新
  115. setCurrentBackgroundImageId(backgroundImageId);
  116. // 获取背景图片 URL(从映射中获取)
  117. const { getBackgroundImageUrlByIdSync, loadBackgroundImageConfig } = await import('@/utils/storage');
  118. // 确保配置已加载
  119. await loadBackgroundImageConfig();
  120. const backgroundImageUrl = getBackgroundImageUrlByIdSync(backgroundImageId);
  121. if (backgroundImageUrl) {
  122. setCurrentBackgroundImageUrl(backgroundImageUrl);
  123. }
  124. // 更新分享头像状态:更新 localStorage
  125. // 注意:数据库中的 has_shared_avatar 已在 claimShareReward 接口中更新
  126. setHasSharedAvatar(true);
  127. // 额外同步一次确保数据一致性(claimShareReward 接口已更新,这里作为双重保障)
  128. try {
  129. await updateUserUIState(userId, {
  130. has_shared_avatar: true,
  131. });
  132. console.log('✅ 分享头像状态已同步到数据库');
  133. } catch (error) {
  134. console.error('⚠️ 同步分享头像状态到数据库失败(claimShareReward 接口已更新,不影响主流程):', error);
  135. // 即使同步失败,claimShareReward 接口已经更新了数据库,不影响主流程
  136. }
  137. message.success('分享奖励已领取!新年背景已解锁');
  138. // 跳转到分享成功页面
  139. setTimeout(() => {
  140. navigate('/share-success');
  141. }, 1000);
  142. } else {
  143. message.error(result.message || '领取分享奖励失败,请重试');
  144. }
  145. } catch (error) {
  146. console.error('领取分享奖励失败:', error);
  147. message.destroy();
  148. message.error('领取分享奖励失败,请重试');
  149. }
  150. };
  151. const handleTouchStart = (event: TouchEvent<HTMLDivElement>) => {
  152. touchStartXRef.current = event.touches[0]?.clientX ?? null;
  153. };
  154. const handleTouchEnd = (event: TouchEvent<HTMLDivElement>) => {
  155. const startX = touchStartXRef.current;
  156. if (startX === null) {
  157. return;
  158. }
  159. if (!shareImages.length) {
  160. return;
  161. }
  162. const endX = event.changedTouches[0]?.clientX ?? startX;
  163. const deltaX = endX - startX;
  164. const threshold = 30;
  165. if (deltaX > threshold && activeShareIndex > 0) {
  166. setActiveShareIndex((prev) => Math.max(prev - 1, 0));
  167. } else if (deltaX < -threshold && activeShareIndex < shareImages.length - 1) {
  168. setActiveShareIndex((prev) => Math.min(prev + 1, shareImages.length - 1));
  169. }
  170. touchStartXRef.current = null;
  171. };
  172. const handlePreviousImage = () => {
  173. if (activeShareIndex > 0) {
  174. setActiveShareIndex((prev) => prev - 1);
  175. }
  176. };
  177. const handleNextImage = () => {
  178. if (activeShareIndex < shareImages.length - 1) {
  179. setActiveShareIndex((prev) => prev + 1);
  180. }
  181. };
  182. useEffect(() => {
  183. if (showShareModal) {
  184. const previousOverflow = document.body.style.overflow;
  185. document.body.style.overflow = 'hidden';
  186. return () => {
  187. document.body.style.overflow = previousOverflow;
  188. };
  189. }
  190. return undefined;
  191. }, [showShareModal]);
  192. return (
  193. <div>
  194. {/* 顶部返回按钮 */}
  195. <button
  196. type="button"
  197. onClick={handleBack}
  198. style={{
  199. position: 'absolute',
  200. left: px2vmin(16),
  201. top: px2vmin(56),
  202. width: px2vmin(48),
  203. height: px2vmin(48),
  204. padding: 0,
  205. border: 'none',
  206. background: 'none',
  207. }}
  208. aria-label="Back"
  209. >
  210. <img
  211. src={backButton}
  212. alt="Back"
  213. style={{
  214. width: '100%',
  215. height: '100%',
  216. display: 'block',
  217. }}
  218. />
  219. </button>
  220. {/* 顶部进度条 */}
  221. <img
  222. src={getOssUrl('02-one-gift.gif')}
  223. alt="share progress bar"
  224. style={{
  225. position: 'absolute',
  226. left: '50%',
  227. top: px2vmin(64),
  228. width: px2vmin(195),
  229. transform: 'translateX(-50%)',
  230. }}
  231. />
  232. {/* 中部头像动画区域 */}
  233. <img
  234. src={avatarAnimaZone}
  235. alt="avatar anima zone"
  236. style={{
  237. position: 'absolute',
  238. left: '50%',
  239. top: px2vmin(192),
  240. width: px2vmin(343),
  241. transform: 'translateX(-50%)',
  242. }}
  243. />
  244. {!showShareModal && (
  245. <>
  246. {/* 标题文案 */}
  247. <div
  248. style={{
  249. position: 'absolute',
  250. left: '50%',
  251. top: px2vmin(504),
  252. transform: 'translateX(-50%)',
  253. color: '#FFFFFF',
  254. fontSize: px2vmin(24),
  255. fontWeight: 'bold',
  256. whiteSpace: 'nowrap',
  257. }}
  258. >
  259. Congratulation!
  260. </div>
  261. {/* 描述文案 */}
  262. <div
  263. style={{
  264. position: 'absolute',
  265. left: '50%',
  266. top: px2vmin(552),
  267. transform: 'translateX(-50%)',
  268. color: '#FFFFFF',
  269. fontSize: px2vmin(14),
  270. textAlign: 'center',
  271. width: px2vmin(230),
  272. }}
  273. >
  274. 哇,是一顶小帽子,谢谢你!要到新年了,快来分享我的新装扮并解锁限定新年背景吧
  275. </div>
  276. {/* 分享按钮 */}
  277. <button
  278. type="button"
  279. onClick={handleShareClick}
  280. style={{
  281. position: 'absolute',
  282. left: '50%',
  283. top: px2vmin(656),
  284. transform: 'translateX(-50%)',
  285. border: 'none',
  286. padding: 0,
  287. background: 'none',
  288. width: px2vmin(279),
  289. height: px2vmin(56),
  290. }}
  291. >
  292. <img
  293. src={shareBtn}
  294. alt="Share"
  295. style={{
  296. width: '100%',
  297. height: '100%',
  298. display: 'block',
  299. }}
  300. />
  301. </button>
  302. </>
  303. )}
  304. {showShareModal && (
  305. <>
  306. {/* 分享图片区域 - 在弹窗上方显示 */}
  307. <div
  308. style={{
  309. position: 'fixed',
  310. inset: 0,
  311. zIndex: 9997,
  312. display: 'flex',
  313. justifyContent: 'center',
  314. alignItems: 'flex-start',
  315. paddingTop: px2vmin(118),
  316. pointerEvents: 'none',
  317. }}
  318. >
  319. <div
  320. style={{
  321. position: 'relative',
  322. width: px2vw(375),
  323. display: 'flex',
  324. justifyContent: 'center',
  325. }}
  326. >
  327. {shareImages.length > 0 && (
  328. <div
  329. style={{
  330. position: 'relative',
  331. display: 'flex',
  332. alignItems: 'center',
  333. justifyContent: 'center',
  334. }}
  335. >
  336. {/* 图片容器 */}
  337. <div
  338. onTouchStart={handleTouchStart}
  339. onTouchEnd={handleTouchEnd}
  340. style={{
  341. position: 'relative',
  342. width: px2vmin(311),
  343. display: 'flex',
  344. justifyContent: 'center',
  345. overflow: 'visible',
  346. pointerEvents: 'auto',
  347. }}
  348. >
  349. {/* 左侧按钮 */}
  350. {activeShareIndex > 0 && (
  351. <button
  352. type="button"
  353. onClick={handlePreviousImage}
  354. style={{
  355. position: 'absolute',
  356. left: px2vmin(-12),
  357. top: '50%',
  358. transform: 'translateY(-50%)',
  359. zIndex: 10001,
  360. border: 'none',
  361. padding: 0,
  362. background: 'none',
  363. width: px2vmin(24),
  364. height: px2vmin(24),
  365. cursor: 'pointer',
  366. pointerEvents: 'auto',
  367. display: 'flex',
  368. alignItems: 'center',
  369. justifyContent: 'center',
  370. }}
  371. aria-label="Previous image"
  372. >
  373. <img
  374. src={chevronLeftImage}
  375. alt="Previous"
  376. style={{
  377. width: '100%',
  378. height: '100%',
  379. display: 'block',
  380. }}
  381. />
  382. </button>
  383. )}
  384. <button
  385. type="button"
  386. onClick={handleShareClose}
  387. style={{
  388. position: 'absolute',
  389. right: 0,
  390. top: 0,
  391. transform: 'translate(-10%, -130%)',
  392. width: px2vmin(30),
  393. height: px2vmin(30),
  394. padding: 0,
  395. border: 'none',
  396. background: 'none',
  397. pointerEvents: 'auto',
  398. zIndex: 10000,
  399. }}
  400. aria-label="Close share modal"
  401. >
  402. <img
  403. src={closeBtn}
  404. alt="Close"
  405. style={{ width: '100%', height: '100%', display: 'block' }}
  406. />
  407. </button>
  408. <div
  409. style={{
  410. position: 'relative',
  411. width: '100%',
  412. display: 'flex',
  413. justifyContent: 'center',
  414. overflow: 'visible',
  415. }}
  416. >
  417. {activeShareIndex > 0 && (
  418. <img
  419. src={shareImages[activeShareIndex - 1]?.url}
  420. alt="Previous share preview"
  421. aria-hidden
  422. style={{
  423. position: 'absolute',
  424. height: 'auto',
  425. maxWidth: '100%',
  426. opacity: 1,
  427. pointerEvents: 'none',
  428. zIndex: 0,
  429. }}
  430. />
  431. )}
  432. {activeShareIndex < shareImages.length - 1 && (
  433. <img
  434. src={shareImages[activeShareIndex + 1]?.url}
  435. alt="Next share preview"
  436. aria-hidden
  437. style={{
  438. position: 'absolute',
  439. height: 'auto',
  440. maxWidth: '100%',
  441. opacity: 1,
  442. pointerEvents: 'none',
  443. zIndex: 0,
  444. }}
  445. />
  446. )}
  447. {/* 当前图片的旋转副本(作为背景) */}
  448. <img
  449. src={shareImages[activeShareIndex]?.url}
  450. alt="Share preview background"
  451. aria-hidden
  452. style={{
  453. position: 'absolute',
  454. left: '50%',
  455. top: 0,
  456. width: '100%',
  457. height: 'auto',
  458. display: 'block',
  459. borderRadius: px2vmin(12),
  460. boxShadow: 'none',
  461. transformOrigin: 'left bottom',
  462. transform: 'translateX(-50%) rotate(1.55deg)',
  463. zIndex: 0.5,
  464. pointerEvents: 'none',
  465. }}
  466. />
  467. {/* 当前图片 */}
  468. <img
  469. src={shareImages[activeShareIndex]?.url}
  470. alt="Share preview"
  471. style={{
  472. width: '100%',
  473. height: 'auto',
  474. display: 'block',
  475. borderRadius: px2vmin(12),
  476. boxShadow: 'none',
  477. position: 'relative',
  478. zIndex: 1,
  479. }}
  480. />
  481. </div>
  482. {/* 右侧按钮 */}
  483. {activeShareIndex < shareImages.length - 1 && (
  484. <button
  485. type="button"
  486. onClick={handleNextImage}
  487. style={{
  488. position: 'absolute',
  489. right: px2vmin(-12),
  490. top: '50%',
  491. transform: 'translateY(-50%)',
  492. zIndex: 10001,
  493. border: 'none',
  494. padding: 0,
  495. background: 'none',
  496. width: px2vmin(24),
  497. height: px2vmin(24),
  498. cursor: 'pointer',
  499. pointerEvents: 'auto',
  500. display: 'flex',
  501. alignItems: 'center',
  502. justifyContent: 'center',
  503. }}
  504. aria-label="Next image"
  505. >
  506. <img
  507. src={chevronRightImage}
  508. alt="Next"
  509. style={{
  510. width: '100%',
  511. height: '100%',
  512. display: 'block',
  513. }}
  514. />
  515. </button>
  516. )}
  517. </div>
  518. </div>
  519. )}
  520. {shareImages.length > 0 && (
  521. <div
  522. style={{
  523. position: 'absolute',
  524. top: px2vmin(514),
  525. left: '50%',
  526. transform: 'translateX(-50%)',
  527. display: 'flex',
  528. alignItems: 'center',
  529. gap: px2vmin(8),
  530. pointerEvents: 'auto',
  531. }}
  532. >
  533. {shareImages.map((item, index) => {
  534. const isActive = index === activeShareIndex;
  535. return (
  536. <button
  537. key={item.id}
  538. type="button"
  539. aria-label={`Select share image ${index + 1}`}
  540. onClick={() => setActiveShareIndex(index)}
  541. style={{
  542. width: isActive ? px2vmin(24) : px2vmin(6),
  543. height: px2vmin(6),
  544. borderRadius: isActive ? px2vmin(20) : px2vmin(6),
  545. background: isActive ? 'rgba(169, 132, 228, 1)' : 'rgba(232, 222, 248, 1)',
  546. border: 'none',
  547. padding: 0,
  548. }}
  549. />
  550. );
  551. })}
  552. </div>
  553. )}
  554. </div>
  555. </div>
  556. {/* 底部弹窗 */}
  557. {shareImages.length > 0 && (
  558. <div
  559. style={{
  560. position: 'fixed',
  561. bottom: 0,
  562. left: '50%',
  563. transform: 'translateX(-50%)',
  564. width: px2vw(375),
  565. height: px2vmin(162),
  566. backgroundColor: '#34313D',
  567. borderTopLeftRadius: px2vmin(32),
  568. borderTopRightRadius: px2vmin(32),
  569. zIndex: 9999,
  570. display: 'flex',
  571. flexDirection: 'column',
  572. alignItems: 'center',
  573. paddingTop: px2vmin(12),
  574. paddingBottom: px2vmin(12),
  575. }}
  576. >
  577. {/* 顶部拖拽指示条 */}
  578. <div
  579. style={{
  580. width: px2vmin(32),
  581. height: px2vmin(4),
  582. backgroundColor: '#79747E',
  583. borderRadius: px2vmin(4),
  584. flexShrink: 0,
  585. }}
  586. />
  587. {/* 提示文字和按钮 */}
  588. <div
  589. style={{
  590. flex: 1,
  591. display: 'flex',
  592. flexDirection: 'column',
  593. alignItems: 'center',
  594. justifyContent: 'center',
  595. width: '100%',
  596. }}
  597. >
  598. {/* 提示文字 */}
  599. <div
  600. style={{
  601. color: '#FFFFFF',
  602. textAlign: 'center',
  603. lineHeight: px2vmin(14),
  604. marginBottom: px2vmin(18),
  605. }}
  606. >
  607. 长按上方图片保存到相册后领取奖励
  608. </div>
  609. {/* 领取奖励按钮 */}
  610. <button
  611. type="button"
  612. onClick={handleSaveImage}
  613. style={{
  614. border: 'none',
  615. padding: 0,
  616. background: 'none',
  617. height: px2vmin(36),
  618. cursor: 'pointer',
  619. }}
  620. >
  621. <img
  622. src={claimGiftBtnImage}
  623. alt="Claim Gift"
  624. style={{
  625. height: '100%',
  626. display: 'block',
  627. }}
  628. />
  629. </button>
  630. </div>
  631. </div>
  632. )}
  633. </>
  634. )}
  635. </div>
  636. );
  637. };