Index.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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'];
  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. 'apiUrl' => Config::get('buildadmin.api_url'),
  36. 'upload' => keys_to_camel_case(get_upload_config(), ['max_size', 'save_name', 'allowed_suffixes', 'allowed_mime_types']),
  37. 'cdnUrl' => full_url(),
  38. 'cdnUrlParams' => Config::get('buildadmin.cdn_url_params'),
  39. ],
  40. 'terminal' => [
  41. 'phpDevelopmentServer' => str_contains($_SERVER['SERVER_SOFTWARE'], 'Development Server'),
  42. 'npmPackageManager' => Config::get('terminal.npm_package_manager'),
  43. ]
  44. ]);
  45. }
  46. /**
  47. * 管理员登录
  48. * @return void
  49. * @throws Throwable
  50. */
  51. public function login(): void
  52. {
  53. // 检查登录态
  54. if ($this->auth->isLogin()) {
  55. $this->success(__('You have already logged in. There is no need to log in again~'), [
  56. 'type' => $this->auth::LOGGED_IN
  57. ], $this->auth::LOGIN_RESPONSE_CODE);
  58. }
  59. $captchaSwitch = Config::get('buildadmin.admin_login_captcha');
  60. // 检查提交
  61. if ($this->request->isPost()) {
  62. $username = $this->request->post('username');
  63. $password = $this->request->post('password');
  64. $keep = $this->request->post('keep');
  65. $rule = [
  66. 'username|' . __('Username') => 'require|length:3,30',
  67. 'password|' . __('Password') => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
  68. ];
  69. $data = [
  70. 'username' => $username,
  71. 'password' => $password,
  72. ];
  73. if ($captchaSwitch) {
  74. $rule['captchaId|' . __('CaptchaId')] = 'require';
  75. $rule['captchaInfo|' . __('Captcha')] = 'require';
  76. $data['captchaId'] = $this->request->post('captchaId');
  77. $data['captchaInfo'] = $this->request->post('captchaInfo');
  78. }
  79. $validate = Validate::rule($rule);
  80. if (!$validate->check($data)) {
  81. $this->error($validate->getError());
  82. }
  83. if ($captchaSwitch) {
  84. $captchaObj = new ClickCaptcha();
  85. if (!$captchaObj->check($data['captchaId'], $data['captchaInfo'])) {
  86. $this->error(__('Captcha error'));
  87. }
  88. }
  89. AdminLog::instance()->setTitle(__('Login'));
  90. $res = $this->auth->login($username, $password, (bool)$keep);
  91. if ($res === true) {
  92. $this->success(__('Login succeeded!'), [
  93. 'userInfo' => $this->auth->getInfo()
  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. */
  109. public function logout(): void
  110. {
  111. if ($this->request->isPost()) {
  112. $refreshToken = $this->request->post('refreshToken', '');
  113. if ($refreshToken) Token::delete((string)$refreshToken);
  114. $this->auth->logout();
  115. $this->success();
  116. }
  117. }
  118. }