Index.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 后台首页
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\admin\model\Adminlog;
  16. use app\common\controller\Backend;
  17. use think\addons\Service;
  18. use think\exception\ValidateException;
  19. use think\facade\Cache;
  20. use think\facade\Db;
  21. use think\facade\Env;
  22. use think\facade\Event;
  23. use think\facade\Session;
  24. use util\File;
  25. class Index extends Backend
  26. {
  27. protected $noNeedLogin = ['login', 'captcha'];
  28. protected $noNeedRight = ['index', 'cache', 'logout', 'captcha'];
  29. //初始化
  30. protected function initialize()
  31. {
  32. parent::initialize();
  33. //移除HTML标签
  34. $this->request->filter('trim,strip_tags,htmlspecialchars');
  35. }
  36. //后台首页
  37. public function index()
  38. {
  39. if ($this->request->isPost()) {
  40. return json($this->auth->getSidebar([
  41. 'auth.rule' => '菜单',
  42. 'addon' => ['new', 'red'],
  43. ]));
  44. }
  45. $this->assign('title', 'YZNCMS后台管理系统');
  46. return $this->fetch();
  47. }
  48. //登录判断
  49. public function login()
  50. {
  51. $url = $this->request->get('url', '', 'url_clean');
  52. $url = $url && !preg_match('/\/index\/(login|logout)$/', $url) ? $url : 'index/index';
  53. if ($this->auth->isLogin()) {
  54. $this->success("你已经登录,无需重复登录", $url);
  55. }
  56. //保持会话有效时长,单位:小时
  57. $keeyloginhours = 24;
  58. if ($this->request->isPost()) {
  59. $data = $this->request->post();
  60. $keeplogin = $this->request->post('keeplogin');
  61. $rule = [
  62. 'verify|验证码' => 'require|captcha',
  63. 'username|用户名' => 'require|alphaDash|length:3,30',
  64. 'password|密码' => 'require|length:6,30',
  65. '__token__' => 'require|token',
  66. ];
  67. Adminlog::setTitle('登录');
  68. try {
  69. $this->validate($data, $rule);
  70. } catch (ValidateException $e) {
  71. // 验证失败 输出错误信息
  72. $this->error($e->getError(), $url, ['token' => $this->request->buildToken()]);
  73. }
  74. if ($this->auth->login($data['username'], $data['password'], $keeplogin ? $keeyloginhours * 3600 : 0)) {
  75. Event::trigger("admin_login_after", $this->request);
  76. $this->success('恭喜您,登陆成功', $url);
  77. } else {
  78. $msg = $this->auth->getError();
  79. $msg = $msg ?: '用户名或者密码错误!';
  80. $this->error($msg, $url, ['token' => $this->request->buildToken()]);
  81. }
  82. }
  83. if ($this->auth->autologin()) {
  84. Session::delete("referer");
  85. $this->redirect($url);
  86. }
  87. Event::trigger("admin_login_init", $this->request);
  88. $this->assign('keeyloginhours', $keeyloginhours);
  89. $this->assign('title', '登录');
  90. return $this->fetch();
  91. }
  92. //手动退出登录
  93. public function logout()
  94. {
  95. if ($this->auth->logout()) {
  96. Event::trigger("admin_logout_after", $this->request);
  97. $this->success('注销成功!', url("admin/index/login"));
  98. }
  99. }
  100. //缓存更新
  101. public function cache()
  102. {
  103. $runtimePath = app()->getRootPath() . 'runtime' . DS;
  104. try {
  105. $type = $this->request->request("type");
  106. switch ($type) {
  107. case 'all':
  108. case 'data':
  109. File::del_dir($runtimePath . 'cache');
  110. Cache::clear();
  111. if ($type == 'data') {
  112. break;
  113. }
  114. case 'template':
  115. // 模板缓存
  116. File::del_dir($runtimePath . 'admin' . DS . 'temp'); //后台
  117. File::del_dir($runtimePath . 'index' . DS . 'temp'); //前台
  118. File::del_dir($runtimePath . 'temp');
  119. if ($type == 'template') {
  120. break;
  121. }
  122. case 'addons':
  123. // 插件缓存
  124. Service::refresh();
  125. if ($type == 'addons') {
  126. break;
  127. }
  128. case 'browser':
  129. // 浏览器缓存
  130. // 只有生产环境下才修改
  131. if (!Env::get('APP_DEBUG')) {
  132. $version = config('site.version');
  133. $newversion = preg_replace_callback("/(.*)\.([0-9]+)\$/", function ($match) {
  134. return $match[1] . '.' . ($match[2] + 1);
  135. }, $version);
  136. if ($newversion && $newversion != $version) {
  137. Db::startTrans();
  138. try {
  139. \app\common\model\Config::where('name', 'version')->update(['value' => $newversion]);
  140. \app\common\model\Config::refreshFile();
  141. Db::commit();
  142. } catch (\Exception $e) {
  143. Db::rollback();
  144. throw new \Exception($e->getMessage());
  145. }
  146. }
  147. }
  148. if ($type == 'browser') {
  149. break;
  150. }
  151. }
  152. } catch (\Exception $e) {
  153. $this->error($e->getMessage());
  154. }
  155. Event::trigger("wipecache_after");
  156. $this->success('清理缓存');
  157. }
  158. //验证码
  159. public function captcha()
  160. {
  161. return \think\captcha\facade\Captcha::create();
  162. }
  163. }