config.ts 5.93 KB
import { IsPhoneExist, isAccountExist } from '/@/api/system/system';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { useI18n } from '/@/hooks/web/useI18n';
import { ChineseRegexp, EmailRegexp, emailRule, phoneRegexp } from '/@/utils/rules';

let olderPhoneNumber;

const { t } = useI18n();
export const columns: BasicColumn[] = [
  {
    title: t('system.tenant.search.username'),
    dataIndex: 'username',
    width: 120,
  },
  {
    title: t('system.tenant.search.name'),
    dataIndex: 'realName',
    width: 120,
  },
  {
    title: t('system.tenant.search.phoneNumber'),
    dataIndex: 'phoneNumber',
    width: 120,
  },
  {
    title: t('system.tenant.search.email'),
    dataIndex: 'email',
    width: 120,
  },
  {
    title: t('system.tenant.search.status'),
    dataIndex: 'userStatusEnum',
    width: 120,
    slots: { customRender: 'status' },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: t('system.tenant.search.username'),
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
      placeholder: t('system.tenant.search.usernamePlaceholder'),
    },
  },
  {
    field: 'realName',
    label: t('system.tenant.search.name'),
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
      placeholder: t('system.tenant.search.namePlaceholder'),
    },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: 'username',
    label: t('system.tenant.search.username'),
    component: 'Input',
    colProps: { span: 12 },
    dynamicDisabled: false,
    componentProps: {
      maxLength: 36,
      placeholder: t('system.tenant.search.usernamePlaceholder'),
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject(t('system.tenant.search.usernamePlaceholder'));
              } else if (ChineseRegexp.test(value)) {
                reject(t('system.tenant.valid.usernameUnMatchChinese'));
              } else if (EmailRegexp.test(value)) {
                reject(t('system.tenant.valid.usernameUnMatchEmail'));
              } else {
                if (values.username != undefined && values.id == undefined) {
                  isAccountExist(value).then(({ data }) => {
                    if (data != null) {
                      reject(t('system.tenant.valid.usernameExist'));
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
  },
  // {
  //   field: 'password',
  //   label: '密码',
  //   component: 'InputPassword',
  //   required: true,
  //   colProps: { span: 12 },
  // },
  {
    field: 'realName',
    label: t('system.tenant.search.name'),
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    componentProps: {
      maxLength: 10,
    },
  },

  {
    label: t('system.tenant.search.phoneNumber'),
    field: 'phoneNumber',
    component: 'Input',
    colProps: { span: 12 },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject(t('system.tenant.search.phoneNumberPlaceholder'));
              } else if (!phoneRegexp.test(value)) {
                reject(t('system.tenant.valid.phoneNumberNotTruth'));
              } else {
                if (values.phoneNumber != undefined) {
                  // 此处可以用防抖函数优化性能
                  IsPhoneExist(value).then(({ data }) => {
                    if (data != null) {
                      reject(t('system.tenant.valid.phoneNumberExist'));
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
    componentProps({ formActionType }) {
      const { clearValidate } = formActionType;
      return {
        maxlength: 11,
        onChange(value) {
          if (value == olderPhoneNumber) {
            clearValidate('phoneNumber');
          }
        },
      };
    },
  },
  {
    label: t('system.tenant.search.role'),
    field: 'roleIds',
    component: 'Select',
    colProps: { span: 12 },
    slot: 'roleSlot',
    rules: [
      {
        required: true,
        message: t('system.tenant.search.rolePlaceholder'),
        type: 'array',
      },
    ],
  },
  {
    label: t('system.tenant.search.email'),
    field: 'email',
    component: 'Input',
    colProps: { span: 12 },
    rules: emailRule,
  },
  // {
  //   field: 'accountExpireTime',
  //   label: '有效期',
  //   component: 'DatePicker',
  //   colProps: { span: 12 },
  //   componentProps: {
  //     showTime: true,
  //     format: 'YYYY-MM-DD HH:mm:ss',
  //   },
  // },
  {
    field: 'enabled',
    label: t('system.tenant.search.status'),
    component: 'RadioButtonGroup',
    colProps: { span: 12 },
    defaultValue: true,
    componentProps: {
      options: [
        { label: t('common.enableText'), value: true },
        { label: t('common.disableText'), value: false },
      ],
    },
  },
  // {
  //   field: 'remark',
  //   label: '备注',
  //   component: 'InputTextArea',
  //   colProps: { span: 24 },
  //   componentProps: {
  //     maxLength: 255,
  //     placeholder: '请输入备注',
  //   },
  // },
  {
    field: 'organizationIds',
    label: t('business.affiliatedOrganizationText'),
    component: 'Input',
    slot: 'organizationId',
    required: true,
  },
];