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