消息模块完善
This commit is contained in:
@@ -25,8 +25,43 @@ App({
|
||||
});
|
||||
},
|
||||
});
|
||||
//启动未读消息轮询(tabbar红点)
|
||||
this.startUnreadPolling();
|
||||
},
|
||||
onShow: function () {
|
||||
updateManager();
|
||||
//从后台切回前台,立即刷新未读红点
|
||||
this.refreshUnread();
|
||||
},
|
||||
onHide: function () {},
|
||||
globalData: {
|
||||
unread: 0, //未读消息数(tabbar红点共享缓存)
|
||||
},
|
||||
|
||||
/**
|
||||
* 未读消息轮询:小程序前台每10s刷新一次底部tabbar红点
|
||||
* (切后台后JS执行会被系统挂起,定时器自然暂停,回前台由onShow立即补一次)
|
||||
*/
|
||||
startUnreadPolling: function () {
|
||||
if (this.unreadTimer) {
|
||||
return;
|
||||
}
|
||||
this.unreadTimer = setInterval(() => {
|
||||
this.refreshUnread();
|
||||
}, 10000);
|
||||
},
|
||||
|
||||
/**
|
||||
* 刷新当前 tab 页 tabbar 实例的红点(非tab页无tabbar实例,自动跳过)
|
||||
*/
|
||||
refreshUnread: function () {
|
||||
if (!wx.getStorageSync('token')) {
|
||||
return;
|
||||
}
|
||||
const page = getCurrentPages().pop();
|
||||
const tabBar = page && typeof page.getTabBar === 'function' ? page.getTabBar() : null;
|
||||
if (tabBar && typeof tabBar.refreshUnread === 'function') {
|
||||
tabBar.refreshUnread();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@@ -107,5 +107,6 @@ module.exports = {
|
||||
MessageList: ApiRootUrl + 'message/list', //消息列表
|
||||
MessageDetail: ApiRootUrl + 'message/detail', //消息详情
|
||||
MessageSetRead: ApiRootUrl + 'message/setRead', //设为已读
|
||||
MessageUnreadCount: ApiRootUrl + 'message/unreadCount', //未读消息数(tabbar红点)
|
||||
|
||||
};
|
||||
@@ -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',
|
||||
},
|
||||
];
|
||||
];
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
// pages/message/index.js
|
||||
import { fetchMessageList, messageSetRead } from '../../../services/message/fetchMessage';
|
||||
|
||||
// 消息类型 tab:与后端 message_type 对齐(''=全部)
|
||||
const TYPE_TABS = [
|
||||
{ key: '', label: '全部' },
|
||||
{ key: '0', label: '系统消息' },
|
||||
{ key: '1', label: '公告消息' },
|
||||
{ key: '2', label: '需求消息' },
|
||||
];
|
||||
|
||||
Page({
|
||||
data: {
|
||||
tabs: TYPE_TABS,
|
||||
currentType: '', //当前筛选的消息类型:''=全部
|
||||
currentTabIndex: 0,
|
||||
unreadMap: { '0': 0, '1': 0, '2': 0 }, //分类未读数
|
||||
totalUnread: 0, //总未读数
|
||||
list: [],
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
@@ -18,51 +32,99 @@ Page({
|
||||
},
|
||||
|
||||
loadList(refresh = false) {
|
||||
let { pageSize, list } = this.data;
|
||||
let { pageSize, list, currentType } = this.data;
|
||||
const page = refresh ? 1 : this.data.page;
|
||||
|
||||
let params = {
|
||||
page: page,
|
||||
message_type: 0,
|
||||
pageSize: pageSize,
|
||||
};
|
||||
//全部时不传 message_type,由后端返回全类型
|
||||
if (currentType !== '') {
|
||||
params.message_type = currentType;
|
||||
}
|
||||
|
||||
fetchMessageList(params).then((res) => {
|
||||
console.log(res)
|
||||
let newList = res.data.data;
|
||||
let hasMore = res.data.current_page < res.data.last_page;
|
||||
let unreadMap = res.data.unread_map || { '0': 0, '1': 0, '2': 0 };
|
||||
let totalUnread = Number(unreadMap['0'] || 0) + Number(unreadMap['1'] || 0) + Number(unreadMap['2'] || 0);
|
||||
this.setData({
|
||||
list: refresh ? newList : list.concat(newList),
|
||||
page: page + 1,
|
||||
unreadCount: res.data.unread_count,
|
||||
unreadMap: unreadMap,
|
||||
totalUnread: totalUnread,
|
||||
hasMore: hasMore,
|
||||
loading: false
|
||||
});
|
||||
//同步底部tabbar红点
|
||||
this.syncTabBarUnread(totalUnread);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 同步底部tabbar消息红点
|
||||
* @param {number} count 总未读数
|
||||
*/
|
||||
syncTabBarUnread(count) {
|
||||
const tabBar = typeof this.getTabBar === 'function' && this.getTabBar();
|
||||
if (tabBar && typeof tabBar.setUnread === 'function') {
|
||||
tabBar.setUnread(count);
|
||||
}
|
||||
},
|
||||
|
||||
//切换消息类型tab
|
||||
onTabsChange(e) {
|
||||
const index = Number(e.detail.value);
|
||||
const tab = this.data.tabs[index] || this.data.tabs[0];
|
||||
this.setData({
|
||||
currentTabIndex: index,
|
||||
currentType: tab.key
|
||||
});
|
||||
this.loadList(true);
|
||||
},
|
||||
|
||||
showDialog(e) {
|
||||
const id = e.currentTarget.dataset.id;
|
||||
const item = this.data.list.find(msg => msg.id == id);
|
||||
const jumpPage = item?.extra_json?.page || '';
|
||||
this.setData({
|
||||
showTextAndTitle: true,
|
||||
currentMessage: item || {}
|
||||
currentMessage: item || {},
|
||||
//携带跳转页面时提供「前往查看」
|
||||
confirmBtn: jumpPage ? '前往查看' : '关闭'
|
||||
});
|
||||
messageSetRead({ id: id }).then((res) => {
|
||||
console.log(res)
|
||||
// 把查找到的列表中的元素的is_read字段设为1
|
||||
if (item) {
|
||||
const idx = this.data.list.findIndex(msg => msg.id == id);
|
||||
if (idx !== -1) {
|
||||
const key = `list[${idx}].is_read`;
|
||||
this.setData({ [key]: 1 });
|
||||
}
|
||||
messageSetRead({ id: id }).then(() => {
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
const idx = this.data.list.findIndex(msg => msg.id == id);
|
||||
if (idx === -1) {
|
||||
return;
|
||||
}
|
||||
//原来未读的消息:本地未读数-1并同步tabbar红点
|
||||
if (!this.data.list[idx].is_read) {
|
||||
const typeKey = String(item.message_type);
|
||||
const unreadMap = { ...this.data.unreadMap };
|
||||
unreadMap[typeKey] = Math.max(Number(unreadMap[typeKey] || 0) - 1, 0);
|
||||
const totalUnread = Math.max(this.data.totalUnread - 1, 0);
|
||||
this.setData({ unreadMap, totalUnread });
|
||||
this.syncTabBarUnread(totalUnread);
|
||||
}
|
||||
this.setData({ [`list[${idx}].is_read`]: 1 });
|
||||
});
|
||||
},
|
||||
|
||||
closeDialog() {
|
||||
//弹窗确认:有跳转页面则前往,否则仅关闭
|
||||
onDialogConfirm() {
|
||||
const jumpPage = this.data.currentMessage?.extra_json?.page || '';
|
||||
this.setData({ showTextAndTitle: false });
|
||||
if (jumpPage) {
|
||||
wx.navigateTo({
|
||||
url: '/' + String(jumpPage).replace(/^\//, '')
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
@@ -76,8 +138,7 @@ Page({
|
||||
},
|
||||
|
||||
onClearUnreadTap() {
|
||||
messageSetRead({ isAll: 1 }).then((res) => {
|
||||
console.log(res)
|
||||
messageSetRead({ isAll: 1 }).then(() => {
|
||||
wx.showToast({ title: '全部已读', icon: 'success' });
|
||||
this.loadList(true);
|
||||
});
|
||||
@@ -95,4 +156,4 @@ Page({
|
||||
},
|
||||
|
||||
onShareAppMessage() { }
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"t-badge": "tdesign-miniprogram/badge/badge",
|
||||
"t-dialog": "tdesign-miniprogram/dialog/dialog",
|
||||
"t-empty": "tdesign-miniprogram/empty/empty",
|
||||
"t-tag": "tdesign-miniprogram/tag/tag",
|
||||
"t-divider": "tdesign-miniprogram/divider/divider"
|
||||
},
|
||||
"navigationBarTitleText": "消息列表"
|
||||
|
||||
@@ -10,11 +10,26 @@
|
||||
<t-icon bindtap="onClearUnreadTap" name="clear" size="40rpx" />
|
||||
</t-col>
|
||||
</t-row>
|
||||
<t-divider />
|
||||
|
||||
<!-- 消息类型分类 -->
|
||||
<t-tabs value="{{currentTabIndex}}" bind:change="onTabsChange" swipeable="{{false}}" t-class="message-tabs">
|
||||
<t-tab-panel
|
||||
wx:for="{{tabs}}"
|
||||
wx:key="key"
|
||||
label="{{item.label}}"
|
||||
value="{{index}}"
|
||||
badge-props="{{ { count: item.key === '' ? totalUnread : unreadMap[item.key] } }}"
|
||||
/>
|
||||
</t-tabs>
|
||||
|
||||
<view class="message-list">
|
||||
<view class="message-item" wx:for="{{list}}" wx:for-item="item" wx:key="id">
|
||||
<t-cell hover bordered title="{{item.title}}" data-key="showTextAndTitle" bind:tap="showDialog" data-id="{{item.id}}">
|
||||
<t-cell hover bordered data-key="showTextAndTitle" bind:tap="showDialog" data-id="{{item.id}}">
|
||||
<t-avatar class="avatar-example" slot="image" image="{{item.image}}" badge-props="{{ {dot: !item.is_read, offset: [0, 4] } }}" />
|
||||
<view slot="title" class="msg-title">
|
||||
<text class="msg-title-text">{{item.title}}</text>
|
||||
<t-tag size="small" variant="light" class="msg-type-tag">{{item.message_type_text}}</t-tag>
|
||||
</view>
|
||||
<view slot="description">{{_index.ellipsis(item.content, 22)}}</view>
|
||||
<text slot="note">{{item.createtime_text}}</text>
|
||||
</t-cell>
|
||||
@@ -25,11 +40,12 @@
|
||||
</view>
|
||||
<view class="safe-bottom" />
|
||||
</view>
|
||||
<t-dialog visible="{{showTextAndTitle}}" title="{{currentMessage.title}}" confirm-btn="{{ confirmBtn }}" bind:confirm="closeDialog">
|
||||
<t-dialog visible="{{showTextAndTitle}}" title="{{currentMessage.title}}" confirm-btn="{{ confirmBtn }}" bind:confirm="onDialogConfirm">
|
||||
<!-- 适配skyline,增加type="list" -->
|
||||
<scroll-view slot="content" type="list" scroll-y class="long-content">
|
||||
<view class="content-container">
|
||||
<view class="content-type">{{currentMessage.message_type_text}}</view>
|
||||
{{currentMessage.content}}
|
||||
</view>
|
||||
</scroll-view>
|
||||
</t-dialog>
|
||||
</t-dialog>
|
||||
|
||||
@@ -43,4 +43,33 @@
|
||||
.empty-container {
|
||||
min-height: 100vh;
|
||||
padding-top: 50%;
|
||||
}
|
||||
|
||||
/* 消息标题行:标题 + 类型标签 */
|
||||
.msg-title {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.msg-title-text {
|
||||
margin-right: 12rpx;
|
||||
}
|
||||
|
||||
.msg-type-tag {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* 弹窗内容:类型标识 + 正文换行 */
|
||||
.content-container {
|
||||
white-space: pre-line;
|
||||
word-break: break-all;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.content-type {
|
||||
font-size: 22rpx;
|
||||
color: var(--text-secondary, #999);
|
||||
margin-bottom: 12rpx;
|
||||
}
|
||||
@@ -11,4 +11,9 @@ export function fetchMessageDetail(id) {
|
||||
/** 获取提现申请 */
|
||||
export function messageSetRead(param) {
|
||||
return wx.$util.request(wx.$api.MessageSetRead, param, 'POST');
|
||||
}
|
||||
|
||||
/** 获取未读消息数(tabbar红点) */
|
||||
export function fetchMessageUnreadCount() {
|
||||
return wx.$util.request(wx.$api.MessageUnreadCount, {}, 'GET');
|
||||
}
|
||||
Reference in New Issue
Block a user