Index.php 5.6 KB

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