refactor(im): 对齐文赫电竞消息系统(配对群ID+稳定监听)

This commit is contained in:
XingQue
2026-06-29 04:20:39 +08:00
parent 9976bc1d6d
commit 6e2f7bc39f
7 changed files with 191 additions and 51 deletions

View File

@@ -285,7 +285,7 @@ Page({
});
},
// ===== 联系老板(订单群 groupId = 订单号 =====
// ===== 联系老板/商家(配对群 group_Ds{打手}_Sj/Boss{对方} =====
goToChatWithBoss() {
const uid = wx.getStorageSync('uid')
if (!uid) {
@@ -299,10 +299,17 @@ Page({
return
}
const fadanPingtai = this.data.jibenShuju.fadanpingtai || 0
const partnerUid = fadanPingtai === 2
? (this.data.xiangxiShuju.shangjia_id || '')
: (this.data.xiangxiShuju.laoban_id || '')
connectionManager.connectToGroupChat({
identityType: 'dashou',
userId: 'Ds' + uid,
orderId: orderId,
partnerUid,
fadanPingtai,
groupName: (this.data.jibenShuju.nicheng || '订单') + '的订单群',
isCross: this.data.jibenShuju.fadanpingtai || 0,
}).catch((err) => {

View File

@@ -350,6 +350,8 @@ Page({
identityType: 'shangjia',
userId: 'Sj' + uid,
orderId: orderId,
partnerUid: dashouUid,
fadanPingtai: this.data.jibenShuju.fadanpingtai || 0,
groupName,
isCross,
}).catch((err) => {

View File

@@ -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') {

View File

@@ -211,6 +211,8 @@ Page({
identityType: 'normal',
userId: 'Boss' + uid,
orderId: dingdanId,
partnerUid: this.data.xiangxiShuju.dashou || '',
fadanPingtai: this.data.jibenShuju.fadanpingtai || 0,
groupName: this.data.jibenShuju.nicheng + '的订单群',
isCross: 0
}).catch(err => {