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
How to solve the Mysql transaction operation fail...
Div basic layout <div class="main"&g...
Table of contents Stabilization Throttling: Anti-...
//grammar: @media mediatype and | not | only (med...
There are many differences between IE6 and IE7 in ...
Table of contents 1.Linux login interface 2. Writ...
In the forum, netizens often ask, can I read the ...
Recently, due to work needs, I need to format num...
1. Concept They are all attributes of Element, in...
Copy code The code is as follows: <!--doctype ...
I recently watched Apple's press conference a...
width: auto The child element (including content+...
Table of contents Preface Publish-Subscriber Patt...
Bugs As shown in the figure, I started to copy th...
Table of contents 1. Introduction 2. Preparation ...