tiefen_space_h5/components/BottomNav/index.js

162 lines
4.5 KiB
JavaScript
Raw Normal View History

2024-08-13 20:00:46 +08:00
"use client";
2024-06-25 20:18:37 +08:00
2024-12-13 18:24:36 +08:00
import React, { useEffect } from "react";
2024-12-20 20:47:20 +08:00
import { TabBar, Toast, Badge } from "antd-mobile";
2024-08-13 20:00:46 +08:00
import "./index.css";
2024-12-13 18:24:36 +08:00
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import WebSocketComponent from "../Websocket";
import { changeNoticeCount, changeInviter } from "@/store/actions";
import { connect } from "react-redux";
import { get } from "@/utils/storeInfo";
import requireAPI from "@/utils/requireAPI";
2024-12-19 18:33:13 +08:00
import OwnIcon from "../OwnIcon";
2024-12-13 18:24:36 +08:00
function BottomNav({ changeNoticeCount, changeInviter, noticeCount }) {
const searchParams = useSearchParams();
2024-08-13 20:00:46 +08:00
const pathname = usePathname();
const router = useRouter();
2024-12-13 18:24:36 +08:00
useEffect(() => {
const currentInviter = searchParams.get("inviter");
if (currentInviter) {
2024-12-20 20:47:20 +08:00
console.log("Current Inviter: " + currentInviter);
2024-12-13 18:24:36 +08:00
changeInviter(currentInviter);
}
}, [pathname]);
useEffect(() => {
const getDtata = async () => {
try {
const { mid } = get("account");
const body = {
mid,
};
const _data = await requireAPI(
"POST",
"/api/notification/get_unread_count",
{
body,
}
);
if (_data.ret === -1) {
return;
}
2024-12-24 18:39:36 +08:00
changeNoticeCount(_data.data.total);
2024-12-13 18:24:36 +08:00
} catch (error) {}
};
getDtata();
return () => {};
2024-12-24 20:21:53 +08:00
}, [noticeCount, pathname]);
2024-08-13 20:00:46 +08:00
const setRouteActive = (value) => {
router.replace(value);
};
const tabs = [
{
key: "/",
title: "广场",
2024-12-20 20:47:20 +08:00
icon: <OwnIcon lazy className="w-8 h-8" src="/icons/postblur.png" />,
activeIcon: (
<OwnIcon lazy className="w-8 h-8" src="/icons/postfocus.png" />
),
2024-08-13 20:00:46 +08:00
},
{
key: "/space",
title: "空间",
2024-12-20 20:47:20 +08:00
icon: <OwnIcon lazy className="w-8 h-8" src="/icons/space_blur.png" />,
activeIcon: (
<OwnIcon lazy className="w-8 h-8" src="/icons/space_focus.png" />
),
2024-08-13 20:00:46 +08:00
},
2024-12-13 18:24:36 +08:00
{
key: "/noticeDetail",
title: "消息",
icon: (
2024-12-19 18:33:13 +08:00
<div className="relative">
2024-12-20 20:47:20 +08:00
<OwnIcon lazy className="w-8 h-8" src="/icons/streamblur.png" />
2024-12-13 18:24:36 +08:00
{!!noticeCount && (
<Badge
content={noticeCount > 99 ? "+99" : noticeCount}
2024-12-19 18:33:13 +08:00
className="absolute top-0 right-0 z-10"
2024-12-13 18:24:36 +08:00
/>
)}
</div>
),
activeIcon: (
2024-12-19 18:33:13 +08:00
<div className="relative">
2024-12-20 20:47:20 +08:00
<OwnIcon lazy className="w-8 h-8" src="/icons/streamfocus.png" />
2024-12-13 18:24:36 +08:00
{!!noticeCount && (
<Badge
content={noticeCount > 99 ? "+99" : noticeCount}
2024-12-19 18:33:13 +08:00
className="absolute top-0 right-0 z-10"
2024-12-13 18:24:36 +08:00
/>
)}
</div>
),
},
2024-08-13 20:00:46 +08:00
{
key: "/my",
title: "我的",
2024-12-20 20:47:20 +08:00
icon: <OwnIcon lazy className="w-8 h-8" src="/icons/myblur.png" />,
activeIcon: <OwnIcon lazy className="w-8 h-8" src="/icons/myfocus.png" />,
2024-08-13 20:00:46 +08:00
},
];
const checkPath = () => {
2024-12-13 18:24:36 +08:00
const pathnames = ["/", "/space", "/noticeDetail", "/my"];
2024-08-13 20:00:46 +08:00
const isActive = pathnames.some((path) => path === pathname);
return isActive;
};
2024-12-13 18:24:36 +08:00
const handleGetWebsocketData = (data) => {
if (data.d.unread_cnt > 0) {
changeNoticeCount(data.d.unread_cnt);
const { n_type, title } = data.d.notif;
Toast.show({
type: "info",
2024-12-13 19:31:50 +08:00
maskClassName: "notice-toast",
2024-12-13 18:24:36 +08:00
content: (
2024-12-13 19:31:50 +08:00
<div className="flex flex-col gap-2">
2024-12-13 18:24:36 +08:00
<p className="text-base">{`🔔 收到一条${
n_type === 0
? "系统"
: n_type === 1
? "审核"
: n_type === 2
? "付费"
: "活动"
}通知`}</p>
2024-12-13 19:31:50 +08:00
<p className="text-sm text-[#ffffff80]">{title}</p>
2024-12-13 18:24:36 +08:00
</div>
),
position: "top",
2024-12-13 19:31:50 +08:00
duration: 5000,
2024-12-13 18:24:36 +08:00
});
}
};
2024-08-13 20:00:46 +08:00
return (
2024-12-13 18:24:36 +08:00
<>
<TabBar
activeKey={pathname}
onChange={(value) => setRouteActive(value)}
className={!checkPath() ? "hidden" : ""}
>
{tabs.map((item) => (
<TabBar.Item
key={item.key}
icon={pathname === item.key ? item.activeIcon : item.icon}
title={item.title}
className="text-[#ffffff40]"
/>
))}
</TabBar>
<WebSocketComponent getData={handleGetWebsocketData} />
</>
2024-08-13 20:00:46 +08:00
);
}
2024-12-13 18:24:36 +08:00
const mapStateToProps = ({ reducer }) => {
return {
noticeCount: reducer.noticeCount,
};
};
const mapDispatchToProps = {
changeNoticeCount,
changeInviter,
};
export default connect(mapStateToProps, mapDispatchToProps)(BottomNav);