首页 > 其他分享 >Vue + Elementui 实现登录页 手机验证码、倒计时等功能

Vue + Elementui 实现登录页 手机验证码、倒计时等功能

时间:2022-08-31 10:22:19浏览次数:75  
标签:Vue Elementui 50% 验证码 timer blur message font

Vue + Elementui 实现登录页 手机验证码、倒计时等功能

点击打开视频讲解 更加详细

<template>
  <div id="app">
    <div class="left">用代码改变世界</div>
    <el-form
      class="content"
      ref="refForm"
      :rules="rules"
      :model="passwordResetForm"
      label-width="85px"
    >
      <el-form-item prop="accout" label="账 号:">
        <el-input
          size="small"
          clearable
          v-model="passwordResetForm.accout"
          placeholder="输入账号"
          prefix-icon="el-icon-user"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密 码:">
        <el-input
          size="small"
          type="password"
          maxlength="16"
          clearable
          v-model="passwordResetForm.password"
          prefix-icon="el-icon-lock"
          placeholder="输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="checkCode" label="验证码:" class="checkCode">
        <el-input
          size="small"
          clearable
          v-model="passwordResetForm.checkCode"
          placeholder="输入验证码"
        ></el-input>
        <el-button
          @click.stop="sendVerificationCode"
          size="mini"
          type="primary"
          style="margin-left: 10px"
          v-if="show"
          >发送验证码</el-button
        >
        <el-button
          size="mini"
          type="primary"
          style="margin-left: 10px"
          v-if="!show"
          disabled
          >{{ count }}秒后重发</el-button
        >
      </el-form-item>
      <div class="sign">
        <el-button @click.stop="sign" type="primary">登 录</el-button>
      </div>
    </el-form>
    <div class="right">用歌曲祭奠青春</div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "home",
  data() {
    return {
      passwordResetForm: {
        accout: "",
        password: "",
        checkCode: "",
      },
      rules: {
        accout: [{ required: true, message: "账号不能为空", trigger: "blur" }],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            trigger: "blur",
            validator: (rule, value, callback) => {
              let passwordreg =
                /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}/;
              if (!passwordreg.test(value)) {
                callback(
                  new Error("密码必须由数字、字母、特殊字符组合,请输入8-16位")
                );
              } else {
                callback();
              }
            },
          },
        ],
        checkCode: [
          { required: true, message: "验证码不能为空", trigger: "blur" },
          { max: 4, message: "验证码为4位数字", trigger: "blur" },
        ],
      },
      show: true,
      count: "",
      timer: null,
    };
  },
  mounted() {},
  methods: {
    //验证码 倒计时
    sendVerificationCode() {
      let TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
        this.getCode();
      }
    },
    //验证码
    getCode() {
      axios.get("/verificationCode.json").then((res) => {
        if (res.status == 200) {
          setTimeout(() => {
            this.passwordResetForm.checkCode = res.data.code;
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }, 3000);
        }
      });
    },
    //登录
    sign() {
      this.$refs["refForm"].validate((valid) => {
        if (valid) {
          this.$message({
            type: "success",
            message: "登录成功了哎!",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
#app {
  width: 100%;
  height: calc(100vh - 0px);
  background-image: url(../assets/moment.jpg);
  /* 设置图片宽、高 */
  background-size: 100% 100%;
  /*按比例缩放*/
  background-repeat: no-repeat;
}
.content {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 22px 30px;
  box-shadow: 0 0 10px 5px #19a2d0 inset;
}
.checkCode ::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-around;
}
::v-deep .el-form-item__label {
  font-size: 18px;
  font-weight: bold;
  padding: 0 0 0 0;
  color: #fff;
  text-align: left;
}
.sign > button {
  width: 100%;
  font-size: 18px;
}
.left {
  width: 10px;
  font-size: 40px;
  font-weight: bold;
  color: #fff;
  position: absolute;
  top: 50%;
  left: 25%;
  transform: translate(-50%, -50%);
}
.right {
  width: 10px;
  font-size: 40px;
  font-weight: bold;
  color: #fff;
  position: absolute;
  top: 50%;
  left: 75%;
  transform: translate(-50%, -50%);
}
</style>

效果图:

若对您有帮助,请点击跳转到B站一键三连哦!感谢支持!!!

标签:Vue,Elementui,50%,验证码,timer,blur,message,font
From: https://www.cnblogs.com/mochenxiya/p/16642063.html

相关文章

  • Vue学习之--------插槽【默认插槽、具名插槽、作用域插槽】(2022/8/30)
    插槽Vue.js官网介绍:https://vuejs.org/guide/components/slots.html会牵涉到template的用法、占位、实际不渲染到页面中1、默认插槽:1.1基本结构及介绍个人理解:在A......
  • Vue实现下载Excel文件的方法
    1.安装依赖包npminstall-Sfile-savernpminstall-Sxlsxnpminstall-Dscript-loader2.配置文件在项目目录中创建文件夹downloads,放入配置文件Export2Exce......
  • vue3源码学习2-创建和渲染vnode
    创建vnode我们在第一节中在packages/runtime-core/src/apiCreateApp.ts文件的createAppAPI方法中,app.mount()时://通过createVNode方法创建了根组件的vnodeconstvnod......
  • vue+element 的使用问题记录-下拉列表绑定值为整数
     vue+element的使用问题记录 1、下拉列表绑定值为整数问题现象:下拉列表没有显示对应的文字,显示的是数字。  解决方法:对应的对象的类中的数据类型是Integer......
  • Vue-条件渲染
    条件渲染条件渲染的属性有两个:1.v-if/v-elsev-if的方法是删除元素<body> <divid="app"> <divv-if="flag">上课</div> <divv-if="n">......
  • Vue-循环渲染
    循环渲染循环渲染使用的是v-for<body> <divid="app"> <divv-for="iteminarr">{{item}}</div> <divv-for="iteminarr2">{{item.time}}</div> <div......
  • Vue-样式绑定
    1.对class属性进行绑定<style> .app{ width:200px; height:200px; background-color:purple; } .app1{ width:100px; height:100px; bac......
  • vue3 基础-表单元素双向绑定
    通常是在form表单相关的场景中会用到双向绑定相关,核心是v-model的应用.input输入框<!DOCTYPEhtml><htmllang="en"><head><title>input</title><script......
  • VUE3.0+Antdv+Asp.net WebApi开发学生信息管理系统(完)
    在B/S系统开发中,前后端分离开发设计已成为一种标准,而VUE作为前端三大主流框架之一,越来越受到大家的青睐,Antdv是Antd在Vue中的实现。本系列文章主要通过Antdv和Asp.netWebA......
  • 父传子 子穿父 vue3
    不错的博客https://blog.csdn.net/qq_43895215/article/details/124626692......