vue+elementui 实现点击验证码出现60秒倒计时

vue+elementui 实现点击验证码出现60秒倒计时

div部分

 <div class="verCode
 	@click="getVerCode"
	:disabled="disable
	<span>{{buttonName}}</span
</div>

js部分

 <script>
        new Vue({
            el: "#app",
            data: {
                @*获取验证码*@
                buttonName: "获取验证码",
                count: 59,
                disable: false,
               	……省略ruleForm验证
            },
             methods: {
                @*获取手机验证码*@
                getVerCode() {
                    console.log('*', this.ruleForm.phone.length)
                    if (this.ruleForm.phone == '') {
                        this.$message({
                            message: '手机号不能为空',
                            type: 'warning'
                        });
                    } else if (this.ruleForm.phone.length != 11) {
                        this.$message({
                            message: '请输入11位的手机号码',
                            type: 'warning'
                        });
                    } else {
                        @this.abc()   abc方法是点击验证码接口*@
                        console.log('点击了', this.disable)
                        if (this.disable == true) {
                            this.$message({
                                message: '请勿重复点击',
                                type: 'warning'
                            });
                        } else {
                            var timeout = setInterval(() => {
                                if (this.count < 1) {
                                    this.disable = false;
                                    this.buttonName = "获取验证码";
                                    this.count = 59;
                                    clearInterval(timeout);
                                } else {
                                    this.disable = true;
                                    this.buttonName = this.count-- + "s后重发";
                                }
                            }, 1000);
                        }
                    }
                },
			},
		})
### 关于 Spring Boot 和 Vue验证码生成的实现 在构建基于 Spring Boot 和 Vue 的应用时,为了增强系统的安全性,通常会加入验证码机制。这可以通过多种方式来完成,比如短信验证码[^2]、邮箱验证码[^4]以及图形或滑动模块验证码[^5]。 #### 后端部分 (Spring Boot) 对于后端而言,在 `pom.xml` 文件中添加必要的依赖项是第一步操作: ```xml <dependencies> <!-- 验证码发送服务所需的验证支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!-- 如果涉及邮件发送,则需此依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- Redis缓存用于保存临时验证码 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> ``` 接着定义一个简单的 RESTful API 来处理验证码请求。这里以电子邮件为例展示如何创建这样的接口: ```java @RestController @RequestMapping("/api/v1/auth") public class EmailController { @Autowired private JavaMailSender emailSender; /** * 发送带有随机验证码的邮件给指定地址。 */ @PostMapping("/send-email-code") public ResponseEntity<String> sendEmailCode(@RequestParam String emailAddress) { try { // 生成六位数随机验证码 int code = new Random().nextInt(899999) + 100000; MimeMessage message = emailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(emailAddress); helper.setSubject("您的验证码"); helper.setText(String.format("您好,\n\n这是您申请的服务验证码:%d", code)); emailSender.send(message); // 将验证码存储到Redis中以便后续校验 redisTemplate.opsForValue().set(emailAddress, Integer.toString(code), Duration.ofMinutes(5)); return ResponseEntity.ok("已成功发送验证码至 " + emailAddress); } catch (MessagingException e) { throw new RuntimeException(e.getMessage()); } } } ``` 上述代码片段实现了向用户提供电子邮箱地址的情况下发送包含唯一验证码的消息,并将其短暂地储存在 Redis 缓存里等待进一步确认。 #### 前端部分 (Vue.js) 前端方面则主要负责发起获取验证码的 HTTP 请求并将结果显示出来供用户查看。下面是一个简化版的例子说明怎样利用 Axios 库来进行网络通信: ```javascript // main.js 或者相应的入口文件 import axios from 'axios'; Vue.prototype.$http = axios; // 组件内调用方法 methods: { async requestVerificationCode() { const response = await this.$http.post('/api/v1/auth/send-email-code', null, { params: { emailAddress: this.email } }); alert(response.data); // 显示服务器返回的信息 console.log('Response:', response); }, }, ``` 此外还可以考虑集成一些 UI 框架如 ElementUI 提供更友好的交互体验,例如倒计时按钮防止频繁点击触发过多次请求等特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值