Compare commits

...

4 Commits

Author SHA1 Message Date
Anyon
493f85333b 升级Layui版本 2022-05-30 11:13:32 +08:00
Anyon
4ce9e1cacd Update upload.js 2022-05-30 10:53:49 +08:00
Anyon
c67d760cf4 Update upload.js 2022-05-30 10:52:21 +08:00
Anyon
e54bd923cb 增加图片压缩 2022-05-30 10:39:59 +08:00
4 changed files with 58 additions and 18 deletions

View File

@ -12,6 +12,9 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
this.option.hide = this.option.elem.data('hload') ? 1 : 0;
this.option.mult = this.option.elem.data('multiple') > 0;
this.option.type = this.option.safe ? 'local' : this.option.elem.attr('data-uptype') || '';
this.option.quality = parseFloat(this.option.elem.data('quality') || '1.0');
this.option.maxWidth = parseInt(this.option.elem.data('max-width') || '0');
this.option.maxHeight = parseInt(this.option.elem.data('max-height') || '0');
/*! 查找表单元素, 如果没有找到将不会自动写值 */
if (!this.option.elem.data('input') && this.option.elem.data('field')) {
@ -28,9 +31,15 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
this.adapter = new Adapter(this.option, layui.upload.render({
url: '{:url("admin/api.upload/file")}', auto: false, elem: elem, accept: 'file', multiple: this.option.mult, exts: this.option.exts.join('|'), acceptMime: this.option.mimes.join(','), choose: function (object) {
object.files = object.pushFile();
layui.each(object.files, function (idx, file) {
file.quality = that.option.quality;
file.maxWidth = that.option.maxWidth;
file.maxHeight = that.option.maxHeight;
});
that.adapter.event('upload.choose', object.files);
that.adapter.upload(object.files, done), layui.each(object.files, function (index) {
delete object.files[index];
that.adapter.upload(object.files, done);
layui.each(object.files, function (idx) {
delete object.files[idx];
});
}
}));
@ -64,9 +73,22 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
file.notify = new NotifyExtend(file);
}
}), layui.each(files, function (index, file) {
that.hash(file).then(function (file) {
that.event('upload.hash', file).request(file, done);
});
// 图片限宽限高压缩
if (/^image\/*$/.test(file.type) && file.maxWidth > 0 || file.maxHeight > 0 || file.quality !== 1) {
FileToBase64(file).then(function (base64) {
ImageToThumb(base64, file).then(function (base64) {
files[index] = Base64ToFile(base64, file.name);
files[index].notify = file.notify;
that.hash(files[index]).then(function (file) {
that.event('upload.hash', file).request(file, done);
});
});
});
} else {
that.hash(file).then(function (file) {
that.event('upload.hash', file).request(file, done);
});
}
});
};
@ -193,7 +215,11 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
return this;
};
/*! 计算文件 HASH 值 */
/**
* 计算文件 HASH
* @param {File} file 文件对象
* @return {Promise}
*/
Adapter.prototype.hash = function (file) {
var defer = jQuery.Deferred();
file.xext = file.name.indexOf('.') > -1 ? file.name.split('.').pop() : 'tmp';
@ -235,7 +261,12 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
return UploadAdapter;
/*! Base64 内容转 File 对象 */
/**
* Base64 File 对象
* @param {String} base64 Base64内容
* @param {String} filename 新文件名称
* @return {File}
*/
function Base64ToFile(base64, filename) {
var arr = base64.split(',');
var mime = arr[0].match(/:(.*?);/)[1], suffix = mime.split('/')[1];
@ -244,20 +275,23 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
return new File([u8arr], filename + '.' + suffix, {type: mime});
}
/*! File 对象转 Base64 内容 */
/**
* File 对象转 Base64
* @param {File} file 文件对象
* @return {Promise}
*/
function FileToBase64(file) {
var defer = jQuery.Deferred(), reader = new FileReader();
reader.onload = function () {
return (reader.onload = function () {
defer.resolve(this.result);
};
return reader.readAsDataURL(file), defer.promise();
}), reader.readAsDataURL(file), defer.promise();
}
/**
* 图片压缩处理
* @param {String} url
* @param {Object} option
* @constructor
* @param {String} url 图片链接
* @param {Object} option 压缩参数
* @return {Promise}
*/
function ImageToThumb(url, option) {
var defer = jQuery.Deferred(), image = new Image();
@ -282,7 +316,11 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
return defer.promise();
}
/*! 上传状态提示扩展插件 */
/**
* 上传状态提示扩展插件
* @param {File} file 文件对象
* @constructor
*/
function NotifyExtend(file) {
var that = this;
this.notify = Notify.notify({width: 260, title: file.name, showProgress: true, description: '上传进度 <span data-upload-progress>0%</span>', type: 'default', position: 'top-right', closeTimeout: 0});

View File

@ -73,7 +73,9 @@
$.form.load("{:url('state')}", data, 'post', function (ret) {
if (ret.code < 1) $.msg.error(ret.info, 3, function () {
$('#UserTable').trigger('reload');
}); else $('#UserTable').trigger('reload')
}); else {
$('#UserTable').trigger('reload')
}
return false;
}, false);
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long