import React, { CSSProperties } from "react"; import "./index.css"; import { Button, Col, Form, Input, Row, FormInstance } from "antd"; import { LockOutlined, SafetyCertificateOutlined, UserOutlined, } from "@ant-design/icons"; import HCserviceV3 from "../tmpl/hcservice-v3"; import { LocaleContext } from "../locale/LocaleProvider"; import Vertify from "./vertify/index"; import Units from "../units"; import ResetPassword from "../layout/menu/reset-password"; interface LoginProps { onFinish: (token: string) => void; title?: any; programCode?: string; autoSaveUser?: boolean; applicationCode?: string; } interface LoginState { loading?: boolean; needKaptcha?: boolean; kaptchaImg?: string; kaptchaToken?: string; pubkey?: string; vertify: boolean; showSlideVertify: boolean; } class Login extends React.PureComponent { state = { loading: true, needKaptcha: false, kaptchaImg: null, kaptchaToken: null, pubkey: undefined, vertify: false, showSlideVertify: false, }; formRef = React.createRef(); static contextType = LocaleContext; context: React.ContextType; componentDidMount = async () => { const { programCode, applicationCode } = this.props; localStorage.setItem("version", "v1"); if (programCode) Units.setProgramCode(programCode); if (applicationCode) Units.setApplicationCode(applicationCode); window.removeEventListener("keydown", this.handleKeyDown); let pubkey = await HCserviceV3.getRasPubkey(null, programCode, applicationCode); this.setState({ pubkey, loading: false, }); }; getKaptchaToken = async () => { const { programCode, applicationCode } = this.props; let res = await HCserviceV3.getKaptchaToken(programCode, applicationCode); if (res) { let kaptchaImg; if (res.img && res.img.indexOf("data:") == 0) { kaptchaImg = res.img; } else { kaptchaImg = `data:image/png;base64,${res.img}`; } this.setState({ kaptchaImg, kaptchaToken: res.kaptchaToken, needKaptcha: true, }); } }; handleSubmit = async () => { this.setState({ loading: true, }); let { kaptchaToken, pubkey, needKaptcha, vertify } = this.state; await this.formRef.current .validateFields() .then(async (values) => { // if (needKaptcha && !vertify) // return this.setState({ showSlideVertify: false }); const { programCode, applicationCode } = this.props; let json = { username: values.username, password: values.password }; let res = await HCserviceV3.login( json, kaptchaToken, values.kaptchaText, pubkey, programCode, applicationCode ); if (res.status === "success") { this.props.onFinish(res.token); } else if (res.status === "error") { this.setState({ needKaptcha: true, vertify: false }); await this.getKaptchaToken(); } }) .catch((errorInfo) => { }); this.setState({ loading: false, }); }; handleKeyDown = (event) => { //按下enter键,触发login事件 switch (event.keyCode) { case 13: this.handleSubmit(); break; default: break; } }; changeKaptcha = async (event) => { //切换验证码 await this.getKaptchaToken(); }; render() { const { loading, needKaptcha, kaptchaImg, showSlideVertify } = this.state; const { autoSaveUser = true } = this.props; const { translate } = this.context; return ( <>
{this.props.title} } placeholder={translate("${用户名}")} autoComplete={autoSaveUser ? "on" : "off"} /> } /> {needKaptcha ? ( } placeholder={translate("${验证码}")} onKeyDown={this.handleKeyDown} /> ) : ( "" )}
{showSlideVertify ? (
this.setState({ vertify: true, showSlideVertify: false }, () => this.handleSubmit() ) } onFail={() => this.setState({ vertify: false, showSlideVertify: true }) } >
) : ( <> )} ); } } export default Login;