Files
code-bang/frontend/miniapp/custom-tab-bar/index.js
T

109 lines
3.2 KiB
JavaScript
Raw Normal View History

2026-09-17 16:25:31 +08:00
import TabMenu from './data';
2026-10-08 16:28:21 +08:00
import { fetchMessageUnreadCount } from '../services/message/fetchMessage';
2026-09-17 16:25:31 +08:00
Component({
data: {
active: 0,
list: TabMenu,
2026-10-08 16:28:21 +08:00
unread: 0, //未读消息数,>0 时消息tab显示红点
},
lifetimes: {
attached() {
2026-10-19 15:46:10 +08:00
// 先用全局缓存渲染,避免切换 tab 时红点闪没。网络请求交给 show,并与其他实例共用节流
2026-10-08 16:28:21 +08:00
const app = getApp();
2026-10-19 15:46:10 +08:00
const unread = app && app.globalData ? Number(app.globalData.unread) || 0 : 0;
if (unread > 0) {
this.setData({ unread });
2026-10-08 16:28:21 +08:00
}
},
},
2026-10-19 15:46:10 +08:00
// 当前 tab 显示时刷新。30s 内各实例只发一次请求
2026-10-08 16:28:21 +08:00
pageLifetimes: {
show() {
this.refreshUnread();
},
2026-09-17 16:25:31 +08:00
},
methods: {
onChange(event) {
this.setData({ active: event.detail.value });
wx.switchTab({
url: this.data.list[event.detail.value].url.startsWith('/')
? this.data.list[event.detail.value].url
: `/${this.data.list[event.detail.value].url}`,
});
},
init() {
const page = getCurrentPages().pop();
const route = page ? page.route.split('?')[0] : '';
const active = this.data.list.findIndex(
(item) =>
(item.url.startsWith('/') ? item.url.substr(1) : item.url) ===
`${route}`,
);
this.setData({ active });
},
2026-10-08 16:28:21 +08:00
/**
* 从服务端拉取未读消息数
2026-10-19 15:46:10 +08:00
* 未登录时不显示红点。节流记在 globalData,切换 Tab 新建的 tabBar 实例也会共用
* 30s 内只发一次;消息页改未读数走 setUnread,不走这里
2026-10-08 16:28:21 +08:00
*/
refreshUnread(force = false) {
2026-10-19 15:46:10 +08:00
const app = getApp();
const token = (wx.$util && wx.$util.getToken && wx.$util.getToken()) || '';
if (!token) {
2026-10-08 16:28:21 +08:00
this.setData({ unread: 0 });
2026-10-19 15:46:10 +08:00
if (app && app.globalData) {
app.globalData.unread = 0;
}
2026-10-08 16:28:21 +08:00
return;
}
const now = Date.now();
2026-10-19 15:46:10 +08:00
const last = app && app.globalData ? Number(app.globalData.unreadFetchedAt) || 0 : 0;
if (!force && now - last < 30000) {
if (app && app.globalData) {
this.setData({ unread: Number(app.globalData.unread) || 0 });
}
2026-10-08 16:28:21 +08:00
return;
}
2026-10-19 15:46:10 +08:00
if (app && app.globalData && app.globalData.unreadFetching) {
2026-10-08 20:34:08 +08:00
return;
}
2026-10-19 15:46:10 +08:00
if (app && app.globalData) {
app.globalData.unreadFetching = true;
app.globalData.unreadFetchedAt = now;
}
2026-10-08 20:34:08 +08:00
const done = () => {
2026-10-19 15:46:10 +08:00
if (app && app.globalData) {
app.globalData.unreadFetching = false;
}
2026-10-08 20:34:08 +08:00
};
2026-10-08 16:28:21 +08:00
fetchMessageUnreadCount().then((res) => {
if (res.code == 1) {
const unread = Number(res.data?.unread_count) || 0;
this.setData({ unread });
2026-10-19 15:46:10 +08:00
if (app && app.globalData) {
app.globalData.unread = unread;
}
2026-10-08 16:28:21 +08:00
}
2026-10-08 20:34:08 +08:00
}).catch(() => {}).then(done, done);
2026-10-08 16:28:21 +08:00
},
/**
* 供消息页主动同步未读数(读消息/全部已读后红点实时消失)
*/
setUnread(count) {
const unread = Number(count) || 0;
this.setData({ unread });
const app = getApp();
2026-10-19 15:46:10 +08:00
if (app && app.globalData) {
app.globalData.unread = unread;
app.globalData.unreadFetchedAt = Date.now();
}
2026-10-08 16:28:21 +08:00
},
2026-09-17 16:25:31 +08:00
},
});