refactor(im): 对齐文赫电竞消息系统(配对群ID+稳定监听)
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
const app = getApp();
|
||||
import { formatDate } from '../../static/lib/utils';
|
||||
import { resolveAvatarUrl, getDefaultAvatarUrl, resolveAvatarFromStorage } from '../../utils/avatar.js';
|
||||
import { getLocalImUserId } from '../../utils/im-user.js';
|
||||
import { normalizeConversationsList } from '../../utils/conversation-display.js';
|
||||
import { getLocalImUserId, getLocalImIdentity, getImUserIdForRole, loadGroupMetaCache, getDefaultAvatar } from '../../utils/im-user.js';
|
||||
import { enrichGroupConversation, isOrderGroupConversation, sortConversations, recordConversationOpen } from '../../utils/group-chat.js';
|
||||
import { reconnectForRole } from '../../utils/role-tab-bar.js';
|
||||
import { getPrimaryRole } from '../../utils/primary-role.js';
|
||||
import { isStaffMode } from '../../utils/staff-api.js';
|
||||
import { createPage } from '../../utils/base-page.js';
|
||||
const { jianquanxian } = require('../../utils/imAuth/jianquanxian');
|
||||
|
||||
@@ -30,18 +31,51 @@ Page(createPage({
|
||||
this.calculateScrollHeight();
|
||||
const muted = wx.getStorageSync('notificationMuted') || false;
|
||||
this.setData({ notificationMuted: muted });
|
||||
if (app.globalData.messageManager) {
|
||||
app.globalData.messageManager.notificationMuted = muted;
|
||||
}
|
||||
loadGroupMetaCache(app);
|
||||
this._onGlobalConvUpdated = (content) => {
|
||||
if (content && content.conversations) {
|
||||
this.renderConversations(content);
|
||||
} else if (app.loadConversations) {
|
||||
app.loadConversations();
|
||||
}
|
||||
};
|
||||
app.on('conversationsUpdated', this._onGlobalConvUpdated);
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
if (this._onGlobalConvUpdated) {
|
||||
app.off('conversationsUpdated', this._onGlobalConvUpdated);
|
||||
}
|
||||
if (this.conversationsUpdatedListener && wx.goEasy?.im) {
|
||||
wx.goEasy.im.off(wx.GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.conversationsUpdatedListener);
|
||||
this.conversationsUpdatedListener = null;
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
if (!this.checkLoginStatus()) return;
|
||||
loadGroupMetaCache(app);
|
||||
this.registerNotificationComponent();
|
||||
if (app.globalData.messageManager?.unreadTotal != null && app.emitEvent) {
|
||||
app.emitEvent('tabBarBadgeChanged', {
|
||||
badgeText: app.globalData.messageManager.unreadTotal > 0
|
||||
? String(app.globalData.messageManager.unreadTotal) : '',
|
||||
});
|
||||
}
|
||||
this.checkPermissionAndAutoConnect();
|
||||
if (app.ensureConnection) app.ensureConnection();
|
||||
this.setupConversationListener();
|
||||
setTimeout(() => {
|
||||
if (app.loadConversations) app.loadConversations();
|
||||
else if (wx.goEasy?.im) this.loadConversations();
|
||||
}, 200);
|
||||
},
|
||||
|
||||
onHide() {},
|
||||
|
||||
onUnload() {},
|
||||
|
||||
// ========== ==== ================ ==== ================ 鉴权检查 ==========
|
||||
async checkPermissionAndAutoConnect() {
|
||||
const seq = ++this._permissionSeq;
|
||||
@@ -90,26 +124,40 @@ Page(createPage({
|
||||
},
|
||||
|
||||
autoConnect() {
|
||||
const targetUserId = getLocalImUserId(app);
|
||||
const role = isStaffMode() ? 'shangjia' : (getLocalImIdentity(app) || app.globalData.currentRole || 'normal');
|
||||
const uid = wx.getStorageSync('uid');
|
||||
const targetUserId = getImUserIdForRole(role, uid);
|
||||
if (!targetUserId) return;
|
||||
|
||||
if (app.ensureConnection) app.ensureConnection();
|
||||
reconnectForRole(getPrimaryRole(app));
|
||||
const onReady = () => {
|
||||
this.setupConversationListener();
|
||||
if (app.loadConversations) app.loadConversations();
|
||||
else this.loadConversations();
|
||||
};
|
||||
|
||||
const status = wx.goEasy.getConnectionStatus ? wx.goEasy.getConnectionStatus() : 'disconnected';
|
||||
if (status === 'connected' || status === 'reconnected') {
|
||||
const currentUserId = wx.goEasy.im ? wx.goEasy.im.userId : null;
|
||||
if (currentUserId === targetUserId) {
|
||||
this.setupConversationListener();
|
||||
this.loadConversations();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
wx.goEasy.disconnect();
|
||||
} catch (e) {}
|
||||
const status = wx.goEasy?.getConnectionStatus?.() || 'disconnected';
|
||||
const currentUserId = wx.goEasy?.im?.userId;
|
||||
if ((status === 'connected' || status === 'reconnected') && currentUserId === targetUserId) {
|
||||
onReady();
|
||||
return;
|
||||
}
|
||||
|
||||
this.connectGoEasy(targetUserId);
|
||||
const tryConnect = async () => {
|
||||
try {
|
||||
if (app.ensureImForRole) {
|
||||
await app.ensureImForRole(role);
|
||||
} else if (app.connectWithIdentity) {
|
||||
await app.connectWithIdentity(role, targetUserId, true);
|
||||
} else if (app.ensureConnection) {
|
||||
app.ensureConnection();
|
||||
}
|
||||
onReady();
|
||||
} catch (e) {
|
||||
const s = wx.goEasy?.getConnectionStatus?.();
|
||||
if (s === 'connected' || s === 'reconnected') onReady();
|
||||
}
|
||||
};
|
||||
tryConnect();
|
||||
},
|
||||
|
||||
connectGoEasy(userId) {
|
||||
@@ -178,7 +226,21 @@ Page(createPage({
|
||||
this.setData({ allConversations: [] }, () => this.applyFilter());
|
||||
return;
|
||||
}
|
||||
let list = normalizeConversationsList(conversations, app, this._peerProfileCache);
|
||||
const myGoEasyId = wx.goEasy?.im?.userId || getLocalImUserId(app);
|
||||
const defaultAvatar = getDefaultAvatar(app);
|
||||
const ossBase = app.globalData.ossImageUrl || '';
|
||||
|
||||
let list = conversations.map((item) => {
|
||||
if (item.type === 'group') {
|
||||
return enrichGroupConversation(item, myGoEasyId, defaultAvatar, ossBase);
|
||||
}
|
||||
if (!item.data) item.data = {};
|
||||
if (item.data.avatar) {
|
||||
item.data.avatar = resolveAvatarUrl(item.data.avatar, app);
|
||||
}
|
||||
return item;
|
||||
});
|
||||
|
||||
list.forEach(item => {
|
||||
if (!item.lastMessage) {
|
||||
item.lastMessage = { type: 'text', payload: { text: '' }, timestamp: 0 };
|
||||
@@ -186,17 +248,13 @@ Page(createPage({
|
||||
if (item.lastMessage.timestamp) {
|
||||
item.lastMessage.date = formatDate(item.lastMessage.timestamp);
|
||||
}
|
||||
if (item.type === 'group' && item.displayLastMsg) {
|
||||
item.lastMessage.preview = item.displayLastMsg;
|
||||
}
|
||||
});
|
||||
|
||||
list.sort((a, b) => {
|
||||
if (a.top && !b.top) return -1;
|
||||
if (!a.top && b.top) return 1;
|
||||
if (a.unread > 0 && b.unread <= 0) return -1;
|
||||
if (a.unread <= 0 && b.unread > 0) return 1;
|
||||
const ta = (a.lastMessage && a.lastMessage.timestamp) || 0;
|
||||
const tb = (b.lastMessage && b.lastMessage.timestamp) || 0;
|
||||
return tb - ta;
|
||||
});
|
||||
const openTimes = wx.getStorageSync('conversationOpenTimes') || {};
|
||||
list = sortConversations(list, openTimes);
|
||||
|
||||
this.setData({ allConversations: list, displayCount: 10 }, () => {
|
||||
this.applyFilter();
|
||||
@@ -244,7 +302,7 @@ Page(createPage({
|
||||
const { allConversations, activeTab, searchKeyword } = this.data;
|
||||
let filtered = [];
|
||||
if (activeTab === 0) {
|
||||
filtered = allConversations.filter(c => c.type === 'group' && c.data && c.data.orderId);
|
||||
filtered = allConversations.filter(c => isOrderGroupConversation(c));
|
||||
} else if (activeTab === 1) {
|
||||
filtered = allConversations.filter(c => c.type === 'private');
|
||||
} else if (activeTab === 2) {
|
||||
@@ -263,7 +321,7 @@ Page(createPage({
|
||||
|
||||
const tabUnread = [0, 0, 0];
|
||||
allConversations.forEach(c => {
|
||||
if (c.type === 'group' && c.data && c.data.orderId) tabUnread[0] += c.unread || 0;
|
||||
if (isOrderGroupConversation(c)) tabUnread[0] += c.unread || 0;
|
||||
else if (c.type === 'private') tabUnread[1] += c.unread || 0;
|
||||
else if (c.type === 'cs') tabUnread[2] += c.unread || 0;
|
||||
});
|
||||
@@ -294,6 +352,7 @@ Page(createPage({
|
||||
chat(e) {
|
||||
const conversation = e.currentTarget.dataset.conversation;
|
||||
if (!conversation) return;
|
||||
recordConversationOpen(conversation);
|
||||
|
||||
if (conversation.type === 'private') {
|
||||
const param = {
|
||||
@@ -303,12 +362,16 @@ Page(createPage({
|
||||
};
|
||||
wx.navigateTo({ url: '/pages/chat/chat?data=' + encodeURIComponent(JSON.stringify(param)) });
|
||||
} else if (conversation.type === 'group') {
|
||||
const d = conversation.data || {};
|
||||
const param = {
|
||||
groupId: conversation.groupId,
|
||||
orderId: conversation.data.orderId || '',
|
||||
groupName: conversation.data.name,
|
||||
groupAvatar: conversation.data.avatar,
|
||||
isCross: conversation.data.isCross || 0
|
||||
orderId: d.orderId || '',
|
||||
groupName: d.name || '订单群聊',
|
||||
groupAvatar: d.avatar,
|
||||
isCross: d.isCross || 0,
|
||||
orderZhuangtai: d.orderZhuangtai,
|
||||
orderJine: d.orderJine,
|
||||
orderDesc: d.orderDesc || d.orderJieshao || '',
|
||||
};
|
||||
wx.navigateTo({ url: '/pages/group-chat/group-chat?data=' + encodeURIComponent(JSON.stringify(param)) });
|
||||
} else if (conversation.type === 'cs') {
|
||||
|
||||
Reference in New Issue
Block a user