<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\user\controller;
class Login extends Base
{
    public function index()
    {

        if($this->request->isAjax()){
            $param = $this->request->param();
// 数据验证

            if($param['type']  == 'account'){
//第一种 用户名/邮箱/手机
            
           //数据验证
           $rule =[
              'username'   =>'require|min:3',
              'password'   =>'require|min:6',
              'verify_code'=>'require',
            ];
            $msg= [
             'username.require' =>'请输入用户名',
             'username.min'     =>'用户名至少3个字符',
             'password.require' =>'请输入登陆密码',
             'password.min'     => '密码至少6位数',
             'verify_code.require' =>'请输入验证码',
            
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             } 

            $data['username']    = $param['username'];   //用户名
            $data['password']    = $param['password'];   //密码

           $data['verify_code']    = $param['verify_code'];   //用户名
//判断验证码是否正确
            if(!captcha_check($param['verify_code'])){
            return error_json('验证码错误');
            };
        // 正则匹配
              if(preg_match("/^(13|14|15|18)[0-9]{9}$/",$data['username'])){
          //手机登陆
                     $where['mobile']  = $data['username'];
                     $where['is_mobile']  = 1;
                          
                }elseif(preg_match("/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,30}/",$data['username'])){
        //邮箱登录
                     $where['email']  = $data['username'];
                     $where['is_email']  = 1;

                 }elseif(preg_match("/^(?!(\d+)$)[a-zA-Z0-9_\x{4e00}-\x{9fa5}]{3,15}$/u",$data['username'])){
                 
     //最后匹配，用户名登陆
                     $where['username']  = $data['username'];

                 }else{
                   return error_json('请输入正确的账号格式');
                 }


                   
                    $user = model('user')->infoData($where);

                     if(empty($user)){
                        return error_json('该用户不存在！');
                     } 
                     
                     if($user['status'] ==-1){
                       return error_json('当前账号禁止登陆');
                     }
                     
                     if($user['status'] ==0){
                       return error_json('当前账号等待审核中');
                     }   
                     $password = password_key($user['username'],$data['password'],$user['passsalt']); //动态生成的密码
                     if($password != $user['password']){
                        return error_json('账号或密码错误');
                     }


                    session('uid',$user['uid']);
                    session('username',$user['username']);
         

                    $uid= $user['uid'];
                   //更新登陆时间和IP
                   $info['login_time'] = time();
                   $info['login_ip']   = get_client_ip();      
                   model('user')->updateInfo($uid,$info);
                   return  success_json('登录成功');


            }

        }else{
            return $this->fetch();
        }
      
    }


 
}
