import { Button, Col, Form, Input, Row } from "antd"; import React, { useEffect, useState } from "react"; import HCserviceV3 from "../tmpl/hcservice-v3"; import { useLocale } from "../locale/useLocale"; import { useParams } from "react-router-dom"; import Units from "../units"; import { SafetyCertificateOutlined } from "@ant-design/icons"; import { useSessionStorageState } from 'ahooks'; import { KeepAliveTab } from "../hooks/use-tabs"; type LoginPropType = { onFinish?: (token: string) => void; title?: any; programCode?: string; autoSaveUser?: boolean; version?: string; style?: { [key: string]: any }; applicationCode?: string; }; type FormType = { username: string; password: string; kaptchaText: string; }; const defStyle = { input: { borderRadius: "50px", background: "#EEF3FB" }, kaptcha: { borderRadius: "30px", background: "#EEF3FB" }, button: { shape: "round" } } const Login = (props: LoginPropType) => { const { version = "v2", style = defStyle } = props; // locale const { translate } = useLocale(); const [keepAliveTabs, setKeepAliveTabs] = useSessionStorageState('keepAliveTabs', { defaultValue: [], }); useEffect(() => { localStorage.setItem("version", version); Units.logout(); // 登录页-清除数据缓存 }, []); const { programCode: routeProgramCode, applicationCode: routeApplicationCode } = useParams(); useEffect(() => { if (routeProgramCode) Units.setProgramCode(routeProgramCode); if (routeApplicationCode) Units.setApplicationCode(routeApplicationCode); }, [routeProgramCode, routeApplicationCode]); const [loading, setLoading] = useState(false); const [needKaptcha, setNeedKaptcha] = useState(false); const [kaptchaImg, setKaptchaImg] = useState(null); const [kaptchaToken, setKaptchaToken] = useState(null); // form const [form] = Form.useForm(); const submit = async () => { setKeepAliveTabs([]); try { const { username, password, kaptchaText } = await form.validateFields(); setLoading(true); const { programCode, applicationCode, onFinish } = props; const pubkey = await HCserviceV3.getRasPubkey( null, programCode || routeProgramCode, applicationCode || routeApplicationCode ); const res = await HCserviceV3.login( { username, password }, kaptchaToken, kaptchaText, pubkey, programCode || routeProgramCode, applicationCode || routeApplicationCode ); if (res.status === "success") { onFinish(res.token); } else if (res.status === "error") { setNeedKaptcha(true); await getKaptchaToken(); } setLoading(false); } catch (err) { console.error(err); } }; const handleKeyDown = (event) => { //按下enter键,触发login事件 switch (event.keyCode) { case 13: submit(); break; default: break; } }; const getKaptchaToken = async () => { let res = await HCserviceV3.getKaptchaToken(routeProgramCode, routeApplicationCode); if (res) { let kaptchaImg; if (res.img && res.img.indexOf("data:") == 0) { kaptchaImg = res.img; } else { kaptchaImg = `data:image/png;base64,${res.img}`; } setKaptchaImg(kaptchaImg); setKaptchaToken(res.kaptchaToken); } }; // button return (
{needKaptcha ? ( } placeholder={translate("${验证码}")} //onKeyDown={this.handleKeyDown} /> ) : ( "" )}
); }; export default Login;