Preface:This article only introduces the steps to integrate the graphic verification code component in the Ant Design Pro login function. For the server-side method, please refer to "Verification Code Function Based on OAuth2.0 Authorization System" text:In the Ant Design Pro template, the account and password login function part (as shown below) does not have a graphic verification code developed, so we need to implement this function ourselves. The login function here is actually a form submission, so we only need to develop a graphic verification code form control ourselves. The specific implementation is as follows. 1. Graphic verification code form control code CaptchaInput.tsx: import React, {useState, useEffect} from 'react'; import {Input, message} from 'antd'; import {SafetyCertificateOutlined} from '@ant-design/icons'; import api from '@/utils/api'; import stringUtil from "@/utils/stringUtil"; import request from "@/utils/request"; import {useIntl} from "umi"; interface CaptchaInputValue { captchaCode?: string; captchaKey?: string; } interface CaptchaInputProps { value?: CaptchaInputValue; onChange?: (value: CaptchaInputValue) => void; } /** * Get verification code */ const getCaptcha = async () => { try { const data = await request(api.captcha); if (data.code === 1) { return data.data; } } catch (error) { message.error('Failed to obtain department tree, please try again'); return []; } message.error('Failed to obtain department tree, please try again'); return []; } const CaptchaInput: React.FC<CaptchaInputProps> = ({value = {}, onChange}) => { const intl = useIntl(); const [captchaCode, setCaptchaCode] = useState<string>(''); const [captchaKey, setCaptchaKey] = useState<string>(''); const [imageData, setImageData] = useState<string>(''); // trigger change const triggerChange = (changedValue: { captchaCode?: string; captchaKey?: string }) => { if (onChange) { onChange({captchaCode, captchaKey, ...value, ...changedValue}); } }; useEffect(() => { getCaptcha().then((data: any) => { setCaptchaKey(data.captchaKey); setImageData(data.image); triggerChange({captchaKey: data.captchaKey}); }) }, []); // Input box changes const onChangeInput = (e: React.ChangeEvent<HTMLInputElement>) => { const code = e.target.value || ''; if (stringUtil.isNotEmpty(code)) { setCaptchaCode(code); } triggerChange({captchaCode: code}); } // Time type changes const onClickImage = () => { getCaptcha().then((data: any) => { setCaptchaKey(data.captchaKey); setImageData(data.image); triggerChange({captchaKey: data.captchaKey}); }) }; return ( <span> <Input.Group compact> <Input prefix={<SafetyCertificateOutlined style={{color: "#319cff"}}/>} placeholder={intl.formatMessage({ id: 'pages.login.captcha.placeholder', defaultMessage: 'Please enter the verification code', })} onChange={onChangeInput} style={{width: '75%', marginRight: 5, padding: '6.5px 11px 6.5px 11px', verticalAlign: 'middle'}}/> <img style={{width: '23%', height: '35px', verticalAlign: 'middle', padding: '0px 0px 0px 0px'}} src={imageData} onClick={onClickImage}/> </Input.Group> </span> ); }; export default CaptchaInput; 2. Login page integration components: import CaptchaInput from './components/CaptchaInput'; ... ... const handleSubmit = (values: LoginParamsType) => { const { dispatch } = props; values.client_id = "hanxiaozhang"; values.client_secret = "hanxiaozhang"; values.scope = "hanxiaozhang"; values.grant_type = "password"; values.captcha_key = values.captchaComp.captchaKey; values.captcha_code = values.captchaComp.captchaCode; delete values.captchaComp; dispatch({ type: 'login/login', payload: {...values, type}, }); }; ... ... <Form.Item name="captchaComp" rules={[{ validateTrigger: 'onBlur', validator: async (rule, value) => { // console.log(rule) if (stringUtil.isEmpty(value.captchaCode)) { throw new Error('Please enter the verification code!'); } } },]}> <CaptchaInput/> </Form.Item> ... ... 3. Use: The effect after integration is as follows: The above is the details of integrating the graphic verification code component in the Ant Design Pro login function. For more information about Ant Design Pro login, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to use binlog for data recovery in MySQL
>>: Implementation of Docker building Maven+Tomcat basic image
There are significant differences between centos7...
Preface There are many ways to center horizontall...
In MySQL, most indexes (such as PRIMARY KEY, UNIQ...
1. How to construct? Let's review the common ...
Let's take a look at ufw (Uncomplicated Firew...
Getting Started with JavaScript JavaScript is a l...
Table of contents 1. Declare and initialize array...
Table of contents 1. Grammar 2. Examples 3. Other...
Zabbix deployment documentation After zabbix is ...
Environmental Description: There is a running MyS...
MySQL 8.0.20 installation and configuration super...
The mysql on the server is installed with version...
1. What is Docker Swarm? Docker Swarm is a cluste...
Introduction Use simple jQuery+CSS to create a cus...
1. Install the cross-system file transfer tool un...