<?php
//模块公共函数
namespace app\index\controller;

use \think\Controller;
use \think\Loader;
use \think\Db;
use \think\Session;

class Commone extends Controller
{
    protected function _initialize()
    {

        $web_config = Db::name('config')->where('id',1)->find();
        $this->assign('web_config',$web_config);
       

    }
    
    public function AI(){
        if(!Session::has('user_id')) {
           $this->redirect('index/index/login'); 
        }
    }
    
    public function ADMIN_AI(){
        if(config('cate')!='1') {
            exit($this->fetch('/error'));  
        }else{
           $cate_id = $this->get_user('cate_id'); 
           if($cate_id == '0'){
              exit($this->fetch('/error'));     
           }
        }
    }
    public function INDEX_AI(){
        $cate = $this->get_user('cate_id');
        if(config('cate')!='1' && $cate!='1') {
              exit($this->fetch('/error'));     
        }
    }
    
   
    
    public function get_user($a=""){
        
        $where['id'] = Session::get('user_id');
        $user = Db::name('user')->field($a)->where($where)->find();
        return $user[$a];
    }
    
    public function get_land($a){
        
        $where['id'] = $a;
        $land = Db::name('land')->where($where)->find();
        return $land;
    }
    
    
    public function get_game($a){
        
        $where['id'] = $a;
        $land = Db::name('game_list')->where($where)->find();
        return $land;
    }

    public function password($password, $password_code='lshi4AsSUrUOwWV')
    {
        return md5(md5($password) . md5($password_code));
    }

    public function getparam($arr){
        $data = $this->request->param();
        $newdata = [];
        foreach($arr as $k=>$v){
            if(isset($data[$v]) && $data[$v] !=""){
                $newdata[$v] = $data[$v];
            }
        }
        return $newdata;
    }
    
    public function json_error(string $message="发生错误",int $code=50050){
        throw new ApiException($code,$message);
    }
    
    public function json_success(string $msg=null, $data=[],string $url = null){
        $returnData = ['code'=>0,'msg'=>$msg];
        if($data){
            $returnData['result']=$data;
        }
        $response = \think\Response::create($returnData, 'json', 200);
        throw new \think\exception\HttpResponseException($response);
    }
    
    

}