config.ts 5.07 KB
import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { findDictItemByCode } from '/@/api/system/dict';

// 表格配置
export const columns: BasicColumn[] = [
  {
    title: '接口名称',
    dataIndex: 'interfaceName',
    width: 150,
  },
  {
    title: '请求方式',
    dataIndex: 'requestContentType',
    width: 90,
    format(text) {
      return Number(text) === 0 ? '普通请求' : Number(text) === 1 ? 'SQL请求' : 'websocket请求';
    },
  },
  {
    title: '接口内容',
    dataIndex: 'content',
    width: 80,
    slots: { customRender: 'content' },
  },
  {
    title: '状态',
    dataIndex: 'state',
    width: 80,
    customRender: ({ record }) => {
      const status = record.state;
      const color = status == 1 ? 'green' : 'red';
      const text = status == 1 ? '发布' : '未发布';
      return h(Tag, { color: color }, () => text);
    },
  },
];

// 查询配置
export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '接口名称',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 36,
      placeholder: '请输入接口名称',
    },
  },
  {
    field: 'state',
    label: '发布状态',
    component: 'Select',
    colProps: { span: 8 },
    componentProps: {
      options: [
        {
          label: '发布',
          value: 1,
        },
        {
          label: '未发布',
          value: 0,
        },
      ],
      placeholder: '请选择发布状态',
    },
  },
];

//表单配置
export const schemas: FormSchema[] = [
  {
    field: 'interfaceName',
    label: '接口名称',
    colProps: { span: 24 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 64,
      placeholder: '请输入接口名称',
    },
  },
  {
    field: 'requestContentType',
    label: '请求方式',
    component: 'ApiSelect',
    required: true,
    colProps: { span: 24 },
    defaultValue: '0',
    componentProps: ({ formActionType }) => {
      const { setFieldsValue, updateSchema } = formActionType;
      return {
        api: findDictItemByCode,
        params: {
          dictCode: 'dataview_select_methods',
        },
        placeholder: '请选择请求方式',
        labelField: 'itemText',
        valueField: 'itemValue',
        getPopupContainer: () => document.body,
        async onChange(e) {
          setFieldsValue({ requestHttpType: e === '1' ? 'POST' : 'GET' });
          const res = await findDictItemByCode({
            dictCode: e === '1' ? 'dataview_select_sql_request' : 'dataview_select_request',
          });
          const options = res.map((m) => ({ label: m.itemText, value: m.itemValue }));
          updateSchema({
            field: 'requestHttpType',
            componentProps: {
              options,
            },
          });
        },
      };
    },
  },
  {
    field: 'requestOriginUrl',
    label: '源地址',
    colProps: { span: 24 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 64,
      placeholder: '请输入源地址',
    },
  },
  {
    field: 'requestHttpType',
    label: '请求类型',
    component: 'ApiSelect',
    required: true,
    colProps: { span: 6 },
    defaultValue: 'GET',
    componentProps: {
      placeholder: '请选择请求类型',
      api: findDictItemByCode,
      params: {
        dictCode: 'dataview_select_request',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
    },
    ifShow: ({ values }) =>
      values['requestContentType'] === '0' || values['requestContentType'] === '1',
  },
  {
    field: 'requestUrl',
    label: '',
    component: 'Input',
    required: true,
    colProps: { span: 18 },
    componentProps: {
      maxLength: 64,
      placeholder: '请输入接口地址',
    },
    ifShow: ({ values }) =>
      values['requestContentType'] === '0' || values['requestContentType'] === '1',
  },
  {
    field: 'requestSQLKey',
    label: '键名',
    colProps: { span: 12 },
    component: 'Input',
    defaultValue: 'sql',
    componentProps: {
      disabled: true,
    },
    ifShow: ({ values }) => values['requestContentType'] === '1',
  },
  {
    field: 'requestSQLContent',
    label: '键值',
    colProps: { span: 24 },
    component: 'InputTextArea',
    defaultValue: 'select * from  where',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入键值',
      rows: 6,
    },
    ifShow: ({ values }) => values['requestContentType'] === '1',
  },
  {
    field: 'slot',
    label: '参数设置',
    component: 'Input',
    slot: 'selectMethods',
    colProps: { span: 24 },
    ifShow: ({ values }) => values['requestContentType'] !== '1',
  },
  {
    field: 'testSlot',
    label: '',
    component: 'Input',
    slot: 'testSql',
    colProps: { span: 24 },
    ifShow: ({ values }) => values['requestContentType'] === '1',
  },
];