提交代码
This commit is contained in:
1 parent
aacdbdad81
commit
a0e5c9a024
11 files changed
+6086
-26
No files matched your search
@@ -0,0 +1,308 @@
|
||||
// 文件上传工具 - 移动端适配
|
||||
import { request } from '@/common/request.js';
|
||||
import { REQUEST_CONFIG } from '@/common/config.js';
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*/
|
||||
function getToken() {
|
||||
try {
|
||||
const token = uni.getStorageSync('access_token');
|
||||
return token ? `Bearer ${token}` : '';
|
||||
} catch (e) {
|
||||
console.error('❌ 获取Token失败:', e);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建完整的上传URL
|
||||
*/
|
||||
function buildUploadUrl(url) {
|
||||
let fullUrl;
|
||||
if (REQUEST_CONFIG.baseURL.startsWith('/')) {
|
||||
// 开发环境,baseURL是'/api',需要使用完整的服务器URL
|
||||
fullUrl = `${REQUEST_CONFIG.serverUrl}${url}`;
|
||||
} else {
|
||||
// 生产环境,直接拼接
|
||||
fullUrl = REQUEST_CONFIG.baseURL + url;
|
||||
}
|
||||
return fullUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传单个文件
|
||||
* @param {string} url - 上传接口URL
|
||||
* @param {Object} options - 上传选项
|
||||
* @param {string} options.filePath - 文件路径
|
||||
* @param {string} options.name - 文件字段名,默认'file'
|
||||
* @param {Object} options.formData - 额外的表单数据
|
||||
* @param {Function} options.onProgress - 进度回调
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function uploadFile(url, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { filePath, name = 'file', formData = {}, onProgress } = options;
|
||||
|
||||
if (!filePath) {
|
||||
return reject(new Error('文件路径不能为空'));
|
||||
}
|
||||
|
||||
const token = getToken();
|
||||
const clientid = uni.getStorageSync('clientid') || REQUEST_CONFIG.clientId;
|
||||
const fullUrl = buildUploadUrl(url);
|
||||
|
||||
console.log('🚀 开始上传文件:', {
|
||||
url: fullUrl,
|
||||
filePath,
|
||||
name,
|
||||
});
|
||||
|
||||
const uploadTask = uni.uploadFile({
|
||||
url: fullUrl,
|
||||
filePath,
|
||||
name,
|
||||
formData,
|
||||
header: {
|
||||
authorization: token,
|
||||
clientid: clientid,
|
||||
},
|
||||
success: (res) => {
|
||||
console.log('✅ 文件上传成功:', res);
|
||||
|
||||
if (res.statusCode === 200) {
|
||||
try {
|
||||
const data = JSON.parse(res.data);
|
||||
if (data.code === 200) {
|
||||
resolve(data);
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: data.msg || '上传失败',
|
||||
icon: 'none',
|
||||
});
|
||||
reject(new Error(data.msg || '上传失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ 解析上传响应失败:', error);
|
||||
reject(new Error('解析响应数据失败'));
|
||||
}
|
||||
} else {
|
||||
reject(new Error(`上传失败: HTTP ${res.statusCode}`));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('❌ 文件上传失败:', err);
|
||||
uni.showToast({
|
||||
title: '上传失败',
|
||||
icon: 'none',
|
||||
});
|
||||
reject(new Error(err.errMsg || '上传失败'));
|
||||
},
|
||||
});
|
||||
|
||||
// 监听上传进度
|
||||
if (onProgress && typeof onProgress === 'function') {
|
||||
uploadTask.onProgressUpdate((res) => {
|
||||
onProgress(res);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传多个文件
|
||||
* @param {string} url - 上传接口URL
|
||||
* @param {Array} files - 文件列表
|
||||
* @param {Object} options - 上传选项
|
||||
* @returns {Promise<Array>}
|
||||
*/
|
||||
export function uploadFiles(url, files, options = {}) {
|
||||
const uploadPromises = files.map((file, index) => {
|
||||
return uploadFile(url, {
|
||||
...options,
|
||||
filePath: file.tempFilePath || file.path || file.url,
|
||||
onProgress: (progress) => {
|
||||
if (options.onProgress) {
|
||||
options.onProgress({
|
||||
...progress,
|
||||
fileIndex: index,
|
||||
totalFiles: files.length,
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(uploadPromises);
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择并上传图片
|
||||
* @param {string} url - 上传接口URL
|
||||
* @param {Object} options - 选择和上传选项
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function chooseAndUploadImage(url, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const {
|
||||
count = 1,
|
||||
sizeType = ['compressed'],
|
||||
sourceType = ['album', 'camera'],
|
||||
...uploadOptions
|
||||
} = options;
|
||||
|
||||
uni.chooseImage({
|
||||
count,
|
||||
sizeType,
|
||||
sourceType,
|
||||
success: (chooseRes) => {
|
||||
console.log('📷 选择图片成功:', chooseRes);
|
||||
|
||||
if (count === 1) {
|
||||
// 单张图片上传
|
||||
uploadFile(url, {
|
||||
...uploadOptions,
|
||||
filePath: chooseRes.tempFilePaths[0],
|
||||
})
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
} else {
|
||||
// 多张图片上传
|
||||
uploadFiles(url, chooseRes.tempFiles, uploadOptions)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('❌ 选择图片失败:', err);
|
||||
reject(new Error(err.errMsg || '选择图片失败'));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择并上传文件
|
||||
* @param {string} url - 上传接口URL
|
||||
* @param {Object} options - 选择和上传选项
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function chooseAndUploadFile(url, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { count = 1, type = 'all', extension = [], ...uploadOptions } = options;
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 微信小程序使用 wx.chooseMessageFile
|
||||
uni.chooseMessageFile({
|
||||
count,
|
||||
type,
|
||||
extension,
|
||||
success: (chooseRes) => {
|
||||
console.log('📁 选择文件成功:', chooseRes);
|
||||
|
||||
if (count === 1) {
|
||||
uploadFile(url, {
|
||||
...uploadOptions,
|
||||
filePath: chooseRes.tempFiles[0].path,
|
||||
})
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
} else {
|
||||
uploadFiles(url, chooseRes.tempFiles, uploadOptions)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('❌ 选择文件失败:', err);
|
||||
reject(new Error(err.errMsg || '选择文件失败'));
|
||||
},
|
||||
});
|
||||
// #endif
|
||||
|
||||
// #ifndef MP-WEIXIN
|
||||
// H5和其他平台使用普通文件选择
|
||||
uni.chooseFile({
|
||||
count,
|
||||
type,
|
||||
extension,
|
||||
success: (chooseRes) => {
|
||||
console.log('📁 选择文件成功:', chooseRes);
|
||||
|
||||
if (count === 1) {
|
||||
uploadFile(url, {
|
||||
...uploadOptions,
|
||||
filePath: chooseRes.tempFilePaths[0],
|
||||
})
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
} else {
|
||||
uploadFiles(url, chooseRes.tempFiles, uploadOptions)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('❌ 选择文件失败:', err);
|
||||
reject(new Error(err.errMsg || '选择文件失败'));
|
||||
},
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩图片
|
||||
* @param {string} src - 图片路径
|
||||
* @param {Object} options - 压缩选项
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function compressImage(src, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { quality = 80, compressedWidth, compressedHeight } = options;
|
||||
|
||||
uni.compressImage({
|
||||
src,
|
||||
quality,
|
||||
compressedWidth,
|
||||
compressedHeight,
|
||||
success: (res) => {
|
||||
console.log('✅ 图片压缩成功:', res);
|
||||
resolve(res.tempFilePath);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('❌ 图片压缩失败:', err);
|
||||
reject(new Error(err.errMsg || '图片压缩失败'));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片信息
|
||||
* @param {string} src - 图片路径
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function getImageInfo(src) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.getImageInfo({
|
||||
src,
|
||||
success: (res) => {
|
||||
resolve(res);
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error(err.errMsg || '获取图片信息失败'));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 默认导出
|
||||
export default {
|
||||
uploadFile,
|
||||
uploadFiles,
|
||||
chooseAndUploadImage,
|
||||
chooseAndUploadFile,
|
||||
compressImage,
|
||||
getImageInfo,
|
||||
};
|
||||
Reference in new issue
Block a user