fix(chat): 订单详情进群统一走 connectToGroupChat,groupId=订单号,修复历史消息与身份

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-06-28 07:23:55 +08:00
parent eaf05663ee
commit 848c2f6486
4 changed files with 108 additions and 126 deletions

View File

@@ -1,6 +1,7 @@
// pages/fighter-order-detail/fighter-order-detail.js - 【最终版:提交前公告弹窗 + 修改图片至少保留一张】
const app = getApp()
import request from '../../utils/request.js'
import connectionManager from '../../utils/xiaoxilj.js'
const COS = require('../../utils/cos-wx-sdk-v5.min.js')
Page({
@@ -284,7 +285,7 @@ Page({
});
},
// ===== 联系老板(订单群 groupId 订单号,与商家端 sjddxq 一致 =====
// ===== 联系老板(订单群 groupId = 订单号) =====
goToChatWithBoss() {
const uid = wx.getStorageSync('uid')
if (!uid) {
@@ -298,37 +299,15 @@ Page({
return
}
const targetUserId = 'Ds' + uid
const connected = wx.goEasy && wx.goEasy.getConnectionStatus && wx.goEasy.getConnectionStatus() === 'connected'
const currentUserId = wx.goEasy?.im?.userId
if (!connected || currentUserId !== targetUserId) {
if (app.ensureConnection) app.ensureConnection()
wx.showToast({ title: '连接中,请稍后再试', icon: 'none' })
return
}
const orderIdStr = String(orderId)
const groupName = (this.data.jibenShuju.nicheng || '订单') + '的订单群'
const isCross = this.data.jibenShuju.fadanpingtai || 0
wx.goEasy.im.subscribeGroup({
groupIds: [orderIdStr],
onSuccess: () => {
const param = {
groupId: orderIdStr,
orderId: orderIdStr,
groupName,
groupAvatar: '',
isCross,
}
wx.navigateTo({
url: '/pages/group-chat/group-chat?data=' + encodeURIComponent(JSON.stringify(param)),
})
},
onFailed: (err) => {
console.error('订阅群组失败', err)
wx.showToast({ title: '连接群聊失败', icon: 'none' })
},
connectionManager.connectToGroupChat({
identityType: 'dashou',
userId: 'Ds' + uid,
orderId: orderId,
groupName: (this.data.jibenShuju.nicheng || '订单') + '的订单群',
isCross: this.data.jibenShuju.fadanpingtai || 0,
}).catch((err) => {
console.error('跳转群聊失败', err)
wx.showToast({ title: '跳转聊天失败', icon: 'none' })
})
},

View File

@@ -41,21 +41,25 @@ Page({
p = { ...p, ...d };
} catch (e) {}
}
const orderId = String(p.orderId || p.groupId || '');
const groupId = String(p.groupId || p.orderId || '');
this.setData({
groupId: p.groupId,
groupId,
groupName: p.groupName,
groupAvatar: p.groupAvatar,
orderId: p.orderId,
isCross: p.isCross
orderId,
isCross: p.isCross,
});
wx.setNavigationBarTitle({ title: this.data.groupName });
if (p.currentUserId) {
const localImId = getLocalImUserId(app);
const userId = localImId || p.currentUserId;
if (userId) {
this.setData({
currentUser: {
id: p.currentUserId,
name: p.currentUserName || `用户${p.currentUserId.replace(/^[A-Za-z]+/,'')}`,
avatar: resolveAvatarUrl(p.currentUserAvatar, app)
}
id: userId,
name: p.currentUserName || app.globalData.currentUser?.name || `用户${wx.getStorageSync('uid') || ''}`,
avatar: resolveAvatarUrl(p.currentUserAvatar || wx.getStorageSync('touxiang'), app),
},
});
} else {
this.initCurrentUser();
@@ -65,10 +69,11 @@ Page({
initCurrentUser() {
const imId = getLocalImUserId(app);
const uid = wx.getStorageSync('uid');
if (!imId) return;
this.setData({
currentUser: {
id: imId || ('Boss' + uid),
name: app.globalData.currentUser?.name || `用户${uid}`,
id: imId,
name: app.globalData.currentUser?.name || wx.getStorageSync('nicheng') || `用户${uid}`,
avatar: resolveAvatarUrl(wx.getStorageSync('touxiang'), app),
},
});
@@ -120,20 +125,25 @@ Page({
},
autoConnect() {
const targetUserId = getLocalImUserId(app);
const targetUserId = this.data.currentUser?.id || getLocalImUserId(app);
if (!targetUserId) return;
const afterReady = () => {
this.subscribeGroupIfNeeded().then(() => {
this.setupAllListeners();
this.loadHistory(true);
this.markGroupMessageAsRead();
});
};
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.subscribeGroupIfNeeded();
this.setupAllListeners();
this.loadHistory(true);
afterReady();
return;
} else {
wx.goEasy.disconnect();
}
wx.goEasy.disconnect();
}
this.connectGoEasy(targetUserId);
},
@@ -148,18 +158,18 @@ Page({
avatar: resolveAvatarUrl(currentUser.avatar, app),
},
onSuccess: () => {
this.subscribeGroupIfNeeded();
this.setupAllListeners();
this.loadHistory(true).then(()=>{
this.markGroupMessageAsRead
this.subscribeGroupIfNeeded().then(() => {
this.setupAllListeners();
this.loadHistory(true);
this.markGroupMessageAsRead();
});
},
onFailed: (error) => {
if (error.code === 408) {
this.subscribeGroupIfNeeded();
this.setupAllListeners();
this.loadHistory(true);
this.subscribeGroupIfNeeded().then(() => {
this.setupAllListeners();
this.loadHistory(true);
});
} else {
wx.showToast({ title: '连接失败,请重试', icon: 'none' });
}
@@ -169,11 +179,16 @@ Page({
subscribeGroupIfNeeded() {
const { groupId } = this.data;
if (!groupId) return;
wx.goEasy.im.subscribeGroup({
groupIds: [groupId],
onSuccess: () => {},
onFailed: () => {}
if (!groupId || !wx.goEasy?.im?.subscribeGroup) return Promise.resolve();
return new Promise((resolve) => {
wx.goEasy.im.subscribeGroup({
groupIds: [String(groupId)],
onSuccess: () => resolve(true),
onFailed: (error) => {
console.warn('subscribeGroup failed', error);
resolve(false);
},
});
});
},

View File

@@ -2,6 +2,7 @@
const app = getApp()
import request from '../../utils/request.js'
import PopupService from '../../services/popupService.js' // 新增:导入弹窗服务
import connectionManager from '../../utils/xiaoxilj.js'
import { STAFF_API, isStaffMode, syncStaffUi, getStaffContext, staffCan } from '../../utils/staff-api.js'
/** 可申请罚款的订单状态:进行中/已完成/退款中/已退款/退款失败/结算中 */
@@ -324,7 +325,7 @@ Page({
})
},
// ===== 联系打手(替打手订阅 + 发机器人消息 + 跳转 =====
// ===== 联系打手(订单群 groupId = 订单号 =====
goToChatWithDashou() {
if (isStaffMode() && !staffCan('imChat')) {
wx.showToast({ title: '当前角色无订单聊天权限', icon: 'none' })
@@ -339,43 +340,35 @@ Page({
return
}
const targetUserId = 'Sj' + uid
if (this.data.currentIMUserId !== targetUserId) {
this.ensureIMConnection()
wx.showToast({ title: '连接中,请稍后再试', icon: 'none' })
return
}
const that = this
const groupName = this.data.jibenShuju.nicheng + '的订单群'
const isCross = this.data.jibenShuju.fadanpingtai || 0
const dashouUid = this.data.dashouInfo.yonghuid
const that = this
// 第一步:商家自己订阅群组
wx.goEasy.im.subscribeGroup({
groupIds: [orderId],
onSuccess: () => {
// 如果有打手,则需要帮打手订阅并发送消息
if (dashouUid) {
that.bangDashouDingyue(orderId, dashouUid)
.then(() => {
that.tiaozhuanQunLiao(orderId, groupName, isCross)
})
.catch((err) => {
console.error('替打手订阅失败,仍可跳转群聊', err)
wx.showToast({ title: '通知接单员可能失败', icon: 'none' })
that.tiaozhuanQunLiao(orderId, groupName, isCross)
})
} else {
// 无打手,直接跳转
that.tiaozhuanQunLiao(orderId, groupName, isCross)
}
},
onFailed: (error) => {
console.error('订阅群组失败', error)
wx.showToast({ title: '连接群聊失败', icon: 'none' })
}
})
const openGroupChat = () => {
connectionManager.connectToGroupChat({
identityType: 'shangjia',
userId: 'Sj' + uid,
orderId: orderId,
groupName,
isCross,
}).catch((err) => {
console.error('跳转群聊失败', err)
wx.showToast({ title: '跳转聊天失败', icon: 'none' })
})
}
if (dashouUid) {
that.bangDashouDingyue(orderId, dashouUid)
.then(openGroupChat)
.catch((err) => {
console.error('替打手订阅失败,仍可进入群聊', err)
wx.showToast({ title: '通知接单员可能失败', icon: 'none' })
openGroupChat()
})
} else {
openGroupChat()
}
},
// 帮打手订阅群组 + 发送机器人消息
@@ -432,20 +425,6 @@ Page({
})
},
// 跳转群聊页面
tiaozhuanQunLiao(orderId, groupName, isCross) {
const param = {
groupId: orderId,
orderId: orderId,
groupName: groupName,
groupAvatar: '',
isCross: isCross
}
wx.navigateTo({
url: '/pages/group-chat/group-chat?data=' + encodeURIComponent(JSON.stringify(param))
})
},
// ========== 以下所有业务功能完全保持不变 ==========
showGhDashouConfirm() { this.setData({ showGhDashouModal: true }) },
closeGhDashouModal() { this.setData({ showGhDashouModal: false }) },
@@ -476,7 +455,13 @@ Page({
}
},
showFakuanModal() { this.setData({ showFakuanModal: true, fakuanLiyou: '', fakuanJine: '', yingxiangQiangdan: true }) },
showFakuanModal() {
const dingdanId = this.data.jibenShuju.dingdan_id
if (dingdanId) {
this.jiazaiXiangxiShuju(dingdanId)
}
this.setData({ showFakuanModal: true, fakuanLiyou: '', fakuanJine: '', yingxiangQiangdan: true })
},
closeFakuanModal() { this.setData({ showFakuanModal: false }) },
inputFakuanLiyou(e) { this.setData({ fakuanLiyou: e.detail.value.slice(0, 50) }) },
inputFakuanJine(e) { this.setData({ fakuanJine: e.detail.value }) },

View File

@@ -155,29 +155,29 @@ class ConnectionManager {
}
}
// 3. 从会话列表查找真实群组ID
let realGroupId = null;
// 3. 从会话列表查找群组;找不到则用订单号作为 groupId首聊场景
const orderIdStr = String(orderId);
let realGroupId = orderIdStr;
try {
const result = await new Promise((resolve, reject) => {
wx.goEasy.im.latestConversations({
onSuccess: (res) => {
const conversations = res.content?.conversations || [];
const found = conversations.find(c => c.type === 'group' && c.data && c.data.orderId === orderId);
resolve(found ? found.groupId : null);
const found = conversations.find((c) => {
if (c.type !== 'group') return false;
if (c.groupId === orderIdStr) return true;
const data = c.data || {};
return String(data.orderId || '') === orderIdStr;
});
resolve(found ? found.groupId : orderIdStr);
},
onFailed: reject
onFailed: reject,
});
});
realGroupId = result;
realGroupId = result || orderIdStr;
} catch (err) {
console.error('获取会话列表失败:', err);
wx.showToast({ title: '获取群聊信息失败', icon: 'none' });
throw err;
}
if (!realGroupId) {
wx.showToast({ title: '暂未创建群聊', icon: 'none' });
return;
console.warn('获取会话列表失败使用订单号作为群ID', err);
realGroupId = orderIdStr;
}
// 4. 🔥 关键:主动订阅群组并等待成功,确保发送消息时订阅已完成
@@ -202,10 +202,13 @@ class ConnectionManager {
// 5. 跳转群聊页(参数与消息列表完全一致)
const param = {
groupId: realGroupId,
orderId: orderId,
orderId: orderIdStr,
groupName: groupName || '订单群聊',
groupAvatar: groupAvatar || '',
isCross: isCross || 0
isCross: isCross || 0,
currentUserId: userId,
currentUserName: app.globalData.currentUser?.name || `用户${uid}`,
currentUserAvatar: avatar,
};
const path = '/pages/group-chat/group-chat?data=' + encodeURIComponent(JSON.stringify(param));
wx.navigateTo({ url: path });