+function(){
class img_compress_upload {
constructor (data){
let this_default ={
max_height: 1024,
max_width: 1024,
max_num: 1,
list_box:'',
add_btn:'',
accept:'',// 可设置的文件的上传类型
del_btn_class: 'del',
submit_btn:'',
submit_form:'',
},
self = this;
this._data = $.extend(this_default, data);
this._max_height = this._data.max_height;
this._max_width = this._data.max_width;
this._add_btn = this._data.add_btn;
this._list_box = this._data.list_box;
this._del_btn_class = this._data.del_btn_class;
this._submit_btn = this._data.submit_btn;
this._submit_form = this._data.submit_form;
this._index = -1;
this._accept = this._data.accept;
this._add_event();
}
_add_event(){
let self = this;
$('body').append("");
this._add_file = $('#add_file');
this._add_file.on('change',function (){//监听图片选择
if($(this).val()){//存在图片
self._storage();
}
});
this._add_btn.on('click',function(){
self._index = -1;
self._add_file.click();
});
this._list_box.on('click', 'li:not(".add_btn")', function(event){
self._index = $(this).index();
self._add_file.click();
});
this._list_box.on('click', '.'+this._del_btn_class, function(event){
$(this).parents('li').remove();
event.stopPropagation();
});
}
_storage (){
let self = this,
reader = new FileReader(),
file=this._add_file[0].files[0];
var straccpet = '.jpg,.jpeg,.png,.gif,.webp,.tiff,.bmp ,.mp4,.wmv ,.m4v,.flv,.3gp'
var index = file.name.lastIndexOf('.')
// 获取后缀
var ext = file.name.substr(index + 1)
// 输出结果
if (straccpet.indexOf(ext) === -1) {
alert('文件上传的格式不对,请重新上传')
return false
}
// 限制文件大小上传
if ((file.size / 1024 / 1024) > 10) {
alert('文件上传大小不能大于10M!')
return false
}
// if(!/image\/\w+/.test(file.type)){
// alert("请上传图片格式文件");
// return false;
// }
reader.readAsDataURL(file);
reader.onload = function(e){
if(reader.result){
if('.mp4,.wmv ,.m4v,.flv,.3gp'.indexOf(ext) !== -1){
self._modify_img_and_show(reader.result,file.name,'vido');
} else {
self._compress(reader.result);//压缩图片
}
}
}
}
_modify_img_and_show (img_data,filename,type){
this._add_file.val('');
if(this._index > -1){//修改
let this_box = this._list_box.find('li').eq(this._index);
if(type){
this_box.html('' + filename +' x')
} else {
this_box.html(' x')
}
// this_box.find('img').attr('src', img_data);
// this_box.find('input').val(img_data);
}else{
let this_html = ''
if(!type){
this_html = '