Frontend.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 前台控制器基类
  15. // +----------------------------------------------------------------------
  16. namespace app\common\controller;
  17. use app\common\library\Auth;
  18. use think\facade\Config;
  19. use think\facade\Cookie;
  20. use think\facade\Event;
  21. class Frontend extends BaseController
  22. {
  23. /**
  24. * 无需登录的方法,同时也就不需要鉴权了
  25. * @var array
  26. */
  27. protected $noNeedLogin = [];
  28. /**
  29. * 无需鉴权的方法,但需要登录
  30. * @var array
  31. */
  32. protected $noNeedRight = [];
  33. /**
  34. * 权限Auth
  35. * @var Auth
  36. */
  37. protected $auth = null;
  38. public function initialize()
  39. {
  40. //移除HTML标签
  41. $this->request->filter('trim,strip_tags,htmlspecialchars');
  42. $modulename = app()->http->getName();
  43. $controller = preg_replace_callback('/\.[A-Z]/', function ($d) {
  44. return strtolower($d[0]);
  45. }, $this->request->controller());
  46. $controllername = parse_name($controller);
  47. $actionname = strtolower($this->request->action());
  48. $this->auth = Auth::instance();
  49. // token
  50. $token = $this->request->server('HTTP_TOKEN', (string) $this->request->request('token', Cookie::get('token')));
  51. $path = str_replace('.', '/', $controllername) . '/' . $actionname;
  52. // 设置当前请求的URI
  53. $this->auth->setRequestUri($path);
  54. // 检测是否需要验证登录
  55. if (!$this->auth->match($this->noNeedLogin)) {
  56. //初始化
  57. $this->auth->init($token);
  58. //检测是否登录
  59. if (!$this->auth->isLogin()) {
  60. $this->error('请登录后操作', 'index/user/login');
  61. }
  62. // 判断是否需要验证权限
  63. if (!$this->auth->match($this->noNeedRight)) {
  64. // 判断控制器和方法判断是否有对应权限
  65. /*if (!$this->auth->check($path)) {
  66. $this->error('你没有权限访问');
  67. }*/
  68. }
  69. } else {
  70. // 如果有传递token才验证是否登录状态
  71. if ($token) {
  72. $this->auth->init($token);
  73. }
  74. }
  75. $this->assign('user', $this->auth->getUser());
  76. $site = Config::get("site");
  77. // 上传信息配置后
  78. $upload = array_merge(\app\common\model\Config::upload(), ...Event::trigger("upload_config_init"));
  79. // 配置信息
  80. $config = [
  81. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version'])),
  82. 'upload' => $upload,
  83. 'modulename' => $modulename,
  84. 'controllername' => $controllername,
  85. 'actionname' => $actionname,
  86. 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
  87. 'moduleurl' => rtrim(url("/{$modulename}", [], false), '/'),
  88. ];
  89. $config = array_merge($config, Config::get("view.tpl_replace_string"), ...Event::trigger("config_init"));
  90. Config::set(array_merge(Config::get('upload'), $upload), 'upload');
  91. // 加载当前控制器语言包
  92. $this->assign('site', $site);
  93. $this->assign('config', $config);
  94. }
  95. /**
  96. * 渲染配置信息
  97. * @param mixed $name 键名或数组
  98. * @param mixed $value 值
  99. */
  100. protected function assignconfig($name, $value = '')
  101. {
  102. $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
  103. }
  104. //刷新Token
  105. protected function token()
  106. {
  107. $check = $this->request->checkToken('__token__');
  108. // 刷新token
  109. $token = $this->request->buildToken();
  110. if ($this->request->isAjax()) {
  111. header('__token__: ' . $token);
  112. }
  113. if (false === $check) {
  114. $this->error('令牌错误!', '', ['__token__' => $token]);
  115. }
  116. }
  117. }