San3yuan | ff75c54 | 2025-06-06 20:30:52 +0800 | [diff] [blame] | 1 | import React, { useEffect, useRef } 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 | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame^] | 8 | import type { MenuProps } from 'antd'; |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 9 | import { useNavigate } from "react-router"; |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame^] | 10 | import { Dropdown } from "antd"; |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 11 | |
San3yuan | 8166d1b | 2025-06-05 23:15:53 +0800 | [diff] [blame] | 12 | |
San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 13 | interface SelfStatusProps { |
| 14 | className?: string; |
| 15 | } |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 16 | |
San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 17 | const SelfStatus: React.FC<SelfStatusProps> = () => { |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 18 | |
| 19 | const nav = useNavigate() |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 20 | const userName = useAppSelector(state => state.user.userName); |
| 21 | const role = useAppSelector(state => state.user.role); |
| 22 | const uploadTraffic = useAppSelector(state => state.user.uploadTraffic); |
| 23 | const downloadTraffic = useAppSelector(state => state.user.downloadTraffic); |
| 24 | const downloadPoints = useAppSelector(state => state.user.downloadPoints); |
San3yuan | 30e245f | 2025-06-07 20:04:23 +0800 | [diff] [blame] | 25 | const avatar = useAppSelector(state => state.user.avatar) || 'https://pic.baike.soso.com/ugc/baikepic2/6664/20220301143956-1127285627_png_800_800_370852.jpg/0'; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 26 | const dispatch = useAppDispatch(); |
阳菜,放晴! | 2f98704 | 2025-06-08 14:54:50 +0800 | [diff] [blame] | 27 | const { refresh } = useApi(() => request.get(getUserInfo), false); |
| 28 | const fenchData = async () => { |
| 29 | const data = await refresh(); |
| 30 | console.log(data) |
| 31 | |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 32 | if (data) { |
阳菜,放晴! | 2f98704 | 2025-06-08 14:54:50 +0800 | [diff] [blame] | 33 | dispatch({ type: "user/getUserInfo", payload: data.userInfo }); |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 34 | } |
阳菜,放晴! | 2f98704 | 2025-06-08 14:54:50 +0800 | [diff] [blame] | 35 | } |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame^] | 36 | |
| 37 | const logOut = () =>{ |
| 38 | dispatch({type:"user/logout"}) |
| 39 | nav('/') |
| 40 | } |
| 41 | |
| 42 | const menu: MenuProps['items'] =[ |
| 43 | { |
| 44 | key:'1', |
| 45 | label:(<span onClick={logOut}>登出</span>) |
| 46 | } |
| 47 | ] |
阳菜,放晴! | 2f98704 | 2025-06-08 14:54:50 +0800 | [diff] [blame] | 48 | useEffect(() => { |
| 49 | fenchData(); |
| 50 | |
| 51 | }, [ dispatch]); |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 52 | |
| 53 | return ( |
| 54 | <div className={style.container}> |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame^] | 55 | <div className={style.left} |
| 56 | |
| 57 | > |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 58 | {avatar && avatar.length > 0 ? ( |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame^] | 59 | <img className={style.avatar} onClick={() => nav('/homepage')} style={{ cursor: 'pointer'}} src={avatar} alt="User Avatar" />):null} |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 60 | </div> |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 61 | <div className={style.right}> |
| 62 | <div className={style.info}> |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame^] | 63 | <Dropdown menu={{ items: menu }}> |
| 64 | <p className={style.userName}>{userName}</p> |
| 65 | </Dropdown> |
| 66 | <p className={style.role}>用户组: {role && role.trim().length? role:'N/A'}</p> |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 67 | <p className={style.uploadTraffic}>上传量: {uploadTraffic ? uploadTraffic : 0}</p> |
| 68 | <p className={style.downloadTraffic}>下载量: {downloadTraffic ? downloadTraffic : 0}</p> |
San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 69 | |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 70 | <p className={style.shareRatio}> |
| 71 | 分享率: {uploadTraffic && downloadTraffic ? (uploadTraffic / downloadTraffic).toFixed(2) : "N/A"} |
| 72 | </p> |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 73 | <p className={style.downloadPoints}>下载积分: {downloadPoints ? downloadPoints : 0}</p> |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 74 | </div> |
| 75 | <button className={style.signInButton}>签到</button> |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 76 | </div> |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 77 | </div> |
| 78 | ); |
| 79 | }; |
San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 80 | |
San3yuan | 2534d42 | 2025-04-08 21:43:18 +0800 | [diff] [blame] | 81 | export default SelfStatus; |