| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | Original reference: https://gitee.com/karson/fastadmin
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 前台控制器基类
- // +----------------------------------------------------------------------
- namespace app\common\controller;
- use app\common\library\Auth;
- use think\facade\Config;
- use think\facade\Cookie;
- use think\facade\Event;
- class Frontend extends BaseController
- {
- /**
- * 无需登录的方法,同时也就不需要鉴权了
- * @var array
- */
- protected $noNeedLogin = [];
- /**
- * 无需鉴权的方法,但需要登录
- * @var array
- */
- protected $noNeedRight = [];
- /**
- * 权限Auth
- * @var Auth
- */
- protected $auth = null;
- public function initialize()
- {
- //移除HTML标签
- $this->request->filter('trim,strip_tags,htmlspecialchars');
- $modulename = app()->http->getName();
- $controller = preg_replace_callback('/\.[A-Z]/', function ($d) {
- return strtolower($d[0]);
- }, $this->request->controller());
- $controllername = parse_name($controller);
- $actionname = strtolower($this->request->action());
- $this->auth = Auth::instance();
- // token
- $token = $this->request->server('HTTP_TOKEN', (string) $this->request->request('token', Cookie::get('token')));
- $path = str_replace('.', '/', $controllername) . '/' . $actionname;
- // 设置当前请求的URI
- $this->auth->setRequestUri($path);
- // 检测是否需要验证登录
- if (!$this->auth->match($this->noNeedLogin)) {
- //初始化
- $this->auth->init($token);
- //检测是否登录
- if (!$this->auth->isLogin()) {
- $this->error('请登录后操作', 'index/user/login');
- }
- // 判断是否需要验证权限
- if (!$this->auth->match($this->noNeedRight)) {
- // 判断控制器和方法判断是否有对应权限
- /*if (!$this->auth->check($path)) {
- $this->error('你没有权限访问');
- }*/
- }
- } else {
- // 如果有传递token才验证是否登录状态
- if ($token) {
- $this->auth->init($token);
- }
- }
- $this->assign('user', $this->auth->getUser());
- $site = Config::get("site");
- // 上传信息配置后
- $upload = array_merge(\app\common\model\Config::upload(), ...Event::trigger("upload_config_init"));
- // 配置信息
- $config = [
- 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version'])),
- 'upload' => $upload,
- 'modulename' => $modulename,
- 'controllername' => $controllername,
- 'actionname' => $actionname,
- 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
- 'moduleurl' => rtrim(url("/{$modulename}", [], false), '/'),
- ];
- $config = array_merge($config, Config::get("view.tpl_replace_string"), ...Event::trigger("config_init"));
- Config::set(array_merge(Config::get('upload'), $upload), 'upload');
- // 加载当前控制器语言包
- $this->assign('site', $site);
- $this->assign('config', $config);
- }
- /**
- * 渲染配置信息
- * @param mixed $name 键名或数组
- * @param mixed $value 值
- */
- protected function assignconfig($name, $value = '')
- {
- $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
- }
- //刷新Token
- protected function token()
- {
- $check = $this->request->checkToken('__token__');
- // 刷新token
- $token = $this->request->buildToken();
- if ($this->request->isAjax()) {
- header('__token__: ' . $token);
- }
- if (false === $check) {
- $this->error('令牌错误!', '', ['__token__' => $token]);
- }
- }
- }
|