Frontend.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * 返回码,默认为null,当设置了该值后将输出json数据
  12. * @var int
  13. */
  14. protected $code = null;
  15. /**
  16. * 返回内容,默认为null,当设置了该值后将输出json数据
  17. * @var mixed
  18. */
  19. protected $data = null;
  20. /**
  21. * 返回文本,默认为空
  22. * @var mixed
  23. */
  24. protected $msg = '';
  25. /**
  26. *
  27. * @var Auth
  28. */
  29. protected $user = null;
  30. /**
  31. * 无需登录的方法,默认全部都无需登录
  32. * @var array
  33. */
  34. protected $noNeedLogin = ['*'];
  35. /**
  36. * 布局模板
  37. * @var string
  38. */
  39. protected $layout = '';
  40. public function _initialize()
  41. {
  42. //移除HTML标签
  43. $this->request->filter('strip_tags');
  44. $modulename = $this->request->module();
  45. $controllername = strtolower($this->request->controller());
  46. $actionname = strtolower($this->request->action());
  47. $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
  48. $this->user = Auth::instance();
  49. // 设置当前请求的URI
  50. $this->user->setRequestUri($path);
  51. // 检测当前是否登录并进行初始化
  52. $this->user->init();
  53. // 检测是否需要验证登录
  54. if (!$this->user->match($this->noNeedLogin))
  55. {
  56. $this->checkLogin();
  57. }
  58. // 将auth对象渲染至视图
  59. $this->view->assign("user", $this->user);
  60. // 如果有使用模板布局
  61. if ($this->layout)
  62. {
  63. $this->view->engine->layout('layout/' . $this->layout);
  64. }
  65. // 语言检测
  66. $lang = Lang::detect();
  67. $site = Config::get("site");
  68. // 配置信息
  69. $config = [
  70. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  71. 'upload' => \app\common\model\Config::upload(),
  72. 'modulename' => $modulename,
  73. 'controllername' => $controllername,
  74. 'actionname' => $actionname,
  75. 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
  76. 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
  77. 'language' => $lang
  78. ];
  79. $this->loadlang($controllername);
  80. $this->assign('site', $site);
  81. $this->assign('config', $config);
  82. }
  83. protected function checkLogin()
  84. {
  85. //检测是否登录
  86. if (!$this->user->isLogin())
  87. {
  88. $url = Session::get('referer');
  89. $url = $url ? $url : $this->request->url();
  90. $this->error(__('Please login first'), url('/user/login', ['url' => $url]));
  91. }
  92. }
  93. /**
  94. * 加载语言文件
  95. * @param string $name
  96. */
  97. protected function loadlang($name)
  98. {
  99. Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
  100. }
  101. /**
  102. * 析构方法
  103. *
  104. */
  105. public function __destruct()
  106. {
  107. //判断是否设置code值,如果有则变动response对象的正文
  108. if (!is_null($this->code))
  109. {
  110. $this->result($this->data, $this->code, $this->msg, 'json');
  111. }
  112. }
  113. }