index.js 2.73 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1
import React, { useState } from 'react';
2
import { Alert, Checkbox } from 'antd';
张烨's avatar
张烨 committed
3 4
import { login } from '@/services/user/api';
import { USER_MODE } from '@/utils/constants';
dengxiaofeng's avatar
dengxiaofeng committed
5
import LoginForm from './components/Login';
dengxiaofeng's avatar
dengxiaofeng committed
6
import styles from './style.less';
张烨's avatar
张烨 committed
7
import { setAuthority } from '@/utils/authority';
dengxiaofeng's avatar
dengxiaofeng committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
const { UserName, Password, Submit } = LoginForm;

const LoginMessage = ({ content }) => (
  <Alert
    style={{
      marginBottom: 24,
    }}
    message={content}
    type="error"
    showIcon
  />
);

const Login = props => {
  const { userLogin = {}, submitting } = props;
  const { status, type: loginType } = userLogin;
  const [autoLogin, setAutoLogin] = useState(true);
  const [type, setType] = useState('account');
  const handleSubmit = values => {
    /* eslint-disable no-console */
28
    console.log(values); // eg:{userName: "123", password: "123"}
29 30
    const { userName: loginName, password } = values;
    login({ loginName, password }).then(result => {
31 32 33
      console.log(result);
      if (result.success === true && result.pass === true) {
        const { userMode } = result;
34 35
        localStorage.setItem('userMode', userMode);
        if (userMode === USER_MODE.ADMIN || userMode === USER_MODE.SUPER) {
张烨's avatar
张烨 committed
36
          setAuthority(['login', USER_MODE.ADMIN, USER_MODE.SUPER]);
37 38 39
          window.location.href = '/dbm/dbInit/';
        }
        if (userMode === USER_MODE.COMMON) {
张烨's avatar
张烨 committed
40
          setAuthority(['login', USER_MODE.COMMON]);
41
          window.location.href = '/ou/orgList/';
42
        }
43 44 45 46
      } else {
        alert('错误,没有权限');
      }
    });
dengxiaofeng's avatar
dengxiaofeng committed
47 48 49
  };

  return (
dengxiaofeng's avatar
dengxiaofeng committed
50
    <div className={styles.main}>
dengxiaofeng's avatar
dengxiaofeng committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      <LoginForm activeKey={type} onTabChange={setType} onSubmit={handleSubmit}>
        {status === 'error' && loginType === 'account' && !submitting && (
          <LoginMessage content="账户或密码错误(admin/ant.design)" />
        )}
        <UserName
          name="userName"
          placeholder="用户名: admin or user"
          rules={[
            {
              required: true,
              message: '请输入用户名!',
            },
          ]}
        />
        <Password
          name="password"
          placeholder="密码: ant.design"
          rules={[
            {
              required: true,
              message: '请输入密码!',
            },
          ]}
        />
        <div>
          <Checkbox
            checked={autoLogin}
            onChange={e => setAutoLogin(e.target.checked)}
          >
            自动登录
          </Checkbox>
          <a
            style={{
              float: 'right',
            }}
          >
            忘记密码
          </a>
        </div>
        <Submit loading={submitting}>登录</Submit>
      </LoginForm>
    </div>
  );
};

export default Login;