Index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller;
  4. use Throwable;
  5. use ba\ClickCaptcha;
  6. use think\facade\Config;
  7. use think\facade\Validate;
  8. use app\common\facade\Token;
  9. use app\admin\model\AdminLog;
  10. use app\common\controller\Backend;
  11. class Index extends Backend
  12. {
  13. protected array $noNeedLogin = ['logout', 'login','mplogin'];
  14. protected array $noNeedPermission = ['index'];
  15. /**
  16. * 后台初始化请求
  17. * @return void
  18. * @throws Throwable
  19. */
  20. public function index(): void
  21. {
  22. $adminInfo = $this->auth->getInfo();
  23. $adminInfo['super'] = $this->auth->isSuperAdmin();
  24. unset($adminInfo['token'], $adminInfo['refresh_token']);
  25. $menus = $this->auth->getMenus();
  26. if (!$menus) {
  27. $this->error(__('No background menu, please contact super administrator!'));
  28. }
  29. $this->success('', [
  30. 'adminInfo' => $adminInfo,
  31. 'menus' => $menus,
  32. 'siteConfig' => [
  33. 'siteName' => get_sys_config('site_name'),
  34. 'version' => get_sys_config('version'),
  35. 'cdnUrl' => full_url(),
  36. 'apiUrl' => Config::get('buildadmin.api_url'),
  37. 'upload' => get_upload_config(),
  38. ],
  39. 'terminal' => [
  40. 'installServicePort' => Config::get('terminal.install_service_port'),
  41. 'npmPackageManager' => Config::get('terminal.npm_package_manager'),
  42. ]
  43. ]);
  44. }
  45. /**
  46. * 管理员登录
  47. * @return void
  48. * @throws Throwable
  49. */
  50. public function login(): void
  51. {
  52. // 检查登录态
  53. if ($this->auth->isLogin()) {
  54. $this->success(__('You have already logged in. There is no need to log in again~'), [
  55. 'type' => $this->auth::LOGGED_IN
  56. ], $this->auth::LOGIN_RESPONSE_CODE);
  57. }
  58. $captchaSwitch = Config::get('buildadmin.admin_login_captcha');
  59. // 检查提交
  60. if ($this->request->isPost()) {
  61. $username = $this->request->post('username');
  62. $password = $this->request->post('password');
  63. $keep = $this->request->post('keep');
  64. $rule = [
  65. 'username|' . __('Username') => 'require|length:3,30',
  66. 'password|' . __('Password') => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
  67. ];
  68. $data = [
  69. 'username' => $username,
  70. 'password' => $password,
  71. ];
  72. if ($captchaSwitch) {
  73. $rule['captchaId|' . __('CaptchaId')] = 'require';
  74. $rule['captchaInfo|' . __('Captcha')] = 'require';
  75. $data['captchaId'] = $this->request->post('captchaId');
  76. $data['captchaInfo'] = $this->request->post('captchaInfo');
  77. }
  78. $validate = Validate::rule($rule);
  79. if (!$validate->check($data)) {
  80. $this->error($validate->getError());
  81. }
  82. if ($captchaSwitch) {
  83. $captchaObj = new ClickCaptcha();
  84. if (!$captchaObj->check($data['captchaId'], $data['captchaInfo'])) {
  85. $this->error(__('Captcha error'));
  86. }
  87. }
  88. AdminLog::setTitle(__('Login'));
  89. $res = $this->auth->login($username, $password, (bool)$keep);
  90. if ($res === true) {
  91. $this->success(__('Login succeeded!'), [
  92. 'userInfo' => $this->auth->getInfo(),
  93. 'routePath' => '/admin'
  94. ]);
  95. } else {
  96. $msg = $this->auth->getError();
  97. $msg = $msg ?: __('Incorrect user name or password!');
  98. $this->error($msg);
  99. }
  100. }
  101. $this->success('', [
  102. 'captcha' => $captchaSwitch
  103. ]);
  104. }
  105. /**
  106. * 管理员登录
  107. * @return void
  108. * @throws Throwable
  109. */
  110. public function mplogin(): void
  111. {
  112. // 检查登录态
  113. if ($this->auth->isLogin()) {
  114. $this->success(__('You have already logged in. There is no need to log in again~'), [
  115. 'type' => $this->auth::LOGGED_IN
  116. ], $this->auth::LOGIN_RESPONSE_CODE);
  117. }
  118. // $captchaSwitch = Config::get('buildadmin.admin_login_captcha');
  119. // halt($captchaSwitch);
  120. // 检查提交
  121. if ($this->request->isPost()) {
  122. $username = $this->request->post('username');
  123. $password = $this->request->post('password');
  124. // $keep = $this->request->post('keep');
  125. $rule = [
  126. 'username|' . __('Username') => 'require|length:3,30',
  127. 'password|' . __('Password') => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
  128. ];
  129. $data = [
  130. 'username' => $username,
  131. 'password' => $password,
  132. ];
  133. $validate = Validate::rule($rule);
  134. if (!$validate->check($data)) {
  135. $this->error($validate->getError());
  136. }
  137. AdminLog::setTitle(__('Login'));
  138. $res = $this->auth->mplogin($username, $password);
  139. if ($res === true) {
  140. $this->success(__('Login succeeded!'), [
  141. 'userInfo' => $this->auth->getInfo(),
  142. 'routePath' => '/admin'
  143. ]);
  144. } else {
  145. $msg = $this->auth->getError();
  146. $msg = $msg ?: __('Incorrect user name or password!');
  147. $this->error($msg);
  148. }
  149. }
  150. $this->success('', [
  151. ]);
  152. }
  153. /**
  154. * 管理员注销
  155. * @return void
  156. */
  157. public function logout(): void
  158. {
  159. if ($this->request->isPost()) {
  160. $refreshToken = $this->request->post('refreshToken', '');
  161. if ($refreshToken) Token::delete((string)$refreshToken);
  162. $this->auth->logout();
  163. $this->success();
  164. }
  165. }
  166. }