87 lines
2.7 KiB
JavaScript
87 lines
2.7 KiB
JavaScript
/**
|
||
* 消息列表页 U23(重构:用 messageStore + conversation-item)
|
||
*
|
||
* 不变量(AGENTS.md):
|
||
* - 第 3 条:Mutual Match 后才能聊天——列表只展示 mutualMatch 状态的会话;
|
||
* 若 conversation.canSendMessage=false(已 Block/Unmatch),仍展示但置灰,点击进入后禁止输入。
|
||
* - 第 4 条:Block/Unmatch 后会话立即停用,UI 不删除而是显示"已停用"提示。
|
||
* - 第 6 条:单向活动意向不泄露——此页只展示已建立的 Mutual 会话,不会泄露对方是否单独选过我。
|
||
* - 第 9 条:DB 是 Source of Truth——本地不缓存完整会话历史,每次进入页面重新拉取最新列表。
|
||
*/
|
||
const { messageStore, appStore, authStore } = require('../../stores');
|
||
const { ROUTE, to } = require('../../constants');
|
||
|
||
Page({
|
||
data: {
|
||
conversations: [],
|
||
loading: false,
|
||
unreadTotal: 0,
|
||
emptyActions: [
|
||
{ text: '去今日推荐', event: 'home', variant: 'primary' },
|
||
],
|
||
},
|
||
|
||
onLoad() {
|
||
// 绑定 messageStore 字段到 page data
|
||
this._unsubMsg = messageStore.bindToPage(this, {
|
||
conversations: 'conversations',
|
||
totalUnreadCount: 'unreadTotal',
|
||
});
|
||
this._unsubLoading = messageStore.subscribe((state) => {
|
||
if (state.conversationsLoading !== this.data.loading) {
|
||
this.setData({ loading: state.conversationsLoading });
|
||
}
|
||
});
|
||
this.setData({ loading: messageStore.state.conversationsLoading });
|
||
},
|
||
|
||
onShow() {
|
||
if (!authStore.state.isLoggedIn) return;
|
||
// 每次显示重新拉取会话列表(最新的 lastMessage / 未读数会刷新)
|
||
messageStore.fetchConversations().catch((err) => appStore.toastError(err));
|
||
},
|
||
|
||
onUnload() {
|
||
this._unsubMsg && this._unsubMsg();
|
||
this._unsubLoading && this._unsubLoading();
|
||
},
|
||
|
||
onPullDownRefresh() {
|
||
messageStore.fetchConversations().finally(() => wx.stopPullDownRefresh());
|
||
},
|
||
|
||
onReachBottom() {
|
||
if (messageStore.state.conversationsHasMore) {
|
||
messageStore.loadMoreConversations().catch(() => {});
|
||
}
|
||
},
|
||
|
||
onGoChat(e) {
|
||
const { conversationId, canSendMessage } = e.detail;
|
||
if (!canSendMessage) {
|
||
appStore.toastError('当前会话已停用,无法继续聊天');
|
||
return;
|
||
}
|
||
wx.navigateTo({ url: to.chat(conversationId) });
|
||
},
|
||
|
||
onShortcutTap(e) {
|
||
const key = e.currentTarget.dataset.key;
|
||
if (key === 'new-friends') {
|
||
wx.switchTab({ url: ROUTE.TAB.DISCOVER });
|
||
return;
|
||
}
|
||
if (key === 'system') {
|
||
wx.navigateTo({ url: ROUTE.SETTINGS.NOTIFICATIONS });
|
||
return;
|
||
}
|
||
wx.navigateTo({ url: ROUTE.MAIN.FATE_CENTER });
|
||
},
|
||
|
||
onEmptyAction(e) {
|
||
if (e.detail.event === 'home') {
|
||
wx.switchTab({ url: ROUTE.TAB.HOME });
|
||
}
|
||
},
|
||
});
|