116 lines
3.6 KiB
JavaScript
116 lines
3.6 KiB
JavaScript
import TabMenu from './data';
|
|
import { fetchMessageUnreadCount } from '../services/message/fetchMessage';
|
|
|
|
Component({
|
|
data: {
|
|
active: 0,
|
|
list: TabMenu,
|
|
unread: 0, //未读消息数,>0 时消息tab显示红点
|
|
},
|
|
|
|
lifetimes: {
|
|
attached() {
|
|
// 先用全局缓存渲染,避免切换 tab 时红点闪没。网络请求交给 show
|
|
const app = getApp();
|
|
const unread = app && app.globalData ? Number(app.globalData.unread) || 0 : 0;
|
|
if (unread > 0) {
|
|
this.setData({ unread });
|
|
}
|
|
},
|
|
},
|
|
|
|
// 每次显示当前 Tab 时拉一次未读数,停在页面上不再重复请求
|
|
pageLifetimes: {
|
|
show() {
|
|
const app = getApp();
|
|
if (app && app.globalData) {
|
|
app.globalData.unreadOwner = this;
|
|
}
|
|
this.refreshUnread();
|
|
},
|
|
},
|
|
|
|
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 });
|
|
},
|
|
|
|
/**
|
|
* 从服务端拉取未读消息数。未登录时不显示红点
|
|
* 进行中的请求不重复发;消息页 setUnread 会作废更早的响应,避免红点被旧结果盖回去
|
|
*/
|
|
refreshUnread() {
|
|
const app = getApp();
|
|
const token = (wx.$util && wx.$util.getToken && wx.$util.getToken()) || '';
|
|
if (!token) {
|
|
this.setData({ unread: 0 });
|
|
if (app && app.globalData) {
|
|
app.globalData.unread = 0;
|
|
}
|
|
return;
|
|
}
|
|
if (app && app.globalData && app.globalData.unreadFetching) {
|
|
this.setData({ unread: Number(app.globalData.unread) || 0 });
|
|
return;
|
|
}
|
|
const seq = ((app && app.globalData && app.globalData.unreadSeq) || 0) + 1;
|
|
if (app && app.globalData) {
|
|
app.globalData.unreadSeq = seq;
|
|
app.globalData.unreadFetching = true;
|
|
}
|
|
const done = () => {
|
|
if (app && app.globalData && app.globalData.unreadSeq === seq) {
|
|
app.globalData.unreadFetching = false;
|
|
}
|
|
};
|
|
fetchMessageUnreadCount().then((res) => {
|
|
if (app && app.globalData && app.globalData.unreadSeq !== seq) {
|
|
return;
|
|
}
|
|
if (res.code == 1) {
|
|
const unread = Number(res.data?.unread_count) || 0;
|
|
this.setData({ unread });
|
|
if (app && app.globalData) {
|
|
app.globalData.unread = unread;
|
|
// 请求返回时人可能已经切到另一个 Tab,把红点写到正在显示的那一份
|
|
const owner = app.globalData.unreadOwner;
|
|
if (owner && owner !== this && typeof owner.setData === 'function') {
|
|
owner.setData({ unread });
|
|
}
|
|
}
|
|
}
|
|
}).catch(() => {}).then(done, done);
|
|
},
|
|
|
|
/**
|
|
* 供消息页主动同步未读数(读消息/全部已读后红点实时消失)
|
|
*/
|
|
setUnread(count) {
|
|
const unread = Number(count) || 0;
|
|
this.setData({ unread });
|
|
const app = getApp();
|
|
if (app && app.globalData) {
|
|
app.globalData.unread = unread;
|
|
app.globalData.unreadSeq = (app.globalData.unreadSeq || 0) + 1;
|
|
app.globalData.unreadFetching = false;
|
|
}
|
|
},
|
|
},
|
|
});
|