获取验证码功能


前言

在日常的开发中,我们可能经常会做获取验证码的功能,市场上也有很多已经封装好的第三方组件,但很多时候需要我们二开。下面是基于Ant Design组件来进行二开的


一、官方组件

源码如下:

<ProFormCaptcha
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={'prefixIcon'} />,
                }}
                captchaProps={{
                  size: 'large',
                }}
                placeholder={'请输入验证码'}
                captchaTextRender={(timing, count) => {
                  if (timing) {
                    return `${count} ${'获取验证码'}`;
                  }
                  return '获取验证码';
                }}
                name="captcha"
                rules={[
                  {
                    required: true,
                    message: '请输入验证码!',
                  },
                ]}
                onGetCaptcha={async () => {
                  message.success('获取验证码成功!验证码为:1234');
                }}
              />

这是ProComponents登录组件中所使用的获取手机验证码的,有一个60秒倒计时,可符合日常简单的开发使用。

二、基于官方组件二次开发

先将倒计时功能封装为单独的ts文件

import { useEffect, useRef, useState } from 'react';

/** 验证码倒计时 */
export function useCaptchaTimer() {
  const [second, setSecond] = useState(60);
  const [btnDisabled, setBtnDisabled] = useState(true);
  const timerRef = useRef<any>(null);

  useEffect(() => {
    return () => clearInterval(timerRef.current);
  }, []);

  useEffect(() => {
    if (second > 0 && second < 60) {
      // 定时器超时
    } else {
      clearInterval(timerRef.current);
      setBtnDisabled(false);
      setSecond(60);
    }
  }, [second]);

  // 开始倒计时
  const startCountdown = () => {
    clearInterval(timerRef.current);
    timerRef.current = setInterval(() => setSecond(prevSecond => prevSecond - 1), 1000);
    setBtnDisabled(true);
  };

  // 停止倒计时
  const stopCountdown = () => {
    clearInterval(timerRef.current);
    setBtnDisabled(false);
    setSecond(60);
  };

  return { second, btnDisabled, startCountdown, stopCountdown };
}

然后,在需要使用的组件中导入即可使用

<ProFormText
              label='验证码'
              name='code'
              fieldProps={{
                size: 'large',
                prefix: <LockOutlined />,
              }}
              placeholder='请输入验证码!'
              rules={[
                {
                  required: true,
                  message: '请输入验证码!',
                },
              ]}
            />
            <Button
              className='mt-1.5'
              size='large'
              disabled={btnDisabled}
              onClick={getCaptcha}
              loading={loading}
            >
              {!btnDisabled ? '获取验证码' : `${second}s 后重新获取`}
            </Button>

在这里,可配合输入框来进行动态的获取,当输入框为空时,获取验证码按钮为禁用状态。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
获取验证码功能需要用到 Android 中的计时器(CountDownTimer)和短信验证。以下是一种简单的实现方式: 1. 在 XML 布局文件中添加一个按钮和一个 EditText,用于用户输入手机号码: ``` <EditText android:id="@+id/et_phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号码"/> <Button android:id="@+id/btn_get_verification_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取验证码"/> ``` 2. 在 Activity 中找到这两个控件,并设置按钮的点击事件: ```java public class MainActivity extends AppCompatActivity { private EditText etPhoneNumber; private Button btnGetVerificationCode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etPhoneNumber = findViewById(R.id.et_phone_number); btnGetVerificationCode = findViewById(R.id.btn_get_verification_code); // 设置按钮的点击事件 btnGetVerificationCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取用户输入的手机号码 String phoneNumber = etPhoneNumber.getText().toString(); // TODO: 调用短信验证 API,发送验证码 // 启动倒计时器 startCountDown(); } }); } // 启动倒计时器 private void startCountDown() { // 创建一个计时器,设置时间间隔为 1 秒,总时间为 60 秒 CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) { // 时间间隔结束调用的方法 @Override public void onTick(long millisUntilFinished) { // 更新按钮上的文字 btnGetVerificationCode.setText(millisUntilFinished / 1000 + " 秒后重新发送"); // 禁用按钮 btnGetVerificationCode.setEnabled(false); } // 总时间结束调用的方法 @Override public void onFinish() { // 更新按钮上的文字 btnGetVerificationCode.setText("获取验证码"); // 启用按钮 btnGetVerificationCode.setEnabled(true); } }; // 启动计时器 countDownTimer.start(); } } ``` 注意:上述代码中关于短信验证的部分需要自己调用相应的 API 实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小童不学前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值