Index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AdminLog;
  4. use app\common\controller\Backend;
  5. use think\Config;
  6. use think\Hook;
  7. use think\Validate;
  8. use think\Db;
  9. /**
  10. * 后台首页
  11. * @internal
  12. */
  13. class Index extends Backend
  14. {
  15. protected $noNeedLogin = ['login'];
  16. protected $noNeedRight = ['index', 'logout'];
  17. protected $layout = '';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. //移除HTML标签
  22. $this->request->filter('trim,strip_tags,htmlspecialchars');
  23. }
  24. /**
  25. * 后台首页
  26. */
  27. public function index()
  28. {
  29. //各项为审核的,待处理的数量
  30. $wait = [
  31. 'report' => Db::name('report')->where('status',0)->count(),//举报待处理
  32. 'useridconfirm' => Db::name('user_idconfirm')->where('status',0)->count(),//实名认证
  33. 'takecash' => Db::name('take_cash')->where('status',0)->count(),//提现申请
  34. 'topicdongtai' => Db::name('topic_dongtai')->where('auit_status',0)->count(),//动态
  35. ];
  36. //左侧菜单
  37. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  38. /*'dashboard' => 'hot',
  39. 'addon' => ['new', 'red', 'badge'],
  40. 'auth/rule' => __('Menu'),
  41. 'general' => ['new', 'purple'],*/
  42. //各项为审核的,待处理的数量
  43. 'report' => $wait['report'],
  44. 'useridconfirm' => $wait['useridconfirm'],
  45. 'takecash' => $wait['takecash'],
  46. 'topicdongtai' => $wait['topicdongtai'],
  47. ], $this->view->site['fixedpage']);
  48. $action = $this->request->request('action');
  49. if ($this->request->isPost()) {
  50. if ($action == 'refreshmenu') {
  51. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  52. }
  53. }
  54. $this->view->assign('menulist', $menulist);
  55. $this->view->assign('navlist', $navlist);
  56. $this->view->assign('fixedmenu', $fixedmenu);
  57. $this->view->assign('referermenu', $referermenu);
  58. $this->view->assign('title', __('Home'));
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 管理员登录
  63. */
  64. public function login()
  65. {
  66. $url = $this->request->get('url', 'index/index');
  67. if ($this->auth->isLogin()) {
  68. $this->success(__("You've logged in, do not login again"), $url);
  69. }
  70. if ($this->request->isPost()) {
  71. $username = $this->request->post('username');
  72. $password = $this->request->post('password');
  73. $keeplogin = $this->request->post('keeplogin');
  74. $token = $this->request->post('__token__');
  75. $rule = [
  76. 'username' => 'require|length:3,30',
  77. 'password' => 'require|length:3,30',
  78. '__token__' => 'require|token',
  79. ];
  80. $data = [
  81. 'username' => $username,
  82. 'password' => $password,
  83. '__token__' => $token,
  84. ];
  85. if (Config::get('fastadmin.login_captcha')) {
  86. $rule['captcha'] = 'require|captcha';
  87. $data['captcha'] = $this->request->post('captcha');
  88. }
  89. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  90. $result = $validate->check($data);
  91. if (!$result) {
  92. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  93. }
  94. AdminLog::setTitle(__('Login'));
  95. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  96. if ($result === true) {
  97. Hook::listen("admin_login_after", $this->request);
  98. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  99. } else {
  100. $msg = $this->auth->getError();
  101. $msg = $msg ? $msg : __('Username or password is incorrect');
  102. $this->error($msg, $url, ['token' => $this->request->token()]);
  103. }
  104. }
  105. // 根据客户端的cookie,判断是否可以自动登录
  106. if ($this->auth->autologin()) {
  107. $this->redirect($url);
  108. }
  109. $background = Config::get('fastadmin.login_background');
  110. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  111. $this->view->assign('background', $background);
  112. $this->view->assign('title', __('Login'));
  113. Hook::listen("admin_login_init", $this->request);
  114. return $this->view->fetch();
  115. }
  116. /**
  117. * 退出登录
  118. */
  119. public function logout()
  120. {
  121. if ($this->request->isPost()) {
  122. $this->auth->logout();
  123. Hook::listen("admin_logout_after", $this->request);
  124. $this->success(__('Logout successful'), 'index/login');
  125. }
  126. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  127. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  128. return $html;
  129. }
  130. }