yanzhen-design-vue
Version:
199 lines (198 loc) • 6.47 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const utils = require("@yanzhen-libs/utils");
const lodashEs = require("lodash-es");
const utils$1 = require("./utils.cjs");
function useUploadRules(fileListRef, props) {
const acceptRef = vue.computed(() => {
if (utils.isEmptyValue(props.accept)) return void 0;
const accept = lodashEs.isString(props.accept) ? props.accept.split(",") : lodashEs.isArray(props.accept) ? props.accept : [];
return accept.reduce((prev, value) => {
switch (true) {
case (lodashEs.isString(value) && value.startsWith(".")):
case (lodashEs.isString(value) && value.includes("/") && value.split("/").length >= 2):
return prev.concat(value);
case lodashEs.isString(value):
return prev.concat(`.${value}`);
}
return prev;
}, []).join(",");
});
const acceptMsg = "不支持的文件格式!";
const validateAccept = (rule, { file }) => {
var _a;
const accept = (_a = vue.unref(acceptRef)) == null ? void 0 : _a.split(",");
const { name, type } = file;
if (!utils.isEmptyArray(accept)) {
const [type1, ...types] = type.split("/");
if (accept.includes(`${type1}/*`)) {
return true;
}
for (let v of accept) {
v = v.toLowerCase();
const [t1, ...t2] = v.split("/");
switch (true) {
case (v.startsWith(".") && name.toLowerCase().endsWith(v)):
case (type1 === t1 && t2.join("/") === "*"):
case (type1 === t1 && t2.join("/") === types.join("/")):
return true;
}
}
return false;
}
return true;
};
const maxCountRef = vue.computed(() => {
if (utils.isNumeral(props.max)) {
return utils.getNumeric(props.max);
} else if (utils.isNumeral(props.maxCount)) {
return utils.getNumeric(props.maxCount);
}
return void 0;
});
const maxCountMsg = vue.computed(() => {
if (!utils.isEmptyValue(props.maxMsg)) {
return props.maxMsg;
}
const maxCount = vue.unref(maxCountRef);
if (maxCount === void 0) {
return "上传文件超过最多上次数量!";
}
return `最多上传${maxCount}个文件!`;
});
const validateMaxCount = (rule, { fileList }) => {
const maxCount = vue.unref(maxCountRef);
if (!utils.isNumeral(maxCount) || maxCount <= 0) {
return true;
}
console.log(fileList, "fileListfileList");
const length = !utils.isEmptyArray(fileList) ? fileList.length : 0;
return maxCount > length - 1;
};
const maxCountRemainderRef = vue.computed(() => {
const maxCount = vue.unref(maxCountRef);
const fileList = vue.unref(fileListRef);
if (utils.isNumeral(maxCount)) {
return maxCount - fileList.length;
}
return Number.POSITIVE_INFINITY;
});
const maxCountRemainderMsg = "超过剩余最大选项数量限制!";
const validateMaxCountRemainder = (rule, { selectFileList }) => {
if (!utils.isEmptyArray(selectFileList)) {
const maxCountRemainder = vue.unref(maxCountRemainderRef);
return selectFileList.length <= maxCountRemainder;
}
return true;
};
const maxSizeRef = vue.computed(() => {
const maxSize = props.maxSize;
const defaultMaxSize = props.defaultMaxSize ?? 200;
switch (true) {
case utils.isEmptyValue(defaultMaxSize):
case (defaultMaxSize > 1024 || defaultMaxSize <= 0):
return utils.isNumeral(maxSize) ? utils.getNumeric(maxSize) : void 0;
}
if (utils.isNumeral(maxSize)) {
switch (true) {
case maxSize > defaultMaxSize:
return utils.getNumeric(maxSize);
}
return utils.bytes.parse(`${String(maxSize)}MB`);
}
return utils.bytes.parse("10MB");
});
const maxSizeMsg = vue.computed(() => {
if (!utils.isEmptyValue(props.maxSizeMsg)) {
return props.maxSizeMsg;
}
const maxSize = vue.unref(maxSizeRef);
if (maxSize === void 0) {
return "上传文件大小超过限制!";
}
const size = utils.bytes.format(maxSize, { unit: "MB" });
return `上传文件大小不能超过${size}!`;
});
const validateMaxSize = (rule, { file }) => {
const maxSize = vue.unref(maxSizeRef);
const { size } = file;
if (!utils.isNumeral(maxSize)) return true;
return (size ?? 0) < maxSize;
};
const rulesRef = [
{
message: maxCountRemainderMsg,
validator: validateMaxCountRemainder
},
{
message: maxCountMsg,
validator: validateMaxCount
},
{
message: acceptMsg,
validator: validateAccept
},
{
message: maxSizeMsg,
validator: validateMaxSize
}
];
function handleValidate(value, rules = rulesRef) {
const errors = [];
if (utils.isEmptyArray(rules)) {
rules = vue.unref(rulesRef);
}
for (const rule of rules) {
if (!(rule == null ? void 0 : rule.validator(rule, {
...value,
fileList: !utils.isEmptyArray(value.fileList) ? value.fileList : vue.unref(fileListRef)
}))) {
let errorType = null;
switch (rule.message) {
case acceptMsg:
errorType = utils$1.imageErrorTypes.ACCEPT;
break;
case maxSizeMsg:
console.log(
"校验失败-图片过大",
value,
props.maxSizeMsg,
rule.message,
vue.unref(maxSizeRef)
);
errorType = {
type: utils$1.imageErrorTypes.MAX_SIZE,
useDocMsg: Boolean(value.isImageType && utils.isEmptyValue(props.maxSizeMsg)),
maxSize: vue.unref(maxSizeRef) ?? 0
};
break;
case maxCountMsg:
errorType = utils$1.imageErrorTypes.MAX_COUNT;
break;
case maxCountRemainderMsg:
errorType = utils$1.imageErrorTypes.MAX_COUNT_REMAINDER;
break;
default:
errorType = utils$1.imageErrorTypes.UNKNOWN;
break;
}
errors.push({
status: "error",
message: vue.unref(rule.message),
type: errorType
});
}
}
return utils.isEmptyValue(errors) ? void 0 : errors;
}
return {
acceptRef,
maxCountRef,
maxCountRemainderRef,
maxSizeRef,
rules: rulesRef,
validate: handleValidate
};
}
exports.useUploadRules = useUploadRules;