import React, { useState, useEffect } from 'react'; import { Form, Input, Button, Checkbox, Tabs, message } from 'antd'; import { UserOutlined, LockOutlined, EyeInvisibleOutlined, EyeTwoTone, HomeOutlined } from '@ant-design/icons'; import { history, useIntl } from 'umi'; import './login.less'; import { getCaptcha, supplierLogin, expertLogin, accountLogin, getUserinfo, findMenuList, queryUserOrgAll } from '@/servers/api/login'; import { encryptWithRsa } from '@/utils/encryptWithRsa' import Password from 'antd/lib/input/Password'; const { TabPane } = Tabs; const LoginPage: React.FC = () => { const [activeKey, setActiveKey] = useState('supplierLogin'); const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [captchaImg, setCaptchaImg] = useState(''); const [captchaKey, setCaptchaKey] = useState(''); //切换后 走不同接口 const loginApiMap: { [key: string]: (params: any) => Promise } = { supplierLogin, expertLogin, accountLogin }; const intl = useIntl(); useEffect(() => { fetchCaptcha(); }, [activeKey]); const onFinish = async (values: any) => { setLoading(true); try { const params = { ...values, password: encryptWithRsa(values.password, false), encryptValue: encryptWithRsa(values.identifying) }; const loginRes = await loginApiMap[activeKey](params); if (loginRes.code === 200) { sessionStorage.setItem('token', loginRes.data.token); //存入供应商用户id if (activeKey === 'supplierLogin') { sessionStorage.setItem('userId', loginRes.data.supplierUser.userId); } else if (activeKey === 'expertLogin') { //存入专家用户id // sessionStorage.setItem('userId', loginRes.data.expertUser.userId); } else if (activeKey === 'accountLogin') { //存入招标代理用户id sessionStorage.setItem('userId', loginRes.data.user.userId); //部门 queryUserOrgAll().then((res) => { const { code, data } = res; if (code == 200) { sessionStorage.setItem('userOrgAll', JSON.stringify(data) ); } }) } sessionStorage.setItem('currentUser', JSON.stringify(loginRes.data)); getUserinfo().then(async (res) => { // if(res.code == 200) { const roleIdList = res.authorityList.map((item: any) => { return item.roleId }) console.log(roleIdList, 'roleIdList'); const menuList = await findMenuList({ roleIdList }); sessionStorage.setItem('menuList', JSON.stringify(menuList.data)); // } }) message.success('登录成功'); history.push('/index'); } else { message.error(loginRes.message || '登录失败'); } } finally { setLoading(false); } }; const fetchCaptcha = async () => { const res = await getCaptcha(); if (res.code === 200) { setCaptchaImg(res.data.base64Image); setCaptchaKey(res.data.code); } }; const handleTabChange = (key: string) => { setActiveKey(key); form.resetFields(); }; // 根据当前选中的Tab决定跳转到哪个注册页面 const handleRegister = () => { switch (activeKey) { case 'supplierLogin': history.push('/register/supplier'); break; case 'expertLogin': history.push('/register/expert'); break; default: // 招标代理不提供注册功能 break; } }; // 渲染注册链接(只在供应商和专家Tab下显示) const renderRegisterLink = () => { if (activeKey === 'agent') { return null; // 招标代理不显示注册链接 } return (
{intl.formatMessage({ id: 'login.register.tip' })} {intl.formatMessage({ id: 'login.register.action' })}
); }; return (
{/* */}
{intl.formatMessage({ id: 'login.title' })}
} placeholder={intl.formatMessage({ id: 'login.username.placeholder' })} size="large" /> } placeholder={intl.formatMessage({ id: 'login.password.placeholder' })} iconRender={visible => (visible ? : )} size="large" /> } />
{intl.formatMessage({ id: 'login.remember' })} {intl.formatMessage({ id: 'login.forgot' })}
{activeKey !== 'accountLogin' && renderRegisterLink()}
); }; export default LoginPage;