前端代码
function fn(){ //发送邮箱从而获取验证码, 这里利用了闭包达到了节流的目的
var flag=true
return function(){
if(flag) {
var count = 60
this.value = count + "秒后重新获取"
fetch('/user/varification?email=' + email.value) //将输入框中的邮箱发送过去
flag=false
that = this
var timer = setInterval(function () {
if (count > 0) {
count--
that.value = count + "秒后重新获取"
} else {
clearInterval(timer)
that.value = "请重新获取"
flag=true
}
}, 1000)
}
}
}
后端代码
@GetMapper("/user/varification")
public void sendEmail(String email, HttpSession session) { //生成验证码并通过邮箱发送
// 自定义纯数字的验证码(随机4位数字,可重复)
RandomGenerator randomGenerator = new RandomGenerator("0123456789", 6);
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
lineCaptcha.setGenerator(randomGenerator);
lineCaptcha.createCode(); // 重新生成字符串验证码
String code=lineCaptcha.getCode(); //获取验证码
System.out.println(code) //打印结果"152463"
session.setAttribute("varification",code); //将验证码存放在session中,方便进行验证
//可以设置session的存活事件
//将验证码通过邮箱发送
SimpleMailMessage ss=new SimpleMailMessage();
ss.setFrom("3529742868@qq.com"); //这个表示定义发送人
ss.setTo(email); //这个表示定义接收人
ss.setSubject("验证码"); //这个表示定义标题
ss.setText(""); //这个表示定义正文内容
jms.send(ss); //开始发送
}
该文章描述了一个前端函数用于触发邮箱验证码的获取,利用闭包实现节流功能。后端接收到请求后生成随机验证码,存储到HttpSession并发送至用户邮箱。
6918

被折叠的 条评论
为什么被折叠?



