<?php
namespace app\adminghd\common;
use think\Controller;
use think\Session;
// use think\Request;
// use think\Db;
use think\Response;

class Base extends Controller
{
    protected function _initialize()
    {
    	if(!session('adminghd_user_id')){//没有登录信息
    	//	$this->redirect('login/index');
    		return $this->error('您还未登录',url('Login/logoutJump'));
    		//Response::create(['status' => '0','msg'=> '还未登录，验证失败'], 'json')->send();
    		exit;
    	}else{
    		//登录信息超时
    		$log_time=session('adminghd_user_login_time')-time();
    		if($log_time < 0){
    			Session::clear();
    			//Response::create(['status' => '0','msg'=> '登录超时，需重新登陆'], 'json')->send();
    			return $this->error('登录超时',url('Login/logoutJump'));
    			exit;
    		}
    	}
    }
    //简单的token生成函数
  /*  protected function create_token()
    {
        $str = md5(uniqid(md5(microtime(true)), true)); //生成一个不会重复的字符串
        $str = sha1($str); //加密
        return $str;
    }
    //验证Token
    public function checkToken($token)
    {
        $user = Db::name('user')->where('token', $token)->find();
        if (empty($user)) {
            return ['status'=>0,'msg'=>'token错误，验证失败'];
        }else{
            if (time() - $user['time_out'] > 0) {
                return ['status'=>0,'msg'=>'token长时间未使用而过期，需重新登陆'];
            }
            return ['status'=>1,'msg'=>'成功！','data'=>$user];
        }
    }
    //通过分组id获取分组名称
    public function getGroupName($id){
        $res = Db::name('group')->field('group_name')->where('group_id',$id)->find();
        if($res){
            return $res['group_name'];
        }else{
            // return json_encode(['status'=>0,'msg'=>'分组匹配不到']);
            return '';
        }
    }
    //通过城市id获取城市名称
    public function getCityName($id){
        $res = Db::name('city')->field('city_name')->where('city_id',$id)->find();
        if($res){
            return $res['city_name'];
        }else{
            // return json_encode(['status'=>0,'msg'=>'城市匹配不到']);
            return '';
        }
    }
    //通过工厂id获取工厂名称
    public function getFirmName($id){
        $res = Db::name('firm')->field('firm_name')->where('firm_id',$id)->find();
        if($res){
            return $res['firm_name'];
        }else{
            // return json_encode(['status'=>0,'msg'=>'工厂匹配不到']);
            return '';
        }
    }
    //通过品牌id获取品牌名称
    public function getBrandName($id){
        $res = Db::name('brand')->field('brand_name')->where('brand_id',$id)->find();
        if($res){
            return $res['brand_name'];
        }else{
            // return json_encode(['status'=>0,'msg'=>'品牌匹配不到']);
            return '';
        }
    }
    //通过代理商id获取代理商名称
    public function getVenderName($id){
        $res = Db::name('vender')->field('vender_name')->where('vender_id',$id)->find();
        if($res){
            return $res['vender_name'];
        }else{
            // return json_encode(['status'=>0,'msg'=>'代理商匹配不到']);
            return '';
        }
    }
    //通过角色id获取角色名称
    public function getRoleName($id){
        $res = Db::name('role')->field('role_name')->where('role_id',$id)->find();
        if($res){
            return $res['role_name'];
        }else{
            // return json_encode(['status'=>0,'msg'=>'角色匹配不到']);
            return '';
        }
    }*/
}
