(已解决)vueQQ邮箱注册发送验证码前端设计,如何发送验证码设计倒计时

我们之前已经通过前端测试成功完成qq邮箱动态验证码发送(未使用redis,我准备自己了解完后,后期有时间补上)

衔接文章:


1:

spingboot 后端发送QQ邮箱验证码

2:


这段代码建设图形化界面

 <div class="container">
              <button
                @click="toggleCaptchaButton"
                :class="{ 'disabled': isSending || isCounting }"
                :style="{ display: displayStyle }"
                class="itemcode-seconde"
              >
                <span
                  v-if="isSending"
                  class="f-size"
                >发送中..</span>
                <span
                  v-else-if="isCounting"
                  class="f-size"
                >{{ countdown }}s</span>
                <span
                  v-else
                  class="f-size"
                >发送</span>
              </button>
            </div>

(效果如图)


js代码:
 

import router from "../../router";
import axios from "axios";
export default {
  data() {
    return {
      // 邮箱
      email: [],
      //输入的验证码
      Captcha: [],
      // 用户登录
      user: {
        userName: "",
        userPwd: "",
      },
      //接收到的验证码
      EmailCode: {
        EmailCode: "",
      },
      // token验证数据
      token: "", // 将token存储为一个字符串或数字
      // 动态隐藏登录框
      isHidden: false,
      amHidden: false,
      //验证码变换
      isSending: false, // 是否正在发送验证码
      isCounting: false, // 是否正在倒计时
      countdown: 0, // 倒计时时间(秒),初始化为0,只在倒计时开始时设置为60
      countdownInterval: null, // 用于存储定时器的ID
    };
  },
  computed: {
    // 动态隐藏登录框赋值
    displayStyle() {
      return this.isHidden ? "none" : "block";
    },
    displaystyles() {
      return this.amHidden ? "block" : "none";
    },
  },
  created() {
    // 设置默认值,当组件初始化时,isHidden为false,displayStyle为'block'
    this.isHidden = true;
    this.amHidden = true;
  },
  methods: {
    handleSubmit(event) {
      event.preventDefault(); // 阻止表单提交的默认行为
      this.toggleCaptchaButton(); // 调用发送验证码的方法
    },
    // 动态隐藏登录框
    toggleDisplay() {
      this.isHidden = !this.isHidden;
      this.amHidden = !this.amHidden;
    },
    // 发送注册请求
    enrollData() {
      if ((this.Captcha = this.EmailCode.EmailCode)) {
        axios
          .post("http://localhost:8080/enroll", JSON.stringify(this.email), {
            headers: {
              "Content-Type": "application/json",
            },
          })
          .then((response) => {
            this.Captcha = response.data.data;
            this.$message({
              message: "验证成功!",
              type: "success",
            });
          });
      } else {
        this.$message({
          message: "验证失败,",
          type: "error",
        });
      }
    },
    // 验证码变换+发送验证码请求
    toggleCaptchaButton() {
      // 发送验证码请求
      // 检查是否正在发送请求或倒计时中
      if (this.isSending || this.isCounting) {
        console.log("验证码请求或倒计时中,请稍后再试");
        return; // 提前返回,避免重复执行
      }
      axios
        .post("http://localhost:8080/mail", JSON.stringify(this.email), {
          headers: {
            "Content-Type": "application/json",
          },
        })
        .then((response) => {
          //   成功
          const EmailCode = response.data.data;
          this.EmailCode.EmailCode = EmailCode;
          console.log("请求mail已经成功接受到验证码" + EmailCode);
        })
        .catch((error) => {
          // 网络请求错误或后端返回非2xx的响应
          console.error(error);
        });
      // 验证码变换
      if (!this.isSending && !this.isCounting) {
        this.isSending = true; // 开始发送验证码,设置为不可点击状态
        // 模拟发送验证码的过程
        setTimeout(() => {
          this.isSending = false; // 发送完成
          this.startCountdown(); // 调用倒计时
        }, 2500);
      }
    },
    startCountdown() {
      this.isCounting = true; // 开始倒计时
      this.countdown = 60; // 设置倒计时时间为60秒
      this.countdownInterval = setInterval(() => {
        this.countdown--; // 倒计时减1秒
        if (this.countdown <= 0) {
          this.stopCountdown(); // 倒计时结束,调用停止倒计时的方法
        }
      }, 1000); // 每秒更新一次倒计时时间
    },
    stopCountdown() {
      clearInterval(this.countdownInterval); // 清除定时器
      this.isCounting = false; // 倒计时结束
      this.countdown = 0; // 重置倒计时时间为0
    },

    //登录请求(点击)
    submitData() {
      axios
        .post("http://localhost:8080/login", JSON.stringify(this.user), {
          headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + this.token,
          },
        })
        .then((response) => {
          const token = response.data.data;
          // 将token存储到组件的data属性中
          this.token = token;
          if (
            response.data &&
            response.data.code === 0 &&
            response.data.msg === "错误!"
          ) {
            this.$message({
              message: "登录失败," + response.data.msg,
              type: "error",
            });
          } else {
            this.$message({
              message: "登录成功!",
              type: "success",
            });
            router.push({
              path: "/index",
              query: { token: { token } },
            });
          }
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
  beforeDestroy() {
    if (this.countdownInterval) {
      clearInterval(this.countdownInterval); // 组件销毁前清除定时器
    }
  },
};
</script>

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WinForm 中实现发送邮箱验证码倒计时按钮可以通过以下步骤完成: 1. 在窗体中添加一个按钮和一个文本框,用于输入邮箱地址和显示倒计时信息。 2. 在按钮的 Click 事件中,先进行邮箱地址的验证,如果验证不通过,则弹出提示框。 3. 如果验证通过,则调用发送邮件的方法,并开启一个计时器,用于实现倒计时功能。 4. 在计时器的 Tick 事件中,更新倒计时信息,并在倒计时结束时停止计时器。 以下是示例代码: ```csharp private int countDown = 60; // 倒计时时间(秒) private Timer timer = new Timer(); private void btnSend_Click(object sender, EventArgs e) { string email = txtEmail.Text.Trim(); if (!Regex.IsMatch(email, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")) { MessageBox.Show("邮箱地址格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // 调用发送邮件的方法 SendEmail(email); // 开启计时器 countDown = 60; timer.Interval = 1000; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } private void timer_Tick(object sender, EventArgs e) { countDown--; if (countDown == 0) { timer.Stop(); btnSend.Enabled = true; btnSend.Text = "发送验证码"; } else { btnSend.Enabled = false; btnSend.Text = string.Format("{0}秒后重发", countDown); } } ``` 在上述代码中,SendEmail 方法需要根据具体情况进行实现,用于发送邮件验证码。在计时器的 Tick 事件中,根据倒计时时间更新按钮的文本和状态,并在倒计时结束时停止计时器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值