This commit is contained in:
wangdongbo 2026-01-22 17:02:15 +08:00
parent 16c7d2b261
commit 46ce29f46e
3 changed files with 177 additions and 57 deletions

View File

@ -199,12 +199,10 @@ const toggleMorePanel = () => {
// //
const showImagePicker = () => { const showImagePicker = () => {
chooseImage( chooseImage(
(res) => { (file) => {
console.log('选择图片成功,返回数据:', res); console.log('选择图片成功,文件对象:', file);
// //
const imageFile = res.tempFiles && res.tempFiles.length > 0 ? res.tempFiles[0] : res; sendImageMessage(file);
console.log('准备发送图片:', imageFile);
sendImageMessage(imageFile);
}, },
(err) => { (err) => {
console.error('选择图片失败:', err); console.error('选择图片失败:', err);
@ -221,12 +219,10 @@ const showImagePicker = () => {
const takePhoto = () => { const takePhoto = () => {
takePhotoUtil( takePhotoUtil(
(res) => { (file) => {
console.log('拍照成功,返回数据:', res); console.log('拍照成功,文件对象:', file);
// //
const imageFile = res.tempFiles && res.tempFiles.length > 0 ? res.tempFiles[0] : res; sendImageMessage(file);
console.log('准备发送图片:', imageFile);
sendImageMessage(imageFile);
}, },
(err) => { (err) => {
console.error('拍照失败:', err); console.error('拍照失败:', err);

View File

@ -346,31 +346,133 @@ export const chooseMedia = async (options, onSuccess, onFail) => {
}; };
/** /**
* 选择图片 * 选择图片针对 TIM SDK 优化
* @param {function} onSuccess - 成功回调 * @param {function} onSuccess - 成功回调
* @param {function} onFail - 失败回调 * @param {function} onFail - 失败回调
*/ */
export const chooseImage = (onSuccess, onFail) => { export const chooseImage = async (onSuccess, onFail) => {
// 检查权限
const sourceType = ['album', 'camera'];
if (sourceType.includes('album')) {
const hasPermission = await checkAlbumPermission();
if (!hasPermission) {
console.log('用户未授予相册权限');
if (onFail) {
onFail({ errMsg: '未授权相册权限' });
}
return;
}
}
// 使用 wx.chooseImage 以确保与 TIM SDK 兼容
// #ifdef MP-WEIXIN
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: sourceType,
success: function (res) {
console.log('wx.chooseImage 成功,完整返回数据:', JSON.stringify(res));
console.log('tempFilePaths:', res.tempFilePaths);
console.log('tempFiles:', res.tempFiles);
// TIM SDK 需要完整的 wx.chooseImage 返回对象,而不是单个文件
// 直接传递整个 res 对象
if (onSuccess) onSuccess(res);
},
fail: function (err) {
// 用户取消选择
if (err.errMsg && err.errMsg.includes('cancel')) {
console.log('用户取消选择');
return;
}
// 权限相关错误
if (err.errMsg && (err.errMsg.includes('permission') || err.errMsg.includes('auth') || err.errMsg.includes('拒绝'))) {
console.error('相册权限被拒绝:', err);
uni.showModal({
title: '需要相册权限',
content: '请在设置中开启相册权限后重试',
confirmText: '去设置',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting();
}
}
});
if (onFail) {
onFail(err);
}
return;
}
// 其他错误
console.error('选择图片失败:', err);
if (onFail) {
onFail(err);
} else {
showMessage('选择图片失败,请重试');
}
}
});
// #endif
// #ifndef MP-WEIXIN
// 非微信小程序环境,使用 uni.chooseMedia
chooseMedia({ chooseMedia({
count: 1, count: 1,
mediaType: ['image'], mediaType: ['image'],
sizeType: ['original', 'compressed'], sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'] sourceType: sourceType
}, onSuccess, onFail); }, onSuccess, onFail);
// #endif
}; };
/** /**
* 拍照 * 拍照针对 TIM SDK 优化
* @param {function} onSuccess - 成功回调 * @param {function} onSuccess - 成功回调
* @param {function} onFail - 失败回调 * @param {function} onFail - 失败回调
*/ */
export const takePhoto = (onSuccess, onFail) => { export const takePhoto = (onSuccess, onFail) => {
// 使用 wx.chooseImage 以确保与 TIM SDK 兼容
// #ifdef MP-WEIXIN
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['camera'],
success: function (res) {
console.log('wx.chooseImage (拍照) 成功,完整返回数据:', JSON.stringify(res));
console.log('tempFilePaths:', res.tempFilePaths);
console.log('tempFiles:', res.tempFiles);
// TIM SDK 需要完整的 wx.chooseImage 返回对象
if (onSuccess) onSuccess(res);
},
fail: function (err) {
// 用户取消
if (err.errMsg && err.errMsg.includes('cancel')) {
console.log('用户取消拍照');
return;
}
console.error('拍照失败:', err);
if (onFail) {
onFail(err);
} else {
showMessage('拍照失败,请重试');
}
}
});
// #endif
// #ifndef MP-WEIXIN
// 非微信小程序环境
chooseMedia({ chooseMedia({
count: 1, count: 1,
mediaType: ['image'], mediaType: ['image'],
sizeType: ['original', 'compressed'], sizeType: ['original', 'compressed'],
sourceType: ['camera'] sourceType: ['camera']
}, onSuccess, onFail); }, onSuccess, onFail);
// #endif
}; };
// ==================== 录音相关工具方法 ==================== // ==================== 录音相关工具方法 ====================

View File

@ -2161,19 +2161,30 @@ class TimChatManager {
console.log('发送图片消息conversationID:', conversationID, 'groupID:', groupID); console.log('发送图片消息conversationID:', conversationID, 'groupID:', groupID);
// 处理文件对象 - 确保获取正确的文件 // imageFile 现在是完整的 wx.chooseImage 返回对象
let actualFile = imageFile; console.log('接收到的图片选择结果:', imageFile);
if (imageFile?.tempFiles?.length > 0) { console.log('类型:', typeof imageFile);
actualFile = imageFile.tempFiles[0]; console.log('keys:', imageFile ? Object.keys(imageFile) : 'null');
console.log('从 tempFiles 中提取文件:', actualFile);
} else if (imageFile?.tempFilePath) { // 验证对象
// 如果已经是单个文件对象 if (!imageFile) {
actualFile = imageFile; console.error('图片选择结果为空');
console.log('使用单个文件对象:', actualFile); this.triggerCallback('onError', '图片文件无效');
return { success: false, error: '图片选择结果为空' };
} }
// 获取图片尺寸信息 // 获取文件路径用于显示预览
const imageInfo = await this.getImageInfo(actualFile); let previewPath = '';
if (imageFile.tempFilePaths && imageFile.tempFilePaths.length > 0) {
previewPath = imageFile.tempFilePaths[0];
} else if (imageFile.tempFiles && imageFile.tempFiles.length > 0) {
previewPath = imageFile.tempFiles[0].tempFilePath || imageFile.tempFiles[0].path;
}
console.log('预览路径:', previewPath);
// 获取图片尺寸信息(用于本地预览)
const imageInfo = await this.getImageInfo(previewPath);
const localMessage = { const localMessage = {
ID: `local_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, ID: `local_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
@ -2181,7 +2192,7 @@ class TimChatManager {
type: 'TIMImageElem', type: 'TIMImageElem',
payload: { payload: {
imageInfoArray: [{ imageInfoArray: [{
url: this.getImageUrl(actualFile), url: previewPath,
width: imageInfo.width, width: imageInfo.width,
height: imageInfo.height height: imageInfo.height
}] }]
@ -2200,17 +2211,20 @@ class TimChatManager {
// 触发消息接收回调让UI立即显示 // 触发消息接收回调让UI立即显示
this.triggerCallback('onMessageReceived', localMessage) this.triggerCallback('onMessageReceived', localMessage)
console.log('准备创建 TIM 图片消息groupID:', groupID, 'file:', actualFile); console.log('准备创建 TIM 图片消息groupID:', groupID, 'imageFile:', imageFile);
try {
// 创建图片消息 - 直接传递 wx.chooseImage 的完整返回对象
const message = this.tim.createImageMessage({ const message = this.tim.createImageMessage({
to: groupID, to: groupID,
conversationType: TIM.TYPES.CONV_GROUP, conversationType: TIM.TYPES.CONV_GROUP,
payload: { file: actualFile } payload: {
file: imageFile // 传递完整的 wx.chooseImage 返回对象
}
}) })
console.log('TIM 图片消息已创建:', message); console.log('TIM 图片消息已创建:', message);
try {
console.log('开始发送图片消息...'); console.log('开始发送图片消息...');
const sendResult = await this.tim.sendMessage(message); const sendResult = await this.tim.sendMessage(message);
console.log('图片消息发送成功:', sendResult); console.log('图片消息发送成功:', sendResult);
@ -2218,6 +2232,11 @@ class TimChatManager {
return { success: true, message: localMessage } return { success: true, message: localMessage }
} catch (error) { } catch (error) {
console.error('图片消息发送失败:', error) console.error('图片消息发送失败:', error)
console.error('错误详情:', {
message: error.message,
stack: error.stack,
imageFile: imageFile
});
localMessage.status = 'failed' localMessage.status = 'failed'
// 如果是因为未登录导致的失败,尝试重连 // 如果是因为未登录导致的失败,尝试重连
@ -2478,37 +2497,40 @@ class TimChatManager {
} }
getImageUrl(imageFile) { getImageUrl(imageFile) {
// 处理 tempFiles 数组格式 // 支持 tempFilePath 或 path
if (imageFile?.tempFiles?.length > 0) {
return imageFile.tempFiles[0].tempFilePath
}
// 处理单个文件对象
if (imageFile?.tempFilePath) { if (imageFile?.tempFilePath) {
return imageFile.tempFilePath return imageFile.tempFilePath;
}
if (imageFile?.path) {
return imageFile.path;
} }
// 处理字符串路径 // 处理字符串路径
if (typeof imageFile === 'string') { if (typeof imageFile === 'string') {
return imageFile return imageFile;
} }
console.warn('无法获取图片URL使用默认图片:', imageFile); console.warn('无法获取图片URL使用默认图片:', imageFile);
return '/static/home/photo.png' return '/static/home/photo.png';
} }
// 获取图片尺寸信息 // 获取图片尺寸信息
getImageInfo(imageFile) { getImageInfo(imagePath) {
return new Promise((resolve) => { return new Promise((resolve) => {
let imagePath = ''; // 如果传入的是对象,尝试提取路径
if (typeof imagePath === 'object') {
// 获取图片路径 - 处理多种格式 if (imagePath?.tempFilePath) {
if (imageFile?.tempFiles?.length > 0) { imagePath = imagePath.tempFilePath;
imagePath = imageFile.tempFiles[0].tempFilePath; } else if (imagePath?.path) {
} else if (imageFile?.tempFilePath) { imagePath = imagePath.path;
imagePath = imageFile.tempFilePath;
} else if (typeof imageFile === 'string') {
imagePath = imageFile;
} else { } else {
console.warn('无法获取图片路径,使用默认尺寸:', imageFile); console.warn('无法从对象中获取图片路径,使用默认尺寸:', imagePath);
// 默认尺寸 resolve({ width: 400, height: 300 });
return;
}
}
// 如果不是字符串,使用默认尺寸
if (typeof imagePath !== 'string' || !imagePath) {
console.warn('图片路径无效,使用默认尺寸:', imagePath);
resolve({ width: 400, height: 300 }); resolve({ width: 400, height: 300 });
return; return;
} }