From e54bd923cb3f3099144a908dec856d424e1b99fd Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 30 May 2022 10:39:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9B=BE=E7=89=87=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/view/api/upload.js | 33 +++++++++++++++++++++++++++------ app/admin/view/user/index.html | 4 +++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/admin/view/api/upload.js b/app/admin/view/api/upload.js index 67c1a242c..bee928d11 100644 --- a/app/admin/view/api/upload.js +++ b/app/admin/view/api/upload.js @@ -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); + }); + } }); }; @@ -257,7 +279,6 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) { * 图片压缩处理 * @param {String} url * @param {Object} option - * @constructor */ function ImageToThumb(url, option) { var defer = jQuery.Deferred(), image = new Image(); diff --git a/app/admin/view/user/index.html b/app/admin/view/user/index.html index 8482aeb9a..cb2fbadfe 100644 --- a/app/admin/view/user/index.html +++ b/app/admin/view/user/index.html @@ -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); });