Index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $counts = [
  29. 'user_count'=> rand(1000,9999),
  30. 'goods_count'=> rand(1000,9999),
  31. 'recharge_total'=> rand(1000,9999),
  32. 'withdraw_total'=> rand(1000,9999),
  33. ];
  34. $quicks = SystemQuick::field('id,title,icon,href')
  35. ->where(['status' => 1])
  36. ->order('sort')
  37. ->limit(8)
  38. ->select();
  39. $this->assign('quicks', $quicks);
  40. $this->assign('counts', $counts);
  41. return $this->fetch();
  42. }
  43. /**
  44. * 修改管理员信息
  45. * @return string
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function editAdmin()
  51. {
  52. $id = session('admin.id');
  53. $row = (new SystemAdmin())
  54. ->withoutField('password')
  55. ->find($id);
  56. empty($row) && $this->error('用户信息不存在');
  57. if ($this->request->isPost()) {
  58. $post = $this->request->post();
  59. $this->isDemo && $this->error('演示环境下不允许修改');
  60. $rule = [];
  61. $this->validate($post, $rule);
  62. try {
  63. $save = $row
  64. ->allowField(['head_img', 'phone', 'remark', 'update_time'])
  65. ->save($post);
  66. } catch (\Exception $e) {
  67. $this->error('保存失败');
  68. }
  69. $save ? $this->success('保存成功') : $this->error('保存失败');
  70. }
  71. $this->assign('row', $row);
  72. return $this->fetch();
  73. }
  74. /**
  75. * 修改密码
  76. * @return string
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function editPassword()
  82. {
  83. $id = session('admin.id');
  84. $row = (new SystemAdmin())
  85. ->withoutField('password')
  86. ->find($id);
  87. if (!$row) {
  88. $this->error('用户信息不存在');
  89. }
  90. if ($this->request->isPost()) {
  91. $post = $this->request->post();
  92. $this->isDemo && $this->error('演示环境下不允许修改');
  93. $rule = [
  94. 'password|登录密码' => 'require',
  95. 'password_again|确认密码' => 'require',
  96. ];
  97. $this->validate($post, $rule);
  98. if ($post['password'] != $post['password_again']) {
  99. $this->error('两次密码输入不一致');
  100. }
  101. try {
  102. $save = $row->save([
  103. 'password' => password($post['password']),
  104. ]);
  105. } catch (\Exception $e) {
  106. $this->error('保存失败');
  107. }
  108. if ($save) {
  109. $this->success('保存成功');
  110. } else {
  111. $this->error('保存失败');
  112. }
  113. }
  114. $this->assign('row', $row);
  115. return $this->fetch();
  116. }
  117. }