Index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller;
  4. use app\common\facade\Token;
  5. use ba\ClickCaptcha;
  6. use think\facade\Config;
  7. use think\facade\Validate;
  8. use app\common\controller\Backend;
  9. use app\admin\model\AdminLog;
  10. class Index extends Backend
  11. {
  12. protected $noNeedLogin = ['logout', 'login'];
  13. protected $noNeedPermission = ['index'];
  14. public function index()
  15. {
  16. $adminInfo = $this->auth->getInfo();
  17. $adminInfo['super'] = $this->auth->isSuperAdmin();
  18. unset($adminInfo['token'], $adminInfo['refreshToken']);
  19. $menus = $this->auth->getMenus();
  20. if (!$menus) {
  21. $this->error(__('No background menu, please contact super administrator!'));
  22. }
  23. $this->success('', [
  24. 'adminInfo' => $adminInfo,
  25. 'menus' => $menus,
  26. 'siteConfig' => [
  27. 'siteName' => get_sys_config('site_name'),
  28. 'version' => get_sys_config('version'),
  29. 'cdnUrl' => full_url(),
  30. 'apiUrl' => Config::get('buildadmin.api_url'),
  31. 'upload' => get_upload_config(),
  32. ],
  33. 'terminal' => [
  34. 'installServicePort' => Config::get('terminal.install_service_port'),
  35. 'npmPackageManager' => Config::get('terminal.npm_package_manager'),
  36. ]
  37. ]);
  38. }
  39. public function login()
  40. {
  41. // 检查登录态
  42. if ($this->auth->isLogin()) {
  43. $this->success(__('You have already logged in. There is no need to log in again~'), [
  44. 'routePath' => '/admin'
  45. ], 302);
  46. }
  47. $captchaSwitch = Config::get('buildadmin.admin_login_captcha');
  48. // 检查提交
  49. if ($this->request->isPost()) {
  50. $username = $this->request->post('username');
  51. $password = $this->request->post('password');
  52. $keep = $this->request->post('keep');
  53. $rule = [
  54. 'username|' . __('Username') => 'require|length:3,30',
  55. 'password|' . __('Password') => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
  56. ];
  57. $data = [
  58. 'username' => $username,
  59. 'password' => $password,
  60. ];
  61. if ($captchaSwitch) {
  62. $rule['captchaId|' . __('CaptchaId')] = 'require';
  63. $rule['captchaInfo|' . __('Captcha')] = 'require';
  64. $data['captchaId'] = $this->request->post('captchaId');
  65. $data['captchaInfo'] = $this->request->post('captchaInfo');
  66. }
  67. $validate = Validate::rule($rule);
  68. if (!$validate->check($data)) {
  69. $this->error($validate->getError());
  70. }
  71. if ($captchaSwitch) {
  72. $captchaObj = new ClickCaptcha();
  73. if (!$captchaObj->check($data['captchaId'], $data['captchaInfo'])) {
  74. $this->error(__('Captcha error'));
  75. }
  76. }
  77. AdminLog::setTitle(__('Login'));
  78. $res = $this->auth->login($username, $password, (bool)$keep);
  79. if ($res === true) {
  80. $this->success(__('Login succeeded!'), [
  81. 'userInfo' => $this->auth->getInfo(),
  82. 'routePath' => '/admin'
  83. ]);
  84. } else {
  85. $msg = $this->auth->getError();
  86. $msg = $msg ?: __('Incorrect user name or password!');
  87. $this->error($msg);
  88. }
  89. }
  90. $this->success('', [
  91. 'captcha' => $captchaSwitch
  92. ]);
  93. }
  94. public function logout()
  95. {
  96. if ($this->request->isPost()) {
  97. $refreshToken = $this->request->post('refresh_token', '');
  98. if ($refreshToken) Token::delete((string)$refreshToken);
  99. $this->auth->logout();
  100. $this->success();
  101. }
  102. }
  103. }