Base.php 5.25 KB
<?php

/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */

namespace app\admin\controller;
use app\admin\logic\UpgradeLogic;
use think\Controller;
use think\Db;
use think\response\Json;
use think\Session;
class Base extends Controller {

    /**
     * 析构函数
     */
    function __construct() 
    {

        Session::start();
        parent::__construct();
        $upgradeLogic = new UpgradeLogic();
        //$upgradeMsg = $upgradeLogic->checkVersion(); //升级包消息
        //$this->assign('upgradeMsg',$upgradeMsg);
        //用户中心面包屑导航
        $navigate_admin = navigate_admin();
        $this->assign('navigate_admin',$navigate_admin);

        //腾讯云地址前缀
        $qclurl=QCLOUD_IMGURL;
        $this->assign('qclurl',$qclurl);
        //获取页面地址
        getlasturl();
        //tpversion();
   }    
    
    /*
     * 初始化操作
     */
    public function _initialize() 
    {

        $this->request->isAjax() ? define('IS_AJAX',true) : define('IS_AJAX',false);  // 
        ($this->request->method() == 'GET') ? define('IS_GET',true) : define('IS_GET',false);  // 
        ($this->request->method() == 'POST') ? define('IS_POST',true) : define('IS_POST',false);  // 
        
        define('MODULE_NAME',$this->request->module());  // 当前模块名称是
        define('CONTROLLER_NAME',$this->request->controller()); // 当前控制器名称
        define('ACTION_NAME',$this->request->action()); // 当前操作名称是
        define('PREFIX',C('database.prefix')); // 数据库表前缀
        $this->assign('action',ACTION_NAME);

        $this->assign('p_erpid',getERPId());//当前ERP

        $r=M('manager_config')->find();
        $arr= explode(',',$r['storelist']);
            if($r['site_status']==1 && !in_array(getAdmStoId(),$arr) &&  !in_array(ACTION_NAME,array('get_post'))
            && !in_array(CONTROLLER_NAME,array('Ueditor','Uploadify','Wechat'))    ) {
                $this->redirect('/up_system.html');
            }


        //过滤不需要登陆的行为
        if(in_array(ACTION_NAME,array('get_post','notify')) || in_array(CONTROLLER_NAME,array('Ueditor','Uploadify','Wechat'))){
        	//return;
        }else{
            $sid=getAdmStoId();
            if(empty($sid))
                $this->redirect('/err.html');
        }

        $this->public_assign();
        /*--访问到聊天数据库,超时时间为15分钟--*/
        $out=strtotime("+15 minute");
        $dda['login_timeout']=$out;
        $is_chat = tpCache("basic.is_chat", getAdmStoId());
        $is_server=session('is_server');
        if($is_chat && $is_server) {
            $chat_db=Db::connect('chat_db');
            $rs=$chat_db->name('chat_login_state')->where('admin_id',getAdminId())->find();
            if ($rs ){
                $dda['admin_ip'] = $rs['admin_id'] . '111';
                $chat_db->name('chat_login_state')->where('admin_id', getAdminId())->save($dda);
            }

        }


    }
    
    /**
     * 保存公告变量到 smarty中 比如 导航 
     */
    public function public_assign()
    {
       $tpshop_config = array();
       $tp_config = D('store')->alias('a')->join('store_config b','b.store_id= a.store_id','left')->join('store_distribut c',' c.store_id=a.store_id','left')  ->where("a.store_id",getAdmStoId())->find();

       $tpshop_config['basic_default_storage'] = $tp_config['default_storage'];
       $this->assign('tpshop_config', $tpshop_config);
       $this->assign('tpshop_shopname', $tp_config['store_name']);
       $this->assign('tpshop_logo', $tp_config['store_logo']);
    }

    public function check_priv()
    {
    	$ctl = CONTROLLER_NAME;
    	$act = ACTION_NAME;
        $act_list = session('act_list');
		//无需验证的操作
		$uneed_check = array('login','logout','vertifyHandle','vertify','imageUp','upload','login_task');
        $role_right='';
    	if($ctl == 'Index' || $act_list == 'all'){
    		//后台首页控制器无需验证,超级管理员无需验证
    		return true;
    	}elseif(strpos($act,'ajax') || in_array($act,$uneed_check)){
    		//所有ajax请求不需要验证权限
    		return true;
    	}else{
    		$right = M('system_menu')->where("id", "in", $act_list)
                ->cache("system_menu_".getAdmStoId())->getField('right',true);
    		foreach ($right as $val){
    			$role_right .= $val.',';
    		}
    		$role_right = explode(',', $role_right);
    		//检查是否拥有此操作权限
    		if(!in_array($ctl.'Controller@'.$act, $role_right)){
    			$this->error('您没有操作权限,请联系超级管理员分配权限',U('Admin/Index/welcome'));
    		}
    	}
    }
    
    public function ajaxReturn($data,$type = 'json'){                        
            exit(json_encode($data));
    }

}