Frontend.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\library\Auth;
  4. use think\Config;
  5. use think\Controller;
  6. use think\Hook;
  7. use think\Lang;
  8. use think\Loader;
  9. /**
  10. * 前台控制器基类
  11. */
  12. class Frontend extends Controller
  13. {
  14. /**
  15. * 布局模板
  16. * @var string
  17. */
  18. protected $layout = '';
  19. /**
  20. * 无需登录的方法,同时也就不需要鉴权了
  21. * @var array
  22. */
  23. protected $noNeedLogin = [];
  24. /**
  25. * 无需鉴权的方法,但需要登录
  26. * @var array
  27. */
  28. protected $noNeedRight = [];
  29. /**
  30. * 权限Auth
  31. * @var Auth
  32. */
  33. protected $auth = null;
  34. public function _initialize()
  35. {
  36. //移除HTML标签
  37. $this->request->filter('trim,strip_tags,htmlspecialchars');
  38. $modulename = $this->request->module();
  39. $controllername = Loader::parseName($this->request->controller());
  40. $actionname = strtolower($this->request->action());
  41. // 如果有使用模板布局
  42. if ($this->layout) {
  43. $this->view->engine->layout('layout/' . $this->layout);
  44. }
  45. $this->auth = Auth::instance();
  46. // token
  47. $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
  48. $path = str_replace('.', '/', $controllername) . '/' . $actionname;
  49. // 设置当前请求的URI
  50. $this->auth->setRequestUri($path);
  51. // 检测是否需要验证登录
  52. if (!$this->auth->match($this->noNeedLogin)) {
  53. //初始化
  54. $this->auth->init($token);
  55. //检测是否登录
  56. if (!$this->auth->isLogin()) {
  57. $this->error(__('Please login first'), 'index/user/login');
  58. }
  59. // 判断是否需要验证权限
  60. if (!$this->auth->match($this->noNeedRight)) {
  61. // 判断控制器和方法判断是否有对应权限
  62. if (!$this->auth->check($path)) {
  63. $this->error(__('You have no permission'));
  64. }
  65. }
  66. } else {
  67. // 如果有传递token才验证是否登录状态
  68. if ($token) {
  69. $this->auth->init($token);
  70. }
  71. }
  72. $this->view->assign('user', $this->auth->getUser());
  73. // 语言检测
  74. $lang = strip_tags($this->request->langset());
  75. $site = Config::get("site");
  76. $upload = \app\common\model\Config::upload();
  77. // 上传信息配置后
  78. Hook::listen("upload_config_init", $upload);
  79. // 配置信息
  80. $config = [
  81. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  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. 'language' => $lang
  89. ];
  90. $config = array_merge($config, Config::get("view_replace_str"));
  91. Config::set('upload', array_merge(Config::get('upload'), $upload));
  92. // 配置信息后
  93. Hook::listen("config_init", $config);
  94. // 加载当前控制器语言包
  95. $this->loadlang($controllername);
  96. $this->assign('site', $site);
  97. $this->assign('config', $config);
  98. }
  99. /**
  100. * 加载语言文件
  101. * @param string $name
  102. */
  103. protected function loadlang($name)
  104. {
  105. Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
  106. }
  107. /**
  108. * 渲染配置信息
  109. * @param mixed $name 键名或数组
  110. * @param mixed $value 值
  111. */
  112. protected function assignconfig($name, $value = '')
  113. {
  114. $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
  115. }
  116. }