huang
2025-11-26 792236ef78c2cdd3a989fb40a7f2e2487c4e17b6
mes-web/src/views/device/DeviceEditDialog.vue
@@ -40,11 +40,13 @@
            </el-form-item>
            <el-form-item label="设备类型" prop="deviceType">
              <el-select v-model="deviceForm.deviceType" placeholder="选择设备类型" style="width: 100%;">
                <el-option label="大车设备" value="大车设备" />
                <el-option label="大理片笼" value="大理片笼" />
                <el-option label="卧转立扫码" value="卧转立扫码" />
                <el-option label="卧转立" value="卧转立" />
              <el-select v-model="deviceForm.deviceType" placeholder="选择设备类型" style="width: 100%;" :loading="deviceTypesLoading">
                <el-option
                  v-for="type in deviceTypes"
                  :key="type"
                  :label="type"
                  :value="type"
                />
              </el-select>
            </el-form-item>
@@ -347,6 +349,10 @@
const testing = ref(false)
const testResult = ref(null)
// 设备类型列表
const deviceTypes = ref([])
const deviceTypesLoading = ref(false)
// 设备逻辑参数(根据设备类型动态显示)
const deviceLogicParams = reactive({})
@@ -447,6 +453,8 @@
watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal
  if (newVal) {
    // 加载设备类型列表
    loadDeviceTypes()
    if (isEdit.value && props.deviceData) {
      loadDeviceData(props.deviceData)
    } else {
@@ -490,9 +498,9 @@
  }
  if (value !== 'S7 Communication' && S7_PLC_TYPES.includes(deviceForm.plcType)) {
    ElMessage.warning('S7系列PLC通常使用S7 Communication协议,请确认协议选择是否正确')
      ElMessage.warning('S7系列PLC通常使用S7 Communication协议,请确认协议选择是否正确')
    return
  }
    }
  if (value !== 'Modbus TCP' && MODBUS_PLC_TYPES.includes(deviceForm.plcType)) {
    ElMessage.warning('Modbus 类型PLC通常使用 Modbus TCP 协议,请确认协议选择是否正确')
@@ -500,6 +508,38 @@
}
// 方法定义
// 加载设备类型列表
const loadDeviceTypes = async () => {
  if (deviceTypes.value.length > 0) {
    // 如果已经加载过,不再重复加载
    return
  }
  // 所有支持的设备类型(确保包含所有有配置组件的类型)
  const supportedTypes = ['大车设备', '大理片笼', '卧转立扫码', '卧转立']
  try {
    deviceTypesLoading.value = true
    const res = await deviceConfigApi.getDeviceTypes()
    if (res?.data && Array.isArray(res.data)) {
      // 合并数据库中的类型和支持的类型,去重并排序
      const dbTypes = res.data
      const allTypes = [...new Set([...supportedTypes, ...dbTypes])].sort()
      deviceTypes.value = allTypes
    } else {
      // 如果API返回失败,使用默认类型
      deviceTypes.value = supportedTypes
      console.warn('获取设备类型列表失败,使用默认类型')
    }
  } catch (error) {
    console.error('加载设备类型列表失败:', error)
    // 失败时使用默认类型
    deviceTypes.value = supportedTypes
    ElMessage.warning('加载设备类型列表失败,使用默认类型')
  } finally {
    deviceTypesLoading.value = false
  }
}
const parseJsonSafe = (str, defaultValue = null) => {
  if (!str) return defaultValue
  try {