feat(上传): 支持多图 data-limit 数量限制

背景与目的:多图上传需要按最终图片总数限制选择数量,参数名对齐 issue 中的 data-limit 约定。

主要改动:新增 data-limit;已有图片与在途上传共同占用名额,超限时只接收剩余文件。每次选择批次使用独立适配器状态,避免并发上传互相覆盖,并同步 README 用法。

影响范围与兼容性:影响后台多图上传控件;未设置或设为 0 时仍不限量,现有上传事件与单图逻辑保持兼容。

验证入口及实际结果:本地 node --check 和并发批次定向行为检查通过;尚未进行真实浏览器多图上传回归。
This commit is contained in:
邹景立 2026-11-27 13:03:04 +08:00
parent 5a2999c7ca
commit d888b4f0e6
3 changed files with 39 additions and 9 deletions

View File

@ -50,6 +50,7 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
option.safe = readOptionBool(resolveValue($elem, $input, 'safe', 0)) ? 1 : 0;
option.hide = readOptionBool(resolveValue($elem, $input, 'hload', 0)) ? 1 : 0;
option.mult = parseInt(resolveValue($elem, $input, 'multiple', 0)) > 0;
option.limit = Math.max(0, parseInt(resolveValue($elem, $input, 'limit', 0)) || 0);
option.path = normalizePath(resolveValue($elem, $input, 'path', ''));
option.type = option.safe ? 'local' : String(resolveValue($elem, $input, 'uptype', '') || '');
option.quality = parseFloat(resolveValue($elem, $input, 'quality', '1.0') || '1.0');
@ -77,9 +78,18 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
this.adapter = new Adapter(this.option, layui.upload.render({
url: '{:url("admin/api.upload/file",[],false,true)}', auto: false, elem: elem, accept: 'file', multiple: this.option.mult, exts: this.option.exts.join('|'), acceptMime: this.option.mimes.join(','), choose: function (obj) {
that.option = resolveOption(that.option.elem, that.option);
that.adapter.config(that.option);
that.adapter = new Adapter(that.option, that.adapter.uploader);
obj.items = [], obj.files = obj.pushFile();
let selected = Object.keys(obj.files).length;
let limitInput = that.option.elem.data('limit-input');
let current = limitInput ? String(limitInput.value || '').split('|').filter(Boolean).length : 0;
let pending = parseInt(that.option.elem.data('limit-pending')) || 0;
let remaining = that.option.limit > 0 ? Math.max(0, that.option.limit - current - pending) : Infinity;
layui.each(obj.files, function (idx, file) {
if (obj.items.length >= remaining) {
delete obj.files[idx];
return;
}
obj.items.push(file);
file.path = that.option.path;
file.quality = that.option.quality;
@ -88,8 +98,16 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
file.cutWidth = that.option.cutWidth;
file.cutHeight = that.option.cutHeight;
});
if (selected > obj.items.length) {
$.msg.notify('温馨提示', '最多可上传 ' + that.option.limit + ' 张图片!', 3000, {type: 'error', width: 280});
}
that.adapter.event('upload.choose', obj.items);
that.adapter.upload(obj.items, done);
if (obj.items.length > 0) {
if (that.option.limit > 0) {
that.option.elem.data('limit-pending', pending + obj.items.length);
}
that.adapter.upload(obj.items, done);
}
layui.each(obj.files, function (idx) {
delete obj.files[idx];
});
@ -289,6 +307,10 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
this.count.success++, file.xstate = 1, file.xstats = message;
if (file.notify) file.notify.setSuccess(message || file.xstats || '')
}
if (name === 'upload.done' || name === 'upload.error') {
let pending = parseInt(this.option.elem.data('limit-pending')) || 0;
this.option.elem.data('limit-pending', Math.max(0, pending - 1));
}
if (this.option.elem) {
this.option.elem.triggerHandler(name, data);
if (this.option.input) this.option.input.triggerHandler(name, data);
@ -373,4 +395,4 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
}, timeout || 2000), this;
};
}
});
});

