no message

This commit is contained in:
wangdongbo 2026-02-08 14:11:59 +08:00
parent b3a7310f0b
commit b98f835ab1
2 changed files with 68 additions and 13 deletions

View File

@ -41,13 +41,13 @@ export async function sendTextMessage(content) {
/**
* 发送图片消息
* @param {string} imageUrl - 图片URL字符串
* @param {string|File} imageSource - 图片URL字符串或文件对象
* @param {string} imageName - 图片名称可选
* @returns {Promise<boolean>} 发送是否成功
*/
export async function sendImageMessage(imageUrl, imageName = '图片') {
if (!imageUrl) {
toast('图片URL不能为空');
export async function sendImageMessage(imageSource, imageName = '图片') {
if (!imageSource) {
toast('图片不能为空');
return false;
}
@ -57,7 +57,17 @@ export async function sendImageMessage(imageUrl, imageName = '图片') {
return false;
}
const result = await globalTimChatManager.sendImageMessage(imageUrl);
// 如果是URL字符串需要先下载转换为文件对象
let imageFile = imageSource;
if (typeof imageSource === 'string') {
imageFile = await downloadImageAsFile(imageSource, imageName);
if (!imageFile) {
toast('图片下载失败');
return false;
}
}
const result = await globalTimChatManager.sendImageMessage(imageFile);
if (result?.success) {
return true;
@ -72,6 +82,44 @@ export async function sendImageMessage(imageUrl, imageName = '图片') {
}
}
/**
* 将图片URL下载转换为文件对象小程序环境
* @param {string} imageUrl - 图片URL
* @param {string} fileName - 文件名
* @returns {Promise<Object|null>} 文件对象或null
*/
async function downloadImageAsFile(imageUrl, fileName = '图片') {
return new Promise((resolve) => {
uni.downloadFile({
url: imageUrl,
success: (res) => {
if (res.statusCode === 200) {
// 返回包含 tempFiles 数组的对象,模拟 uni.chooseMedia 的返回格式
const fileObj = {
tempFiles: [{
tempFilePath: res.tempFilePath,
path: res.tempFilePath,
size: 0,
type: 'image'
}],
// 也保留直接的 tempFilePath 以兼容其他代码
tempFilePath: res.tempFilePath
};
console.log('图片下载成功,文件对象:', fileObj);
resolve(fileObj);
} else {
console.error('下载图片失败,状态码:', res.statusCode);
resolve(null);
}
},
fail: (error) => {
console.error('下载图片失败:', error);
resolve(null);
}
});
});
}
/**
* 发送宣教文章消息
* @param {Object} article - 文章对象 { _id, title, cover, url }

View File

@ -2189,7 +2189,7 @@ class TimChatManager {
async sendImageMessage(imageFile) {
if (!this.tim) {
this.triggerCallback('onError', 'IM未初始化')
return
return { success: false, error: 'IM未初始化' }
}
if (!this.conversation) {
@ -2240,20 +2240,27 @@ class TimChatManager {
// 触发消息接收回调让UI立即显示
this.triggerCallback('onMessageReceived', localMessage)
const message = this.tim.createImageMessage({
to: groupID,
conversationType: TIM.TYPES.CONV_GROUP,
payload: { file: imageFile }
})
try {
console.log('准备创建图片消息imageFile:', imageFile)
const message = this.tim.createImageMessage({
to: groupID,
conversationType: TIM.TYPES.CONV_GROUP,
payload: { file: imageFile }
})
console.log('图片消息创建成功:', message)
if (!message) {
throw new Error('createImageMessage 返回空值')
}
await this.tim.sendMessage(message)
localMessage.status = 'success'
return { success: true, message: localMessage }
} catch (error) {
console.error('图片消息发送失败:', error)
localMessage.status = 'failed'
return { success: false, error }
return { success: false, error: error.message || error }
}
}
// 发送语音消息