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

let olderPhoneNumber;

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '姓名',
    dataIndex: 'realName',
    width: 120,
  },
  {
    title: '手机号码',
    dataIndex: 'phoneNumber',
    width: 120,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: 120,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'userStatusEnum',
    width: 120,
    slots: { customRender: 'status' },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入用户名',
    },
  },
  {
    field: 'realName',
    label: '姓名',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入姓名',
    },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 12 },
    dynamicDisabled: false,
    componentProps: {
      maxLength: 36,
      placeholder: '请输入用户名',
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入用户名');
              } else if (ChineseRegexp.test(value)) {
                reject('用户名不能含有中文');
              } else if (EmailRegexp.test(value)) {
                reject('用户名不能为电子邮箱格式');
              } else {
                if (values.username != undefined && values.id == undefined) {
                  isAccountExist(value).then(({ data }) => {
                    if (data != null) {
                      reject('用户名已存在');
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    colProps: { span: 12 },
  },
  {
    field: 'realName',
    label: '姓名',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    componentProps: {
      maxLength: 10,
    },
  },
  {
    label: '角色',
    field: 'roleIds',
    component: 'Select',
    colProps: { span: 12 },
    slot: 'roleSlot',
    rules: [
      {
        required: true,
        message: '请选择角色',
        type: 'array',
      },
    ],
  },
  {
    label: '手机号',
    field: 'phoneNumber',
    component: 'Input',
    colProps: { span: 12 },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入手机号');
              } else if (!phoneRegexp.test(value)) {
                reject('请输入正确的手机号');
              } else {
                if (values.phoneNumber != undefined) {
                  // 此处可以用防抖函数优化性能
                  IsPhoneExist(value).then(({ data }) => {
                    if (data != null) {
                      reject('手机号已存在');
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
    componentProps({ formActionType }) {
      const { clearValidate } = formActionType;
      return {
        maxlength: 11,
        onChange(value) {
          if (value == olderPhoneNumber) {
            clearValidate('phoneNumber');
          }
        },
      };
    },
  },
  {
    label: '邮箱',
    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: '状态',
    component: 'RadioButtonGroup',
    colProps: { span: 12 },
    defaultValue: true,
    componentProps: {
      options: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    },
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    colProps: { span: 24 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入备注',
    },
  },
  {
    field: 'organizationIds',
    label: '所属组织',
    component: 'Input',
    slot: 'organizationId',
    required: true,
  },
];