View File

@ -616,13 +616,15 @@ $(function () {
if (this.dataset.inited) return; else this.dataset.inited = 'true';
let $bt = $('<div class="uploadimage"><span><a data-file="mul" class="layui-icon layui-icon-upload-drag"></a></span><span data-file="images"></span></div>');
let ims = this.value ? this.value.split('|') : [], $in = $(this).after($bt);
copyUploadAttrs($in, $bt.find('[data-file]'), {
let $file = $bt.find('[data-file]').data('limit-input', this);
copyUploadAttrs($in, $file, {
'path': '', 'size': 0, 'type': 'gif,png,jpg,jpeg', 'max-width': 0, 'max-height': 0,
'cut-width': 0, 'cut-height': 0, 'uptype': '', 'safe': 0, 'hload': 0, 'quality': '1.0',
'cut-width': 0, 'cut-height': 0, 'uptype': '', 'safe': 0, 'hload': 0, 'quality': '1.0', 'limit': 0,
}).on('push', function (evt, src) {
ims.push(src), $in.val(ims.join('|')).trigger('change'), showImageContainer([src]);
ims.push(src), $in.val(ims.join('|')).trigger('change'), showImageContainer([src]), updateUploadButton();
}) && (ims.length > 0 && showImageContainer(ims));
initUploadFiles($bt, true);
updateUploadButton();
function showImageContainer(srcs) {
$(srcs).each(function (idx, src, $img) {
@ -634,10 +636,15 @@ $(function () {
ims = [], $bt.prevAll('.uploadimage').map(function () {
ims.push($(this).attr('data-tips-image'));
});
ims.reverse(), $in.val(ims.join('|')).trigger('change');
ims.reverse(), $in.val(ims.join('|')).trigger('change'), updateUploadButton();
}), $bt.before($img);
});
}
function updateUploadButton() {
let limit = parseInt(readUploadAttr($in, 'limit', 0)) || 0;
$file.toggleClass('layui-hide', limit > 0 && ims.filter(Boolean).length >= limit);
}
});
};
@ -1120,4 +1127,4 @@ $(function () {
/*! 初始化系统菜单及表单验证 */
$.menu.listen() && $.form.reInit($body);
});
});

View File

@ -88,6 +88,7 @@ ThinkAdmin 做的是通用后台基础。客户怎么分配、订单如何流转
- **相同文件可以少传一次。** 上传流程支持计算文件哈希,检查对应存储文件是否已存在;命中后直接复用结果。
- **图片按用途处理。** 单图、多图、图片选择都有对应入口,压缩质量、最大宽高和裁切尺寸按页面需要设置。
- **多图数量可控。** 在隐藏输入字段上设置 `data-limit` 限制最终图片总数,已有图片与上传中的文件都计入上限;未设置或设为 `0` 时不限量,例如 `['data-limit' => 3]` 最多保留 3 张图片。
- **上传记录集中管理。** 后台可以查找文件记录,并在授权范围内编辑、删除或清理重复记录。
- **公开文件和安全文件分开存放。** 例如支付证书可以使用安全模式,保存到本地 `safefile/`,读取权限由业务接口另行控制。
@ -445,4 +446,4 @@ npm run build
本项目基于 [MIT License](license) 开源,可按许可证条款使用、修改和分发。使用或分发时应保留相应版权声明和许可文本;第三方依赖遵循各自的许可证。
版权信息以许可证文件中的声明为准。官方网站:[thinkadmin.top](https://thinkadmin.top);备案信息:[粤ICP备16006642号](https://beian.miit.gov.cn)。
版权信息以许可证文件中的声明为准。官方网站:[thinkadmin.top](https://thinkadmin.top);备案信息:[粤ICP备16006642号](https://beian.miit.gov.cn)。