Index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use app\admin\service\AuthService;
  15. use library\Controller;
  16. use library\tools\Data;
  17. use think\Console;
  18. use think\Db;
  19. /**
  20. * 后台入口管理
  21. * Class Index
  22. * @package app\admin\controller
  23. */
  24. class Index extends Controller
  25. {
  26. /**
  27. * 显示后台首页
  28. * @throws \ReflectionException
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public function index()
  34. {
  35. $this->title = '系统管理后台';
  36. $this->menus = AuthService::getAuthMenu();
  37. if (empty($this->menus) && !session('user.id')) {
  38. $this->redirect('@admin/login');
  39. } else {
  40. $this->fetch();
  41. }
  42. }
  43. /**
  44. * 后台环境信息
  45. * @return mixed
  46. */
  47. public function main()
  48. {
  49. $this->think_ver = \think\App::VERSION;
  50. $this->mysql_ver = Db::query('select version() as ver')[0]['ver'];
  51. $this->fetch();
  52. }
  53. /**
  54. * 清理系统运行缓存
  55. */
  56. public function clearRuntime()
  57. {
  58. if (!AuthService::isLogin()) {
  59. $this->error('需要登录才能操作哦!');
  60. }
  61. $this->list = [
  62. [
  63. 'title' => 'Clean up running cached files',
  64. 'message' => nl2br(Console::call('clear')->fetch()),
  65. ], [
  66. 'title' => 'Clean up invalid session files',
  67. 'message' => nl2br(Console::call('xclean:session')->fetch()),
  68. ],
  69. ];
  70. $this->fetch('admin@index/command');
  71. }
  72. /**
  73. * 压缩发布系统
  74. */
  75. public function buildOptimize()
  76. {
  77. if (!AuthService::isLogin()) {
  78. $this->error('需要登录才能操作哦!');
  79. }
  80. $this->list = [
  81. [
  82. 'title' => 'Build route cache',
  83. 'message' => nl2br(Console::call('optimize:route')->fetch()),
  84. ], [
  85. 'title' => 'Build database schema cache',
  86. 'message' => nl2br(Console::call('optimize:schema')->fetch()),
  87. ], [
  88. 'title' => 'Optimizes PSR0 and PSR4 packages',
  89. 'message' => nl2br(Console::call('optimize:autoload')->fetch()),
  90. ], [
  91. 'title' => 'Build config and common file cache',
  92. 'message' => nl2br(Console::call('optimize:config')->fetch()),
  93. ],
  94. ];
  95. $this->fetch('admin@index/command');
  96. }
  97. /**
  98. * 修改密码
  99. * @param integer $id
  100. * @throws \think\Exception
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @throws \think\exception\DbException
  104. * @throws \think\exception\PDOException
  105. */
  106. public function pass($id)
  107. {
  108. $this->applyCsrfToken();
  109. if (intval($id) !== intval(session('user.id'))) {
  110. $this->error('只能修改当前用户的密码!');
  111. }
  112. if ($this->request->isGet()) {
  113. $this->verify = true;
  114. $this->_form('SystemUser', 'admin@user/pass', 'id', [], ['id' => $id]);
  115. } else {
  116. $data = $this->_input([
  117. 'password' => $this->request->post('password'),
  118. 'repassword' => $this->request->post('repassword'),
  119. 'oldpassword' => $this->request->post('oldpassword'),
  120. ], [
  121. 'oldpassword' => 'require',
  122. 'password' => 'require|min:4',
  123. 'repassword' => 'require|confirm:password',
  124. ], [
  125. 'oldpassword.require' => '旧密码不能为空!',
  126. 'password.require' => '登录密码不能为空!',
  127. 'password.min' => '登录密码长度不能少于4位有效字符!',
  128. 'repassword.require' => '重复密码不能为空!',
  129. 'repassword.confirm' => '重复密码与登录密码不匹配,请重新输入!',
  130. ]);
  131. $user = Db::name('SystemUser')->where(['id' => $id])->find();
  132. if (md5($data['oldpassword']) !== $user['password']) {
  133. $this->error('旧密码验证失败,请重新输入!');
  134. }
  135. $result = AuthService::checkPassword($data['password']);
  136. if (empty($result['code'])) $this->error($result['msg']);
  137. if (Data::save('SystemUser', ['id' => $user['id'], 'password' => md5($data['password'])])) {
  138. $this->success('密码修改成功,下次请使用新密码登录!', '');
  139. } else {
  140. $this->error('密码修改失败,请稍候再试!');
  141. }
  142. }
  143. }
  144. /**
  145. * 修改用户资料
  146. * @param integer $id 会员ID
  147. */
  148. public function info($id = 0)
  149. {
  150. $this->applyCsrfToken();
  151. if (intval($id) === intval(session('user.id'))) {
  152. $this->_form('SystemUser', 'admin@user/form', 'id', [], ['id' => $id]);
  153. } else {
  154. $this->error('只能修改登录用户的资料!');
  155. }
  156. }
  157. }