import Taro from "@tarojs/taro";
import { useEffect, useMemo, useState } from "react";
import { View } from "@tarojs/components";
import { SafeArea, Tabbar } from "@nutui/nutui-react-taro";
import { globalStore } from "@/store/global-store";
import { CustomTheme, Icon } from "@/components";
import { business } from "@/services";
export default function CustomTabBar() {
const { tabBar, setTabBar } = globalStore((state: any) => state);
const [scaleFactor, setScaleFactor] = useState(1);
const [unreadCount, setUnreadCount] = useState(0);
const userRoleVO = globalStore((state: any) => state.userRoleVO);
useEffect(() => {
const appBaseInfo = Taro.getAppBaseInfo();
// @ts-ignore
const { fontSizeSetting, fontSizeScaleFactor } = appBaseInfo;
if (fontSizeScaleFactor == 1) {
setScaleFactor(fontSizeSetting / 17);
} else {
setScaleFactor(fontSizeScaleFactor);
}
}, []);
// 获取未读消息数量
const fetchUnreadCount = async () => {
try {
const { data } = await business.messageReceiver.getUnreadCount({
messageReceiverUnreadCountQry: {},
});
if (data.success && data.data !== undefined) {
setUnreadCount(data.data);
}
} catch (error) {
console.error("获取未读消息数量失败:", error);
}
};
// 初始化时获取未读消息数量
useEffect(() => {
fetchUnreadCount();
}, []);
// 每30秒刷新一次未读消息数量
useEffect(() => {
const timer = setInterval(() => {
fetchUnreadCount();
}, 30000); // 30秒
return () => clearInterval(timer);
}, []);
const tabBarList = useMemo(() => {
// 如果没有用户角色信息,默认显示所有菜单项
return [
{
pagePath: "/pages/main/index/index",
selectedIconPath: ,
iconPath: (
),
text: "工作台",
},
{
pagePath: "/pages/main/menu/index",
selectedIconPath: (
),
iconPath: (
),
text: "菜单",
},
{
pagePath: "/pages/main/message/index",
selectedIconPath: ,
iconPath: (
),
text: "消息",
},
{
pagePath: "/pages/main/center/index",
selectedIconPath: ,
iconPath: (
),
text: "我的",
},
];
}, [scaleFactor, userRoleVO]);
useEffect(() => {
const selected = tabBarList.findIndex((item) => {
return (
Taro.getCurrentInstance().router?.path.indexOf(item.pagePath) !== -1
);
});
console.log("selected", selected);
setTabBar(selected);
}, [tabBarList, userRoleVO]);
const switchTab = async (index: number, pagePath: string) => {
setTabBar(index);
await Taro.switchTab({ url: pagePath });
};
return (
{
await switchTab(index, tabBarList[index].pagePath);
}}
>
{tabBarList.map((item, index) => {
const isMessageTab = item.pagePath === "/pages/main/message/index";
return (
);
})}
);
}