config.ts 7.7 KB
import { findDictItemByCode } from '/@/api/system/dict';
import { FormSchema, useComponentRegister } from '/@/components/Form';
import { DictEnum } from '/@/enums/dictEnum';
import RegisterAddressInput from './RegisterAddressInput.vue';
import { createPickerSearch } from '/@/utils/pickerSearch';
import { ControlGroup } from '../ControlGroup';

export enum FormFieldsEnum {
  ADDRESS = 'address',
  FUNCTION_CODE = 'functionCode',
  START_REGISTER_ADDRESS = 'startRegisterAddress',
  DATA_VALID = 'dataValid',
  // 线圈个数
  COIL_NUMBER = 'coilNumber',
  // 寄存器个数
  REGISTER_NUMBER = 'registerNumber',
  // 线圈值
  COIL_VALUE = 'coilValue',
  // 寄存器值
  REGISTER_VALUE = 'registerValue',
  // 线圈组值
  COIL_VALUES = 'coilValues',
  // 寄存器组值
  REGISTER_VALUES = 'registerValues',
}

useComponentRegister('RegisterAddressInput', RegisterAddressInput);
useComponentRegister('ControlGroup', ControlGroup);

enum FunctionCodeEnum {
  // 读取线圈状态01
  READ_COIL_STATE_01 = '01',
  // 读取输入状态02
  READ_INPUT_STATE_02 = '02',
  // 读取保持寄存器
  READ_KEEP_REGISTER_03 = '03',
  // 读取输入寄存器
  READ_INPUT_REGISTER_04 = '04',
  // 写入耽搁线圈寄存器
  WRITE_SINGLE_COIL_REGISTER_05 = '05',
  // 写入单个保持寄存器
  WRITE_SINGLE_KEEP_COIL_REGISTER_06 = '06',
  // 写入多个线圈状态
  WRITE_MULTIPLE_COIL_STATE_15 = '15',
  // 写入多个保持寄存器
  WRITE_MULTIPLE_KEEP_REGISTER_16 = '16',
}

const showCoilNumber = (value: FunctionCodeEnum) =>
  [
    FunctionCodeEnum.READ_COIL_STATE_01,
    FunctionCodeEnum.READ_INPUT_STATE_02,
    FunctionCodeEnum.WRITE_MULTIPLE_COIL_STATE_15,
  ].includes(value);

const showRegisterNumber = (value: FunctionCodeEnum) =>
  [
    FunctionCodeEnum.READ_KEEP_REGISTER_03,
    FunctionCodeEnum.READ_INPUT_REGISTER_04,
    FunctionCodeEnum.WRITE_MULTIPLE_KEEP_REGISTER_16,
  ].includes(value);

const showCoilValue = (value: FunctionCodeEnum) =>
  [FunctionCodeEnum.WRITE_SINGLE_COIL_REGISTER_05].includes(value);

const showRegisterValue = (value: FunctionCodeEnum) =>
  [FunctionCodeEnum.WRITE_SINGLE_KEEP_COIL_REGISTER_06].includes(value);

const isWriteCoilGroup = (value: FunctionCodeEnum) =>
  [FunctionCodeEnum.WRITE_MULTIPLE_COIL_STATE_15].includes(value);

const isWriteRegisterGroup = (value: FunctionCodeEnum) =>
  [FunctionCodeEnum.WRITE_MULTIPLE_KEEP_REGISTER_16].includes(value);

