useOptions.ts
4.29 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import optionsApi, { type Option, type OptionsData } from '../services/options'
// 缓存的下拉数据
const cachedOptions = ref<OptionsData | null>(null)
const isLoading = ref(false)
const currentLang = ref('')
/**
* 下拉选项管理 composable
* 统一管理所有下拉数据,支持多语言
* @author zzy
*/
export function useOptions() {
const { locale } = useI18n()
// 语言映射
const getLangParam = () => {
const lang = locale.value
if (lang === 'cn') return 'zh-cn'
if (lang === 'gb') return 'en'
return 'zh-cn'
}
// 加载所有下拉选项
const loadOptions = async (force = false) => {
const lang = getLangParam()
if (!force && cachedOptions.value && currentLang.value === lang) {
return cachedOptions.value
}
if (isLoading.value) return cachedOptions.value
try {
isLoading.value = true
const res: any = await optionsApi.getAll(lang)
const data = (res && (res.Data ?? res.data)) || res
cachedOptions.value = data
currentLang.value = lang
return data
} catch (err) {
console.error('加载下拉选项失败', err)
return null
} finally {
isLoading.value = false
}
}
// 监听语言变化,自动重新加载
watch(locale, () => {
loadOptions(true)
})
// 转换为 VaSelect 格式 { text, value }
const toSelectOptions = (options: Option[] | undefined) => {
if (!options) return []
return options.map(opt => ({ text: opt.label, value: opt.value }))
}
// 获取各类下拉选项
const getRobotTypeOptions = () => toSelectOptions(cachedOptions.value?.robotType)
const getRobotStatusOptions = () => toSelectOptions(cachedOptions.value?.robotStatus)
const getOnlineStatusOptions = () => toSelectOptions(cachedOptions.value?.onlineStatus)
const getOperatingModeOptions = () => toSelectOptions(cachedOptions.value?.operatingMode)
const getProtocolTypeOptions = () => toSelectOptions(cachedOptions.value?.protocolType)
const getMapTypeOptions = () => toSelectOptions(cachedOptions.value?.mapType)
const getMapNodeTypeOptions = () => toSelectOptions(cachedOptions.value?.mapNodeType)
const getMapResourceTypeOptions = () => toSelectOptions(cachedOptions.value?.mapResourceType)
const getActionCategoryOptions = () => toSelectOptions(cachedOptions.value?.actionCategory)
const getActionBlockTypeOptions = () => toSelectOptions(cachedOptions.value?.actionBlockType)
const getExecutionScopeOptions = () => toSelectOptions(cachedOptions.value?.executionScope)
const getParameterValueTypeOptions = () => toSelectOptions(cachedOptions.value?.parameterValueType)
const getParameterSourceTypeOptions = () => toSelectOptions(cachedOptions.value?.parameterSourceType)
const getTaskTypeOptions = () => toSelectOptions(cachedOptions.value?.taskType)
const getTaskStatusOptions = () => toSelectOptions(cachedOptions.value?.taskStatus)
const getTaskStepTypeOptions = () => toSelectOptions(cachedOptions.value?.taskStepType)
const getStepPropertyTypeOptions = () => toSelectOptions(cachedOptions.value?.stepPropertyType)
const getAfterActionTypeOptions = () => toSelectOptions(cachedOptions.value?.afterActionType)
const getNodeValueTypeOptions = () => toSelectOptions(cachedOptions.value?.nodeValueType)
// 根据值获取标签
const getLabelByValue = (options: Option[] | undefined, value: any) => {
if (!options || value === undefined || value === null) return ''
const opt = options.find(o => o.value === value || String(o.value) === String(value))
return opt?.label || String(value)
}
return {
cachedOptions,
isLoading,
loadOptions,
toSelectOptions,
getLabelByValue,
// 机器人
getRobotTypeOptions,
getRobotStatusOptions,
getOnlineStatusOptions,
getOperatingModeOptions,
getProtocolTypeOptions,
// 地图
getMapTypeOptions,
getMapNodeTypeOptions,
getMapResourceTypeOptions,
// 动作
getActionCategoryOptions,
getActionBlockTypeOptions,
getExecutionScopeOptions,
getParameterValueTypeOptions,
getParameterSourceTypeOptions,
// 任务
getTaskTypeOptions,
getTaskStatusOptions,
getTaskStepTypeOptions,
getStepPropertyTypeOptions,
getAfterActionTypeOptions,
getNodeValueTypeOptions,
}
}