uniapp验证码

一、 页面结构

假设你有一个发送短信按钮,点击按钮时会触发发送短信并启动倒计时。

bash 复制代码
<template>
  <view>
    <button @click="sendSms" :disabled="isSending">{{ buttonText }}</button>
  </view>
</template>

二、脚本部分

在脚本中,定义一个倒计时的变量 time 和控制按钮状态的 isSending。

bash 复制代码
<script>
export default {
  data() {
    return {
      isSending: false, // 是否正在发送短信
      time: 60,         // 倒计时时间
      buttonText: '发送验证码' // 按钮文本
    };
  },
  methods: {
    // 发送短信的方法
    sendSms() {
      if (this.isSending) return; // 防止重复点击
      this.isSending = true;  // 设置为发送状态
      this.buttonText = `${this.time}s后重新获取`;

      // 启动倒计时
      const countdown = setInterval(() => {
        this.time--;
        this.buttonText = `${this.time}s后重新获取`;

        // 如果倒计时结束
        if (this.time <= 0) {
          clearInterval(countdown); // 清除定时器
          this.isSending = false;  // 恢复按钮
          this.time = 60;          // 重置倒计时
          this.buttonText = '发送验证码'; // 重置按钮文本
        }
      }, 1000);

      // 这里可以调用发送短信的接口
      // 假设发送短信成功后,继续倒计时
      // this.sendSmsApi();
    }
  }
};
</script>

三、样式部分(可选)

你可以为按钮和倒计时文本添加一些简单的样式,使其更直观。

bash 复制代码
<style scoped>
button {
  background-color: #007aff;
  color: #fff;
  padding: 10px 20px;
  border-radius: 5px;
}

button:disabled {
  background-color: #b0b0b0;
}
</style>

说明:

复制代码
isSending 控制按钮是否可点击,防止用户在倒计时期间重复点击。
time 用于记录倒计时的秒数,从 60 秒开始。
每秒通过 setInterval 更新按钮文本,并在倒计时结束时恢复原状态。
sendSms 方法负责触发发送短信和启动倒计时。
相关推荐
特立独行的猫a2 分钟前
跨平台开发实战:uni-app x 鸿蒙HarmonyOS网络模块封装与轮播图实现
android·网络·uni-app·harmonyos·轮播图·uni-app-x
iOS阿玮18 小时前
开工第一天,别让AI写的代码触发3.2f封号。
uni-app·app·apple
特立独行的猫a1 天前
uniapp-x的HarmonyOS鸿蒙应用开发:tabbar底部导航栏的实现
华为·uni-app·harmonyos·鸿蒙·uniapp-x
迪巴拉15251 天前
基于Yolov8训练的Flask后端和Uniapp野生菌识别系统
yolo·flask·uni-app
中国胖子风清扬7 天前
GPUI 在 macOS 上编译问题排查指南
spring boot·后端·macos·小程序·rust·uni-app·web app
码云数智-园园8 天前
uni-app 实现物流进度跟踪功能:从 UI 到数据驱动的完整方案
ui·uni-app
予你@。10 天前
UniApp + Vue3 实现 Tab 点击滚动定位(微信小程序)
微信小程序·小程序·uni-app
游戏开发爱好者810 天前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
予你@。10 天前
uni-app progress 组件使用详解
uni-app
iOS阿玮10 天前
春节提审高峰来袭!App Store 审核时长显著延长。
uni-app·app·apple