zy-vue-library/hooks/useIsExistenceDuplicateSele.../index.js

25 lines
774 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { uniqBy } from "lodash-es";
import { ElMessage } from "element-plus";
/**
* @param {Array} list - 需要检查重复项的目标数组
* @param {string} key - 用于去重判断的对象属性名
* @param {string} [message="存在重复项,请勿重复选择"] - 可选的错误提示信息
* @returns {Promise<void>} 如果无重复项则 resolve存在重复项时 reject 并提示错误
*/
export default function useIsExistenceDuplicateSelection(
list,
key,
message = "存在重复项,请勿重复选择"
) {
return new Promise((resolve, reject) => {
const newList = uniqBy(list, key);
if (newList.length !== list.length) {
ElMessage.error(message);
reject(new Error(message));
} else {
resolve();
}
});
}