AddUserModal.jsx 5.6 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 7
const AddUserModal = props => {
  const { title, visible, orgID, onCancel, onSelect } = 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();
皮倩雯's avatar
皮倩雯 committed
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 98 99 100 101 102 103
  return (
    <Modal
      title={title}
      visible={visible}
      onCancel={onCancel}
      onOk={submitAddUser}
      okText="确认"
      cancelText="取消"
陈前坚's avatar
陈前坚 committed
104
    >
105
      <Form form={addUserForm} labelCol={{ span: 4 }}>
106
        <span style={{position: 'absolute', top: '21%', color: 'red', fontSize: '16px'}}>*</span>
107
        <Form.Item
皮倩雯's avatar
皮倩雯 committed
108
          hasFeedback 
109 110
          name="loginName"
          label="登录名称"
皮倩雯's avatar
皮倩雯 committed
111 112 113 114 115 116 117
          rules={[{ 
            pattern:new RegExp(/^[^\u4e00-\u9fa5]+$/),
            message: '不支持中文' },
          {
            pattern: new RegExp(/^[^\s]*$/),
            message: '不能为空' }
          ]}
118 119 120
        >
          <Input placeholder="登录名称不支持中文" />
        </Form.Item>
121
        <span style={{position: 'absolute', top: '35%', color: 'red', fontSize: '16px'}}>*</span>
122
        <Form.Item
皮倩雯's avatar
皮倩雯 committed
123
          hasFeedback 
124 125
          name="password"
          label="账户密码"
皮倩雯's avatar
皮倩雯 committed
126 127
          rules={[{ 
            pattern: /^[^\s]*$/,
128 129 130 131 132 133 134 135
            message: '不能为空' 
          },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/,
            message: '密码长度必须大于6,且必须包含数字和字母'
          }
          ] 
        }
136 137 138
        >
          <Input placeholder="请输入账户密码,至少6位" />
        </Form.Item>
139
        <span style={{position: 'absolute', top: '49%', color: 'red', fontSize: '16px'}}>*</span>
140
        <Form.Item
皮倩雯's avatar
皮倩雯 committed
141
          hasFeedback 
142 143
          name="userName"
          label="用户名称"
皮倩雯's avatar
皮倩雯 committed
144 145 146
          rules={[{
            pattern: /^[^\s]*$/,
            message: '不能为空' }]}
147 148 149 150
        >
          <Input placeholder="请输入用户姓名" />
        </Form.Item>
        <Form.Item
皮倩雯's avatar
皮倩雯 committed
151
          hasFeedback 
152 153 154 155 156 157 158 159 160 161 162 163
          name="phone"
          label="手机号码"
          rules={[
            {
              pattern: new RegExp(/^1(3|4|5|6|7|8|9)\d{9}$/),
              message: '请输入11位手机号码!',
            },
          ]}
        >
          <Input placeholder="请输入11位手机号码" autoComplete="off" />
        </Form.Item>
        <Form.Item
皮倩雯's avatar
皮倩雯 committed
164
          hasFeedback 
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
          name="email"
          label="电子邮箱"
          rules={[
            {
              type: 'email',
              message: '请输入正确的电子邮箱!',
            },
          ]}
        >
          <Input placeholder="请输入电子邮箱" autoComplete="off" />
        </Form.Item>
      </Form>
    </Modal>
  );
};
陈前坚's avatar
陈前坚 committed
180 181

export default AddUserModal;