<?php
namespace app\common\controller;
use app\common\library\Auth;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
class Frontend extends Controller
{
protected $layout = '';
protected $noNeedLogin = [];
protected $noNeedRight = [];
protected $auth = null;
public function _initialize()
{
$this->request->filter('strip_tags');
$modulename = $this->request->module();
$controllername = strtolower($this->request->controller());
$actionname = strtolower($this->request->action());
if ($this->layout) {
$this->view->engine->layout('layout/'.$this->layout);
}
$this->auth = Auth::instance();
$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
$path = str_replace('.', '/', $controllername).'/'.$actionname;
$this->auth->setRequestUri($path);
if (!$this->auth->match($this->noNeedLogin)) {
$this->auth->init($token);
if (!$this->auth->isLogin()) {
$this->error(__('Please login first'), 'user/login');
}
if (!$this->auth->match($this->noNeedRight)) {
if (!$this->auth->check($path)) {
$this->error(__('You have no permission'));
}
}
} else {
if ($token) {
$this->auth->init($token);
}
}
$this->view->assign('user', $this->auth->getUser());
$lang = strip_tags($this->request->langset());
$site = Config::get('site');
$upload = \app\common\model\Config::upload();
Hook::listen('upload_config_init', $upload);
$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,
];
$config = array_merge($config, Config::get('view_replace_str'));
Config::set('upload', array_merge(Config::get('upload'), $upload));
Hook::listen('config_init', $config);
$this->loadlang($controllername);
$this->assign('site', $site);
$this->assign('config', $config);
}
protected function loadlang($name)
{
Lang::load(APP_PATH.$this->request->module().'/lang/'.$this->request->langset().'/'.str_replace('.', '/', $name).'.php');
}
protected function assignconfig($name, $value = '')
{
$this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
}
}