export const formSchemas: FormSchema[] = [
  {
    field: FormFieldsEnum.ADDRESS,
    component: 'ApiSelect',
    label: '从机地址',
    rules: [{ required: true, message: '请选择从机地址' }],
    defaultValue: '01',
    componentProps: () => {
      return {
        api: async (params: Recordable) => {
          try {
            const result = await findDictItemByCode(params);
            return result.map((item, index) => ({
              ...item,
              itemText: `${index + 1} - ${item.itemText}`,
            }));
          } catch (error) {
            return [];
          }
        },
        params: {
          dictCode: DictEnum.SLAVE_ADDRESS,
        },
        labelField: 'itemText',
        valueField: 'itemValue',
        ...createPickerSearch(),
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: FormFieldsEnum.FUNCTION_CODE,
    component: 'ApiSelect',
    label: '功能码',
    defaultValue: '01',
    rules: [{ required: true, message: '请选择功能码' }],
    componentProps: () => {
      return {
        api: findDictItemByCode,
        params: {
          dictCode: DictEnum.FUNCTION_CODE,
        },
        labelField: 'itemText',
        valueField: 'itemValue',
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: FormFieldsEnum.START_REGISTER_ADDRESS,
    label: '起始寄存器地址',
    component: 'RegisterAddressInput',
    valueField: 'value',
    changeEvent: 'update:value',
    defaultValue: '0',
  },
  {
    field: FormFieldsEnum.REGISTER_NUMBER,
    label: '寄存器个数',
    component: 'InputNumber',
    ifShow: ({ model }) => showRegisterNumber(model[FormFieldsEnum.FUNCTION_CODE]),
    defaultValue: 1,
    rules: [{ required: true, message: '请输入寄存器个数' }],
    componentProps: {
      min: 1,
      max: 64,
      step: 1,
      placeholder: '请输入寄存器个数',
    },
  },
  {
    field: FormFieldsEnum.COIL_NUMBER,
    label: '线圈个数',
    component: 'InputNumber',
    ifShow: ({ model }) => showCoilNumber(model[FormFieldsEnum.FUNCTION_CODE]),
    defaultValue: 1,
    rules: [{ required: true, message: '请输入线圈个数' }],
    componentProps: {
      min: 1,
      max: 64,
      step: 1,
      placeholder: '请输入线圈个数',
    },
  },
  {
    field: FormFieldsEnum.COIL_VALUE,
    label: '线圈值',
    component: 'RegisterAddressInput',
    valueField: 'value',
    changeEvent: 'update:value',
    ifShow: ({ model }) => showCoilValue(model[FormFieldsEnum.FUNCTION_CODE]),
    defaultValue: '0',
    rules: [{ required: true, message: '请输入线圈值' }],
    componentProps: {
      placeholder: '请输入线圈值',
    },
  },
  {
    field: FormFieldsEnum.REGISTER_VALUE,
    label: '寄存器值',
    component: 'RegisterAddressInput',
    valueField: 'value',
    changeEvent: 'update:value',
    ifShow: ({ model }) => showRegisterValue(model[FormFieldsEnum.FUNCTION_CODE]),
    defaultValue: '0',
    rules: [{ required: true, message: '请输入寄存器值' }],
    componentProps: {
      placeholder: '请输入寄存器值',
    },
  },
  {
    field: FormFieldsEnum.REGISTER_VALUES,
    label: '',
    component: 'ControlGroup',
    colProps: { span: 24 },
    valueField: 'value',
    changeEvent: 'update:value',
    ifShow: ({ model }) => isWriteRegisterGroup(model[FormFieldsEnum.FUNCTION_CODE]),
    componentProps: ({ formModel }) => {
      const length = formModel[FormFieldsEnum.REGISTER_NUMBER];
      return {
        length: length || 1,
        itemColProps: { span: 12, style: { paddingRight: '10px' } },
        component: 'RegisterAddressInput',
        itemLabel: (index: number) => `#${index} 寄存器值`,
        showTotalControl: false,
        itemProps: () => {
          return {
            defaultValue: '0',
          } as FormSchema;
        },
      };
    },
  },
  {
    field: FormFieldsEnum.COIL_VALUES,
    label: '',
    component: 'ControlGroup',
    colProps: { span: 24 },
    valueField: 'value',
    changeEvent: 'update:value',
    ifShow: ({ model }) => isWriteCoilGroup(model[FormFieldsEnum.FUNCTION_CODE]),
    componentProps: ({ formModel }) => {
      const length = formModel[FormFieldsEnum.COIL_NUMBER];
      return {
        length: length || 1,
        itemColProps: { span: 6 },
        itemLabel: (index: number) => `#${index} 线圈状态值`,
        itemProps: (index: number) => {
          return {
            defaultValue: 0,
            helpMessage: [`设置从起始地址偏移 ${index} 位的线圈开关量状态。`],
          } as FormSchema;
        },
        totalControlProps: {
          label: '全开或全关',
          helpMessage: ['将以下所有线圈的开关量状态全部置为 1 或 0,实现一键全开或全关.'],
          colProps: { span: 24 },
        } as FormSchema,
      };
    },
  },
  {
    field: FormFieldsEnum.DATA_VALID,
    label: '数据校验',
    component: 'ApiSelect',
    colProps: { span: 24 },
    rules: [{ required: true, message: '请选择数据校验方式' }],
    componentProps: () => {
      return {
        api: findDictItemByCode,
        params: {
          dictCode: DictEnum.DATA_VALIDATE,
        },
        labelField: 'itemText',
        valueField: 'itemValue',
        placeholder: '请选择数据校验方式',
        getPopupContainer: () => document.body,
      };
    },
  },
];