import { useEffect, useState } from "react"; import { ImageUploader, Toast, Mask, Image } from "antd-mobile"; import { useNavigate } from "react-router-dom"; import { user as member } from "../../api/index"; import { getDepName, studyTimeFormat } from "../../utils/index"; import { loginAction, logoutAction } from "../../store/user/loginUserSlice"; import { ImageUploadItem } from "antd-mobile/es/components/image-uploader"; import styles from "./index.module.scss"; import { useDispatch, useSelector } from "react-redux"; import { TabBarFooter } from "../../components"; import moreIcon from "../../assets/images/commen/icon-more.png"; const MemberPage = () => { const dispatch = useDispatch(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [currentDepartment, setCurrentDepartment] = useState(""); const [visible, setVisible] = useState(false); const [stats, setStats] = useState({}); const [fileList, setFileList] = useState([ { url: "", }, ]); const user = useSelector((state: any) => state.loginUser.value.user); const departments = useSelector( (state: any) => state.loginUser.value.departments ); const currentDepId = useSelector( (state: any) => state.loginUser.value.currentDepId ); useEffect(() => { document.title = "我的"; }, []); useEffect(() => { if (departments.length > 0) { setCurrentDepartment(getDepName() || departments[0].name); } }, [departments]); useEffect(() => { if (currentDepId === 0) { return; } getData(); }, [currentDepId, user]); const getData = () => { setLoading(true); member.courses(currentDepId, 0).then((res: any) => { setStats(res.data.stats); setLoading(false); }); }; const setClick = () => { setVisible(true); }; const getTotal = (num1: number, num2: number) => { let value = 0; if (num1) { value = value + num1; } if (num2 && num2 > 0) { value = value + num2; } return value; }; const beforeUpload = (file: File) => { if (file.size > 2 * 1024 * 1024) { Toast.show("超过2M限制,不允许上传"); return null; } return file; }; const propsUpload = async (file: File) => { console.log(file); member.avatar(file).then((res: any) => { Toast.show("头像更换成功"); getData(); }); }; const mockUpload = async (file: File) => { setVisible(false); const data = new FormData(); data.append("file", file); try { let res = await member.avatar(data); if (res) { Toast.show("头像更换成功"); getUser(); } } catch (e) { console.error("上传失败", e); } return { url: URL.createObjectURL(file), }; }; const getUser = () => { member.detail().then((res: any) => { const data = res.data; dispatch(loginAction(data)); setFileList([]); }); }; return (
{user && user.name && ( <>
{user.name}
{currentDepartment}
)}
setClick()} src={moreIcon} />
{studyTimeFormat(stats.today_learn_duration)[0] !== 0 && ( <> {studyTimeFormat(stats.today_learn_duration)[0] || 0}{" "} 天 )} {studyTimeFormat(stats.today_learn_duration)[1] !== 0 && ( <> {" "} {studyTimeFormat(stats.today_learn_duration)[1] || 0}{" "} 时 )} {" "} {studyTimeFormat(stats.today_learn_duration)[2] || 0}{" "} {" "} {studyTimeFormat(stats.today_learn_duration)[3] || 0}{" "} 今日学习
{studyTimeFormat(stats.learn_duration || 0)[0] !== 0 && ( <> {studyTimeFormat(stats.learn_duration || 0)[0] || 0}{" "} 天 )} {studyTimeFormat(stats.learn_duration || 0)[1] !== 0 && ( <> {" "} {studyTimeFormat(stats.learn_duration || 0)[1] || 0}{" "} 时 )} {" "} {studyTimeFormat(stats.learn_duration || 0)[2] || 0}{" "} {" "} {studyTimeFormat(stats.learn_duration || 0)[3] || 0}{" "} 累计学习
所在部门
{currentDepartment}
课时总进度
{getTotal( stats.required_finished_course_count, stats.nun_required_finished_course_count )}{" "} /{" "} {getTotal( stats.required_course_count, stats.nun_required_course_count )}
必修课
{stats.required_finished_course_count || 0} /{" "} {stats.required_course_count || 0}
{stats.nun_required_course_count > 0 && (
选修课
{stats.nun_required_finished_course_count || 0}{" "} / {stats.nun_required_course_count || 0}
)}
「PlayEdu提供技术支持」
{ setVisible(false); }} >
{ setVisible(false); if (departments.length === 1) { Toast.show({ content: "暂无可切换部门", }); return; } navigate("/change-department"); }} > 切换部门
更换头像
{ setVisible(false); navigate("/change-password"); }} > 修改密码
{ setVisible(false); dispatch(logoutAction()); window.location.href = "/login"; }} > 退出登录
); }; export default MemberPage;