优化结构
This commit is contained in:
@@ -116,6 +116,7 @@
|
||||
class="field-control"
|
||||
:min="numberConstraint(field.key).min"
|
||||
:max="numberConstraint(field.key).max"
|
||||
:precision="numberConstraint(field.key).integer ? 0 : undefined"
|
||||
:step="1"
|
||||
controls-position="right"
|
||||
@update:model-value="clearFieldError(selected, field.key)"
|
||||
@@ -231,10 +232,21 @@ const TARGETS = {
|
||||
}
|
||||
|
||||
const TARGET_ORDER = Object.keys(TARGETS)
|
||||
const RECONNECT_INTERVAL_KEY = 'reconnect_interval'
|
||||
const RECONNECT_DEFAULT_SECONDS = 5
|
||||
const MQ_RECONNECT_TARGETS = new Set(['mq/emqx', 'mq/rabbitmq'])
|
||||
const RECONNECT_INTERVAL_FIELD = {
|
||||
key: RECONNECT_INTERVAL_KEY,
|
||||
label: '重连间隔(秒)',
|
||||
type: 'number',
|
||||
required: true,
|
||||
description: '连接中断后再次尝试连接的等待时间,最小 1 秒。'
|
||||
}
|
||||
const NUMBER_CONSTRAINTS = {
|
||||
port: { min: 1, max: 65535 },
|
||||
keep_alive: { min: 1 },
|
||||
connect_timeout: { min: 1 },
|
||||
reconnect_interval: { min: 1, integer: true },
|
||||
prefetch_count: { min: 0 },
|
||||
heartbeat: { min: 0 },
|
||||
max_message_size: { min: 0 },
|
||||
@@ -264,13 +276,56 @@ const errorKey = (item, fieldKey) => `${integrationKey(item)}:${fieldKey}`
|
||||
const listKey = (item, fieldKey) => `${integrationKey(item)}:${fieldKey}`
|
||||
const cloneConfig = (value) => JSON.parse(JSON.stringify(value || {}))
|
||||
|
||||
const communicationFields = (item) => {
|
||||
const fields = Array.isArray(item.fields)
|
||||
? item.fields.map((field) =>
|
||||
field.key === RECONNECT_INTERVAL_KEY
|
||||
? {
|
||||
...field,
|
||||
label: field.label || RECONNECT_INTERVAL_FIELD.label,
|
||||
type: 'number',
|
||||
required: true,
|
||||
description: field.description || RECONNECT_INTERVAL_FIELD.description
|
||||
}
|
||||
: { ...field }
|
||||
)
|
||||
: []
|
||||
if (
|
||||
!MQ_RECONNECT_TARGETS.has(integrationKey(item)) ||
|
||||
fields.some((field) => field.key === RECONNECT_INTERVAL_KEY)
|
||||
) {
|
||||
return fields
|
||||
}
|
||||
|
||||
const connectTimeoutIndex = fields.findIndex(
|
||||
(field) => field.key === 'connect_timeout'
|
||||
)
|
||||
fields.splice(
|
||||
connectTimeoutIndex < 0 ? fields.length : connectTimeoutIndex + 1,
|
||||
0,
|
||||
{ ...RECONNECT_INTERVAL_FIELD }
|
||||
)
|
||||
return fields
|
||||
}
|
||||
|
||||
const normalizeIntegration = (item) => {
|
||||
const config = cloneConfig(item.config)
|
||||
const fields = communicationFields(item)
|
||||
if (
|
||||
MQ_RECONNECT_TARGETS.has(integrationKey(item)) &&
|
||||
(config[RECONNECT_INTERVAL_KEY] === null ||
|
||||
typeof config[RECONNECT_INTERVAL_KEY] === 'undefined' ||
|
||||
config[RECONNECT_INTERVAL_KEY] === '')
|
||||
) {
|
||||
config[RECONNECT_INTERVAL_KEY] = RECONNECT_DEFAULT_SECONDS
|
||||
}
|
||||
|
||||
const normalized = {
|
||||
...item,
|
||||
enabled: Boolean(item.enabled),
|
||||
configured: Boolean(item.configured),
|
||||
config: cloneConfig(item.config),
|
||||
fields: Array.isArray(item.fields) ? item.fields : []
|
||||
config,
|
||||
fields
|
||||
}
|
||||
normalized._savedEnabled = normalized.enabled
|
||||
normalized._savedConfig = cloneConfig(normalized.config)
|
||||
@@ -359,6 +414,8 @@ const validate = (item, enabled = item.enabled) => {
|
||||
const constraint = numberConstraint(field.key)
|
||||
if (!Number.isFinite(number)) {
|
||||
message = `${field.label}必须是数字`
|
||||
} else if (constraint.integer && !Number.isInteger(number)) {
|
||||
message = `${field.label}必须是整数`
|
||||
} else if (constraint.min !== undefined && number < constraint.min) {
|
||||
message = `${field.label}不能小于 ${constraint.min}`
|
||||
} else if (constraint.max !== undefined && number > constraint.max) {
|
||||
|
||||
Reference in New Issue
Block a user