手机验证码注册,登录

//前端登录页面表单及javascript+ajax传输

<input type="text" name="nickname"  placeholder="请输入名称" class="login-password">

<input type="text" name="mobile"  placeholder="请输入手机号" class="login-password">
<input type="text" class="reg-yzm" name="tel_code" maxlength="6" placeholder="输入验证码" />
<input type="button" οnclick="code();" id="login_time" class="yzm-hq" value="获取验证码" />
<input type="password" name="password"  placeholder="请输入(6-16字母、数字、下划线)密码" class="login-password">
<input type="password" name="passwords"  placeholder="确认密码" class="login-name">
<input type="button" class="login-btn" οnclick="check();" value="立即注册">

<script>
    function check(){
        var nickname = $('input[name = nickname]').val();
        var mobile = $('input[name = mobile]').val();
        var tel_code = $('input[name = tel_code]').val();
        var password = $('input[name = password]').val();
        var passwords = $('input[name = passwords]').val();
        var url = "{:url('index/user/mobileReg')}";
        var data = {'nickname':nickname,'mobile':mobile,'tel_code':tel_code,'password':password};
       if(nickname == ''){
              layer.open({
              content: '请输入昵称',
              skin: 'msg',
              time: 2
         });
         return false;
        }
        if(!mobile.match(/^1[34578]\d{9}$/)){
            layer.open({
                  content: '请输入正确的手机号',
                  skin: 'msg',
                  time: 2 //2秒后自动关闭
              });
            return false;
         }
        if(mobile == ''){
              layer.open({
              content: '请输入手机号',
              skin: 'msg',
              time: 2
         });
         return false;
        }
        if(tel_code == ''){
              layer.open({
              content: '请输入验证码',
              skin: 'msg',
              time: 2
         });
         return false;
        }
        if(password == '' || !password.match(/^(\w){6,16}$/)){
              layer.open({
              content: '请输入规范密码',
              skin: 'msg',
              time: 2
         });
         return false;
        }
        if(passwords == ''){
              layer.open({
              content: '请再次输入密码',
              skin: 'msg',
              time: 2
         });
         return false;
        }
        if(passwords != password){
              layer.open({
              content: '两次输入密码不一致',
              skin: 'msg',
              time: 2
         });
         return false;
        }
        $.post(url,data,function(result){
            if(result.flag){
                layer.open({
                      content: result.flag,
                      skin: 'msg',
                      time: 2
                });
            }else{
                alert(result.success);
                window.location.href=document.referrer;
            }
        },'json')

    }


    function code()
    {
        var mobile = $('input[name = mobile]').val();
        var url = "{:url('index/user/code')}";
        if(mobile == ''){
              layer.open({
              content: '手机号不能为空',
              skin: 'msg',
              time: 2
         });
         return false;
        }

        $.post(url,{'mobile':mobile},function(result){
            if(result.flag){
                layer.open({
                  content: result.flag,
                  skin: 'msg',
                  time: 2
             });
            }else{
                layer.open({
                  content: result.success,
                  skin: 'msg',
                  time: 2
             });
            }
        },'json');

        $('#login_time').attr('disabled',true);
        var num = 60;
        function each()
        {
            num--;
            if(num == 0)
            {
                $('#login_time').attr('disabled',false);
                $('#login_time').val(' 获取验证码');
            }else
            {
                $('#login_time').val('('+num+')');
                $('#login_time').attr('disabled',true);
                var time = setTimeout(each,1000);        // 延时定时器是隔多长时间后,执行,并且只执行一次   所以,放在这是让内部的函数
            }
        }
        var time = setTimeout(each,1000);
    }

</script>

//在config.php中写

return[

'JHAPI'=>[
            'key'=>'6e4b8c644************43b5f93d7f2',                     //  聚合数据短信验证api        appkey
            'tpl_id'=>'1***23',           //聚合数据模板id  
            'company'=>'***网',  //自定义
    ],

];

// 发送手机验证码


    public function code(){

        $param = $this->request->param();

        // 调用配置文件
        $API =  config('JHAPI');

        // 生成验证码
        $code = mt_rand(1000,9999);
        $smsConf = array(
        'key'       => $API['key'],      //您申请的APPKEY
        'mobile'    => $param['mobile'],    //接受短信的用户手机号码
        'tpl_id'    => $API['tpl_id'],   //您申请的短信模板ID,根据实际情况修改
        'tpl_value' =>urlencode('#code#='.$code.'&#company#='.$API['company'])
       );

        $data['mobile'] = $param['mobile'];
        $data['code'] = $code;
        $data['create_time'] = time();
        $url = 'http://v.juhe.cn/sms/send?mobile='.$smsConf['mobile'].'&tpl_id='.$smsConf['tpl_id'].'&tpl_value='.$smsConf['tpl_value'].'&key='.$smsConf['key'];
        //请求发送短信
        // $result = curt_url($url);

        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $contents = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($contents,true);

        if($result['error_code'] == 0){
            Db::name('user_api')->insert($data);
            echo json_encode(array('success'=>'发送成功'));
            exit;
        }else{
            echo json_encode(array('flag'=>'发送失败,错误码:'.$result['error_code']));
            exit;
        }
    }

//登录验证

public function mobileReg(){
        $param = $this->request->param();

        $res = Db::name('user_api')
              ->where(['mobile'=>$param['mobile']])
              ->order('create_time DESC')
              ->find();

        if($res['mobile'] != $param['mobile']){
            echo json_encode(['flag'=>'手机号有误']);
            exit;
        }
        if($res['code'] != $param['tel_code']){
            echo json_encode(['flag'=>'验证码有误']);
            exit;
        }
        $difftime = time() - $res['create_time'];
        if($difftime > 120){
            echo json_encode(['flag'=>'验证码过期']);
            exit;
        }
        $count = Db::name('user')
                  ->where(['mobile'=>$param['mobile']])
                  ->count();

        if($count>0){
            echo json_encode(['flag'=>'账号已存在']);
            exit;
        }else{
            $data = [];
            $data['usertype'] = 2;
            // $data['last_login_ip'] = $_SERVER['HTTP_CLIENT_IP'];
            $data['last_login_time'] = time();
            $data['create_time'] = time();
            $data['status'] = 1;
            $data['mobile'] = $param['mobile'];
            $data['nickname'] = $param['nickname'];
            $data['password'] = $param['password'];
            $result = Db::name('user')
                    ->insertGetId($data);

            if(is_numeric($result)){
                echo json_encode(['success'=>'注册成功']);
                exit;
            }else{
                echo json_encode(['flag'=>'注册失败']);
                exit;
            }
        }


    }

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值