php 函数

        //发送邮件验证码    
        public function send_email_register_code(Request $request){
            $toEmail = $request->get('email');
            if(!filter_var($toEmail, FILTER_VALIDATE_EMAIL)){
                return Y::error('邮箱验证失败!',["code"=>"ERROR_FORMAT"]);
            }
            //判断数据库是否有30分钟内没验证的code
            $emailCode = EmailCode::where(['email'=>$toEmail])->where(['status'=>1])->where('created_at', '>', date("Y-m-d H:i:s", strtotime("-3 minute")))->take(1)->orderBy('id','desc')->get();
            if(!$emailCode->isEmpty()){
                return Y::error('请不要重复发送!',["code"=>"ERROR_REPEAT"]);
            }
            //生成6位code,保存email code表
            $code =  mt_rand(100000,999999);
            $data = array(
                 'uid'=> 0,
                 'email'=> $toEmail,
                 'code'=> $code,
                 'type'=> 'register',
                 'status'=> 1
            );
            $createData = EmailCode::create($data);
            if(!$createData){
                return Y::error('发送失败,请联系管理员!',["code"=>"ERROR_CREATE"]);
            }
            //send发模板 raw 发送文本
            $subject = "江苏ak有限公司邮件验证信息";
            Mail::send('home.member.account.email_register_code',['code'=>$code],function($message) use ($toEmail, $subject){
                $message ->to($toEmail)->subject($subject);
            });
            return Y::success('发送邮件成功!请查收!',["code"=>"SUCCESS"]);
        }

前台js点击倒计时

    // 定时器    
        var wait = 180;
        get_code_time = function (o) {
            if (wait == 0) {
                o.removeAttribute("disabled");
                o.value = "获取验证码";
                wait = 180;
            } else {
                o.setAttribute("disabled", true);
                o.value = "(" + wait + ")秒后重新获取";
                wait--;
                setTimeout(function () {
                    get_code_time(o)
                }, 1000)
            }
        };
        $("#emailSendBtn").click(function () {
            var email = $.trim($("#user_email").val());
            var o = this;
            if (isEmail(email) == false) {
                alert_message('提示','请输入合法的电子邮箱!',null,function(){
                    close_alert_div();
                    $("#user_email").focus();
                });
                return false;
            }
            //发送验证码
            $.ajax({
                url: "{{route("send_email_register_code")}}",
                data: { "_token": "{{ csrf_token() }}", "email": email },
                type: "post",
                dataType: 'json',
                success:function (data) {
                    close_loading_div();
                    if(data.data.code == "SUCCESS"){
                        get_code_time(o);
                    }
                    alert_message('提示',data.msg,null,function(){
                        close_alert_div();
                    });
                },
                error:function (data) {
                    close_loading_div();
                    alert_message('提示',data.msg,null,function(){
                        close_alert_div();
                    })
                }
            });
        });
            //验证邮箱
            function isEmail(email) {
                var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                return email_reg.test(email);
            }