审查视图

application/common/controller/Frontend.php 4.0 KB
Karson authored
1 2 3 4
<?php

namespace app\common\controller;
5
use app\common\library\Auth;
Karson authored
6 7 8 9
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
10
use think\Loader;
Karson authored
11
12 13 14
/**
 * 前台控制器基类
 */
Karson authored
15 16 17 18 19 20 21 22 23
class Frontend extends Controller
{

    /**
     * 布局模板
     * @var string
     */
    protected $layout = '';
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 权限Auth
38
     * @var Auth
39 40 41
     */
    protected $auth = null;
Karson authored
42 43 44
    public function _initialize()
    {
        //移除HTML标签
45
        $this->request->filter('trim,strip_tags,htmlspecialchars');
Karson authored
46
        $modulename = $this->request->module();
47
        $controllername = Loader::parseName($this->request->controller());
Karson authored
48 49 50
        $actionname = strtolower($this->request->action());

        // 如果有使用模板布局
51
        if ($this->layout) {
Karson authored
52 53
            $this->view->engine->layout('layout/' . $this->layout);
        }
54 55 56
        $this->auth = Auth::instance();

        // token
57
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
58 59 60 61 62

        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
63
        if (!$this->auth->match($this->noNeedLogin)) {
64 65 66
            //初始化
            $this->auth->init($token);
            //检测是否登录
67
            if (!$this->auth->isLogin()) {
68
                $this->error(__('Please login first'), 'index/user/login');
69 70
            }
            // 判断是否需要验证权限
71
            if (!$this->auth->match($this->noNeedRight)) {
72
                // 判断控制器和方法判断是否有对应权限
73
                if (!$this->auth->check($path)) {
74 75 76
                    $this->error(__('You have no permission'));
                }
            }
77
        } else {
78
            // 如果有传递token才验证是否登录状态
79
            if ($token) {
80 81 82 83 84
                $this->auth->init($token);
            }
        }

        $this->view->assign('user', $this->auth->getUser());
Karson authored
85 86

        // 语言检测
87
        $lang = strip_tags($this->request->langset());
Karson authored
88 89 90 91 92 93 94

        $site = Config::get("site");

        $upload = \app\common\model\Config::upload();

        // 上传信息配置后
        Hook::listen("upload_config_init", $upload);
95
Karson authored
96 97 98 99 100 101 102 103 104 105 106
        // 配置信息
        $config = [
            'site'           => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
            'upload'         => $upload,
            'modulename'     => $modulename,
            'controllername' => $controllername,
            'actionname'     => $actionname,
            'jsname'         => 'frontend/' . str_replace('.', '/', $controllername),
            'moduleurl'      => rtrim(url("/{$modulename}", '', false), '/'),
            'language'       => $lang
        ];
107
        $config = array_merge($config, Config::get("view_replace_str"));
108
Karson authored
109
        Config::set('upload', array_merge(Config::get('upload'), $upload));
110
Karson authored
111 112
        // 配置信息后
        Hook::listen("config_init", $config);
113
        // 加载当前控制器语言包
Karson authored
114 115 116 117 118 119 120 121 122 123 124
        $this->loadlang($controllername);
        $this->assign('site', $site);
        $this->assign('config', $config);
    }

    /**
     * 加载语言文件
     * @param string $name
     */
    protected function loadlang($name)
    {
125
        Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
Karson authored
126
    }
127
Karson authored
128 129
    /**
     * 渲染配置信息
130
     * @param mixed $name  键名或数组
131
     * @param mixed $value 值
Karson authored
132 133 134 135 136 137
     */
    protected function assignconfig($name, $value = '')
    {
        $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
    }
}