Vue(60s倒计时获取验证码)

本文介绍如何在Vue应用中实现一个60秒倒计时功能,用于在用户输入手机号后发送短信验证码进行身份验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(手机号+短信验证码)

 

<template>
  <div class="taofuli-cash" v-if="showCash">
    <img class="cash-bg" :src="bgLogin" />
    <div class="cash-con">
      <div class="cash-title">提现身份验证</div>
      <div class="cash-desc">
        <form action="">
          <input
            type="text"
            class="cash-name"
            @blur="onCheckName"
            placeholder="请输入您的手机号"
            v-model="form.name"
          />
          <div class="cash-box">
            <input
              type="text"
              class="cash-code"
              @blur="onCheckCode"
              placeholder="请输入验证码"
              v-model="form.code"
            />
            <input
              :class="[showGetCode ? 'cash-got' : 'cash-get']"
              @click="onGetCode"
              :disabled="showGetCode"
              type="button"
              :value="codeText"
            />
          </div>
          <div class="cash-error">{{ errMsg }}</div>
          <input
            @click="onSubmit"
            class="cash-submit"
            type="button"
            value="确定"
          />
        </form>
      </div>
    </div>
    <img
      :src="closeIcon"
      alt=""
      @click="closeCash"
      class="lucky-close cash-close"
    />
  </div>
</template>
<script>
import bgLogin from "@/assets/images/10.png";
import closeIcon from "@/assets/images/8.png";
import { verifyName, verifyCode } from "@/libs/tools.js";
export default {
  name: "withdraw-cash",
  props: {
    showCash: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      handleCash: this.showCash,
      bgLogin, // 提现身份验证
      closeIcon, // 关闭按钮
      errMsg: "", // 错误提示信息
      showGetCode: false, // 获取验证码
      codeText: "获取验证码", // 获取验证码文字
      countdown: 60, // 60秒
      form: {
        name: "15252140315",
        code: 99
      }
    };
  },
  created() {},
  methods: {
    closeCash() {
      this.$emit("on-cash-close", this.showCash);
    },
    onCheckName() {
      if (!verifyName(this.form.name)) this.errMsg = "请输入正确的手机号";
      else this.errMsg = "";
    },
    onCheckCode() {
      if (!verifyCode(this.form.code)) this.errMsg = "请输入正确的验证码";
      else this.errMsg = "";
    },
    onSubmit() {
      if (verifyName(this.form.name) && verifyCode(this.form.code)) {
        // 提交
        console.log(123);
      } else {
        if (!verifyName(this.form.name)) this.errMsg = "请输入正确的手机号";
        if (!verifyCode(this.form.code)) this.errMsg = "请输入正确的验证码";
      }
    },
    // 倒计时
    setTimeDown() {
      if (this.countdown == 0) {
        this.showGetCode = false;
        this.countdown = 60;
        this.codeText = "获取验证码";
        return;
      } else {
        this.showGetCode = true;
        this.codeText = this.countdown + "s";
        this.countdown--;
      }
      setTimeout(() => {
        this.setTimeDown();
      }, 1000);
    },
    // 获取验证码
    onGetCode() {
      this.setTimeDown();
    }
  }
};
</script>
// common.less

@btn-color: #ff4b59;
@border-color: #b53df9;
@txt-color3: #333;
@txt-color9: #999;
@txt-red: #ff0000;
@rule-bg: #ff4c5a;
@cash-input-bg: #f1f1f1;
@count-bg: #ffdf6f;
@lucky-bg: #7816ff;
@white: #fff;
@white50: rgba(255,255,255, 0.5);
@black-50: rgba(0, 0, 0, 0.5);
@black-27: rgba(0, 0, 0, 0.27);
@zero: 0;
@line-clamp: 2;
@size-03: 0.03rem;
@size-05: 0.05rem;
@size-10: 0.10rem;
@size-14: 0.14rem;
@size-16: 0.16rem;
@size-20: 0.20rem;
@size-24: 0.24rem;
@size-26: 0.26rem;
@size-28: 0.28rem;
@size-30: 0.30rem;
@size-32: 0.32rem;
@size-36: 0.36rem;
@size-50: 0.50rem;
@size-57: 0.57rem;
@size-58: 0.58rem;
@size-140: 1.4rem;
@size-156: @size-140 + @size-16;
@size-117: @size-57 + @size-30*2;
@size-127: @size-57 + @size-30*2 + @size-20/2;
@size-122: (@size-127) - (@size-05);
@size-401: 4.01rem;
@size-412: (@size-50*10+@size-58+@size-10)-(@size-156);
@size-470: @size-20*20+@size-50+@size-20;
.tx-center {
  text-align: center;
}
.whp100 {
  width: 100%;
  height: 100%;
}
.wp100 {
  width: 100%;
}
.hp100 {
  height: 100%;
}
.tfl-ofhide {
  overflow: hidden;
}
.tfl-block {
  display: block;
}
.tfl-inblock {
  display: inline-block;
}
.abscenter {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.absX {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
.absY {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
html, body {
  .whp100;
  max-width: 750px;
  margin: auto;
  font-size: 100px;
  background-color: @lucky-bg;
}

/** 提现验证身份 */
.taofuli-cash {
  .cash-bg,
  .cash-con {
    width: 79%;
    .tfl-block;
    .abscenter;
  }
  .cash-con {
    height: @size-50*10+@size-58+@size-10;
    .tfl-ofhide;
    .cash-title {
      height: @size-156;
      line-height: @size-156;
      text-align: center;
      font-size: @size-50;
      color: @white;
    }
    .cash-desc {
      height: @size-412;
      padding-top: @size-32;
      input,
      div.cash-box {
        .tfl-block;
        margin: auto;
        width: 81%;
        height: @size-20*4;
        border-radius: @size-20*2/10;
      }
      input.cash-name,
      div.cash-box {
        font-size: @zero;
      }
      input.cash-name {
        margin-bottom: (@size-24+@size-03);
      }
      div.cash-box input {
        .tfl-inblock;
      }
      input.cash-name,
      div.cash-box input {
        padding: 0 @size-50/2;
        font-size: @size-24;
        background-color: @cash-input-bg;
      }
      input.cash-code {
        width: 60%;
        margin-right: 3%;
      }
      div.cash-box input.cash-get,
      div.cash-box input.cash-got {
        width: 37%;
      }
      div.cash-box input.cash-get,
      input.cash-submit {
        background-image: linear-gradient(90deg,#7682ff 0%,#8c56ff 100%), linear-gradient(#816cff, #816cff);
	      background-blend-mode: normal,normal;
        color: @white;
      }
      div.cash-box input.cash-got {
        color: @txt-color9;
        background: @cash-input-bg;
      }
      input.cash-submit {
        margin: (@size-50)-(@size-05) auto 0;
        font-size: @size-30;
      }
      .cash-error {
        width: 81%;
        height: 0.18rem;
        margin: @size-20/2 auto 0;
        color: @txt-red;
        font-size: @size-20;
      }
    }
  }
}
.cash-close {
  bottom: @size-30*10+@size-05+@size-03;
}
// tools.js

export const verifyName = val => {
  let reg = /^1[3456789]\d{9}$/;
  if (!reg.test(val)) return false;
  else return true;
};
export const verifyCode = val => {
  let reg = /^\d{4}$/;
  if (!reg.test(val)) return false;
  else return true;
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值