手机短信验证码(后端加注释)

<?php
/**
 * Created by PhpStorm.
 * User: Chan
 * Date: 2018/1/27
 * Time: 8:03
 */

namespace app\index\controller;
use think\Controller;
use think\Exception;
use think\exception\PDOException;

class Account extends Controller {
    /**
     * 登陆控制器
     * @return mixed
     */
    public function login(){
        // 表单提交处理
        if(request()->isPost()) {
            // ①获取post数据
           $data=input('post.');
            // ②校验数据
            $num = session('num')?session('num')+1:1;
            session('num',$num);//次数加一
            //记录session值

            $validate=validate('Login');
//                print_r(session('num'));
//                exit();
            if(session('num')>=3){//设置验证规则
                if(!$validate->check($data)){
                    $this->error($validate->getError()) ;
                }//输入的格式判断‘完成’!!
            }else{
                if(!$validate->scene('captcha1')->check($data)){
                    $this->error($validate->getError());
                }
            }//数据格式判断'完成'!!!!!!



            // ③数据库查询
        try{
            $user = model('user')->checkLogin($data);
        }catch(Exception $e){

               return $this->error($e->getMessage());
        }
            // ④seesion保存信息
              $user=session('user',$user);
            // ⑤返回前端处理

            session('num','0');//登录成功置0
            return $this->redirect('index/index/index');
        }

        // 普通请求
        if(request()->isGet()) {
            // 渲染模板
            return $this->fetch();
        }
    }

    /**
     * 注册控制器
     * @return mixed
     */
    public function reg() {
        // 表单提交处理
        if(request()->isPost() && request()->isAjax()) {//第一次传值请求发送短信,验证手机号和验证码的正确性
            // ①获取post数据
            $data=input('post.');
            $type=input('post.type');

            if($type==1){    // ②校验数据type=1的时候,发送短信进行验证码的判定

                $validate=validate('Reginfo');   //手机号的注册
                if(!$validate->scene('reg')->check($data)){
                        $this->result('',0,$validate->getError());//验证数据格式
                    }
               try{//进行手机号是否重复注册的检测
                   $user=model('user')->checkReg($data);
               }catch(Exception $e){
                   return $this->error($e->getMessage());
               }

               session('phone',$data['phone']);  //这个用session保存下一个方法一起存入数据库
               //短信验证码生成代码如下
               $content=randMessage(4);//调用common里面的函数形成四个数随机验证码
               session('phoneMessage',$content);//给session中的$content变量赋值,session保存content
               sendMessage($data['phone'],$content);//发送短信验证码
               return $this->result('',1,'success');
            }

            if($type==2){//校验数据type=2的时候//第二次传值是注册的请求
                $phoneMessage = input('post.phoneMessage');
    //          $validate=validate('Reginfo');   //手机号的注册
                if(session('phoneMessage') == $phoneMessage){
                    return $this->result('',1,'phone captcha success');
                } else {
                    return $this->result('',0,'phone captcha error');
                }

            }

        }

        if(request()->isPost() && !request()->isAjax()) {
            $data = input('post.');
            if(session('phone') != $data['phone']) {
                $this->error('验证手机号码失败');
            }
            $this->redirect(url('account/reginfo'));
        }

        if(request()->isGet()) {
            // 渲染模板
            return $this->fetch();
        }
    }

    /**
     * 详细信息填写控制器
     */
    public function reginfo() {
        // 表单提交处理
        if(request()->isPost()) {
            // ①获取post数据
//            print_r(session('phone'));//手机号已经传过来了
//            exit();
            $data=input('post.');
           $birthday= strtotime($data['year']."-".$data['month']."-".$data['day']);
           $data['birthday']=$birthday;
            $data['phone']=session('phone');//把手机号放入$data
            // ②校验数据
                $validate=validate('Reginfo');   //手机号的注册
                  if(!$validate->scene('reginfo')->check($data)){

                         $this->error($validate->getError());//只需验证数据格式
              }
            // ③数据入库
            try {
                $res=model('User')->add($data);
            } catch (PDOException $e) {
                      print_r($e->getMessage());
                      exit();
            }
            $a=$res->usereduandwork()->save($data);
            $result = $res->userinfo()->save($data);//
               if(!$result){
                  $this->error($res->geterror());
              }
              print_r($result);
               exit();
            // ④返回前端处理
            return $this->redirect('index/index/index');
        }

        // 普通请求
        if(request()->isGet()) {
            // 渲染模板
            return $this->fetch();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/abc455050954/article/details/79209280