Frontend.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. use think\Validate;
  10. /**
  11. * 前台控制器基类
  12. */
  13. class Frontend extends Controller
  14. {
  15. public function __construct(){
  16. exit;
  17. }
  18. /**
  19. * 布局模板
  20. * @var string
  21. */
  22. protected $layout = '';
  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 = $this->request->module();
  43. $controllername = Loader::parseName($this->request->controller());
  44. $actionname = strtolower($this->request->action());
  45. // 检测IP是否允许
  46. check_ip_allowed();
  47. // 如果有使用模板布局
  48. if ($this->layout) {
  49. $this->view->engine->layout('layout/' . $this->layout);
  50. }
  51. $this->auth = Auth::instance();
  52. // token
  53. $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
  54. $path = str_replace('.', '/', $controllername) . '/' . $actionname;
  55. // 设置当前请求的URI
  56. $this->auth->setRequestUri($path);
  57. // 检测是否需要验证登录
  58. if (!$this->auth->match($this->noNeedLogin)) {
  59. //初始化
  60. $this->auth->init($token);
  61. //检测是否登录
  62. if (!$this->auth->isLogin()) {
  63. $this->error(__('Please login first'), 'index/user/login');
  64. }
  65. // 判断是否需要验证权限
  66. if (!$this->auth->match($this->noNeedRight)) {
  67. // 判断控制器和方法判断是否有对应权限
  68. if (!$this->auth->check($path)) {
  69. $this->error(__('You have no permission'));
  70. }
  71. }
  72. } else {
  73. // 如果有传递token才验证是否登录状态
  74. if ($token) {
  75. $this->auth->init($token);
  76. }
  77. }
  78. $this->view->assign('user', $this->auth->getUser());
  79. // 语言检测
  80. $lang = $this->request->langset();
  81. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  82. $site = Config::get("site");
  83. $upload = \app\common\model\Config::upload();
  84. // 上传信息配置后
  85. Hook::listen("upload_config_init", $upload);
  86. // 配置信息
  87. $config = [
  88. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  89. 'upload' => $upload,
  90. 'modulename' => $modulename,
  91. 'controllername' => $controllername,
  92. 'actionname' => $actionname,
  93. 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
  94. 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
  95. 'language' => $lang
  96. ];
  97. $config = array_merge($config, Config::get("view_replace_str"));
  98. Config::set('upload', array_merge(Config::get('upload'), $upload));
  99. // 配置信息后
  100. Hook::listen("config_init", $config);
  101. // 加载当前控制器语言包
  102. $this->loadlang($controllername);
  103. $this->assign('site', $site);
  104. $this->assign('config', $config);
  105. }
  106. /**
  107. * 加载语言文件
  108. * @param string $name
  109. */
  110. protected function loadlang($name)
  111. {
  112. $name = Loader::parseName($name);
  113. $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
  114. $lang = $this->request->langset();
  115. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  116. Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
  117. }
  118. /**
  119. * 渲染配置信息
  120. * @param mixed $name 键名或数组
  121. * @param mixed $value 值
  122. */
  123. protected function assignconfig($name, $value = '')
  124. {
  125. $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
  126. }
  127. /**
  128. * 刷新Token
  129. */
  130. protected function token()
  131. {
  132. $token = $this->request->param('__token__');
  133. //验证Token
  134. if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
  135. $this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
  136. }
  137. //刷新Token
  138. $this->request->token();
  139. }
  140. }