190 lines
5.1 KiB
JavaScript
190 lines
5.1 KiB
JavaScript
|
|
/**
|
|||
|
|
* 文件上传工具
|
|||
|
|
* 统一处理图片、视频、文件上传
|
|||
|
|
*/
|
|||
|
|
import request from './request.js'
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 上传图片
|
|||
|
|
* @param {String} filePath 本地文件路径
|
|||
|
|
* @param {Function} onProgress 上传进度回调
|
|||
|
|
* @returns {Promise} 返回上传结果
|
|||
|
|
*/
|
|||
|
|
export function uploadImage(filePath, onProgress) {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
const token = uni.getStorageSync('token')
|
|||
|
|
|
|||
|
|
const uploadTask = uni.uploadFile({
|
|||
|
|
url: request.baseURL + '/api/file/upload',
|
|||
|
|
filePath: filePath,
|
|||
|
|
name: 'file',
|
|||
|
|
header: {
|
|||
|
|
'Authorization': token ? `Bearer ${token}` : ''
|
|||
|
|
},
|
|||
|
|
success: (res) => {
|
|||
|
|
try {
|
|||
|
|
const data = JSON.parse(res.data)
|
|||
|
|
if (data.code === 200) {
|
|||
|
|
console.log('图片上传成功:', data.data.fileUrl)
|
|||
|
|
resolve(data.data)
|
|||
|
|
} else {
|
|||
|
|
console.error('图片上传失败:', data.message)
|
|||
|
|
reject(new Error(data.message || '上传失败'))
|
|||
|
|
}
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error('解析上传结果失败:', e)
|
|||
|
|
reject(new Error('上传失败'))
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('图片上传失败:', err)
|
|||
|
|
reject(err)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 监听上传进度
|
|||
|
|
if (onProgress && typeof onProgress === 'function') {
|
|||
|
|
uploadTask.onProgressUpdate((res) => {
|
|||
|
|
onProgress(res.progress)
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 批量上传图片
|
|||
|
|
* @param {Array} filePaths 本地文件路径数组
|
|||
|
|
* @param {Function} onProgress 上传进度回调
|
|||
|
|
* @returns {Promise} 返回上传结果数组
|
|||
|
|
*/
|
|||
|
|
export function uploadImages(filePaths, onProgress) {
|
|||
|
|
const total = filePaths.length
|
|||
|
|
let completed = 0
|
|||
|
|
|
|||
|
|
const uploadPromises = filePaths.map((filePath, index) => {
|
|||
|
|
return uploadImage(filePath, (progress) => {
|
|||
|
|
// 计算总体进度
|
|||
|
|
const itemProgress = progress / total
|
|||
|
|
const totalProgress = (completed / total * 100) + itemProgress
|
|||
|
|
if (onProgress) {
|
|||
|
|
onProgress(Math.floor(totalProgress), index, progress)
|
|||
|
|
}
|
|||
|
|
}).then(result => {
|
|||
|
|
completed++
|
|||
|
|
return result
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
return Promise.all(uploadPromises)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 选择并上传图片
|
|||
|
|
* @param {Object} options 选项
|
|||
|
|
* @param {Number} options.count 最多可选择的图片数量,默认9
|
|||
|
|
* @param {Array} options.sizeType 图片尺寸,默认['original', 'compressed']
|
|||
|
|
* @param {Array} options.sourceType 图片来源,默认['album', 'camera']
|
|||
|
|
* @param {Function} options.onProgress 上传进度回调
|
|||
|
|
* @returns {Promise} 返回上传结果数组
|
|||
|
|
*/
|
|||
|
|
export function chooseAndUploadImage(options = {}) {
|
|||
|
|
const {
|
|||
|
|
count = 9,
|
|||
|
|
sizeType = ['original', 'compressed'],
|
|||
|
|
sourceType = ['album', 'camera'],
|
|||
|
|
onProgress
|
|||
|
|
} = options
|
|||
|
|
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
uni.chooseImage({
|
|||
|
|
count,
|
|||
|
|
sizeType,
|
|||
|
|
sourceType,
|
|||
|
|
success: (res) => {
|
|||
|
|
const tempFilePaths = res.tempFilePaths
|
|||
|
|
console.log('选择了', tempFilePaths.length, '张图片')
|
|||
|
|
|
|||
|
|
// 上传图片
|
|||
|
|
uploadImages(tempFilePaths, onProgress)
|
|||
|
|
.then(results => {
|
|||
|
|
console.log('所有图片上传成功')
|
|||
|
|
resolve(results)
|
|||
|
|
})
|
|||
|
|
.catch(err => {
|
|||
|
|
console.error('图片上传失败:', err)
|
|||
|
|
reject(err)
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('选择图片失败:', err)
|
|||
|
|
reject(err)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 上传视频
|
|||
|
|
* @param {String} filePath 本地文件路径
|
|||
|
|
* @param {Function} onProgress 上传进度回调
|
|||
|
|
* @returns {Promise} 返回上传结果
|
|||
|
|
*/
|
|||
|
|
export function uploadVideo(filePath, onProgress) {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
const token = uni.getStorageSync('token')
|
|||
|
|
|
|||
|
|
const uploadTask = uni.uploadFile({
|
|||
|
|
url: request.baseURL + '/api/file/upload',
|
|||
|
|
filePath: filePath,
|
|||
|
|
name: 'file',
|
|||
|
|
header: {
|
|||
|
|
'Authorization': token ? `Bearer ${token}` : ''
|
|||
|
|
},
|
|||
|
|
success: (res) => {
|
|||
|
|
try {
|
|||
|
|
const data = JSON.parse(res.data)
|
|||
|
|
if (data.code === 200) {
|
|||
|
|
console.log('视频上传成功:', data.data.fileUrl)
|
|||
|
|
resolve(data.data)
|
|||
|
|
} else {
|
|||
|
|
console.error('视频上传失败:', data.message)
|
|||
|
|
reject(new Error(data.message || '上传失败'))
|
|||
|
|
}
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error('解析上传结果失败:', e)
|
|||
|
|
reject(new Error('上传失败'))
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('视频上传失败:', err)
|
|||
|
|
reject(err)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 监听上传进度
|
|||
|
|
if (onProgress && typeof onProgress === 'function') {
|
|||
|
|
uploadTask.onProgressUpdate((res) => {
|
|||
|
|
onProgress(res.progress)
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 上传文件
|
|||
|
|
* @param {String} filePath 本地文件路径
|
|||
|
|
* @param {Function} onProgress 上传进度回调
|
|||
|
|
* @returns {Promise} 返回上传结果
|
|||
|
|
*/
|
|||
|
|
export function uploadFile(filePath, onProgress) {
|
|||
|
|
return uploadImage(filePath, onProgress)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default {
|
|||
|
|
uploadImage,
|
|||
|
|
uploadImages,
|
|||
|
|
chooseAndUploadImage,
|
|||
|
|
uploadVideo,
|
|||
|
|
uploadFile
|
|||
|
|
}
|