Index.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AdminLog;
  4. use app\admin\model\trade\Delivery;
  5. use app\admin\model\trade\Order;
  6. use app\admin\model\trade\Rechargeorder;
  7. use app\admin\model\trade\Withdrawal;
  8. use app\common\controller\Backend;
  9. use think\Config;
  10. use think\Hook;
  11. use think\Validate;
  12. /**
  13. * 后台首页
  14. * @internal
  15. */
  16. class Index extends Backend
  17. {
  18. protected $noNeedLogin = ['login'];
  19. protected $noNeedRight = ['index', 'logout'];
  20. protected $layout = '';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. //移除HTML标签
  25. $this->request->filter('trim,strip_tags,htmlspecialchars');
  26. }
  27. /**
  28. * 后台首页
  29. */
  30. public function index()
  31. {
  32. //左侧菜单
  33. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  34. 'trade' => ($this->checkBoxOrder() || $this->checkDeliveryOrder() || $this->checkWithdrawalOrder() || $this->checkRechargeOrder()) ? ['新', 'yellow'] : 0,
  35. 'trade/order' => [$this->checkBoxOrder(), 'yellow'],
  36. 'trade/delivery' => [$this->checkDeliveryOrder(), 'yellow'],
  37. 'trade/withdrawal' => [$this->checkWithdrawalOrder(), 'yellow'],
  38. 'trade/rechargeorder' => [$this->checkRechargeOrder(), 'yellow'],
  39. ], $this->view->site['fixedpage']);
  40. $action = $this->request->request('action');
  41. if ($this->request->isPost()) {
  42. if ($action == 'refreshmenu') {
  43. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  44. }
  45. }
  46. $this->view->assign('menulist', $menulist);
  47. $this->view->assign('navlist', $navlist);
  48. $this->view->assign('fixedmenu', $fixedmenu);
  49. $this->view->assign('referermenu', $referermenu);
  50. $this->view->assign('title', __('Home'));
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 管理员登录
  55. */
  56. public function login()
  57. {
  58. $url = $this->request->get('url', 'index/index');
  59. if ($this->auth->isLogin()) {
  60. $this->success(__("You've logged in, do not login again"), $url);
  61. }
  62. if ($this->request->isPost()) {
  63. $username = $this->request->post('username');
  64. $password = $this->request->post('password');
  65. $keeplogin = $this->request->post('keeplogin');
  66. $token = $this->request->post('__token__');
  67. $rule = [
  68. 'username' => 'require|length:3,30',
  69. 'password' => 'require|length:3,30',
  70. '__token__' => 'require|token',
  71. ];
  72. $data = [
  73. 'username' => $username,
  74. 'password' => $password,
  75. '__token__' => $token,
  76. ];
  77. if (Config::get('fastadmin.login_captcha')) {
  78. $rule['captcha'] = 'require|captcha';
  79. $data['captcha'] = $this->request->post('captcha');
  80. }
  81. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  82. $result = $validate->check($data);
  83. if (!$result) {
  84. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  85. }
  86. AdminLog::setTitle(__('Login'));
  87. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  88. if ($result === true) {
  89. Hook::listen("admin_login_after", $this->request);
  90. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  91. } else {
  92. $msg = $this->auth->getError();
  93. $msg = $msg ? $msg : __('Username or password is incorrect');
  94. $this->error($msg, $url, ['token' => $this->request->token()]);
  95. }
  96. }
  97. // 根据客户端的cookie,判断是否可以自动登录
  98. if ($this->auth->autologin()) {
  99. $this->redirect($url);
  100. }
  101. $background = Config::get('fastadmin.login_background');
  102. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  103. $this->view->assign('background', $background);
  104. $this->view->assign('title', __('Login'));
  105. Hook::listen("admin_login_init", $this->request);
  106. return $this->view->fetch();
  107. }
  108. /**
  109. * 退出登录
  110. */
  111. public function logout()
  112. {
  113. $this->auth->logout();
  114. Hook::listen("admin_logout_after", $this->request);
  115. $this->success(__('Logout successful'), 'index/login');
  116. }
  117. private function checkBoxOrder()
  118. {
  119. return Order::where('backend_read', 0)->where('status', 'neq', 'unpay')->count();
  120. }
  121. private function checkDeliveryOrder()
  122. {
  123. return Delivery::where('backend_read', 0)->where('status','neq','unpay')->count();
  124. }
  125. private function checkWithdrawalOrder()
  126. {
  127. return Withdrawal::where('backend_read', 0)->count();
  128. }
  129. private function checkRechargeOrder()
  130. {
  131. return Rechargeorder::where('backend_read', 0)->count();
  132. }
  133. }