<?php
namespace app\worker\controller;

use app\common\model\Worker as WorkerModel;
use Aliyun\DySDKLite\Sms\SendSms;

use App\Http\Controllers\Sdk\GeetestLib;

use think\facade\Session;
use think\facade\Db;

class Login 
{
	
	public function index()
	{
		return view('login/login',[
			'domain' => request()->domain()
		]);
	}

	//验证手机号
	public function yz_mobile()
    {

        $mobile = trim(input('mobile'));
        if($mobile){
            $data = Db::name('worker')
                    ->where('mobile',$mobile)
                    ->find();

            if($data['status'] == 2){
                $code = 1;
                $msg = '该账号已经被禁用,请联系管理员';
            }elseif(empty($data)){
                $code = 1;
                $msg = '该手机号未注册，请先注册';
            }else{
                $code = 0;
                $msg = '该手机号正确';
            }

            exit(json_encode(array(
                'code' 	=> $code,
                'msg'	=> $msg
            )));

        }else{
            $msg = '请填写正确的手机号';
        }


        exit(json_encode(array(
            'code' 	=> 1,
            'msg'	=> $msg
        )));
    }

    public function jy()
    {
        include("../extend/jy/geetest_config.php");
        include("../extend/jy/GeetestLib.php");

        $GtSdk = new \App\Http\Controllers\Sdk\GeetestLib(GEETEST_ID, GEETEST_KEY);


        $get_ip = get_client_ip();
        $user_id = 'login';
        $digestmod = "md5";
        $params = array(
            "digestmod" => $digestmod,
            "user_id" => $user_id, # 网站用户id
            "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
            "ip_address" => $get_ip, # 请在此处传输用户请求验证时所携带的IP
        );
//        $status = $GtSdk->pre_process($data, 1);
        $result = $GtSdk->register($digestmod, $params);
        session(GeetestLib::GEETEST_SERVER_STATUS_SESSION_KEY, $result->getStatus());
        session("userId", $user_id);


       return $result->getData();

    }

    //短息验证码  发送
    public function yzm()
    {

            Session::delete('yzm_code');

            $rand = rand(000001,999999);

            $mobile = trim(input('post.mobile'));

            $con['rand'] = $rand;
            $con['tel'] = $mobile;

            $preg_phone='/^1[34578]\d{9}$/ims';

            $rtn = [];

            if(!$mobile)
            {

                $rtn['code'] = 1;
                $rtn['message'] = '手机号为空';

            }else{

                if(preg_match($preg_phone,$mobile)){

                    require_once('../extend/alidy/demo/SendSms.php');

                    $alisms = Db::name('alisms')
                             ->field('accesskeyid,accesskeysecret,signname,mobilelogin')
                             ->find(1);
                    $sendSms = new \Aliyun\DySDKLite\Sms\SendSms();
                    $res = get_object_vars($sendSms -> sendSms_yzm($con,$alisms));
                    
                    if($res['Message'] == 'OK')
                    {
                        $rtn['code'] = 0;

                        Session::set('yzm_code',$rand);

                    }

                }else{

                    $rtn['code'] = 1;
                    $rtn['message'] = '手机号格式不正确';

                }

            }


        return $rtn;

    }


    //验证码 验证
    public function yzm_verify()
    {
    	
    	
        $mobile = trim(input('mobile'));
        $data = Db::name('worker')
            ->where('mobile',$mobile)
            ->find();
        $yzm = (int)trim(input('yzm'));
        $yzm_code = Session::get('yzm_code');
        
     
        $res = [];
        if($data['status'] == 2){
            $res['code'] = 1;
            $res['message'] = '该账号已经被禁用,请联系管理员';
        }elseif(empty($data)){
            $res['code'] = 1;
            $res['message'] = '该手机号未注册，请先注册';
        }else{

            if($yzm == $yzm_code){
                $workeradmin = WorkerModel::where(['mobile'=>$mobile])->findOrEmpty()->toArray();
                session('access_worker', $workeradmin);
                $res['code'] = 0;
                echo  json_encode($res);
            }else{

                $res['code'] = 1;
                $res['message'] = '验证码不正确';
                echo  json_encode($res);

            }

        }

    }

	
	
	public function logout()
	{

		session(null); 
		return redirect(url('/login/index'));
	}

}
