HomeBaseController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\home;
  3. use app\BaseController;
  4. use app\common\model\SettingModel;
  5. use app\common\model\UserModel;
  6. use think\exception\HttpResponseException;
  7. use think\facade\View;
  8. use think\Response;
  9. /**
  10. * 控制器基础类
  11. */
  12. abstract class HomeBaseController extends BaseController
  13. {
  14. protected $tab = '';
  15. protected $user = null;
  16. // 初始化
  17. protected function initialize()
  18. {
  19. $setting = SettingModel::select();
  20. $set = [];
  21. foreach ($setting as $v) {
  22. $set[$v['code']] = $v['value'];
  23. }
  24. config($set, 'home');
  25. $user_id = get_user_id();
  26. if (!empty($user_id)) {
  27. $this->user = UserModel::where('id',$user_id)->hidden(['password','salt'])->find();
  28. }
  29. $this->init();
  30. View::assign('tab',$this->tab);
  31. View::assign('user',$this->user);
  32. }
  33. protected function init()
  34. {
  35. }
  36. protected function checkLogin()
  37. {
  38. $sessionUserId = session('home.user.id');
  39. if (empty($sessionUserId)) {
  40. if (request()->isAjax()) {
  41. $res = ['code' => 401, 'msg' => '请登录'];
  42. $response = Response::create($res, 'json');
  43. throw new HttpResponseException($response);
  44. } else {
  45. session('back_url',request()->url());
  46. $response = redirect('/home/login/index');
  47. throw new HttpResponseException($response);
  48. }
  49. }
  50. if (empty($this->user)) {
  51. jump('该用户已删除');
  52. }
  53. if ($this->user['status'] != UserModel::STATUS_PASS) {
  54. jump('该账号暂时无法使用');
  55. }
  56. }
  57. }