blob: 58c910f2ed6411d13b02978a64293eb2b6d08efc [file] [log] [blame]
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +08001import React, { useCallback, useEffect } from 'react';
2import styles from './homepage.module.css';
3import { useApi } from '@/hooks/request';
4import { useSelector } from 'react-redux';
5import { RootState } from '@/store';
6import { useNavigate } from 'react-router';
7import logo from '&/assets/logo.png';
8import { getUserMessage } from '@/api/homepage';
9import request from '@/utils/request'
10import { hotPosts } from '@/api/post';
11import { postUserLogin } from '@/api/auth';
12
13interface WorkItem {
14 postId: number,
15 userId: number,
16 postTitle: string,
17 postContent: string,
18 createdAt: number,
19 postType: string,
20 viewCount: number,
21 hotScore: number,
22 lastCalculated: number
23
24}
25
26interface UserStats {
27 likes: number;
28 following: number;
29 followers: number;
30 mutualFollows: number;
31}
32
33interface UserResponse {
34 username: string;
35 inviteCode: string;
36 stats: UserStats;
37 upload: string;
38 level: string;
39 works: WorkItem[];
40 petImage: string;
41 trafficImage: string;
42}
43
44
45const Homepage: React.FC =() => {
46 const navigate = useNavigate();
47 const userInfo = useSelector((state: RootState) => state.user);
48
49 // 获取用户作品数据
50 // const {data: response, loading, error, refresh} =useApi(()=>request.get(getUserMessage), false);
51 const { data: response, loading, error, refresh } = useApi<UserResponse>(() => request.get(getUserMessage), false);
52 useEffect(() => {
53 refresh(); // 页面首次加载时触发请求
54 }, []);
55 // 用户统计数据
56 const userStats = {
57 likes: response?.stats?.likes ?? 0,
58 following: response?.stats?.following ?? 0,
59 followers: response?.stats?.followers ?? 0,
60 mutualFollows: response?.stats?.mutualFollows ?? 0,
61 uploadAmount: response?.upload ?? '--',
62 level: response?.level ?? '--'
63 };
64
65 const handleLogoClick = () => {
66 navigate('/');
67 };
68
69 return (
70 <div className={styles.container}>
71
72
73 {/* 用户信息主区域 */}
74 <div className={styles.mainContent}>
75 {/* 左侧用户信息区 */}
76 <div className={styles.userProfile}>
77 <div className={styles.userHeader}>
78 <img
79 src={userInfo.avatar || '/default-avatar.png'}
80 alt="用户头像"
81 className={styles.userAvatar}
82 />
83 <div className={styles.userInfo}>
84 <h2 className={styles.username}>阳菜,放睛!</h2>
85 <div className={styles.inviteCode}>邀请码:1314520</div>
86 <button className={styles.editButton}>编辑主页</button>
87 <button
88 className={styles.editButton}
89 onClick={() => navigate('/postDetails', {
90 state: { isNewPost: true }
91 })}
92 >
93 发布种子
94 </button>
95 </div>
96 </div>
97
98 <div className={styles.userStats}>
99 <div className={styles.statItem}>
100 <div className={styles.statNumber}>{userStats.likes}</div>
101 <div className={styles.statLabel}>获赞</div>
102 </div>
103 <div className={styles.statItem}>
104 <div className={styles.statNumber}>{userStats.following}</div>
105 <div className={styles.statLabel}>关注</div>
106 </div>
107 <div className={styles.statItem}>
108 <div className={styles.statNumber}>{userStats.followers}</div>
109 <div className={styles.statLabel}>粉丝</div>
110 </div>
111 <div className={styles.statItem}>
112 <div className={styles.statNumber}>{userStats.mutualFollows}</div>
113 <div className={styles.statLabel}>互关</div>
114 </div>
115 </div>
116
117 <div className={styles.userData}>
118 <div className={styles.dataItem}>
119 <span>您的总上传量为:</span>
120 <strong>{userStats.uploadAmount}</strong>
121 </div>
122 <div className={styles.dataItem}>
123 <span>您的用户等级为:</span>
124 <strong>{userStats.level}</strong>
125 </div>
126 </div>
127
128 <div className={styles.worksSection}>
129 <h3 className={styles.sectionTitle}>我的作品</h3>
130 {loading && <div className={styles.loading}>加载中...</div>}
131 {error && <div className={styles.error}>{error.message}</div>}
132
133 {response && response.works.map(work => (
134 <div key={work.postId} className={styles.workItem}>
135 <h4 className={styles.workTitle}>{work.postTitle}</h4>
136 <div className={styles.workMeta}>
137 <span>发布时间:{work.createdAt}</span>
138 <span>下载量:{work.viewCount} 做种数:{'待定'}</span>
139 </div>
140 </div>
141 )) }
142 </div>
143 </div>
144
145 {/* 右侧内容区 */}
146 <div className={styles.rightContent}>
147 <div className={styles.petSection}>
148 <h3 className={styles.sectionTitle}>宠物图</h3>
149 <div className={styles.petContainer}>
150 <img
151 src="/assets/pet-blue-star.png"
152 alt="蓝色星星宠物"
153 className={styles.petImage}
154 />
155 </div>
156 </div>
157
158 </div>
159 </div>
160 </div>
161 );
162};
163
164export default Homepage;