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 方法负责触发发送短信和启动倒计时。
相关推荐
游戏开发爱好者82 小时前
H5 混合应用加密 Web 资源暴露到 IPA 层防护的完整技术方案
android·前端·ios·小程序·uni-app·iphone·webview
2501_915106322 小时前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone
游戏开发爱好者83 小时前
HTTPS DDoS 排查 异常流量到抓包分析
网络协议·ios·小程序·https·uni-app·iphone·ddos
一点晖光3 小时前
小程序中web-view加载uni-app H5如何使用postMessage方法的解决方案
前端·小程序·uni-app
2501_915918414 小时前
iOS 性能监控 运行时指标与系统行为的多工具协同方案
android·macos·ios·小程序·uni-app·cocoa·iphone
qq_424409194 小时前
uniapp,通过webview内嵌h5页面,如何修改h5的大小
uni-app
00后程序员张4 小时前
IPA 混淆技术全解,从成品包结构出发的 iOS 应用安全实践与工具组合
android·安全·ios·小程序·uni-app·cocoa·iphone
郑州光合科技余经理5 小时前
定制开发实战:海外版外卖系统PHP全栈解决方案
java·服务器·开发语言·javascript·git·uni-app·php
2501_916008895 小时前
IOScer 证书到底是什么和怎么使用的完整说明
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张5 小时前
iOS 抓包工具实战指南,从代理到数据流,全流程工具分工解析
android·ios·小程序·https·uni-app·iphone·webview