// 文件上传工具 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 }