2025-08-04 17:50:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* 文件上传工具类
|
|
|
|
|
* 封装微信小程序文件上传功能
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-05 18:38:32 +08:00
|
|
|
|
const config = require('../../config.js');
|
|
|
|
|
const { getToken } = require('../../utils/auth.js');
|
2025-08-04 17:50:25 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件上传工具类
|
|
|
|
|
*/
|
|
|
|
|
class FileUploader {
|
|
|
|
|
constructor() {
|
|
|
|
|
this.baseUrl = config.baseUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步上传单个文件
|
|
|
|
|
* @param {string} filePath - 文件路径
|
|
|
|
|
* @param {Object} options - 上传配置选项
|
|
|
|
|
* @param {string} options.uploadUrl - 上传接口地址,默认为 /file/upload
|
|
|
|
|
* @param {string} options.name - 上传字段名,默认为 'file'
|
|
|
|
|
* @param {Object} options.formData - 额外表单数据
|
|
|
|
|
* @param {Object} options.header - 额外请求头
|
|
|
|
|
* @returns {Promise} 返回上传结果的Promise
|
|
|
|
|
*/
|
|
|
|
|
syncUploadFile(filePath, options = {}) {
|
|
|
|
|
const {
|
|
|
|
|
uploadUrl = '/file/upload',
|
|
|
|
|
name = 'file',
|
|
|
|
|
formData = {},
|
|
|
|
|
header = {}
|
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
wx.uploadFile({
|
|
|
|
|
url: this.baseUrl + uploadUrl,
|
|
|
|
|
filePath: filePath,
|
|
|
|
|
name: name,
|
|
|
|
|
formData: formData,
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
|
|
|
'Authorization': 'Bearer ' + getToken(),
|
|
|
|
|
...header
|
|
|
|
|
},
|
|
|
|
|
success: (res) => {
|
|
|
|
|
try {
|
|
|
|
|
const data = JSON.parse(res.data);
|
|
|
|
|
resolve({
|
|
|
|
|
success: true,
|
|
|
|
|
data: data,
|
|
|
|
|
url: data.data?.url || ''
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
reject({
|
|
|
|
|
success: false,
|
|
|
|
|
error: error,
|
|
|
|
|
message: '服务器返回数据格式错误'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
console.error('文件上传失败:', err);
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '网络出错,上传失败',
|
|
|
|
|
icon: 'none',
|
|
|
|
|
duration: 1000
|
|
|
|
|
});
|
|
|
|
|
reject({
|
|
|
|
|
success: false,
|
|
|
|
|
error: err,
|
|
|
|
|
message: '网络出错,上传失败'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量上传文件
|
|
|
|
|
* @param {Array<string>} filePaths - 文件路径数组
|
|
|
|
|
* @param {Object} options - 上传配置选项
|
|
|
|
|
* @returns {Promise<Array>} 返回上传成功的URL数组
|
|
|
|
|
*/
|
|
|
|
|
async uploadFiles(filePaths, options = {}) {
|
|
|
|
|
if (!Array.isArray(filePaths) || filePaths.length === 0) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const fileUrls = [];
|
|
|
|
|
|
|
|
|
|
for (const filePath of filePaths) {
|
|
|
|
|
try {
|
2025-08-06 09:56:34 +08:00
|
|
|
|
if (filePath.indexOf(config.baseImgUrl) === 0) {
|
2025-08-04 17:50:25 +08:00
|
|
|
|
fileUrls.push(filePath);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const result = await this.syncUploadFile(filePath, options);
|
|
|
|
|
if (result.success && result.url) {
|
|
|
|
|
fileUrls.push(result.url);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('单个文件上传失败:', error);
|
|
|
|
|
// 继续上传其他文件,不中断整个流程
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileUrls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上传图片(兼容旧版本)
|
|
|
|
|
* @param {string} filePath - 图片路径
|
|
|
|
|
* @returns {Promise} 返回上传结果的Promise
|
|
|
|
|
*/
|
|
|
|
|
syncUploadImage(filePath) {
|
|
|
|
|
return this.syncUploadFile(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量上传图片(兼容旧版本)
|
|
|
|
|
* @param {Array<string>} files - 图片路径数组
|
|
|
|
|
* @returns {Promise<Array>} 返回上传成功的URL数组
|
|
|
|
|
*/
|
|
|
|
|
uploadImages(files) {
|
|
|
|
|
return this.uploadFiles(files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建单例实例
|
|
|
|
|
const fileUploader = new FileUploader();
|
|
|
|
|
|
|
|
|
|
// 导出工具函数
|
|
|
|
|
module.exports = {
|
|
|
|
|
FileUploader,
|
|
|
|
|
fileUploader,
|
|
|
|
|
syncUploadFile: fileUploader.syncUploadFile.bind(fileUploader),
|
|
|
|
|
uploadFiles: fileUploader.uploadFiles.bind(fileUploader),
|
|
|
|
|
syncUploadImage: fileUploader.syncUploadImage.bind(fileUploader),
|
|
|
|
|
uploadImages: fileUploader.uploadImages.bind(fileUploader)
|
|
|
|
|
};
|