import enumService from "@/constants/enum/enum.service"; /** * @description: 通用字典转换 * @author ChenRui * @date 2020/7/29 18:43 */ function encode(code: string, enumName: any, attribute?: string | { key: string; value: string }, defaultVal?: any) { if (enumName) { if (typeof enumName === "string") { let switchEnumFiled; if (code.indexOf(",") != -1) { const codeList = code.split(","); const filedList: string[] = []; codeList.forEach((item) => { filedList.push(enumService.getNameByCode(enumName, item)); }); if (filedList.length > 0) { const enumList: any[] = []; filedList.forEach((item: any) => { enumList.push(item[(attribute as string) || "name"] || defaultVal); }); switchEnumFiled = enumList.join(","); } return switchEnumFiled; } else { switchEnumFiled = enumService.getNameByCode(enumName, code); if (switchEnumFiled != null) { return switchEnumFiled[(attribute as string) || "name"] || defaultVal; } } } else if (Array.isArray(enumName)) { for (const item of enumName) { if (code === item[(attribute as any).key]) { return item[(attribute as any).value]; } } } } return defaultVal || ""; } const switchEnumConvert = (code: string, enumName: any, attribute?: string | { key: string; value: string }, defaultVal?: any) => { return code != null ? encode(code, enumName, attribute, defaultVal) : ""; }; export default switchEnumConvert;