Frontend.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\library\Auth;
  4. use think\Config;
  5. use think\Controller;
  6. use think\Lang;
  7. use think\Session;
  8. class Frontend extends Controller
  9. {
  10. /**
  11. *
  12. * @var Auth
  13. */
  14. protected $user = null;
  15. /**
  16. * 无需登录的方法,默认全部都无需登录
  17. * @var array
  18. */
  19. protected $noNeedLogin = ['*'];
  20. /**
  21. * 布局模板
  22. * @var string
  23. */
  24. protected $layout = '';
  25. public function _initialize()
  26. {
  27. //移除HTML标签
  28. $this->request->filter('strip_tags');
  29. $modulename = $this->request->module();
  30. $controllername = strtolower($this->request->controller());
  31. $actionname = strtolower($this->request->action());
  32. $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
  33. // 如果有使用模板布局
  34. if ($this->layout)
  35. {
  36. $this->view->engine->layout('layout/' . $this->layout);
  37. }
  38. // 语言检测
  39. $lang = Lang::detect();
  40. $site = Config::get("site");
  41. // 配置信息
  42. $config = [
  43. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  44. 'upload' => \app\common\model\Config::upload(),
  45. 'modulename' => $modulename,
  46. 'controllername' => $controllername,
  47. 'actionname' => $actionname,
  48. 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
  49. 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
  50. 'language' => $lang
  51. ];
  52. $this->loadlang($controllername);
  53. $this->assign('site', $site);
  54. $this->assign('config', $config);
  55. }
  56. protected function checkLogin()
  57. {
  58. //检测是否登录
  59. if (!$this->user->isLogin())
  60. {
  61. $url = Session::get('referer');
  62. $url = $url ? $url : $this->request->url();
  63. $this->error(__('Please login first'), url('/user/login', ['url' => $url]));
  64. }
  65. }
  66. /**
  67. * 加载语言文件
  68. * @param string $name
  69. */
  70. protected function loadlang($name)
  71. {
  72. Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
  73. }
  74. /**
  75. * 渲染配置信息
  76. * @param mixed $name 键名或数组
  77. * @param mixed $value 值
  78. */
  79. protected function assignconfig($name, $value = '')
  80. {
  81. $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
  82. }
  83. }