解决发送问卷的问题

This commit is contained in:
wangdongbo 2026-02-03 10:46:48 +08:00
parent 2b86ffd1bb
commit b6fe4e9877
3 changed files with 123 additions and 120 deletions

View File

@ -5,38 +5,45 @@
<text v-if="required" class="required">*</text>
</view>
<view class="right">
<view class="value" :class="{ muted: !displayText }">{{ displayText || placeholder }}</view>
<view class="value" :class="{ muted: !displayText }">{{
displayText || placeholder
}}</view>
<uni-icons type="arrowright" size="16" color="#999" />
</view>
</view>
</template>
<script setup>
import { computed, onUnmounted, ref } from 'vue';
import { computed, onUnmounted, ref } from "vue";
const emits = defineEmits(['change']);
const emits = defineEmits(["change"]);
const props = defineProps({
form: { type: Object, default: () => ({}) },
name: { default: '' },
name: { default: "" },
required: { type: Boolean, default: false },
title: { default: '' },
title: { default: "" },
disableChange: { type: Boolean, default: false },
placeholder: { default: '' },
placeholder: { default: "" },
mult: { type: Boolean, default: false },
});
const placeholder = computed(() => (props.placeholder ? String(props.placeholder) : `请选择${props.name || ''}`));
const placeholder = computed(() =>
props.placeholder ? String(props.placeholder) : `请选择${props.name || ""}`
);
const rawValue = computed(() => props.form?.[props.title]);
const displayText = computed(() => {
const v = rawValue.value;
if (Array.isArray(v)) return v.filter((i) => i !== null && i !== undefined && String(i).trim()).join('');
return v ? String(v) : '';
if (Array.isArray(v))
return v
.filter((i) => i !== null && i !== undefined && String(i).trim())
.join("");
return v ? String(v) : "";
});
const activeEventName = ref('');
const activeEventName = ref("");
function clearListener() {
if (activeEventName.value) uni.$off(activeEventName.value);
activeEventName.value = '';
activeEventName.value = "";
}
onUnmounted(clearListener);
@ -45,16 +52,20 @@ function open() {
clearListener();
activeEventName.value = `diagnosis-change_${Date.now()}`;
uni.$on(activeEventName.value, (data) => {
emits('change', { title: props.title, value: data });
emits("change", { title: props.title, value: data });
});
if (props.mult) {
const cur = Array.isArray(rawValue.value) ? rawValue.value : [];
uni.setStorageSync('diagnosis-list-selection', cur);
uni.setStorageSync("diagnosis-list-selection", cur);
}
uni.navigateTo({
url: `/pages/library/diagnosis-list?eventName=${encodeURIComponent(activeEventName.value)}&mult=${props.mult ? 'YES' : 'NO'}&value=${encodeURIComponent(displayText.value || '')}`,
url: `/pages/library/diagnosis-list?eventName=${encodeURIComponent(
activeEventName.value
)}&mult=${props.mult ? "YES" : "NO"}&value=${encodeURIComponent(
displayText.value || ""
)}`,
});
}
</script>

View File

@ -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({
@ -1013,22 +1011,17 @@ uni.$on("sendSurvey", async (data) => {
extension: "",
};
//
const message = timChatManager.tim.createCustomMessage({
to: chatInfo.value.conversationID.replace("GROUP", ""),
conversationType: timChatManager.TIM.TYPES.CONV_GROUP,
payload: customMessage,
});
// 使 timChatManager sendCustomMessage
const result = await timChatManager.sendCustomMessage(customMessage, "SURVEY");
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);

View File

@ -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('无法获取群聊IDconversationID:', 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('无法获取群聊IDconversationID:', 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