San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 1 | import React, { useEffect } from "react"; |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 2 | import { useAppSelector } from "../../hooks/store"; |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 3 | import style from "./style.module.css" |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 4 | import { useApi } from "@/hooks/request"; |
| 5 | import request from "@/utils/request"; |
| 6 | import { getUserInfo } from "@/api/user"; |
| 7 | import { useAppDispatch } from "@/hooks/store"; |
San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 8 | interface SelfStatusProps { |
| 9 | className?: string; |
| 10 | } |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 11 | |
San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 12 | const SelfStatus: React.FC<SelfStatusProps> = () => { |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 13 | const userName = useAppSelector(state => state.user.userName); |
| 14 | const role = useAppSelector(state => state.user.role); |
| 15 | const uploadTraffic = useAppSelector(state => state.user.uploadTraffic); |
| 16 | const downloadTraffic = useAppSelector(state => state.user.downloadTraffic); |
| 17 | const downloadPoints = useAppSelector(state => state.user.downloadPoints); |
| 18 | const avatar = useAppSelector(state => state.user.avatar); |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 19 | const dispatch = useAppDispatch(); |
| 20 | const { data, refresh } = useApi(() => request.get(getUserInfo), false); |
| 21 | useEffect(() => { |
| 22 | if (avatar.length === 0) { |
| 23 | refresh(); // 触发 API 请求 |
| 24 | } |
| 25 | }, [avatar, refresh]); |
| 26 | |
| 27 | useEffect(() => { |
| 28 | if (data) { |
| 29 | dispatch({ type: "user/getUserInfo", payload: data }); |
| 30 | } |
| 31 | }, [data, dispatch]); |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 32 | |
| 33 | return ( |
| 34 | <div className={style.container}> |
| 35 | <div className={style.left}> |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 36 | {avatar && avatar.length > 0 ? ( |
| 37 | <img className={style.avatar} src={avatar} alt="User Avatar" />):null} |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 38 | </div> |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 39 | <div className={style.right}> |
| 40 | <div className={style.info}> |
| 41 | <p className={style.userName}>{userName}</p> |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 42 | <p className={style.role}>用户组: {role && role.trim().length? role:'N/A'}</p> |
| 43 | <p className={style.uploadTraffic}>上传量: {uploadTraffic ? uploadTraffic : 0}</p> |
| 44 | <p className={style.downloadTraffic}>下载量: {downloadTraffic ? downloadTraffic : 0}</p> |
San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 45 | |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 46 | <p className={style.shareRatio}> |
| 47 | 分享率: {uploadTraffic && downloadTraffic ? (uploadTraffic / downloadTraffic).toFixed(2) : "N/A"} |
| 48 | </p> |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 49 | <p className={style.downloadPoints}>下载积分: {downloadPoints ? downloadPoints : 0}</p> |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 50 | </div> |
| 51 | <button className={style.signInButton}>签到</button> |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 52 | </div> |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 53 | </div> |
| 54 | ); |
| 55 | }; |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 56 | |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 57 | export default SelfStatus; |