import React, { useState, useEffect } from 'react'; import { Form, Input, Button, Checkbox, Tabs, message } from 'antd'; import { UserOutlined, LockOutlined, EyeInvisibleOutlined, EyeTwoTone } from '@ant-design/icons'; import { history, useIntl } from 'umi'; import './login.less'; import { getCaptcha, supplierLogin, expertLogin, accountLogin, getUserinfo, refreshDictCache, findMenuList, changePasswordOnFirstLogin } from '@/servers/api/login'; import ChangePasswordModal from './components/ChangePasswordModal'; import { encryptWithRsa } from '@/utils/encryptWithRsa' 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 [userId, setUserId] = useState(''); // ======= 修改密码弹窗控制 ======= const [showChangePwd, setShowChangePwd] = useState(false); // const [captchaKey, setCaptchaKey] = useState(''); //切换后 走不同接口 const loginApiMap: { [key: string]: (params: any) => Promise } = { supplierLogin, expertLogin, accountLogin }; const intl = useIntl(); useEffect(() => { fetchCaptcha(); if (!sessionStorage.getItem('dict')) { refreshDictCache().then((res) => { if (res.code == 200) { sessionStorage.setItem('dict', JSON.stringify(res.data)) } }) } form.setFieldsValue({ password: 'cosco2025', identifying: '1' }) }, [activeKey]); // 修改密码确认回调 const handleChangePwd = async (values: { userId: string; newPassword: string; confirmPassword: string; }) => { try { await changePasswordOnFirstLogin({ userId: userId, newPassword: encryptWithRsa(values.newPassword, false), confirmPassword: encryptWithRsa(values.confirmPassword, false) }).then((res) => { if (res.data) { setShowChangePwd(false); message.success('修改成功,请重新登录'); } else { message.success('修改密码失败'); } }) } catch (e: any) { message.error(e?.message || '修改密码失败'); } }; 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) { if (values.remember) { localStorage.setItem('remember_user', JSON.stringify({ username: values.username, password: values.password, })); } else { localStorage.removeItem('remember_user'); } sessionStorage.setItem('activeKey', activeKey) sessionStorage.setItem('token', loginRes.data.token); //存入供应商用户id if (activeKey === 'supplierLogin') { sessionStorage.setItem('userId', loginRes.data.supplierUser.userId); if (loginRes.data?.supplierUser?.firstLogin === 1) { setUserId(loginRes.data.supplierUser.userId) setShowChangePwd(true); return } } else if (activeKey === 'expertLogin') { //存入专家用户id // sessionStorage.setItem('userId', loginRes.data.expertUser.userId); } else if (activeKey === 'accountLogin') { //存入招标代理用户id sessionStorage.setItem('userId', loginRes.data.user.userId); } sessionStorage.setItem('currentUser', JSON.stringify(loginRes.data)); await getUserinfo().then(async (res) => { const roleIdList = res.authorityList.map((item: any) => item.roleId); sessionStorage.setItem('Userinfo', JSON.stringify(res)); const menuList: any = await findMenuList({ roleIdList }); sessionStorage.setItem('menuList', JSON.stringify(menuList.data)); message.success('登录成功'); if (activeKey === 'supplierLogin') { history.push('/backend/workbenches'); } else { 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' })}
); }; // 忘记密码点击 const onForgot = () => { history.push('/forgot?type=' + activeKey); } 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' })}
{activeKey !== 'accountLogin' && renderRegisterLink()}
); }; export default LoginPage;