2022-08-19 19:48:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\admin\controller;
|
|
|
|
|
|
2022-11-28 19:11:12 +08:00
|
|
|
use support\Response;
|
|
|
|
|
use Webman\Event\Event;
|
2022-08-19 19:48:37 +08:00
|
|
|
use app\AdminController;
|
|
|
|
|
use app\common\model\system\Admin;
|
2022-11-28 19:11:12 +08:00
|
|
|
use app\common\model\system\AdminLog;
|
2022-08-22 19:07:32 +08:00
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
|
|
|
|
use think\db\exception\DataNotFoundException;
|
|
|
|
|
use think\db\exception\DbException;
|
|
|
|
|
use think\db\exception\ModelNotFoundException;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
class Login extends AdminController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 初始化方法
|
2022-08-22 19:07:32 +08:00
|
|
|
* @return void
|
2022-08-19 19:48:37 +08:00
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->model = new Admin();
|
|
|
|
|
$this->JumpUrl = '/admin/index';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录函数
|
2022-08-22 19:07:32 +08:00
|
|
|
* @return Response
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
|
|
|
|
public function index(): \support\Response
|
|
|
|
|
{
|
|
|
|
|
// 禁止重复访问
|
2022-12-02 11:16:57 +08:00
|
|
|
$session = get_admin_info();
|
|
|
|
|
if (isset($session['id'])) {
|
2022-08-19 19:48:37 +08:00
|
|
|
return $this->redirect('/admin/index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request()->isPost()) {
|
|
|
|
|
$user = request()->post('name');
|
|
|
|
|
$pwd = request()->post('pwd');
|
|
|
|
|
$captcha = request()->post('captcha');
|
2022-12-02 11:16:57 +08:00
|
|
|
if ((isset($session['count']) && $session['count'] >= 5)
|
|
|
|
|
&& (isset($session['time']) && $session['time'] >= strtotime('- 5 minutes'))) {
|
|
|
|
|
return $this->displayResponse('错误次数过多,请稍后再试!');
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证码
|
2022-12-02 11:16:57 +08:00
|
|
|
if (isset($session['isCaptcha'])) {
|
2022-08-19 19:48:37 +08:00
|
|
|
if (!$captcha || !$this->captchaCheck($captcha)) {
|
2022-12-02 11:16:57 +08:00
|
|
|
return $this->displayResponse('验证码错误!');
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证表单令牌
|
2022-12-02 11:16:57 +08:00
|
|
|
if (!request()->checkToken('__token__', request()->all())) {
|
|
|
|
|
return $this->displayResponse('表单令牌错误!', ['token' => token()]);
|
2022-08-19 19:48:37 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
$result = Admin::checkLogin($user, $pwd);
|
|
|
|
|
if (empty($result)) {
|
2022-12-02 11:16:57 +08:00
|
|
|
$session['time'] = time();
|
|
|
|
|
$session['isCaptcha'] = true;
|
|
|
|
|
$session['count'] = isset($session['count']) ? $session['count'] + 1 : 1;
|
|
|
|
|
request()->session()->set(AdminSession, $session);
|
|
|
|
|
// 执行登录失败事件
|
|
|
|
|
Event::emit('adminLoginError', request()->all());
|
|
|
|
|
return $this->displayResponse('用户名或密码错误!', ['token' => token()]);
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($result['status'] !== 1) {
|
2022-12-02 11:16:57 +08:00
|
|
|
return $this->displayResponse('账号已被禁用!');
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-23 11:11:42 +08:00
|
|
|
$result->login_ip = request()->getRealIp();
|
2022-08-19 19:48:37 +08:00
|
|
|
$result->login_time = time();
|
|
|
|
|
$result->count = $result->count + 1;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
$result->save();
|
2022-12-02 11:16:57 +08:00
|
|
|
$session = array_merge($session, $result->toArray());
|
2022-08-22 19:07:32 +08:00
|
|
|
request()->session()->set(AdminSession, $session);
|
2022-08-19 19:48:37 +08:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->error($th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Event::emit('adminLoginSuccess', $result->toArray());
|
2022-12-02 11:16:57 +08:00
|
|
|
return $this->displayResponse('登录成功!', [] , $this->JumpUrl);
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('login/index', [
|
2022-12-02 11:16:57 +08:00
|
|
|
'captcha' => $session['isCaptcha'] ?? false,
|
2022-08-19 19:48:37 +08:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 11:16:57 +08:00
|
|
|
/**
|
|
|
|
|
* 退出登录
|
|
|
|
|
* @param string $msg
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
private function displayResponse(string $msg = 'error', array $data = [], string $url = ''): Response
|
|
|
|
|
{
|
|
|
|
|
$this->adminLoginLog($msg, $url ? 1 : 0);
|
|
|
|
|
return empty($url) ? $this->error($msg, $url, $data) : $this->success($msg, $url);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 19:48:37 +08:00
|
|
|
/**
|
|
|
|
|
* 写入登录日志
|
|
|
|
|
* @param string $error
|
|
|
|
|
* @param int $status
|
|
|
|
|
*/
|
2022-12-02 11:16:57 +08:00
|
|
|
private function adminLoginLog(string $error, int $status = 0)
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
$name = \request()->input('name');
|
|
|
|
|
$userAgent = \request()->header('user-agent');
|
|
|
|
|
$nickname = $this->model->where('name', $name)->value('nickname');
|
|
|
|
|
if (preg_match('/.*?\((.*?)\).*?/', $userAgent, $matches)) {
|
|
|
|
|
$user_os = substr($matches[1], 0, strpos($matches[1], ';'));
|
|
|
|
|
} else {
|
|
|
|
|
$user_os = '未知';
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 11:16:57 +08:00
|
|
|
$user_browser = preg_replace('/[^(]+\((.*?)[^)]+\) .*?/', '$1', $userAgent);
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
$data = [
|
2022-08-23 11:11:42 +08:00
|
|
|
'user_ip' => request()->getRealIp(),
|
2022-08-19 19:48:37 +08:00
|
|
|
'user_agent' => $userAgent,
|
|
|
|
|
'user_os' => $user_os,
|
|
|
|
|
'user_browser' => $user_browser,
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'nickname' => $nickname ?? '未知',
|
|
|
|
|
'error' => $error,
|
|
|
|
|
'status' => $status,
|
|
|
|
|
];
|
|
|
|
|
|
2022-11-28 19:11:12 +08:00
|
|
|
AdminLog::create($data);
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
}
|