新增 upload 的 `text` 属性,用于自定义内部各类场景下的提示文本

pull/1299/head
贤心 2023-07-02 20:32:19 +08:00
parent f52a4fd058
commit 3cfc33f2c0
1 changed files with 32 additions and 19 deletions

View File

@ -75,7 +75,15 @@ layui.define(['lay','layer'], function(exports){
drag: true, // 是否允许拖拽上传
size: 0, // 文件限制大小,默认不限制
number: 0, // 允许同时上传的文件数,默认不限制
multiple: false // 是否允许多文件上传不支持ie8-9
multiple: false, // 是否允许多文件上传,不支持 ie8-9
text: { // 自定义提示文本
"cross-domain": "Cross-domain requests are not supported", // 跨域
"data-format-error": "Please return JSON data format", // 数据格式错误
"check-error": "", // 文件格式校验失败
"error": "", // 上传失败
"limit-number": null, // 限制 number 属性的提示 --- function
"limit-size": null // 限制 size 属性的提示 --- function
}
};
// 初始渲染
@ -181,6 +189,7 @@ layui.define(['lay','layer'], function(exports){
Class.prototype.upload = function(files, type){
var that = this;
var options = that.config;
var text = options.text || {};
var elemFile = that.elemFile[0];
// 获取文件队列
@ -236,7 +245,7 @@ layui.define(['lay','layer'], function(exports){
},
error: function(e){ // 异常回调
options.unified ? (failed += that.fileLength) : failed++;
that.msg([
that.msg(text['error'] || [
'Upload failed, please try again.',
'status: '+ (e.status || '') +' - '+ (e.statusText || 'error')
].join('<br>'));
@ -290,7 +299,7 @@ layui.define(['lay','layer'], function(exports){
try {
res = iframeBody.text();
} catch(e) {
that.msg('Cross-domain requests are not supported');
that.msg(text['cross-domain']);
clearInterval(Class.timer);
error();
}
@ -313,7 +322,7 @@ layui.define(['lay','layer'], function(exports){
res = JSON.parse(res);
} catch(e){
res = {};
return that.msg('Please return JSON data format');
return that.msg(text['data-format-error']);
}
}
}
@ -433,7 +442,7 @@ layui.define(['lay','layer'], function(exports){
// 校验失败提示
if(check){
that.msg('选择的'+ typeName +'中包含不支持的格式');
that.msg(text['check-error'] || ('选择的'+ typeName +'中包含不支持的格式'));
return elemFile.value = '';
}
@ -456,10 +465,12 @@ layui.define(['lay','layer'], function(exports){
}();
if(options.number && that.fileLength > options.number){
return that.msg(
return that.msg(typeof text['limit-number'] === 'function'
? text['limit-number'](options, that.fileLength)
: (
'同时最多只能上传: '+ options.number + ' 个文件'
+'<br>您当前已经选择了: '+ that.fileLength +' 个文件'
);
));
}
// 检验文件大小
@ -474,7 +485,9 @@ layui.define(['lay','layer'], function(exports){
limitSize = size;
}
});
if(limitSize) return that.msg('文件大小不能超过 '+ limitSize);
if(limitSize) return that.msg(typeof text['limit-size'] === 'function'
? text['limit-size'](options, limitSize)
: '文件大小不能超过 '+ limitSize);
}
send();