tenantBaseColumns.ts 5.57 KB
import { BasicColumn } from '/@/components/Table/src/types/table';
import { FormSchema } from '/@/components/Form';
import { getAllRoleList } from '/@/api/system/system';
import { getTableTenantProfileApi, QueryTenantProfilesParam } from '/@/api/tenant/tenantApi';
import { RoleEnum } from '/@/enums/roleEnum';
import { FileItem } from '/@/components/Form/src/components/ApiUpload.vue';
import { createImgPreview } from '/@/components/Preview';
import { uploadThumbnail } from '/@/api/configuration/center/configurationCenter';
import { useI18n } from '/@/hooks/web/useI18n';

const { t } = useI18n();
export function getBasicColumns(): BasicColumn[] {
  return [
    {
      title: t('tenant.list.table.icon'),
      dataIndex: 'icon',
      fixed: 'left',
      width: 50,
      slots: { customRender: 'img' },
    },
    {
      title: t('tenant.list.table.name'),
      dataIndex: 'name',
      width: 150,
    },
    {
      title: t('common.remarkText'),
      dataIndex: 'description',
      width: 150,
    },
    {
      title: t('tenant.list.state'),
      dataIndex: 'userStatusEnum',
      width: 50,
      slots: { customRender: 'status' },
    },
    {
      title: t('tenant.list.period'),
      dataIndex: 'tenantExpireTime',
      width: 150,
    },
    {
      title: t('common.createTimeText'),
      dataIndex: 'createTime',
      width: 150,
    },
  ];
}
export const tenantFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    slot: 'iconSelect',
    component: 'Input',
    componentProps: {
      maxLength: 36,
    },
    show: false,
  },
  {
    field: 'tenantId',
    label: 'tenantId',
    component: 'Input',
    componentProps: {
      maxLength: 36,
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 36) {
              return Promise.reject(t('tenant.list.textRule36'));
            }
            return Promise.resolve();
          },
        },
      ];
    },
    show: false,
  },
  {
    field: 'icon',
    label: t('tenant.list.form.icon'),
    component: 'ApiUpload',
    changeEvent: 'update:fileList',
    valueField: 'fileList',
    componentProps: ({ formModel }) => {
      return {
        listType: 'picture-card',
        maxFileLimit: 1,
        accept: '.png,.jpg,.jpeg,.gif',
        api: async (file: File) => {
          try {
            const formData = new FormData();
            formData.set('file', file);
            const { fileStaticUri, fileName } = await uploadThumbnail(formData);
            return {
              uid: fileStaticUri,
              name: fileName,
              url: fileStaticUri,
            } as FileItem;
          } catch (error) {
            return {};
          }
        },
        onPreview: (fileList: FileItem) => {
          createImgPreview({ imageList: [fileList.url!] });
        },
        onDelete(url: string) {
          formModel.deleteUrl = url!;
        },
      };
    },
  },
  {
    field: 'deleteUrl',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'name',
    label: t('tenant.list.tenantName'),
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'roleIds',
    label: t('tenant.list.form.role'),
    component: 'ApiSelect',
    required: true,
    componentProps: () => {
      return {
        api: async () => {
          const res = await getAllRoleList({ roleType: RoleEnum.TENANT_ADMIN });
          return res;
        },
        mode: 'multiple',
        showSearch: true,
        labelField: 'name',
        valueField: 'id',
        filterOption: (inputValue: string, options: Record<'label' | 'value', string>) => {
          return options.label.toLowerCase().includes(inputValue.toLowerCase());
        },
      };
    },
  },
  {
    field: 'tenantProfileId',
    label: t('tenant.list.form.config'),
    component: 'ApiSelectScrollLoad',
    required: true,
    componentProps: ({ formActionType }) => {
      const { setFieldsValue } = formActionType;
      return {
        api: async (params: QueryTenantProfilesParam) => {
          const { items, total } = await getTableTenantProfileApi(params);
          const defaultRecord = items.find((item) => item.default);
          if (defaultRecord) {
            setFieldsValue({ tenantProfileId: defaultRecord.id.id });
          }
          return { items, total };
        },
        labelField: 'name',
        valueField: 'id.id',
        filterOption: (inputValue: string, options: Record<'label' | 'value', string>) => {
          return options.label.toLowerCase().includes(inputValue.toLowerCase());
        },
      };
    },
  },
  {
    field: 'enabled',
    label: t('tenant.list.state'),
    component: 'RadioButtonGroup',
    defaultValue: true,
    componentProps: {
      options: [
        { label: t('common.enableText'), value: true },
        { label: t('common.disableText'), value: false },
      ],
    },
  },
  {
    field: 'tenantExpireTime',
    label: t('tenant.list.period'),
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    label: t('common.remarkText'),
    field: 'description',
    component: 'InputTextArea',
    componentProps: {
      maxLength: 500,
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 500) {
              return Promise.reject(t('tenant.list.textRule500'));
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
];