Files
qingdao-employment-service/utils/FileUploader.js

150 lines
5.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* LightApp 文件上传工具类封装
* * 使用方法:
* 1. 列表选择上传:
* FileUploader.showMenuAndUpload({ baseUrl: 'http://...' }).then(res => ...);
* * 2. 直接调用某种类型上传:
* FileUploader.directUpload({
* baseUrl: 'http://...',
* chooseType: 'chooseImageUpload'
* }).then(res => ...);
*/
import config from "@/config.js"
export default {
/**
* 默认配置
*/
defaults: {
baseUrl: config.baseUrl, // 必填API的基础路径
uploadPath: '/app/oss/upload', // 上传接口路径
fileKey: 'file',
maxSize: 10,
maxSelectNum: 1,
minTime: 3,
maxTime: 15,
transmissionType: 0, // 0-图片地址, 1-base64
},
/**
* 映射菜单索引到上传类型
*/
typeMapping: {
0: 'chooseImageUpload', // 相册
1: 'takingPicturesUpload', // 相机
2: 'takingVideoUpload', // 视频
3: 'chooseFileUpload' // 文件
},
/**
* 核心方法:调用 SDK 的 chooseFileUpload
* @param {Object} options - 配置项
* @returns {Promise}
*/
_executeUpload: function(options) {
return new Promise((resolve, reject) => {
// 合并配置
const config = {
...this.defaults,
...options
};
// 构造 SDK 需要的参数结构
const pam = {
url: config.baseUrl + config.uploadPath,
fileKey: config.fileKey,
params: config.params || {},
header: config.header || {},
chooseType: config.chooseType || 'chooseFileUpload',
transmissionType: config.transmissionType,
maxSize: config.maxSize,
maxSelectNum: config.maxSelectNum,
minTime: config.minTime,
maxTime: config.maxTime
};
// 针对特定类型的特殊处理 (参考原代码逻辑)
if (config.chooseType === 'takingPicturesUpload') {
// 原代码注释中提到: // pam1.fileKey = 'picfile';
// 如果需要特殊 fileKey 可以在这里处理
}
console.log('开始调用SDK上传, 参数:', pam);
if (typeof lightAppJssdk === 'undefined') {
const msg = 'lightAppJssdk 未定义,请在 爱山东 环境中运行';
alert(msg);
return reject(msg);
}
lightAppJssdk.uploadFile.chooseFileUpload({
arg0: pam,
success: function(data) {
// 支持传入 success 回调,也支持 Promise resolve
if (options.success) options.success(data);
console.log(data)
resolve(data);
},
fail: function(err) {
// 支持传入 fail 回调,也支持 Promise reject
if (options.fail) options.fail(err);
// 默认弹窗提示错误,可以通过 silent: true 关闭
if (!options.silent) alert(typeof err === 'object' ? JSON.stringify(
err) : err);
reject(err);
}
});
});
},
/**
* 场景1: 弹出菜单选择,然后上传 (封装了原 selectFile)
* @param {Object} options - 配置项 (需包含 baseUrl)
*/
showMenuAndUpload: function(options = {}) {
return new Promise((resolve, reject) => {
if (typeof lightAppJssdk === 'undefined') {
alert('lightAppJssdk 未定义');
return reject('lightAppJssdk undefined');
}
const menuList = options.menuList || ['相册', '相机', '视频', '文件'];
lightAppJssdk.notification.showMediaAlert({
arg0: menuList,
success: (data) => {
// data.index 对应 menuList 的索引
const selectedType = this.typeMapping[data.index];
if (!selectedType) {
const err = '未知的选择类型';
if (options.fail) options.fail(err);
return reject(err);
}
// 选中后,调用核心上传方法
this._executeUpload({
...options,
chooseType: selectedType
}).then(resolve).catch(reject);
},
fail: (err) => {
if (options.fail) options.fail(err);
if (!options.silent) alert(err);
reject(err);
}
});
});
},
/**
* 场景2: 直接上传 (不弹窗,直接调起特定类型的上传)
* @param {Object} options - 需包含 baseUrl 和 chooseType
*/
directUpload: function(options = {}) {
if (!options.chooseType) {
// 如果没传类型,默认当做普通文件上传
options.chooseType = 'chooseFileUpload';
}
return this._executeUpload(options);
}
};