Index.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\SystemAdmin;
  4. use app\common\model\SystemQuick;
  5. use app\common\controller\AdminController;
  6. use think\App;
  7. use think\facade\Env;
  8. class Index extends AdminController
  9. {
  10. /**
  11. * 后台主页
  12. * @return string
  13. * @throws \Exception
  14. */
  15. public function index()
  16. {
  17. return $this->fetch('', [
  18. 'admin' => session('admin'),
  19. ]);
  20. }
  21. /**
  22. * 后台欢迎页
  23. * @return string
  24. * @throws \Exception
  25. */
  26. public function welcome()
  27. {
  28. $quicks = SystemQuick::field('id,title,icon,href')
  29. ->where(['status' => 1])
  30. ->order('sort', 'desc')
  31. ->limit(8)
  32. ->select();
  33. $this->assign('quicks', $quicks);
  34. return $this->fetch();
  35. }
  36. /**
  37. * 修改管理员信息
  38. * @return string
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function editAdmin()
  44. {
  45. $id = session('admin.id');
  46. $row = (new SystemAdmin())
  47. ->withoutField('password')
  48. ->find($id);
  49. empty($row) && $this->error('用户信息不存在');
  50. if ($this->request->isPost()) {
  51. $post = $this->request->post();
  52. $this->isDemo && $this->error('演示环境下不允许修改');
  53. $rule = [];
  54. $this->validate($post, $rule);
  55. try {
  56. $save = $row
  57. ->allowField(['head_img', 'phone', 'remark', 'update_time'])
  58. ->save($post);
  59. } catch (\Exception $e) {
  60. $this->error('保存失败');
  61. }
  62. $save ? $this->success('保存成功') : $this->error('保存失败');
  63. }
  64. $this->assign('row', $row);
  65. return $this->fetch();
  66. }
  67. /**
  68. * 修改密码
  69. * @return string
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function editPassword()
  75. {
  76. $id = session('admin.id');
  77. $row = (new SystemAdmin())
  78. ->withoutField('password')
  79. ->find($id);
  80. if (!$row) {
  81. $this->error('用户信息不存在');
  82. }
  83. if ($this->request->isPost()) {
  84. $post = $this->request->post();
  85. $this->isDemo && $this->error('演示环境下不允许修改');
  86. $rule = [
  87. 'password|登录密码' => 'require',
  88. 'password_again|确认密码' => 'require',
  89. ];
  90. $this->validate($post, $rule);
  91. if ($post['password'] != $post['password_again']) {
  92. $this->error('两次密码输入不一致');
  93. }
  94. try {
  95. $save = $row->save([
  96. 'password' => password($post['password']),
  97. ]);
  98. } catch (\Exception $e) {
  99. $this->error('保存失败');
  100. }
  101. if ($save) {
  102. $this->success('保存成功');
  103. } else {
  104. $this->error('保存失败');
  105. }
  106. }
  107. $this->assign('row', $row);
  108. return $this->fetch();
  109. }
  110. }