Index.php 4.0 KB

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