1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;