BaseController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\home;
  9. use think\App;
  10. use think\facade\View;
  11. use think\exception\HttpResponseException;
  12. /**
  13. * 控制器基础类
  14. */
  15. abstract class BaseController
  16. {
  17. /**
  18. * Request实例
  19. * @var \think\Request
  20. */
  21. protected $request;
  22. /**
  23. * 应用实例
  24. * @var \think\App
  25. */
  26. protected $app;
  27. /**
  28. * 是否批量验证
  29. * @var bool
  30. */
  31. protected $batchValidate = false;
  32. /**
  33. * 控制器中间件
  34. * @var array
  35. */
  36. protected $middleware = [];
  37. /**
  38. * 构造方法
  39. * @access public
  40. * @param App $app 应用对象
  41. */
  42. public function __construct(App $app)
  43. {
  44. $this->app = $app;
  45. $this->request = $this->app->request;
  46. // 控制器初始化
  47. $this->initialize();
  48. }
  49. // 初始化
  50. protected function initialize()
  51. {
  52. $params = [
  53. 'module' => \think\facade\App::initialize()->http->getName(),
  54. 'controller' => app('request')->controller(),
  55. 'action' => app('request')->action(),
  56. 'isLogin' => 0,
  57. 'uid' => 0,
  58. 'nickname' => '',
  59. 'mobile_status' => 0,
  60. 'version' => get_config('webconfig.version'),
  61. ];
  62. // 加载控制器语言包
  63. $lang = cookie('think_lang');
  64. if(empty($lang)){
  65. $lang = 'zh-cn';
  66. }
  67. \think\facade\Lang::setLangSet($lang);
  68. $langset = $this->app->lang->getLangSet();
  69. $this->app->lang->load([
  70. app_path() . 'lang/' . $langset . '.php'
  71. ]);
  72. $login_top = '';
  73. $info = $this->checkLogin();
  74. if ($info) {
  75. $login_top = '<a class="nav-img" href="/home/user/index"><img src="' . $info['headimgurl'] . '" alt="' . $info['username'] . '" />' . $info['username'] . '</a>';
  76. $params['isLogin'] = 1;
  77. $params['uid'] = $info['id'];
  78. $params['nickname'] = $info['nickname'];
  79. $params['username'] = $info['username'];
  80. }
  81. $COMMON_NAV = get_navs('NAV_HOME');
  82. View::assign('COMMON_NAV', $COMMON_NAV);
  83. View::assign('webconfig', get_config('webconfig'));
  84. View::assign('params', $params);
  85. View::assign('login_top', $login_top);
  86. }
  87. // 检测用户登录状态
  88. protected function checkLogin()
  89. {
  90. $session_user = get_config('app.session_user');
  91. $login_user = \think\facade\Session::get($session_user);
  92. if ($login_user && is_array($login_user)) {
  93. return $login_user;
  94. } else {
  95. return false;
  96. }
  97. }
  98. //页面跳转方法
  99. public function redirectTo(...$args)
  100. {
  101. throw new HttpResponseException(redirect(...$args));
  102. }
  103. }