From 848c2f64867a1bc04721b8943e56ac31cfdeb879 Mon Sep 17 00:00:00 2001 From: XingQue Date: Sun, 28 Jun 2026 07:23:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(chat):=20=E8=AE=A2=E5=8D=95=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E8=BF=9B=E7=BE=A4=E7=BB=9F=E4=B8=80=E8=B5=B0=20connec?= =?UTF-8?q?tToGroupChat=EF=BC=8CgroupId=3D=E8=AE=A2=E5=8D=95=E5=8F=B7?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=8E=86=E5=8F=B2=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=B8=8E=E8=BA=AB=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- .../fighter-order-detail.js | 43 +++------- pages/group-chat/group-chat.js | 73 +++++++++------- .../merchant-order-detail.js | 83 ++++++++----------- utils/xiaoxilj.js | 35 ++++---- 4 files changed, 108 insertions(+), 126 deletions(-) diff --git a/pages/fighter-order-detail/fighter-order-detail.js b/pages/fighter-order-detail/fighter-order-detail.js index 0051b27..d4bb52a 100644 --- a/pages/fighter-order-detail/fighter-order-detail.js +++ b/pages/fighter-order-detail/fighter-order-detail.js @@ -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' }) }) }, diff --git a/pages/group-chat/group-chat.js b/pages/group-chat/group-chat.js index dbbe03e..9e1aee1 100644 --- a/pages/group-chat/group-chat.js +++ b/pages/group-chat/group-chat.js @@ -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); + }, + }); }); }, diff --git a/pages/merchant-order-detail/merchant-order-detail.js b/pages/merchant-order-detail/merchant-order-detail.js index 8c0afe0..f40d0ff 100644 --- a/pages/merchant-order-detail/merchant-order-detail.js +++ b/pages/merchant-order-detail/merchant-order-detail.js @@ -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 }) }, diff --git a/utils/xiaoxilj.js b/utils/xiaoxilj.js index d934f09..8f330cc 100644 --- a/utils/xiaoxilj.js +++ b/utils/xiaoxilj.js @@ -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 });