66 lines
1.3 KiB
JavaScript
66 lines
1.3 KiB
JavaScript
import { fetchUnreadCount } from "@/api/notification";
|
|
|
|
const MESSAGE_TAB_INDEX = 3;
|
|
|
|
function parseStoredUserInfo() {
|
|
const raw = uni.getStorageSync("userInfo");
|
|
if (!raw) return {};
|
|
|
|
try {
|
|
return typeof raw === "string" ? JSON.parse(raw) : raw;
|
|
} catch (error) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function hasLoginState() {
|
|
const token = uni.getStorageSync("token");
|
|
const userInfo = parseStoredUserInfo();
|
|
return !!token || !!userInfo?.nickName;
|
|
}
|
|
|
|
export function syncMessageTabBarBadge(unreadCount = 0) {
|
|
const count = Number(unreadCount) || 0;
|
|
|
|
if (count > 0) {
|
|
const text = count > 99 ? "99+" : String(count);
|
|
uni.setTabBarBadge({
|
|
index: MESSAGE_TAB_INDEX,
|
|
text,
|
|
});
|
|
return;
|
|
}
|
|
|
|
uni.removeTabBarBadge({
|
|
index: MESSAGE_TAB_INDEX,
|
|
});
|
|
uni.hideTabBarRedDot({
|
|
index: MESSAGE_TAB_INDEX,
|
|
});
|
|
}
|
|
|
|
export async function refreshMessageTabBarBadge() {
|
|
if (!hasLoginState()) {
|
|
syncMessageTabBarBadge(0);
|
|
return 0;
|
|
}
|
|
|
|
try {
|
|
const res = await fetchUnreadCount();
|
|
const count =
|
|
Number(
|
|
res?.data?.count ??
|
|
res?.data?.unreadCount ??
|
|
res?.count ??
|
|
res?.unreadCount ??
|
|
res?.data ??
|
|
0
|
|
) || 0;
|
|
|
|
syncMessageTabBarBadge(count);
|
|
return count;
|
|
} catch (error) {
|
|
return 0;
|
|
}
|
|
}
|