useOptions.ts 4.29 KB
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,
  }
}