Index.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemAdmin;
  4. use app\admin\model\SystemQuick;
  5. use app\common\controller\AdminController;
  6. use app\Request;
  7. use Exception;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\facade\Db;
  12. class Index extends AdminController
  13. {
  14. /**
  15. * 后台主页
  16. * @param Request $request
  17. * @return string
  18. */
  19. public function index(Request $request): string
  20. {
  21. return $this->fetch('', ['admin' => $request->adminUserInfo,]);
  22. }
  23. /**
  24. * 后台欢迎页
  25. * @return string
  26. * @throws Exception
  27. */
  28. public function welcome(): string
  29. {
  30. $tpVersion = \think\facade\App::version();
  31. $mysqlVersion = Db::query("select version() as version")[0]['version'] ?? '未知';
  32. $phpVersion = phpversion();
  33. $versions = compact('tpVersion', 'mysqlVersion', 'phpVersion');
  34. $quick_list = SystemQuick::field('id,title,icon,href')
  35. ->where(['status' => 1])->order('sort', 'desc')->limit(50)->select()->toArray();
  36. $quicks = array_chunk($quick_list, 8);
  37. $this->assign(compact('quicks', 'versions'));
  38. return $this->fetch();
  39. }
  40. /**
  41. * 修改管理员信息
  42. * @param Request $request
  43. * @return string
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. */
  48. public function editAdmin(Request $request): string
  49. {
  50. $id = $this->adminUid;
  51. $row = (new SystemAdmin())
  52. ->withoutField('password')
  53. ->find($id);
  54. empty($row) && $this->error('用户信息不存在');
  55. if ($request->isPost()) {
  56. $post = $request->post();
  57. $this->isDemo && $this->error('演示环境下不允许修改');
  58. $rule = [];
  59. $this->validate($post, $rule);
  60. try {
  61. $login_type = $post['login_type'] ?? 1;
  62. if ($login_type == 2) {
  63. $ga_secret = (new SystemAdmin())->where('id', $id)->value('ga_secret');
  64. if (empty($ga_secret)) $this->error('请先绑定谷歌验证器');
  65. }
  66. $save = $row->allowField(['head_img', 'phone', 'remark', 'update_time', 'login_type'])->save($post);
  67. }catch (\PDOException $e) {
  68. $this->error('保存失败');
  69. }
  70. $save ? $this->success('保存成功') : $this->error('保存失败');
  71. }
  72. $this->assign('row', $row);
  73. $notes = (new SystemAdmin())->notes;
  74. $this->assign('notes', $notes);
  75. return $this->fetch();
  76. }
  77. /**
  78. * 修改密码
  79. * @param Request $request
  80. * @return string
  81. */
  82. public function editPassword(Request $request): string
  83. {
  84. $id = $this->adminUid;
  85. $row = (new SystemAdmin())
  86. ->withoutField('password')
  87. ->find($id);
  88. if (!$row) {
  89. $this->error('用户信息不存在');
  90. }
  91. if ($request->isPost()) {
  92. $post = $request->post();
  93. $this->isDemo && $this->error('演示环境下不允许修改');
  94. $rule = [
  95. 'password|登录密码' => 'require',
  96. 'password_again|确认密码' => 'require',
  97. ];
  98. $this->validate($post, $rule);
  99. if ($post['password'] != $post['password_again']) {
  100. $this->error('两次密码输入不一致');
  101. }
  102. try {
  103. $save = $row->save([
  104. 'password' => password_hash($post['password'], PASSWORD_DEFAULT),
  105. ]);
  106. }catch (Exception $e) {
  107. $this->error('保存失败');
  108. }
  109. if ($save) {
  110. $this->success('保存成功');
  111. }else {
  112. $this->error('保存失败');
  113. }
  114. }
  115. $this->assign('row', $row);
  116. return $this->fetch();
  117. }
  118. /**
  119. * 设置谷歌验证码
  120. * @param Request $request
  121. * @return string
  122. * @throws Exception
  123. */
  124. public function set2fa(Request $request): string
  125. {
  126. $id = $this->adminUid;
  127. $row = (new SystemAdmin())->withoutField('password')->find($id);
  128. if (!$row) $this->error('用户信息不存在');
  129. // You can see: https://gitee.com/wolf-code/authenticator
  130. $ga = new \Wolfcode\Authenticator\google\PHPGangstaGoogleAuthenticator();
  131. if (!$request->isAjax()) {
  132. $old_secret = $row->ga_secret;
  133. $secret = $ga->createSecret(32);
  134. $ga_title = $this->isDemo ? 'EasyAdmin8演示环境' : '可自定义修改显示标题';
  135. $dataUri = $ga->getQRCode($ga_title, $secret);
  136. $this->assign(compact('row', 'dataUri', 'old_secret', 'secret'));
  137. return $this->fetch();
  138. }
  139. $this->isDemo && $this->error('演示环境下不允许修改');
  140. $post = $request->post();
  141. $ga_secret = $post['ga_secret'] ?? '';
  142. $ga_code = $post['ga_code'] ?? '';
  143. if (empty($ga_code)) $this->error('请输入验证码');
  144. if (!$ga->verifyCode($ga_secret, $ga_code)) $this->error('验证码错误');
  145. $row->ga_secret = $ga_secret;
  146. $row->login_type = 2;
  147. $row->save();
  148. $this->success('操作成功');
  149. }
  150. }