lrj
昨天 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b
wx/lib/cosUtil.js
@@ -71,10 +71,16 @@
   * @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}`
  }
  /**
@@ -92,43 +98,55 @@
        return
      }
      const cosConfig = app.globalData.cos
      const uniqueFileName = this.generateUniqueFileName(originalName || 'file.jpg')
      const key = this.generateFilePath(uniqueFileName, fileType)
      // 首先获取文件信息(包括文件大小)
      wx.getFileInfo({
        filePath: filePath,
        success: (fileInfo) => {
          const cosConfig = app.globalData.cos
          const uniqueFileName = this.generateUniqueFileName(originalName || 'file.jpg')
          const key = this.generateFilePath(uniqueFileName, fileType)
      console.log('开始上传文件到COS:', {
        filePath,
        fileType,
        key,
        bucket: cosConfig.bucket
      })
      this.cos.uploadFile({
        Bucket: cosConfig.bucket,
        Region: cosConfig.region,
        Key: key,
        FilePath: filePath,
        onProgress: (progressData) => {
          const percent = Math.round(progressData.percent * 100)
          console.log('上传进度:', percent + '%')
          if (onProgress && typeof onProgress === 'function') {
            onProgress(percent)
          }
        }
      }, (err, data) => {
        if (err) {
          console.error('COS上传失败:', err)
          reject(err)
        } else {
          console.log('COS上传成功:', data)
          resolve({
            key: key,
            url: `https://${data.Location}`,
            etag: data.ETag,
            fileName: uniqueFileName,
            originalName: originalName,
            fileType: fileType
          console.log('开始上传文件到COS:', {
            filePath,
            fileType,
            key,
            fileSize: fileInfo.size,
            bucket: cosConfig.bucket
          })
          this.cos.uploadFile({
            Bucket: cosConfig.bucket,
            Region: cosConfig.region,
            Key: key,
            FilePath: filePath,
            onProgress: (progressData) => {
              const percent = Math.round(progressData.percent * 100)
              console.log('上传进度:', percent + '%')
              if (onProgress && typeof onProgress === 'function') {
                onProgress(percent)
              }
            }
          }, (err, data) => {
            if (err) {
              console.error('COS上传失败:', err)
              reject(err)
            } else {
              console.log('COS上传成功:', data)
              resolve({
                key: key,
                url: `https://${data.Location}`,
                etag: data.ETag,
                fileName: uniqueFileName,
                originalName: originalName,
                fileType: fileType,
                fileSize: fileInfo.size
              })
            }
          })
        },
        fail: (error) => {
          console.error('获取文件信息失败:', error)
          reject(new Error('获取文件信息失败: ' + error.errMsg))
        }
      })
    })