解决发送问卷的问题
This commit is contained in:
parent
2b86ffd1bb
commit
b6fe4e9877
@ -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>
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user