123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- declare (strict_types=1);
- namespace app\admin\controller;
- use Throwable;
- use ba\ClickCaptcha;
- use think\facade\Config;
- use think\facade\Validate;
- use app\common\facade\Token;
- use app\admin\model\AdminLog;
- use app\common\controller\Backend;
- class Index extends Backend
- {
- protected array $noNeedLogin = ['logout', 'login','mplogin'];
- protected array $noNeedPermission = ['index'];
- /**
- * 后台初始化请求
- * @return void
- * @throws Throwable
- */
- public function index(): void
- {
- $adminInfo = $this->auth->getInfo();
- $adminInfo['super'] = $this->auth->isSuperAdmin();
- unset($adminInfo['token'], $adminInfo['refresh_token']);
- $menus = $this->auth->getMenus();
- if (!$menus) {
- $this->error(__('No background menu, please contact super administrator!'));
- }
- $this->success('', [
- 'adminInfo' => $adminInfo,
- 'menus' => $menus,
- 'siteConfig' => [
- 'siteName' => get_sys_config('site_name'),
- 'version' => get_sys_config('version'),
- 'cdnUrl' => full_url(),
- 'apiUrl' => Config::get('buildadmin.api_url'),
- 'upload' => get_upload_config(),
- ],
- 'terminal' => [
- 'installServicePort' => Config::get('terminal.install_service_port'),
- 'npmPackageManager' => Config::get('terminal.npm_package_manager'),
- ]
- ]);
- }
-
- /**
- * 管理员登录
- * @return void
- * @throws Throwable
- */
- public function login(): void
- {
- // 检查登录态
- if ($this->auth->isLogin()) {
- $this->success(__('You have already logged in. There is no need to log in again~'), [
- 'type' => $this->auth::LOGGED_IN
- ], $this->auth::LOGIN_RESPONSE_CODE);
- }
- $captchaSwitch = Config::get('buildadmin.admin_login_captcha');
- // 检查提交
- if ($this->request->isPost()) {
- $username = $this->request->post('username');
- $password = $this->request->post('password');
- $keep = $this->request->post('keep');
- $rule = [
- 'username|' . __('Username') => 'require|length:3,30',
- 'password|' . __('Password') => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
- ];
- $data = [
- 'username' => $username,
- 'password' => $password,
- ];
- if ($captchaSwitch) {
- $rule['captchaId|' . __('CaptchaId')] = 'require';
- $rule['captchaInfo|' . __('Captcha')] = 'require';
- $data['captchaId'] = $this->request->post('captchaId');
- $data['captchaInfo'] = $this->request->post('captchaInfo');
- }
- $validate = Validate::rule($rule);
- if (!$validate->check($data)) {
- $this->error($validate->getError());
- }
- if ($captchaSwitch) {
- $captchaObj = new ClickCaptcha();
- if (!$captchaObj->check($data['captchaId'], $data['captchaInfo'])) {
- $this->error(__('Captcha error'));
- }
- }
- AdminLog::setTitle(__('Login'));
- $res = $this->auth->login($username, $password, (bool)$keep);
- if ($res === true) {
- $this->success(__('Login succeeded!'), [
- 'userInfo' => $this->auth->getInfo(),
- 'routePath' => '/admin'
- ]);
- } else {
- $msg = $this->auth->getError();
- $msg = $msg ?: __('Incorrect user name or password!');
- $this->error($msg);
- }
- }
- $this->success('', [
- 'captcha' => $captchaSwitch
- ]);
- }
-
- /**
- * 管理员登录
- * @return void
- * @throws Throwable
- */
- public function mplogin(): void
- {
- // 检查登录态
- if ($this->auth->isLogin()) {
- $this->success(__('You have already logged in. There is no need to log in again~'), [
- 'type' => $this->auth::LOGGED_IN
- ], $this->auth::LOGIN_RESPONSE_CODE);
- }
- // $captchaSwitch = Config::get('buildadmin.admin_login_captcha');
- // halt($captchaSwitch);
- // 检查提交
- if ($this->request->isPost()) {
-
- $username = $this->request->post('username');
- $password = $this->request->post('password');
-
- // $keep = $this->request->post('keep');
- $rule = [
- 'username|' . __('Username') => 'require|length:3,30',
- 'password|' . __('Password') => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
- ];
- $data = [
- 'username' => $username,
- 'password' => $password,
- ];
-
- $validate = Validate::rule($rule);
- if (!$validate->check($data)) {
- $this->error($validate->getError());
- }
- AdminLog::setTitle(__('Login'));
- $res = $this->auth->mplogin($username, $password);
- if ($res === true) {
- $this->success(__('Login succeeded!'), [
- 'userInfo' => $this->auth->getInfo(),
- 'routePath' => '/admin'
- ]);
- } else {
- $msg = $this->auth->getError();
- $msg = $msg ?: __('Incorrect user name or password!');
- $this->error($msg);
- }
- }
- $this->success('', [
- ]);
- }
- /**
- * 管理员注销
- * @return void
- */
- public function logout(): void
- {
- if ($this->request->isPost()) {
- $refreshToken = $this->request->post('refreshToken', '');
- if ($refreshToken) Token::delete((string)$refreshToken);
- $this->auth->logout();
- $this->success();
- }
- }
- }
|