umi框架编写简单的验证码登录

首先我们需要先创建一个layouts的文件夹,用来存储跳转动画的文件,layouts的文件夹和pages同级。
然后在pages中创建一个login文件夹用来存储登录文件,然后再里面创建login和index文件开始编写代码。
接下来就是废话不多说,上代码

import React, { useState, useEffect, useRef } from 'react'
import './index.less'
import { history } from 'umi'
import { NavBar, AutoCenter, Input, Form, Button,Toast } from 'antd-mobile'
let timer;
export default function Login() {
  const codet = useRef(null);
  const [name, setname] = useState('')
  const [phone, setphone] = useState('')
  const [password, setPass] = useState('')
  const [time, setTime] = useState(5);
  const [disabled, setDisabled] = useState(true)
  const [btnContent, setBtnContent] = useState('获取验证码');
  const [btnDisabled, setBtnDisabled] = useState(false);
  const [sstr, setstr] = useState('');
  const [phonecode, setcode] = useState('');
  function usenum() {
    let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (name.length==0) {
        Toast.show({
            icon: 'fail',
            content: '请输入姓名',
        })
    }else
    if (!phoneReg.test(phone)) {
        Toast.show({
            icon: 'fail',
            content: '手机号错误',
        })
    }else
        if (phonecode.length == 0) {
        Toast.show({
            icon: 'fail',
            content: '请输入验证码',
        })
    }
        else
            if (phonecode != sstr) {
                Toast.show({
                    icon: 'fail',
                    content: '验证码错误',
                })
            }
    else {
        Toast.show({
            icon: 'success',
            content: '登录成功',
        })
        history.push({ pathname: '/home', query: { name } })
    }
}

useEffect(() => {
    console.log(name);
    console.log(phone);
    console.log(password);
    if (name.length > 0 & phone.length > 0 & password.length > 0) {
        console.log(2856725468457);
        setDisabled(false)
    } else {
        setDisabled(true)
    }
    return () => {
        setDisabled(false)
    };
}, [name, phone, password]);

useEffect(() => {
    clearInterval(timer);
    return () => clearInterval(timer);
}, []);

useEffect(() => {
    if (time > 0 && time < 60) {
        setBtnContent(`${time}s后重发`);
    } else {
        clearInterval(timer);
        setBtnDisabled(false);
        setTime(60);
    }
}, [time]);


const getCaptcha = () => {
    let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!phoneReg.test(phone)) {
        Toast.show({
            icon: 'fail',
            content: '请输入正确的手机号',
        });
    } else {
        if (!phone) return;
        timer = setInterval(() => setTime((pre) => pre - 1), 1000);
        setBtnDisabled(true);
        let wer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let sert = '';
        for (let e = 0; e < 6; e++) {
            sert += wer[Math.ceil(Math.random() * wer.length - 1)];
        }
        // setcode(sert);
        setstr(sert);
        Toast.show({
            content: '验证码为' + sert,
        });
    }
};
  return (
    <div>
      <NavBar back={null} backArrow={false}>手机号登录</NavBar>
      <AutoCenter>
        <Form className='autoform' layout='horizontal'
          footer={
            <Button onClick={usenum.bind(this)} block type='submit' color='primary' size='large'>
              提交
            </Button>
          }>
          <Form.Item label='姓名' name='name'
            rules={[{ message: '姓名不能为空' }]}>
            <Input placeholder='请输入姓名' value={name}
              onChange={val => {
                setname(val)
              }} clearable />
          </Form.Item>
          <Form.Item label='手机号'
            rules={[{ message: '手机号不能为空' }]}
            name='phone'>
            <Input placeholder='请输入手机号' value={phone}
              onChange={val => {
                setphone(val)

              }}
              clearable />

          </Form.Item>
          <Form.Item label="短信验证码"
            rules={[{ message: '验证码不能为空' }]}
            extra={
              <Button
                color="warning"
                disabled={btnDisabled}
                onClick={getCaptcha}
              >
                {!btnDisabled ? '获取验证码' : `${time}s后重发`}
              </Button>
            }>
            <Input
              placeholder="请输入验证码"
              value={phonecode}
              ref={codet}
              onChange={(val) => {
                setcode(val);
              }}
            />
          </Form.Item>
        </Form>
      </AutoCenter>
    </div>
  )
}

然后就是样式的代码

.adm-nav-bar {
    --height: 45px;
    --border-bottom: none;
    display: flex;
    align-items: center;
    height: var(--height);
    border-bottom: var(--border-bottom);
    padding: 0 12px;
    white-space: nowrap;
    background-color: darkorchid;
    color: white;
    font-size: 15px;
}

.adm-auto-center {
    display: flex;
    justify-content: center;
    margin-top: 50%;
}

.adm-button .adm-button-warning .adm-button-shape-default {
    --text-color: var(--adm-color-white);
    --background-color: #8f8c8a;
    /* --border-color: var(--color); */
}

.adm-button-warning {
    --color: #dbd7d3;
}

.adm-form {
    --border-inner: solid 1px var(--adm-border-color);
    --border-top: solid 1px var(--adm-border-color);
    --border-bottom: solid 1px var(--adm-border-color);
    --prefix-width: 6.8em;
    ---border-inner: var(--border-inner);
    ---border-top: var(--border-top);
    ---border-bottom: var(--border-bottom);
    ---prefix-width: var(--prefix-width);
    margin-top: 50%;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程雪员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值