diff --git a/components/form-template/form-cell/form-diagnosis-picker.vue b/components/form-template/form-cell/form-diagnosis-picker.vue
index c1a5ef6..ec940f3 100644
--- a/components/form-template/form-cell/form-diagnosis-picker.vue
+++ b/components/form-template/form-cell/form-diagnosis-picker.vue
@@ -5,38 +5,45 @@
*
- {{ displayText || placeholder }}
+ {{
+ displayText || placeholder
+ }}
diff --git a/pages/message/index.vue b/pages/message/index.vue
index a2d1c8f..3b06be5 100644
--- a/pages/message/index.vue
+++ b/pages/message/index.vue
@@ -174,7 +174,7 @@ import { ref, onUnmounted, nextTick, watch, computed } from "vue";
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import { storeToRefs } from "pinia";
import useAccountStore from "@/store/account.js";
-import { globalTimChatManager } from "@/utils/tim-chat.js";
+import { globalTimChatManager, TIM } from "@/utils/tim-chat.js";
import {
startIMMonitoring,
stopIMMonitoring,
@@ -966,9 +966,8 @@ uni.$on("sendSurvey", async (data) => {
const surveyUrl = env.VITE_SURVEY_URL || "";
// 获取客户信息
- const customerId = chatInfo.value.userID || "";
- const customerName = chatInfo.value.customerName || "";
-
+ const customerId = patientId.value || "";
+ const customerName = patientInfo.value.name || "";
// 创建问卷记录
const recordRes = await api("createSurveyRecord", {
corpId,
@@ -998,7 +997,6 @@ uni.$on("sendSurvey", async (data) => {
customerName
)}`;
}
-
// 创建自定义消息
const customMessage = {
data: JSON.stringify({
@@ -1012,23 +1010,18 @@ uni.$on("sendSurvey", async (data) => {
description: "SURVEY",
extension: "",
};
+
+ // 使用 timChatManager 的 sendCustomMessage 方法发送自定义消息
+ const result = await timChatManager.sendCustomMessage(customMessage, "SURVEY");
- // 发送自定义消息
- const message = timChatManager.tim.createCustomMessage({
- to: chatInfo.value.conversationID.replace("GROUP", ""),
- conversationType: timChatManager.TIM.TYPES.CONV_GROUP,
- payload: customMessage,
- });
-
- const sendResult = await timChatManager.tim.sendMessage(message);
-
- if (sendResult.code === 0) {
+ if (result.success) {
+ // 消息已通过 sendCustomMessage 方法添加到列表并显示
uni.showToast({
title: "发送成功",
icon: "success",
});
} else {
- throw new Error(sendResult.message || "发送失败");
+ throw new Error(result.error || "发送失败");
}
} catch (error) {
console.error("发送问卷失败:", error);
diff --git a/utils/tim-chat.js b/utils/tim-chat.js
index b6f2529..d2fa14f 100644
--- a/utils/tim-chat.js
+++ b/utils/tim-chat.js
@@ -2170,6 +2170,93 @@ class TimChatManager {
}
}
+ // 发送自定义消息
+ async sendCustomMessage(customPayload, description = '') {
+ if (!this.tim) {
+ this.triggerCallback('onError', 'IM未初始化')
+ return { success: false, error: 'IM未初始化' }
+ }
+
+ // 检查登录状态
+ if (!this.isLoggedIn) {
+ console.error('IM未登录,无法发送消息');
+ this.triggerCallback('onError', 'IM未登录,请稍后重试')
+ return { success: false, error: 'IM未登录' }
+ }
+
+ // 优先使用 currentConversationID,如果没有则尝试从 conversation 获取
+ let conversationID = this.currentConversationID;
+ if (!conversationID && this.conversation) {
+ conversationID = this.conversation.conversationID;
+ }
+
+ if (!conversationID) {
+ console.error('会话ID不存在');
+ this.triggerCallback('onError', '会话不存在,请重新进入聊天')
+ return { success: false, error: '会话ID不存在' }
+ }
+
+ // 从 conversationID 提取 groupID
+ let groupID = null;
+ if (conversationID.startsWith('GROUP')) {
+ groupID = conversationID.replace('GROUP', '');
+ } else if (this.conversation?.groupProfile?.groupID) {
+ groupID = this.conversation.groupProfile.groupID;
+ }
+
+ if (!groupID) {
+ console.error('无法获取群聊ID,conversationID:', conversationID);
+ this.triggerCallback('onError', '无法获取群聊ID')
+ return { success: false, error: '无法获取群聊ID' }
+ }
+
+ // customPayload 已经是完整的 payload 对象 { data, description, extension }
+ // 直接使用,不需要再包装
+ const payload = customPayload;
+
+ const localMessage = {
+ ID: `local_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
+ flow: 'out',
+ type: 'TIMCustomElem',
+ payload: payload,
+ lastTime: Math.floor(Date.now() / 1000),
+ status: 'sending',
+ avatar: '/static/center/user-avatar.png',
+ conversationID: conversationID,
+ from: this.currentUserID
+ }
+
+ console.log('发送自定义消息,本地消息对象:', localMessage);
+
+ // 触发本地消息回调,立即显示在聊天列表中
+ this.triggerCallback('onMessageReceived', localMessage)
+
+ const message = this.tim.createCustomMessage({
+ to: groupID,
+ conversationType: TIM.TYPES.CONV_GROUP,
+ payload: payload
+ })
+
+ try {
+ await this.tim.sendMessage(message)
+ localMessage.status = 'success'
+ console.log('自定义消息发送成功');
+ return { success: true, message: localMessage }
+ } catch (error) {
+ console.error('自定义消息发送失败:', error)
+ localMessage.status = 'failed'
+
+ // 如果是因为未登录导致的失败,尝试重连
+ if (error.message && (error.message.includes('not login') || error.message.includes('sdk not ready'))) {
+ console.log('检测到未登录错误,尝试重连...');
+ this.isLoggedIn = false;
+ this.ensureIMConnection();
+ }
+
+ return { success: false, error }
+ }
+ }
+
// 发送图片消息
async sendImageMessage(imageFile) {
if (!this.tim) {
@@ -2325,95 +2412,6 @@ class TimChatManager {
}
}
- // 发送自定义消息
- async sendCustomMessage(messageData) {
- if (!this.tim) {
- this.triggerCallback('onError', 'IM未初始化')
- return { success: false, error: 'IM未初始化' }
- }
-
- // 检查登录状态
- if (!this.isLoggedIn) {
- console.error('IM未登录,无法发送消息');
- this.triggerCallback('onError', 'IM未登录,请稍后重试')
- return { success: false, error: 'IM未登录' }
- }
-
- // 优先使用 currentConversationID,如果没有则尝试从 conversation 获取
- let conversationID = this.currentConversationID;
- if (!conversationID && this.conversation) {
- conversationID = this.conversation.conversationID;
- }
-
- if (!conversationID) {
- console.error('会话ID不存在');
- this.triggerCallback('onError', '会话不存在,请重新进入聊天')
- return { success: false, error: '会话ID不存在' }
- }
-
- // 从 conversationID 提取 groupID
- let groupID = null;
- if (conversationID.startsWith('GROUP')) {
- groupID = conversationID.replace('GROUP', '');
- } else if (this.conversation?.groupProfile?.groupID) {
- groupID = this.conversation.groupProfile.groupID;
- }
-
- if (!groupID) {
- console.error('无法获取群聊ID,conversationID:', conversationID);
- this.triggerCallback('onError', '无法获取群聊ID')
- return { success: false, error: '无法获取群聊ID' }
- }
-
- const localMessage = {
- ID: `local_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
- flow: 'out',
- type: 'TIMCustomElem',
- payload: {
- data: JSON.stringify(messageData),
- description: messageData.content || '自定义消息',
- extension: messageData.messageType || 'custom'
- },
- lastTime: Date.now(),
- status: 'sending',
- avatar: '/static/center/user-avatar.png',
- conversationID: conversationID,
- from: this.currentUserID
- }
-
- // 缓存功能已移除
-
- this.triggerCallback('onMessageReceived', localMessage)
-
- const message = this.tim.createCustomMessage({
- to: groupID,
- conversationType: TIM.TYPES.CONV_GROUP,
- payload: {
- data: JSON.stringify(messageData),
- description: messageData.content || '自定义消息',
- extension: messageData.messageType || 'custom'
- }
- })
-
- try {
- await this.tim.sendMessage(message)
- localMessage.status = 'success'
- return { success: true, message: localMessage }
- } catch (error) {
- console.error('自定义消息发送失败:', error)
- localMessage.status = 'failed'
-
- // 如果是因为未登录导致的失败,尝试重连
- if (error.message && (error.message.includes('not login') || error.message.includes('sdk not ready'))) {
- console.log('检测到未登录错误,尝试重连...');
- this.isLoggedIn = false;
- this.ensureIMConnection();
- }
-
- return { success: false, error }
- }
- }
-
// 工具方法
// 判断是否为系统消息
isSystemMessage(message) {
@@ -2883,7 +2881,8 @@ export {
setGlobalIMCallback,
getGroupList,
clearConversationCache,
- clearAllMessageCache
+ clearAllMessageCache,
+ TIM
}
export default globalTimChatManager