lrj
昨天 9f8395fab13ca4b230a0f7d62636e209745c91d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// COS上传工具类
const COS = require('./cos-wx-sdk-v5.min.js')
const app = getApp()
 
/**
 * COS上传工具类
 * 注意:需要先保存后台数据,再上传文件,避免孤立的COS资源
 */
class CosUtil {
  constructor() {
    this.cos = null
    this.initCOS()
  }
 
  /**
   * 初始化COS实例
   */
  initCOS() {
    const cosConfig = app.globalData.cos
    if (!cosConfig) {
      console.error('COS配置未找到')
      return
    }
 
    this.cos = new COS({
      SecretId: cosConfig.secretId,
      SecretKey: cosConfig.secretKey,
      // 可选,设置请求域名
      Domain: `${cosConfig.bucket}.cos.${cosConfig.region}.myqcloud.com`,
      // 可选,设置上传时计算 md5
      UploadCheckContentMd5: true
    })
 
    console.log('COS实例初始化成功')
  }
 
  /**
   * 生成文件路径(按日期分目录,与后台保持一致)
   * @param {string} fileName 文件名
   * @param {string} fileType 文件类型 avatar/image/video/attachment
   * @returns {string} 文件路径
   */
  generateFilePath(fileName, fileType = 'image') {
    const now = new Date()
    const dateDir = now.getFullYear() + 
                   String(now.getMonth() + 1).padStart(2, '0') + 
                   String(now.getDate()).padStart(2, '0')
    
    // 根据文件类型添加前缀
    let prefix = ''
    switch (fileType) {
      case 'avatar':
        prefix = 'avatars/'
        break
      case 'video':
        prefix = 'videos/'
        break
      case 'attachment':
        prefix = 'attachments/'
        break
      default:
        prefix = 'images/'
    }
    
    return `${prefix}${dateDir}/${fileName}`
  }
 
  /**
   * 生成唯一文件名
   * @param {string} originalName 原始文件名
   * @returns {string} 唯一文件名
   */
  generateUniqueFileName(originalName) {
    const timestamp = Date.now()
    const random = Math.random().toString(36).substring(2, 8)
    const extension = originalName.substring(originalName.lastIndexOf('.'))
    return `${timestamp}_${random}${extension}`
  }
 
  /**
   * 上传文件到COS
   * @param {string} filePath 本地文件路径
   * @param {string} fileType 文件类型 avatar/image/video/attachment
   * @param {string} originalName 原始文件名
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  uploadFile(filePath, fileType = 'image', originalName = '', onProgress = null) {
    return new Promise((resolve, reject) => {
      if (!this.cos) {
        reject(new Error('COS未初始化'))
        return
      }
 
      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
          })
        }
      })
    })
  }
 
  /**
   * 上传头像
   * @param {string} filePath 本地文件路径
   * @param {string} originalName 原始文件名
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  uploadAvatar(filePath, originalName = '', onProgress = null) {
    return this.uploadFile(filePath, 'avatar', originalName, onProgress)
  }
 
  /**
   * 上传图片
   * @param {string} filePath 本地文件路径
   * @param {string} originalName 原始文件名
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  uploadImage(filePath, originalName = '', onProgress = null) {
    return this.uploadFile(filePath, 'image', originalName, onProgress)
  }
 
  /**
   * 上传视频
   * @param {string} filePath 本地文件路径
   * @param {string} originalName 原始文件名
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  uploadVideo(filePath, originalName = '', onProgress = null) {
    return this.uploadFile(filePath, 'video', originalName, onProgress)
  }
 
  /**
   * 上传附件
   * @param {string} filePath 本地文件路径
   * @param {string} originalName 原始文件名
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  uploadAttachment(filePath, originalName = '', onProgress = null) {
    return this.uploadFile(filePath, 'attachment', originalName, onProgress)
  }
 
  /**
   * 选择并上传图片
   * @param {object} options 选择图片选项
   * @param {string} fileType 文件类型
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  chooseAndUploadImage(options = {}, fileType = 'image', onProgress = null) {
    return new Promise((resolve, reject) => {
      wx.chooseImage({
        count: options.count || 1,
        sizeType: options.sizeType || ['original', 'compressed'],
        sourceType: options.sourceType || ['album', 'camera'],
        success: (res) => {
          const tempFilePaths = res.tempFilePaths
          
          if (tempFilePaths.length === 1) {
            // 单个文件上传
            const filePath = tempFilePaths[0]
            const originalName = `image_${Date.now()}.jpg`
            this.uploadFile(filePath, fileType, originalName, onProgress)
              .then(resolve)
              .catch(reject)
          } else {
            // 多个文件上传
            const uploadPromises = tempFilePaths.map((filePath, index) => {
              const originalName = `image_${Date.now()}_${index}.jpg`
              return this.uploadFile(filePath, fileType, originalName, onProgress)
            })
            
            Promise.all(uploadPromises)
              .then(resolve)
              .catch(reject)
          }
        },
        fail: reject
      })
    })
  }
 
  /**
   * 选择并上传视频
   * @param {object} options 选择视频选项
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果
   */
  chooseAndUploadVideo(options = {}, onProgress = null) {
    return new Promise((resolve, reject) => {
      wx.chooseVideo({
        sourceType: options.sourceType || ['album', 'camera'],
        maxDuration: options.maxDuration || 60,
        camera: options.camera || 'back',
        success: (res) => {
          const originalName = `video_${Date.now()}.mp4`
          this.uploadVideo(res.tempFilePath, originalName, onProgress)
            .then(resolve)
            .catch(reject)
        },
        fail: reject
      })
    })
  }
 
  /**
   * 删除COS文件
   * @param {string} key 文件key
   * @returns {Promise} 返回删除结果
   */
  deleteFile(key) {
    return new Promise((resolve, reject) => {
      if (!this.cos) {
        reject(new Error('COS未初始化'))
        return
      }
 
      const cosConfig = app.globalData.cos
 
      this.cos.deleteObject({
        Bucket: cosConfig.bucket,
        Region: cosConfig.region,
        Key: key
      }, (err, data) => {
        if (err) {
          console.error('COS删除文件失败:', err)
          reject(err)
        } else {
          console.log('COS删除文件成功:', data)
          resolve(data)
        }
      })
    })
  }
 
  /**
   * 获取文件访问URL
   * @param {string} key 文件key
   * @returns {string} 文件访问URL
   */
  getFileUrl(key) {
    const cosConfig = app.globalData.cos
    return `https://${cosConfig.bucket}.cos.${cosConfig.region}.myqcloud.com/${key}`
  }
 
  /**
   * 批量上传文件
   * @param {Array} files 文件列表 [{filePath, fileType, originalName}]
   * @param {function} onProgress 进度回调
   * @returns {Promise} 返回上传结果列表
   */
  batchUpload(files, onProgress = null) {
    const uploadPromises = files.map((file, index) => {
      const progressCallback = onProgress ? (percent) => {
        onProgress(index, percent, files.length)
      } : null
      
      return this.uploadFile(file.filePath, file.fileType, file.originalName, progressCallback)
    })
 
    return Promise.all(uploadPromises)
  }
}
 
// 创建单例实例
const cosUtil = new CosUtil()
 
module.exports = cosUtil