| | |
| | | * @returns {string} 唯一文件名 |
| | | */ |
| | | generateUniqueFileName(originalName) { |
| | | const timestamp = Date.now() |
| | | const random = Math.random().toString(36).substring(2, 8) |
| | | // 生成类似UUID的唯一标识符 |
| | | const timestamp = Date.now().toString(36) |
| | | const random1 = Math.random().toString(36).substring(2, 10) |
| | | const random2 = Math.random().toString(36).substring(2, 10) |
| | | const random3 = Math.random().toString(36).substring(2, 6) |
| | | const extension = originalName.substring(originalName.lastIndexOf('.')) |
| | | return `${timestamp}_${random}${extension}` |
| | | |
| | | // 格式: timestamp-random1-random2-random3.ext |
| | | // 例如: k8j2l3m4-a1b2c3d4-e5f6g7h8-i9j0.jpg |
| | | return `${timestamp}-${random1}-${random2}-${random3}${extension}` |
| | | } |
| | | |
| | | /** |
| | |
| | | return |
| | | } |
| | | |
| | | // 首先获取文件信息(包括文件大小) |
| | | wx.getFileInfo({ |
| | | filePath: filePath, |
| | | success: (fileInfo) => { |
| | | const cosConfig = app.globalData.cos |
| | | const uniqueFileName = this.generateUniqueFileName(originalName || 'file.jpg') |
| | | const key = this.generateFilePath(uniqueFileName, fileType) |
| | |
| | | filePath, |
| | | fileType, |
| | | key, |
| | | fileSize: fileInfo.size, |
| | | bucket: cosConfig.bucket |
| | | }) |
| | | |
| | |
| | | etag: data.ETag, |
| | | fileName: uniqueFileName, |
| | | originalName: originalName, |
| | | fileType: fileType |
| | | fileType: fileType, |
| | | fileSize: fileInfo.size |
| | | }) |
| | | } |
| | | }) |
| | | }, |
| | | fail: (error) => { |
| | | console.error('获取文件信息失败:', error) |
| | | reject(new Error('获取文件信息失败: ' + error.errMsg)) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | |