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

let olderPhoneNumber;
const { t } = useI18n();

export const columns: BasicColumn[] = [
  {
    title: t('system.account.username'),
    dataIndex: 'username',
    width: 120,
  },
  {
    title: t('system.account.fullname'),
    dataIndex: 'realName',
    width: 120,
  },
  {
    title: t('system.account.phoneNumber'),
    dataIndex: 'phoneNumber',
    width: 120,
  },
  {
    title: t('system.account.email'),
    dataIndex: 'email',
    width: 120,
  },
  {
    title: t('common.createTimeText'),
    dataIndex: 'createTime',
    width: 180,
  },
  {
    title: t('system.account.state'),
    dataIndex: 'userStatusEnum',
    width: 120,
    slots: { customRender: 'status' },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: t('system.account.username'),
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: t('system.account.fullname'),
    label: t('common.fullName'),
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
    },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: 'username',
    label: t('system.account.username'),
    component: 'Input',
    colProps: { span: 12 },
    dynamicDisabled: false,
    componentProps: {
      maxLength: 36,
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject(t('system.account.ruleName1'));
              } else if (ChineseRegexp.test(value)) {
                reject(t('system.account.ruleName2'));
              } else if (EmailRegexp.test(value)) {
                reject(t('system.account.ruleName3'));
              } else {
                if (values.username != undefined && values.id == undefined) {
                  isAccountExist(value).then(({ data }) => {
                    if (data != null) {
                      reject(t('system.account.ruleName4'));
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
  },
  {
    field: 'password',
    label: t('system.account.password'),
    component: 'InputPassword',
    required: true,
    colProps: { span: 12 },
  },
  {
    field: 'realName',
    label: t('system.account.fullname'),
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    componentProps: {
      maxLength: 10,
    },
  },
  {
    label: t('system.account.role'),
    field: 'roleIds',
    component: 'Select',
    colProps: { span: 12 },
    slot: 'roleSlot',
    rules: [
      {
        required: true,
        type: 'array',
      },
    ],
  },
  {
    label: t('system.account.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.account.rulePhone1'));
              } else if (!phoneRegexp.test(value)) {
                reject(t('system.account.rulePhone2'));
              } else {
                if (values.phoneNumber != undefined) {
                  // 此处可以用防抖函数优化性能
                  IsPhoneExist(value).then(({ data }) => {
                    if (data != null) {
                      reject(t('system.account.rulePhone3'));
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
    componentProps({ formActionType }) {
      const { clearValidate } = formActionType;
      return {
        onChange(value) {
          if (value == olderPhoneNumber) {
            clearValidate('phoneNumber');
          }
        },
      };
    },
  },
  {
    label: t('system.account.email'),
    field: 'email',
    component: 'Input',
    colProps: { span: 12 },
    rules: emailRule,
  },
  {
    field: 'accountExpireTime',
    label: t('system.account.period'),
    component: 'DatePicker',
    colProps: { span: 12 },
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    field: 'enabled',
    label: t('system.account.state'),
    component: 'RadioButtonGroup',
    colProps: { span: 12 },
    defaultValue: true,
    componentProps: {
      options: [
        { label: t('common.enableText'), value: true },
        { label: t('common.disableText'), value: false },
      ],
    },
  },
  {
    field: 'remark',
    label: t('common.remarkText'),
    component: 'InputTextArea',
    colProps: { span: 24 },
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'organizationIds',
    label: t('business.affiliatedOrganizationText'),
    component: 'Input',
    slot: 'organizationId',
    required: true,
  },
];