説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Index.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 后台首页
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\admin\model\Adminlog;
  16. use app\common\controller\Adminbase;
  17. use think\addons\Service;
  18. use think\facade\Cache;
  19. use think\facade\Hook;
  20. use util\File;
  21. class Index extends Adminbase
  22. {
  23. protected $noNeedLogin = ['login'];
  24. protected $noNeedRight = ['index', 'cache', 'logout'];
  25. //初始化
  26. protected function initialize()
  27. {
  28. parent::initialize();
  29. //移除HTML标签
  30. $this->request->filter('trim,strip_tags,htmlspecialchars');
  31. }
  32. //后台首页
  33. public function index()
  34. {
  35. if ($this->request->isPost()) {
  36. return json($this->auth->getSidebar());
  37. }
  38. return $this->fetch();
  39. }
  40. //登录判断
  41. public function login()
  42. {
  43. $url = $this->request->get('url', 'admin/index/index', 'url_clean');
  44. if ($this->auth->isLogin()) {
  45. $this->success("你已经登录,无需重复登录", $url);
  46. }
  47. //保持会话有效时长,单位:小时
  48. $keeyloginhours = 24;
  49. if ($this->request->isPost()) {
  50. $data = $this->request->post();
  51. $keeplogin = $this->request->post('keeplogin');
  52. $rule = [
  53. 'verify|验证码' => 'require|captcha',
  54. 'username|用户名' => 'require|alphaDash|length:3,20',
  55. 'password|密码' => 'require|length:3,20',
  56. '__token__' => 'require|token',
  57. ];
  58. $result = $this->validate($data, $rule);
  59. Adminlog::setTitle('登录');
  60. if (true !== $result) {
  61. $this->error($result, $url, ['token' => $this->request->token()]);
  62. }
  63. if ($this->auth->login($data['username'], $data['password'], $keeplogin ? $keeyloginhours * 3600 : 0)) {
  64. Hook::listen("admin_login_after", $this->request);
  65. $this->success('恭喜您,登陆成功', $url);
  66. } else {
  67. $msg = $this->auth->getError();
  68. $msg = $msg ?: '用户名或者密码错误!';
  69. $this->error($msg, $url, ['token' => $this->request->token()]);
  70. }
  71. }
  72. if ($this->auth->autologin()) {
  73. $this->redirect($url);
  74. }
  75. Hook::listen("admin_login_init", $this->request);
  76. $this->assign('keeyloginhours', $keeyloginhours);
  77. return $this->fetch();
  78. }
  79. //手动退出登录
  80. public function logout()
  81. {
  82. if ($this->auth->logout()) {
  83. Hook::listen("admin_logout_after", $this->request);
  84. $this->success('注销成功!', url("admin/index/login"));
  85. }
  86. }
  87. //缓存更新
  88. public function cache()
  89. {
  90. try {
  91. $type = $this->request->request("type");
  92. switch ($type) {
  93. case 'all':
  94. case 'data':
  95. File::del_dir(ROOT_PATH . 'runtime' . DS . 'cache');
  96. Cache::clear();
  97. if ($type == 'data') {
  98. break;
  99. }
  100. case 'template':
  101. File::del_dir(ROOT_PATH . 'runtime' . DS . 'temp');
  102. if ($type == 'template') {
  103. break;
  104. }
  105. case 'addons':
  106. // 插件缓存
  107. Service::refresh();
  108. if ($type == 'addons') {
  109. break;
  110. }
  111. }
  112. } catch (\Exception $e) {
  113. $this->error($e->getMessage());
  114. }
  115. Hook::listen("wipecache_after");
  116. $this->success('清理缓存');
  117. }
  118. }