<?php
/**
*------Create thems Model------
*------SCWPHP  Version 1.0.0------
*------Dev Model Jions------
*------Create Time 2017-10-24 06:31:07------
*/
namespace App\Commons\Model;

use \System\Model;

class SystemUser extends Model
{

    public $timestamps = false;
    protected $table = 'system_user';
    function checkUser($check,$group=array(1))
    {
        if(strstr($check,'||')===false){
            return -6007;
        }
        $user=explode('||',$check);
        if(!isset($user[0]) || !isset($user[1])){
            return -6008;
        }
        if(empty($user[0]) || empty($user[1])){
            return -6009;
        }
        if(!isset($user[2]) || empty($user[2])){
            return -6011;
        }
        $data=$this->where('loginname',$user[0])->whereIn('root_id',$group)->first();       

        if(!$data){
            return -6010;
        }
        // print_r($user[0] . '||' . $data->password . '||' . $user[2].'||'. REMOTE_KEY);
        $token = md5(md5($user[0] . '||' . $data->password . '||' . $user[2].'||'. REMOTE_KEY));
        if ($token!=trim($user[1])){
            return -6012;
        }
        return 1;
    }

    function checkToken($check){
       $re=$this->checkRemoteKey($check);
       if($re<1){
           $re=$this->checkUser($check);
       }
       return $re;
    }


    function checkRemoteKey($check){
        if (strstr($check, '||') === false) {
            return -6007;
        }
        $user = explode('||', $check);
        if (!isset($user[0]) || !isset($user[1])) {
            return -6008;
        }
        if (empty($user[0]) || empty($user[1])) {
            return -6009;
        }
        $token = md5(md5(REMOTE_KEY . '||' . $user[1]));
        if ($user[0] != $token) {
            return -6012;
        }
        return 1;
    }
}
