消息模块完善

This commit is contained in:
Dev
2026-10-08 16:28:21 +08:00
parent 379d29a356
commit 107cbbe971
17 changed files with 401 additions and 28 deletions
+5 -1
View File
@@ -1,21 +1,25 @@
export default [{
key: 'home',
icon: 'home',
text: '首页',
url: 'pages/home/home',
},
{
key: 'goods',
icon: 'sort',
text: '软件',
url: 'pages/goods/list/index',
},
{
key: 'message',
icon: 'comment',
text: '消息',
url: 'pages/message/list/index',
},
{
key: 'usercenter',
icon: 'person',
text: '个人中心',
url: 'pages/usercenter/index',
},
];
];
+57
View File
@@ -1,8 +1,28 @@
import TabMenu from './data';
import { fetchMessageUnreadCount } from '../services/message/fetchMessage';
Component({
data: {
active: 0,
list: TabMenu,
unread: 0, //未读消息数,>0 时消息tab显示红点
},
lifetimes: {
attached() {
//先用 globalData 缓存渲染,避免切换 tab 时红点闪没,再拉取最新
const app = getApp();
if (app && Number(app.globalData?.unread) > 0) {
this.setData({ unread: Number(app.globalData.unread) });
}
this.refreshUnread();
},
},
//所在 tab 页面显示时刷新(切换 tab、从其他页面返回均会触发)
pageLifetimes: {
show() {
this.refreshUnread();
},
},
methods: {
@@ -25,5 +45,42 @@ Component({
);
this.setData({ active });
},
/**
* 从服务端拉取未读消息数
* 未登录/请求失败时保持 0(不显示红点),不影响 tabbar 正常使用
* 5s 内重复触发只发一次请求(app轮询/页面onShow/tabbar show 多处触发)
*/
refreshUnread(force = false) {
if (!wx.getStorageSync('token')) {
this.setData({ unread: 0 });
const app = getApp();
if (app) app.globalData.unread = 0;
return;
}
const now = Date.now();
if (!force && now - (this._lastFetchAt || 0) < 5000) {
return;
}
this._lastFetchAt = now;
fetchMessageUnreadCount().then((res) => {
if (res.code == 1) {
const unread = Number(res.data?.unread_count) || 0;
this.setData({ unread });
const app = getApp();
if (app) app.globalData.unread = unread;
}
}).catch(() => {});
},
/**
* 供消息页主动同步未读数(读消息/全部已读后红点实时消失)
*/
setUnread(count) {
const unread = Number(count) || 0;
this.setData({ unread });
const app = getApp();
if (app) app.globalData.unread = unread;
},
},
});
+5 -1
View File
@@ -11,7 +11,11 @@
wx:key="index"
>
<view class="custom-tab-bar-wrapper">
<t-icon prefix="wr" name="{{item.icon}}" size="48rpx" />
<view class="tab-icon-wrap">
<t-icon prefix="wr" name="{{item.icon}}" size="48rpx" />
<!-- 消息tab未读红点 -->
<view wx:if="{{item.key === 'message' && unread > 0}}" class="unread-dot"></view>
</view>
<view class="text">{{ item.text }}</view>
</view>
</t-tab-bar-item>
@@ -11,6 +11,24 @@
margin-top: 4rpx;
}
/* tab 图标容器:红点定位基准 */
.tab-icon-wrap {
position: relative;
}
/* 消息tab未读红点 */
.unread-dot {
position: absolute;
top: -2rpx;
right: -14rpx;
width: 16rpx;
height: 16rpx;
border-radius: 50%;
background: #fa5151;
border: 2rpx solid #ffffff;
box-sizing: content-box;
}
/* 底部导航整体:顶部细分隔线 + 安全区自适应 */
.custom-tab-bar {
background: rgba(255, 255, 255, 0.96);