Frontend.php 4.2 KB

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