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 方法负责触发发送短信和启动倒计时。
相关推荐
2501_9151063217 小时前
iPhone 文件管理,如何进行应用沙盒文件查看
android·ios·小程序·https·uni-app·iphone·webview
2501_915921431 天前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
云游云记1 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue
2501_915921431 天前
iOS 抓包怎么绕过 SSL Pinning 证书限制,抓取app上的包
android·网络协议·ios·小程序·uni-app·iphone·ssl
予你@。2 天前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app
HashTang3 天前
【AI 编程实战】第 11 篇:让小程序飞起来 - 性能优化实战指南
前端·uni-app·ai编程
lruri3 天前
记录一个修复nvue文件在vscode里面提示ts-plugin报错
uni-app
蓝帆傲亦3 天前
Web前端Mock数据实战指南:正确使用Mock.js提升开发效率
微信小程序·小程序·uni-app
00后程序员张3 天前
iOS 应用代码混淆,对已编译 IPA 进行类与方法混淆
android·ios·小程序·https·uni-app·iphone·webview
木子啊3 天前
Uni-app社会化功能:登录支付分享全攻略
uni-app