|
@@ -1,59 +1,59 @@
|
|
|
define(['md5'], function (SparkMD5, allowMime) {
|
|
|
allowMime = JSON.parse('{$exts|raw}');
|
|
|
- return function (element, callable, option) {
|
|
|
+ return function (element, callable, completeCallable) {
|
|
|
/*! 初始化变量 */
|
|
|
- option = {element: $(element), exts: [], mimes: [], files: {}, cache: {}, load: 0};
|
|
|
- option.count = {total: 0, uploaded: 0}, option.size = option.element.data('size') || 0;
|
|
|
- option.safe = option.element.data('safe') ? 1 : 0, option.hload = option.element.data('hide-load') ? 1 : 0;
|
|
|
- option.field = option.element.data('field') || 'file', option.input = $('[name="_field_"]'.replace('_field_', option.field));
|
|
|
- option.uptype = option.safe ? 'local' : option.element.attr('data-uptype') || '', option.multiple = option.element.data('multiple') > 0;
|
|
|
+ var opt = {element: $(element), exts: [], mimes: [], files: {}, cache: {}, load: 0};
|
|
|
+ opt.count = {total: 0, uploaded: 0}, opt.size = opt.element.data('size') || 0;
|
|
|
+ opt.safe = opt.element.data('safe') ? 1 : 0, opt.hload = opt.element.data('hide-load') ? 1 : 0;
|
|
|
+ opt.field = opt.element.data('field') || 'file', opt.input = $('input[name="' + opt.field + '"]:not([type=file])');
|
|
|
+ opt.uptype = opt.safe ? 'local' : opt.element.attr('data-uptype') || '', opt.multiple = opt.element.data('multiple') > 0;
|
|
|
/*! 文件选择筛选 */
|
|
|
- $((option.element.data('type') || '').split(',')).map(function (i, ext) {
|
|
|
- if (allowMime[ext]) option.exts.push(ext), option.mimes.push(allowMime[ext]);
|
|
|
+ $((opt.element.data('type') || '').split(',')).map(function (i, ext) {
|
|
|
+ if (allowMime[ext]) opt.exts.push(ext), opt.mimes.push(allowMime[ext]);
|
|
|
});
|
|
|
/*! 初始化上传组件 */
|
|
|
- option.uploader = layui.upload.render({
|
|
|
+ opt.uploader = layui.upload.render({
|
|
|
url: '{:url("admin/api.upload/file")}',
|
|
|
- auto: false, elem: element, accept: 'file', multiple: option.multiple,
|
|
|
- exts: option.exts.join('|'), acceptMime: option.mimes.join(','), choose: function (object) {
|
|
|
- option.element.triggerHandler('upload.choose', option.files = object.pushFile());
|
|
|
- option.uploader.config.elem.next().val(''), layui.each(option.files, function (index, file) {
|
|
|
- if (option.size > 0 && file.size > option.size) {
|
|
|
- return delete option.files[index], $.msg.tips('文件大小超出上传限制!');
|
|
|
+ auto: false, elem: element, accept: 'file', multiple: opt.multiple,
|
|
|
+ exts: opt.exts.join('|'), acceptMime: opt.mimes.join(','), choose: function (object) {
|
|
|
+ opt.element.triggerHandler('upload.choose', opt.files = object.pushFile());
|
|
|
+ opt.uploader.config.elem.next().val(''), layui.each(opt.files, function (index, file) {
|
|
|
+ if (opt.size > 0 && file.size > opt.size) {
|
|
|
+ return delete opt.files[index], $.msg.tips('文件大小超出上传限制!');
|
|
|
}
|
|
|
- option.load = option.hload || $.msg.loading('上传进度 <span data-upload-progress>0%</span>');
|
|
|
- option.count.total++, file.index = index, option.cache[index] = file, delete option.files[index];
|
|
|
+ opt.load = opt.hload || $.msg.loading('上传进度 <span data-upload-progress>0%</span>');
|
|
|
+ opt.count.total++, file.index = index, opt.cache[index] = file, delete opt.files[index];
|
|
|
md5file(file).then(function (file) {
|
|
|
- option.element.triggerHandler('upload.hash', file);
|
|
|
+ opt.element.triggerHandler('upload.hash', file);
|
|
|
jQuery.ajax("{:url('admin/api.upload/state')}", {
|
|
|
- data: {key: file.xkey, uptype: option.uptype, safe: option.safe, name: file.name}, method: 'post', success: function (ret) {
|
|
|
+ data: {key: file.xkey, uptype: opt.uptype, safe: opt.safe, name: file.name}, method: 'post', success: function (ret) {
|
|
|
if (parseInt(ret.code) === 404) {
|
|
|
file.xurl = ret.data.url;
|
|
|
- option.uploader.config.url = ret.data.server;
|
|
|
- option.uploader.config.data.key = ret.data.key;
|
|
|
- option.uploader.config.data.safe = ret.data.safe;
|
|
|
- option.uploader.config.data.uptype = ret.data.uptype;
|
|
|
+ opt.uploader.config.url = ret.data.server;
|
|
|
+ opt.uploader.config.data.key = ret.data.key;
|
|
|
+ opt.uploader.config.data.safe = ret.data.safe;
|
|
|
+ opt.uploader.config.data.uptype = ret.data.uptype;
|
|
|
if (ret.data.uptype === 'qiniu') {
|
|
|
- option.uploader.config.data.token = ret.data.token;
|
|
|
+ opt.uploader.config.data.token = ret.data.token;
|
|
|
} else if (ret.data.uptype === 'alioss') {
|
|
|
- option.uploader.config.data['policy'] = ret.data.policy;
|
|
|
- option.uploader.config.data['signature'] = ret.data.signature;
|
|
|
- option.uploader.config.data['OSSAccessKeyId'] = ret.data.OSSAccessKeyId;
|
|
|
- option.uploader.config.data['success_action_status'] = 200;
|
|
|
- option.uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name);
|
|
|
+ opt.uploader.config.data['policy'] = ret.data.policy;
|
|
|
+ opt.uploader.config.data['signature'] = ret.data.signature;
|
|
|
+ opt.uploader.config.data['OSSAccessKeyId'] = ret.data.OSSAccessKeyId;
|
|
|
+ opt.uploader.config.data['success_action_status'] = 200;
|
|
|
+ opt.uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name);
|
|
|
} else if (ret.data.uptype === 'txcos') {
|
|
|
- option.uploader.config.data['q-ak'] = ret.data['q-ak'];
|
|
|
- option.uploader.config.data['policy'] = ret.data.policy;
|
|
|
- option.uploader.config.data['q-key-time'] = ret.data['q-key-time'];
|
|
|
- option.uploader.config.data['q-signature'] = ret.data['q-signature'];
|
|
|
- option.uploader.config.data['q-sign-algorithm'] = ret.data['q-sign-algorithm'];
|
|
|
- option.uploader.config.data['success_action_status'] = 200;
|
|
|
- option.uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name);
|
|
|
+ opt.uploader.config.data['q-ak'] = ret.data['q-ak'];
|
|
|
+ opt.uploader.config.data['policy'] = ret.data.policy;
|
|
|
+ opt.uploader.config.data['q-key-time'] = ret.data['q-key-time'];
|
|
|
+ opt.uploader.config.data['q-signature'] = ret.data['q-signature'];
|
|
|
+ opt.uploader.config.data['q-sign-algorithm'] = ret.data['q-sign-algorithm'];
|
|
|
+ opt.uploader.config.data['success_action_status'] = 200;
|
|
|
+ opt.uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name);
|
|
|
}
|
|
|
object.upload(file.index, file);
|
|
|
} else if (parseInt(ret.code) === 200) {
|
|
|
file.xurl = ret.data.url;
|
|
|
- option.uploader.config.done({uploaded: true, url: file.xurl}, file.index);
|
|
|
+ opt.uploader.config.done({code: 1, url: file.xurl, info: '文件秒传成功!'}, file.index);
|
|
|
} else {
|
|
|
$.msg.tips(ret.info || ret.error.message || '文件上传出错!');
|
|
|
}
|
|
@@ -63,32 +63,34 @@ define(['md5'], function (SparkMD5, allowMime) {
|
|
|
});
|
|
|
}, progress: function (number) {
|
|
|
$('[data-upload-progress]').html(number + '%');
|
|
|
- option.element.triggerHandler('upload.progress', {number: number, event: arguments[2], file: arguments[3]});
|
|
|
- }, done: function (ret, index) {
|
|
|
- option.element.triggerHandler('upload.done', {file: option.cache[index], data: ret});
|
|
|
- if (++option.count.uploaded >= option.count.total) {
|
|
|
- option.element.triggerHandler('upload.complete', {});
|
|
|
- option.element.html(option.element.data('html'));
|
|
|
- option.hload || $.msg.close(option.load);
|
|
|
- option.uploader.reload();
|
|
|
- }
|
|
|
- if (typeof ret.code === 'number' && parseInt(ret.code) === 0) {
|
|
|
- return $.msg.tips(ret.info || '文件上传失败!');
|
|
|
- }
|
|
|
- if (typeof option.cache[index].xurl !== 'string') {
|
|
|
- return $.msg.tips('无效的文件对象!');
|
|
|
- }
|
|
|
- if (typeof ret.uploaded === 'undefined' && typeof option.cache[index].xurl === 'string') {
|
|
|
- ret = {uploaded: true, url: option.cache[index].xurl};
|
|
|
+ opt.element.triggerHandler('upload.progress', {
|
|
|
+ number: number, event: arguments[2], file: arguments[3]
|
|
|
+ });
|
|
|
+ }, done: function (ret, idx) {
|
|
|
+
|
|
|
+ /*! 检查单个文件上传返回的结果 */
|
|
|
+ if (ret.code < 1) return $.msg.tips(ret.info || '文件上传失败!');
|
|
|
+ if (typeof opt.cache[idx].xurl !== 'string') return $.msg.tips('无效的文件上传对象!');
|
|
|
+
|
|
|
+ /*! 单个文件上传成功结果处理 */
|
|
|
+ if (typeof callable === 'function') {
|
|
|
+ callable.call(opt.element, opt.cache[idx].xurl, opt.cache['id']);
|
|
|
+ } else {
|
|
|
+ opt.multiple < 1 && opt.input.val(opt.cache[idx].xurl).trigger('change');
|
|
|
}
|
|
|
- if (ret.uploaded) {
|
|
|
- if (typeof callable === 'function') {
|
|
|
- callable.call(option.element, ret.url, option.cache[index]);
|
|
|
- } else {
|
|
|
- option.input.val(ret.url).trigger('change');
|
|
|
+
|
|
|
+ (opt.hload || $.msg.close(opt.load)), opt.element.html(opt.element.data('html'));
|
|
|
+ opt.element.triggerHandler('upload.done', {file: opt.cache[idx], data: ret});
|
|
|
+
|
|
|
+ /*! 所有文件上传完成后结果处理 */
|
|
|
+ if (++opt.count.uploaded >= opt.count.total) {
|
|
|
+ if (opt.multiple > 0) {
|
|
|
+ var urls = [];
|
|
|
+ for (var i in opt.cache) urls.push(opt.cache[i].xurl)
|
|
|
+ opt.input.val(urls.join('|')).trigger('change');
|
|
|
}
|
|
|
- } else {
|
|
|
- $.msg.tips(ret.info || ret.error.message || '文件上传出错!');
|
|
|
+ opt.element.triggerHandler('upload.complete', {file: opt.cache});
|
|
|
+ (opt.cache = [], opt.files = []), opt.uploader.reload();
|
|
|
}
|
|
|
}
|
|
|
});
|