<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use util\Auth;

class Login extends Controller
{
    
    public function index(){
        if(request()->isPost()){
            $data = input('post.');

            if (strlen($data['username'])>30 || strlen($data['username'])<6)
              {
                return error("用户名必须为6-30位的字符串");
              }
              if(preg_match("/^\d*$/",$data['username']))
              {
                return error("用户名必须包含字母,强度:弱");//全数字
              }
            if (strlen($data['password'])>30 || strlen($data['password'])<6){
                return error("密码必须为6-30位的字符串");
              }
              if(preg_match("/^\d*$/",$data['password']))
              {
                return error("密码必须包含字母,强度:弱");//全数字
              }
              if(preg_match("/^[a-z]*$/i",$data['password']))
              {
                return error("密码必须包含数字,强度:中");//全字母
              }
              if(!preg_match("/^[a-z\d]*$/i",$data['password']))
              {
                return error("密码只能包含数字和字母,强度:强");//有数字有字母 ";
              }



              $user=Db::name('adminuser')->where('username|mobile',$data['username'])->field('a.id,a.username,a.password,a.pic,a.status,w.title')->alias('a')->join('auth_group w','a.auth_group_id = w.id','RIGHT')->find();

            $admin_user = Db::name('adminuser')->field('id,username,status')->where($where)->find();

            
            $captcha = input('verify');
            if(!captcha_check($captcha)){
                $data['status'] = 202;
                 $data['msg'] = '验证码错误!';
                  return json($data);
            }else{
             
                 
            if($user){
                if($user['password'] == md5($data['password'])){
                    //写入Session
                    session('uid',$user['id']);
                    session('username',$user['username']);
                    session('pic',$user['pic']);
                    session('title',$user['title']);
                    
                    // 判断权限
                    if ($user['status'] == 0) {
                         return error('当前用户已禁用');
                    } 
                    if(!empty($_SESSION)){
                        
                        $data['status'] = 200;
                        $data['msg'] = '登录成功!';
                        $data['url'] = url('/admin');
                        return json($data);
                    }else{
                     //返回错误信息
                    $data['status'] = 202;
                    $data['msg'] = '登录失败!';
                    return json($data);
                    }
                }else{
                    //返回错误信息
                    $data['status'] = 202;
                    $data['msg'] = '登录密码错误!';
                    return json($data);
                }
            }else{
                //返回错误信息
                $data['status'] = 202;
                $data['msg'] = '用户不存在!';
                return json($data);
            }
          }
           

        }else{
           
            $site['icp']=db('system')->where(array('enname'=>'icp'))->value('value');
           $site['sitename']=db('system')->where(array('enname'=>'sitename'))->value('value');
            $this->assign([
            'site'=>$site,
            
           ]);
            return view();
        }
    }

   

   
}
