1 changed files with 58 additions and 0 deletions
@ -0,0 +1,58 @@ |
|||||
|
import type { EnumType, OptionItemType } from '@/platform/types'; |
||||
|
import { axios, Environment } from '@/platform/plugin'; |
||||
|
|
||||
|
class EnumTools { |
||||
|
public static yesno(t: any = null): EnumType { |
||||
|
return { |
||||
|
list: [ |
||||
|
{ value: true, label: t('yes') }, |
||||
|
{ value: false, label: t('no') }, |
||||
|
], |
||||
|
map: { |
||||
|
true: t('yes'), |
||||
|
false: t('no'), |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public static truefalse(t: any = null): EnumType { |
||||
|
return { |
||||
|
list: [ |
||||
|
{ value: true, label: t('true') }, |
||||
|
{ value: false, label: t('false') }, |
||||
|
], |
||||
|
map: { |
||||
|
true: t('true'), |
||||
|
false: t('false'), |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public static async fetch(type: string, t: any = null): EnumType { |
||||
|
const response = await axios.get(Environment.apiContextPath('/api/enums/list/') + type); |
||||
|
if (response) { |
||||
|
const result: EnumType = { list: [], map: {} }; |
||||
|
|
||||
|
// list
|
||||
|
const list: OptionItemType[] = response.data as OptionItemType[]; |
||||
|
if (t) { |
||||
|
for (const option of list) { |
||||
|
option.label = t(option.label); |
||||
|
} |
||||
|
} |
||||
|
result.list = list; |
||||
|
|
||||
|
// map
|
||||
|
const map = {}; |
||||
|
for (const option of list) { |
||||
|
map[option.value] = t(option.label); |
||||
|
} |
||||
|
result.map = map; |
||||
|
return result; |
||||
|
} else { |
||||
|
return { list: [], map: {} }; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export { EnumTools }; |
Loading…
Reference in new issue