BaseController.php 3.92 KB
<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use backend\components\filters\AccessControl;

/**
 * Site controller
 */
class BaseController extends Controller
{

    public $layout = 'template';
    
    //视图变量
    protected $_actionParams = [];
    
    //上一页
    protected $_refer;
    
    //用户实例
    protected $user;

    public function init()
    {
        //路径设置
        Yii::setAlias('@web', Yii::getAlias('@admin'));
        Yii::$app->setHomeUrl(Yii::getAlias('@web'));
        Yii::$app->getRequest()->setBaseUrl(Yii::getAlias('@web'));
        Yii::$app->getRequest()->setScriptUrl(Yii::getAlias('@web'));
        
        //初始化用户实例
        $this->user = Yii::$app->user->getIdentity();
        //语言
        Yii::$app->language = 'zh-CN';
        
        parent::init();
    }
    
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
            ],
        ];
    }
    
    public function beforeAction($action) {
        $this->_refer = \common\lib\Referer::get();
        
        $view = $this->getView();
        $view->params['main_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][0] ? : '';
        $view->params['sub_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][1][Yii::$app->controller->action->id][0] ? : '';
        
        return parent::beforeAction($action);
    }
    
    /**
     * 单个设置视图变量
     * @param type $name
     * @param type $value
     * @return \backend\controllers\BaseController
     */
    protected function setParam($name,$value) {
        $this->_actionParams[$name] = $value;
        return $this;
    }
    
    /**
     * 批量设置视图变量
     * @param array $params
     * @return \backend\controllers\BaseController
     */
    protected function setParams(array $params) {
        $this->_actionParams = array_merge($this->_actionParams, $params);
        return $this;
    }
    
    /**
     * 跳转链接
     * @param type $url
     * @param type $statusCode
     * @return type
     */
    public function redirect($url, $statusCode = 302)
    {
        $url = (strpos($url,'http') === 0) ? $url : Yii::getAlias('@web/'.trim($url,'/'));
        return Yii::$app->getResponse()->redirect($url, $statusCode);
    }
    
    /**
     * 返回上一页
     * @param type $defaultUrl
     * @return type
     */
    public function goBack($defaultUrl = null)
    {
        $url = Yii::$app->getUser()->getReturnUrl($defaultUrl);
        $url = (strpos($url,'http') === 0) ? $url : Yii::getAlias('@web/'.trim($url,'/'));
        return Yii::$app->getResponse()->redirect($url);
    }
    
    /**
     * 刷新当前
     * @param type $anchor
     * @return type
     */
    public function refresh($anchor = '')
    {
        $url = Yii::getAlias('@web/'.trim(Yii::$app->getRequest()->getUrl(),'/')) . $anchor;
        return Yii::$app->getResponse()->redirect($url);
    }
    
    /**
     * json返回
     */
    public function renderJson($code,$message,$data = ''){
        $retData =  [
            'code' => (int)$code,
            'message' => $message,
            'data' => $data,
            'md5' => $data ? md5(json_encode($data)):''
        ];
        
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        Yii::$app->response->content = json_encode($retData);
        Yii::$app->response->send();
        Yii::$app->end();
    }
    
    /**
     * 视图输出方法重载
     * @param type $view
     * @param type $params
     * @return type
     */
    public function render($view, $params = array()) {
        $params = array_merge($this->_actionParams, [
            '_refer'=>$this->_refer
        ], $params);
        
        return parent::render($view, $params);
    }
    
    public function afterAction($action, $result) {
        return parent::afterAction($action, $result);
    }
    
}