colly_wyx
2017-08-10 f7a98b088d5f7246cf12ee072169057a8e762664
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
 
/**
 * 后台通用控制器 加了rbac
 * Class AdminCommonController
 */
class System_Controller_Base extends Yaf_Controller_Abstract{
 
    protected $redis;
 
    private $request;
   /**
     * @var string 信息提示模板
     */
    public $defaultMsgTemplate = 'error/showmsg.phtml';
    /**
     * @var string 布局文件
     */
    public $layout;
    /**
     * 栏目模块
     * @var [type]
     */
    public $modules;
 
    public $route;
 
    public $site_url;
 
    public $config = array();
 
    public function __set($name, $value){
        $this->$name = $value;
    }
 
    public function __get($name){
        if(isset($this->$name)){  
            return $this->$name;  
        }else{  
            return NULL;  
        }  
    }
 
    public function init()
    {
        $this->config['site_url'] = Yaf_Registry::get('var');
        $this->request = $this->getRequest();
        //do not call render for ajax request
        if ($this->isAjax()) {
            Yaf_Dispatcher::getInstance()->autoRender(FALSE);
        }
        $this->route = $this->getRoute();
        //初始化redis
        $this->redis = System_Service_Locator::getInstance()->get('redis');
        if(!$this->redis->exists('modules') ){
          $this->redis->set('modules', $this->getModules());
        }
         //更新角色权限
        if(!$this->redis->exists('role_auth')){
          Util::updateRoleAuth();
        }
        //是否渲染模板
        if(!empty($this->layout)){
            //根据角色权限访问
            //$this->modules = $this->modules_service->getModuleListByRoleId();
            $this->setLayout($this->layout);
        }
        //将当前路由值传入模板
        $this->getView()->assign('route', $this->route);
    }
 
    /**
     * 设置布局
     * @param string $layout 布局文件
     */
    private function setLayout($layout)
    {
        $this->getResPonse()->modules = $this->redis->get('modules');
        $this->getResPonse()->route = $this->route;
        $this->getResPonse()->layout = function($content, $modules, $route) use ($layout){
            //$content子视图的内容
            return $this->getView()->render($layout, ['content'=>$content, 'modules' => $modules, 'route' => $route]);
        };
    }
 
    /**
     * 设置布局
     * @param string $layout 布局文件
     */
    public function _setLayout($layout)
    {
        $this->layout = $layout;
    }
 
    /**
     * 获取当前路由
     * @return [type] [description]
     */
    public function getRoute(){
        return array('module' => strtolower($this->getRequest()->module), 'controller' => strtolower($this->getRequest()->controller), 'action' => strtolower($this->getRequest()->action));
    }
 
    /**
     * 获取post数值
     * @param  [type] $name          [description]
     * @param  string $default_value [description]
     * @return [type]                [description]
     */
    public function post($name, $default_value = ""){
        return $this->getRequest()->getPost($name, $default_value);
    }
 
    /**
     * 获取GET数值
     * @param  [type] $name          [description]
     * @param  string $default_value [description]
     * @return [type]                [description]
     */
    public function get($name, $default_value = ""){
        return $this->getRequest()->getQuery($name, $default_value);
    }
 
    /**
     * 判断是否是ajax请求
     * @return boolean [description]
     */
    public function isAjax(){
        return $this->getRequest()->isXmlHttpRequest();
    }
 
    /**
     * 获取栏目列表
     * @return [type] [description]
     */
    public function getModules(){
        $this->module_service = new Service_Module();
        return $this->module_service->getModuleList(array(), array(), array('level' => 1));
    }
}