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
// 文件上传工具
const app = getApp()
 
/**
 * 上传文件到服务器
 * @param {string} filePath 文件路径
 * @param {string} fileType 文件类型 image/video/audio
 * @param {function} onProgress 上传进度回调
 * @returns {Promise} 返回上传结果
 */
function uploadFile(filePath, fileType = 'image', onProgress = null) {
  return new Promise((resolve, reject) => {
    const uploadTask = wx.uploadFile({
      url: app.globalData.baseUrl.replace('/graphql', '/upload'), // 假设有文件上传接口
      filePath: filePath,
      name: 'file',
      formData: {
        'type': fileType,
        'token': app.globalData.token
      },
      header: {
        'Authorization': app.globalData.token ? `Bearer ${app.globalData.token}` : ''
      },
      success: (res) => {
        try {
          const data = JSON.parse(res.data)
          if (data.success) {
            resolve({
              url: data.url,
              fileId: data.fileId,
              fileName: data.fileName
            })
          } else {
            reject(new Error(data.message || '上传失败'))
          }
        } catch (e) {
          reject(new Error('上传响应解析失败'))
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
 
    // 监听上传进度
    if (onProgress && typeof onProgress === 'function') {
      uploadTask.onProgressUpdate((res) => {
        onProgress(res.progress)
      })
    }
  })
}
 
/**
 * 选择并上传图片
 * @param {object} options 选择图片选项
 * @param {function} onProgress 上传进度回调
 * @returns {Promise} 返回上传结果
 */
function chooseAndUploadImage(options = {}, 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) {
          // 单个文件上传
          uploadFile(tempFilePaths[0], 'image', onProgress)
            .then(resolve)
            .catch(reject)
        } else {
          // 多个文件上传
          const uploadPromises = tempFilePaths.map(filePath => 
            uploadFile(filePath, 'image', onProgress)
          )
          
          Promise.all(uploadPromises)
            .then(resolve)
            .catch(reject)
        }
      },
      fail: reject
    })
  })
}
 
/**
 * 选择并上传视频
 * @param {object} options 选择视频选项
 * @param {function} onProgress 上传进度回调
 * @returns {Promise} 返回上传结果
 */
function 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) => {
        uploadFile(res.tempFilePath, 'video', onProgress)
          .then(resolve)
          .catch(reject)
      },
      fail: reject
    })
  })
}
 
/**
 * 预览图片
 * @param {string} current 当前显示图片的链接
 * @param {array} urls 需要预览的图片链接列表
 */
function previewImage(current, urls = []) {
  wx.previewImage({
    current: current,
    urls: urls.length > 0 ? urls : [current]
  })
}
 
/**
 * 保存图片到相册
 * @param {string} filePath 图片路径
 * @returns {Promise}
 */
function saveImageToPhotosAlbum(filePath) {
  return new Promise((resolve, reject) => {
    wx.saveImageToPhotosAlbum({
      filePath: filePath,
      success: resolve,
      fail: reject
    })
  })
}
 
/**
 * 获取文件信息
 * @param {string} filePath 文件路径
 * @returns {Promise}
 */
function getFileInfo(filePath) {
  return new Promise((resolve, reject) => {
    wx.getFileInfo({
      filePath: filePath,
      success: resolve,
      fail: reject
    })
  })
}
 
/**
 * 压缩图片
 * @param {string} src 图片路径
 * @param {number} quality 压缩质量,范围0-100
 * @returns {Promise}
 */
function compressImage(src, quality = 80) {
  return new Promise((resolve, reject) => {
    wx.compressImage({
      src: src,
      quality: quality,
      success: resolve,
      fail: reject
    })
  })
}
 
module.exports = {
  uploadFile,
  chooseAndUploadImage,
  chooseAndUploadVideo,
  previewImage,
  saveImageToPhotosAlbum,
  getFileInfo,
  compressImage
}