Frontend.php 3.5 KB

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