layui.define('layer' , function(exports){
"use strict";
var $ = layui.jquery;
var layer = layui.layer;
var device = layui.device();
var elemDragEnter = 'layui-upload-enter';
var elemIframe = 'layui-upload-iframe';
var msgConf = {
icon: 2
,shift: 6
}, fileType = {
file: '文件'
,video: '视频'
,audio: '音频'
};
var Upload = function(options){
this.options = options;
};
Upload.prototype.init = function(){
var that = this, options = that.options;
var body = $('body'), elem = $(options.elem || '.layui-upload-file');
var iframe = $('<iframe id="'+ elemIframe +'" class="'+ elemIframe +'" name="'+ elemIframe +'"></iframe>');
$('#'+elemIframe)[0] || body.append(iframe);
return elem.each(function(index, item){
item = $(item);
var form = '<form target="'+ elemIframe +'" method="'+ (options.method||'post') +'" key="set-mine" enctype="multipart/form-data" action="'+ (options.url||'') +'"></form>';
var type = item.attr('lay-type') || options.type;
if(!options.unwrap){
form = '<div class="layui-box layui-upload-button">' + form + '<span class="layui-upload-icon"><i class="layui-icon"></i>'+ (
item.attr('lay-title') || options.title|| ('上传'+ (fileType[type]||'图片') )
) +'</span></div>';
}
form = $(form);
if(!options.unwrap){
form.on('dragover', function(e){
e.preventDefault();
$(this).addClass(elemDragEnter);
}).on('dragleave', function(){
$(this).removeClass(elemDragEnter);
}).on('drop', function(){
$(this).removeClass(elemDragEnter);
});
}
if(item.parent('form').attr('target') === elemIframe){
if(options.unwrap){
item.unwrap();
} else {
item.parent().next().remove();
item.unwrap().unwrap();
}
};
item.wrap(form);
item.off('change').on('change', function(){
console.log("上传文件大小:"+item[0].files[0].size/(1024*1024)+"MB")
var size=item[0].files[0].size/(1024*1024)
that.action(this,type,size);
});
});
};
Upload.prototype.action = function(input,type,size){
var that = this, options = that.options, val = input.value;
var item = $(input), ext = item.attr('lay-ext') || options.ext || '';
if(!val){
return;
};
switch(type){
case 'file':
if(ext && !RegExp('\\w\\.('+ ext +')$', 'i').test(escape(val))){
layer.msg('不支持该文件格式', msgConf);
return input.value = '';
}
if(size>200){
layer.msg('文件大小不能超过200MB');
return input.value = '';
}
break;
case 'video':
if(!RegExp('\\w\\.('+ (ext||'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(val))){
layer.msg('不支持该视频格式', msgConf);
return input.value = '';
}
if(size>200){
layer.msg('视频大小不能超过200MB');
return input.value = '';
}
break;
case 'audio':
if(!RegExp('\\w\\.('+ (ext||'mp3|wav|mid') +')$', 'i').test(escape(val))){
layer.msg('不支持该音频格式', msgConf);
return input.value = '';
}
if(size>100){
layer.msg('音频大小不能超过100MB');
return input.value = '';
}
break;
default:
if(!RegExp('\\w\\.('+ (ext||'jpg|png|gif|bmp|jpeg') +')$', 'i').test(escape(val))){
layer.msg('不支持该图片格式', msgConf);
return input.value = '';
}
if(size>2){
layer.msg('图片大小不能超过2MB');
return input.value = '';
}
break;
}
options.before && options.before(input);
item.parent().submit();
var iframe = $('#'+elemIframe), timer = setInterval(function() {
var res;
try {
res = iframe.contents().find('body').text();
} catch(e) {
layer.msg('上传接口存在跨域', msgConf);
clearInterval(timer);
}
if(res){
clearInterval(timer);
iframe.contents().find('body').html('');
try {
res = JSON.parse(res);
} catch(e){
res = {};
return layer.msg('请对上传接口返回JSON字符', msgConf);
}
typeof options.success === 'function' && options.success(res, input);
}
}, 30);
input.value = '';
};
exports('upload', function(options){
var upload = new Upload(options = options || {});
upload.init();
});
});