index.ts 7.47 KB
import { FormSchema } from '/@/components/Form';
import { isBasic, isPem } from '../../mqtt/enum';

/**
 * RestApi表单相关默认值配置
 */
class ApiFormPartialConfig {
  static restEndpointUrlPattern = 'http://localhost/api'; //端点URL模式
  static requestMethod = 'POST'; //请求方式
  static proxyHost = true; //代理主机
  static proxyPort = 1; //代理端口
  static maxParallelRequestsCount = 0; //最大并行请求数
  static readTimeoutMs = 0; //读取超时(毫秒)
  static maxQueueSize = 0; //Redis队列最大数
  static type = 'anonymous'; //凭据类型

  //requestMethod Select options配置
  static getRequestMethod() {
    return [
      { label: 'GET', value: 'GET' },
      { label: 'POST', value: 'POST' },
      { label: 'PUT', value: 'PUT' },
      { label: 'DELETE', value: 'DELETE' },
    ];
  }

  //凭据类型Select options配置
  static getType() {
    return [
      { label: 'Anonymous', value: 'anonymous' },
      { label: 'Basic', value: 'basic' },
      { label: 'PEM', value: 'pem' },
    ];
  }
}

export const modeApiForm: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 32,
      placeholder: '请输入名称',
    },
  },
  {
    field: 'restEndpointUrlPattern',
    label: '端点URL模式',
    colProps: { span: 12 },
    required: true,
    defaultValue: ApiFormPartialConfig.restEndpointUrlPattern,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Endpoint URL pattern',
    },
  },
  {
    field: 'requestMethod',
    component: 'Select',
    label: '请求方式',
    colProps: { span: 12 },
    defaultValue: ApiFormPartialConfig.requestMethod,
    componentProps: {
      placeholder: '请选择Request method',
      options: ApiFormPartialConfig.getRequestMethod(),
    },
  },
  {
    field: 'enableProxy',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '启用代理',
  },

  {
    field: 'proxyHost',
    label: '代理主机',
    colProps: { span: 12 },
    required: ApiFormPartialConfig.proxyHost,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: 'http或者https开头',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },
  {
    field: 'proxyPort',
    label: '代理端口',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: ApiFormPartialConfig.proxyPort,
    componentProps: {
      placeholder: '请输入代理端口',
      maxLength: 255,
      min: 1,
      max: 65535,
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },
  {
    field: 'proxyUser',
    label: '代理用户',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入代理用户',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },
  {
    field: 'proxyPassword',
    label: '代理密码',
    colProps: { span: 12 },
    required: true,
    component: 'InputPassword',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入代理密码',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },

  {
    field: 'useSystemProxyProperties',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '使用系统代理属性',
  },
  {
    field: 'maxParallelRequestsCount',
    label: '最大并行请求数',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: ApiFormPartialConfig.maxParallelRequestsCount,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !!values.useSystemProxyProperties;
    },
  },
  {
    field: 'ignoreRequestBody',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '无请求正文',
  },
  {
    field: 'readTimeoutMs',
    label: '读取超时(毫秒)',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: ApiFormPartialConfig.readTimeoutMs,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !values.useSystemProxyProperties;
    },
  },
  {
    field: 'maxParallelRequestsCount',
    label: '最大并行请求数',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: ApiFormPartialConfig.maxParallelRequestsCount,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !values.useSystemProxyProperties;
    },
  },
  {
    field: 'headers',
    label: 'Headers',
    colProps: { span: 24 },
    defaultValue: { 'Content-Type': 'application/json' },
    component: 'JAddInput',
  },

  {
    field: 'useRedisQueueForMsgPersistence',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '使用redis队列进行消息持久性',
  },
  {
    field: 'trimQueue',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '修剪redis队列',
    ifShow: ({ values }) => {
      return !!values.useRedisQueueForMsgPersistence;
    },
  },
  {
    field: 'maxQueueSize',
    label: 'Redis队列最大数',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: ApiFormPartialConfig.maxQueueSize,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !!values.useRedisQueueForMsgPersistence;
    },
  },
  {
    field: 'type',
    component: 'Select',
    label: '凭据类型',
    colProps: { span: 12 },
    defaultValue: ApiFormPartialConfig.type,
    componentProps: {
      placeholder: '请选择凭据类型',
      options: ApiFormPartialConfig.getType(),
    },
  },
  {
    field: 'uploadFiles',
    component: 'Input',
    label: '',
    slot: 'uploadFilesSlot',
    colProps: { span: 24 },
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: 'username',
    label: '用户名',
    colProps: { span: 12 },
    component: 'Input',
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入用户名',
    },
    ifShow: ({ values }) => isBasic(Reflect.get(values, 'type')),
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
    ifShow: ({ values }) => isBasic(Reflect.get(values, 'type')),
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },

  {
    field: 'description',
    label: '说明',
    colProps: { span: 24 },
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
      rows: 4,
      placeholder: '请输入说明',
    },
  },
];