captain-ui
Version:
有赞vue wap业务组件库
75 lines (68 loc) • 2.11 kB
JavaScript
import "vant/es/toast/style";
import _Toast from "vant/es/toast";
import _extends from "@babel/runtime/helpers/esm/extends";
import { stringify } from '@youzan/utils/url/queryString';
import { ajaxData as ajax } from './index';
import { IMAGE_MAX_SIZE } from '../constants';
var config = {
tokenOptions: {
kdtId: 0,
channel: 'pay_ucert_photo',
maxSize: IMAGE_MAX_SIZE
}
};
var fetchUploadToken = function fetchUploadToken(tokenUrl, tokenOptions, type) {
return ajax({
url: tokenUrl + "?" + stringify(_extends({}, tokenOptions, {
type: type
})),
xhrFields: {
withCredentials: true
}
}).then(function (_ref) {
var uploadToken = _ref.uploadToken;
return uploadToken;
});
};
var uploadSingle = function uploadSingle(file, tokenUrl, options) {
var tokenOptions = options.tokenOptions;
var mimeType = file.type;
var firstSlashIndex = mimeType.indexOf('/');
var type = mimeType.slice(0, firstSlashIndex);
return fetchUploadToken(tokenUrl, tokenOptions, type).then(function (token) {
var fd = new FormData();
fd.append('file', file, file.name);
fd.append('token', token);
return ajax({
url: '//upload.qiniup.com',
type: 'POST',
data: fd,
contentType: 'multipart/form-data; charset=UTF-8',
processData: false,
disableCsrfToken: true
}).then(function (data) {
var questionIndex = data.attachment_url.indexOf('?');
var pureUrl = data.attachment_url.slice(0, questionIndex);
return pureUrl;
});
}).catch(function (e) {
var fileName = file && file.name || '';
var msg = "\u56FE\u7247" + fileName + "\u4E0A\u4F20\u5931\u8D25";
_Toast(msg);
return '';
});
};
/**
* 上传文件
* @param {Array} files 上传文件
* @param {String} tokenUrl 素材银行获取token的请求地址
* @param {Object} options 配置
*/
export var upload = function upload(files, tokenUrl, options) {
if (options === void 0) {
options = config;
}
return Promise.all(files.map(function (file) {
return uploadSingle(file, tokenUrl, options);
}));
};