tapi/app/controller/admin/auth/LoginController.php
2025-08-27 20:20:24 +08:00

46 lines
1.5 KiB
PHP

<?php
namespace app\controller\admin\auth;
use app\BaseController;
use app\entity\SysUser;
use app\enum\UserTypeEnum;
use app\http\HttpAuth;
use app\service\admin\LoginService;
use think\exception\ValidateException;
class LoginController extends BaseController
{
public function captcha()
{
$captcha = file_get_contents("https://v2.eleadmin.com/api/captcha");
$captchaData = json_decode($captcha, true);
return $this->writeSuccess('登录成功', [
'base64' => $captchaData['data']['base64'] ?? '',
'text' => $captchaData['data']['text'] ?? '',
]);
}
public function index()
{
$username = $this->request->post('username', '');
$password = $this->request->post('password', '');
$remember = $this->request->post('remember', false);
$code = $this->request->post('code', '');
$loginService = new LoginService();
try {
$result = $loginService->login($this->request, $this->request->getClient(), $username, $password, $code, $remember);
// 绑定Auth
$this->request->setAuth(new HttpAuth($result['user']['userId'], UserTypeEnum::USER));
security_log_record([SysUser::class, "userLogin"], "{$username}登录了系统", '');
} catch (ValidateException $e) {
return $this->writeError($e->getError());
}
return $this->writeSuccess('登录成功', ['user' => $result['user'], 'access_token' => $result['access_token']]);
}
}