feat: 完善BowongAI SDK文件处理功能

- 修复_getExt方法中PNG类型映射错误
- 新增_generateFileName辅助方法,确保文件名包含正确扩展名
- 优化H5和普通上传方法的文件名生成逻辑
- 增强MIME类型与扩展名映射支持
- 提升跨平台文件处理一致性
This commit is contained in:
imeepos 2025-09-28 15:54:59 +08:00
parent a3b2942872
commit db6c19dae1
1 changed files with 54 additions and 19 deletions

View File

@ -94,7 +94,7 @@ export class BowongAISDK {
} }
// 自动生成文件名(如果未提供) // 自动生成文件名(如果未提供)
const fileName = name || `upload_${Date.now()}_${this._getFileExtension(filePath)}`; const fileName = this._generateFileName(filePath, name, type);
// 自动判断文件类型(如果未提供) // 自动判断文件类型(如果未提供)
const fileType = type || this._getMimeType(filePath); const fileType = type || this._getMimeType(filePath);
@ -238,7 +238,7 @@ export class BowongAISDK {
fileInfo = { size: 0 }; fileInfo = { size: 0 };
} }
// 自动生成文件名(如果未提供) // 自动生成文件名(如果未提供)
const fileName = name || `upload_${Date.now()}.${this._getFileExtension(filePath)}`; const fileName = this._generateFileName(filePath, name, type);
// 自动判断文件类型(如果未提供) // 自动判断文件类型(如果未提供)
const fileType = type || this._getMimeType(filePath); const fileType = type || this._getMimeType(filePath);
@ -386,10 +386,10 @@ export class BowongAISDK {
const errMsg = error.errMsg.toLowerCase(); const errMsg = error.errMsg.toLowerCase();
return errMsg.includes('auth') || return errMsg.includes('auth') ||
errMsg.includes('permission') || errMsg.includes('permission') ||
errMsg.includes('denied') || errMsg.includes('denied') ||
errMsg.includes('authorize') || errMsg.includes('authorize') ||
errMsg.includes('scope'); errMsg.includes('scope');
} }
/** /**
@ -412,8 +412,8 @@ export class BowongAISDK {
console.log('用户设置结果:', settingResult); console.log('用户设置结果:', settingResult);
// 检查是否授权了相册权限 // 检查是否授权了相册权限
if (settingResult.authSetting && if (settingResult.authSetting &&
(settingResult.authSetting['scope.writePhotosAlbum'] || (settingResult.authSetting['scope.writePhotosAlbum'] ||
settingResult.authSetting['scope.camera'])) { settingResult.authSetting['scope.camera'])) {
Taro.showToast({ Taro.showToast({
title: '授权成功', title: '授权成功',
icon: 'success', icon: 'success',
@ -541,6 +541,41 @@ export class BowongAISDK {
return mimeTypes[extension] || 'application/octet-stream'; return mimeTypes[extension] || 'application/octet-stream';
} }
private _getExt(type: string): string {
const mimeTypes: Record<string, string> = {
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif',
'image/webp': 'webp',
'video/mp4': 'mp4',
'video/quicktime': 'mov',
'video/x-msvideo': 'avi'
};
return mimeTypes[type] || 'jpg';
}
/**
*
* @private
*/
private _generateFileName(filePath: string, name?: string, type?: string): string {
// 如果提供了名称,使用提供的名称
if (name) {
// 检查名称是否包含扩展名
if (name.includes('.')) {
return name;
}
// 没有扩展名,需要添加
const fileType = type || this._getMimeType(filePath);
const extension = this._getExt(fileType);
return `${name}.${extension}`;
}
// 没有提供名称,自动生成
const fileExtension = this._getFileExtension(filePath);
return `upload_${Date.now()}.${fileExtension}`;
}
/** /**
* *
* @param params * @param params