AddUserModal.jsx 5.68 KB
Newer Older
1 2
import React, { useEffect } from 'react';
import { Modal, Form, Input, notification, message } from 'antd';
邓超's avatar
邓超 committed
3
import { addUser } from '@/services/userManage/api';
皮倩雯's avatar
皮倩雯 committed
4
import { ok } from '../../../assets/images/icons/ok.svg';
陈前坚's avatar
陈前坚 committed
5

6
const AddUserModal = props => {
7
  const { visible, orgID, onCancel, onSelect, orgTitle1 } = props;
陈前坚's avatar
陈前坚 committed
8 9 10 11 12 13 14 15 16 17
  const [addUserForm] = Form.useForm(); // 添加用户
  /** ***正则验证**** */
  const noChinese = new RegExp(/^[^\u4e00-\u9fa5]+$/); // 不能包含中文
  const isPhone = new RegExp(/^1(3|4|5|6|7|8|9)\d{9}$/); // 手机号
  const isEmail = new RegExp(
    /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/,
  ); // 邮箱

  useEffect(() => {
    addUserForm.resetFields();
18
    console.log(orgID);
陈前坚's avatar
陈前坚 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  }, [orgID]);

  // 提交-添加用户
  const submitAddUser = () => {
    const loginName = addUserForm.getFieldValue('loginName') || '';
    const userName = addUserForm.getFieldValue('userName') || '';
    const password = addUserForm.getFieldValue('password') || '';
    const phone = addUserForm.getFieldValue('phone') || '';
    const email = addUserForm.getFieldValue('email') || '';
    // 正则验证
    if (loginName === '') {
      notification.error({
        message: '提交失败',
        description: '登录名不能为空!',
      });
    } else if (!noChinese.test(loginName)) {
      notification.error({
        message: '提交失败',
        description: '登录名不支持中文!',
      });
39
    } else if (password === '') {
陈前坚's avatar
陈前坚 committed
40 41
      notification.error({
        message: '提交失败',
42
        description: '密码不能为空!',
陈前坚's avatar
陈前坚 committed
43
      });
44
    } else if (password.length < 6) {
45 46
      notification.error({
        message: '提交失败',
47
        description: '密码至少为6位,且包含数字和字母!',
48
      });
49
    } else if (userName === '') {
陈前坚's avatar
陈前坚 committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      notification.error({
        message: '提交失败',
        description: '用户名称不能为空!',
      });
    } else if (phone !== '' && !isPhone.test(phone)) {
      notification.error({
        message: '提交失败',
        description: '请输入11位手机号!',
      });
    } else if (email !== '' && !isEmail.test(email)) {
      notification.error({
        message: '提交失败',
        description: '邮箱格式不正确!',
      });
    }
    // 所有验证通过才可以提交,phone/email为空时不验证
    else if (
      noChinese.test(loginName) &&
      password.length >= 6 &&
      userName &&
      (phone === '' || isPhone.test(phone)) &&
      (email === '' || isEmail.test(email))
    ) {
皮倩雯's avatar
皮倩雯 committed
73
      addUser({ OUID: orgID.id, loginName, userName, password, phone, email })
陈前坚's avatar
陈前坚 committed
74
        .then(res => {
75
          if (res.code === 0) {
陈前坚's avatar
陈前坚 committed
76
            addUserForm.resetFields();
77
            onCancel(); // 设置Modal不可见
陈前坚's avatar
陈前坚 committed
78 79 80 81 82
            notification.success({
              message: '提交成功',
              duration: 2,
            });
            // 重新获取用户表
83
            onSelect();
陈前坚's avatar
陈前坚 committed
84 85 86
          } else {
            notification.error({
              message: '提交失败',
87
              description: res.msg,
陈前坚's avatar
陈前坚 committed
88 89 90 91 92 93 94 95
            });
          }
        })
        .catch(err => {
          message.error(err);
        });
    }
  };
96 97
  const title = (
    <span>
98 99
<span style={{ fontWeight: 'bold', color: 'rgb(24, 144, 255)' }}>{orgTitle1}</span>
      下添加用户
100 101 102
    </span>
  );

103 104 105 106 107
  return (
    <Modal
      title={title}
      visible={visible}
      onCancel={onCancel}
108 109 110 111 112
      maskClosable={false}
      destroyOnClose
      afterClose={() => {
        addUserForm.resetFields();
      }}
113 114 115
      onOk={submitAddUser}
      okText="确认"
      cancelText="取消"
陈前坚's avatar
陈前坚 committed
116
    >
117 118
      <Form form={addUserForm} labelCol={{ span: 4 }}>
        <Form.Item
119
          hasFeedback
120 121
          name="loginName"
          label="登录名称"
122 123
          rules={[
            {
124 125
              pattern: /^[a-zA-Z0-9_]{0,}$/,
              message: '不支持中文和特殊字符',
126
            },
127
            { required: true },
皮倩雯's avatar
皮倩雯 committed
128
          ]}
129
        >
130
          <Input placeholder="登录名称不支持中文与特殊字符" maxLength="20" />
131 132
        </Form.Item>
        <Form.Item
133
          hasFeedback
134 135
          name="password"
          label="账户密码"
136 137
          rules={[
            {
138
              pattern: /^(?=.*[a-zA-Z\d])(?=.*([a-zA-Z].*))(?=.*[0-9].*)[!-~]{6,16}$/,
139
              message: '密码长度6-16位,且必须包含数字和字母,不能存在空格',
140
            },
141
            { required: true },
142
          ]}
143
        >
144
          <Input placeholder="请输入账户密码,至少6位" maxlength="16" />
145
        </Form.Item>
146 147 148 149 150 151 152 153 154
        <Form.Item
          hasFeedback
          name="userName"
          label="用户姓名"
          rules={[
            { required: true },
            { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不支持特殊字符' },
          ]}
        >
155
          <Input placeholder="用户姓名不支持特殊字符" maxLength="20" />
156 157
        </Form.Item>
        <Form.Item
158
          hasFeedback
159 160 161 162
          name="phone"
          label="手机号码"
          rules={[
            {
163 164
              pattern: new RegExp(/^1[0-9]{10}$/),
              message: '手机号码以1开头11位',
165 166 167
            },
          ]}
        >
168
          <Input placeholder="请输入11位手机号码" maxlength="11" autoComplete="off" />
169 170
        </Form.Item>
        <Form.Item
171
          hasFeedback
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
          name="email"
          label="电子邮箱"
          rules={[
            {
              type: 'email',
              message: '请输入正确的电子邮箱!',
            },
          ]}
        >
          <Input placeholder="请输入电子邮箱" autoComplete="off" />
        </Form.Item>
      </Form>
    </Modal>
  );
};
陈前坚's avatar
陈前坚 committed
187 188

export default AddUserModal;