Files
NewTicket/app/index/controller/Login.php
2026-08-14 11:48:53 +08:00

51 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\index\controller;
use app\BaseController;
use app\common\service\AuthService;
use think\facade\Session;
// 已完成
class Login extends BaseController
{
/*
处理登录请求(已完成)
*/
public function login(AuthService $authService)
{
$params = $this->request->only(['username', 'password'], 'post');
if (empty(trim($params['username'])) || empty(trim($params['password']))) { // 验证数据
return $this->error('用户名和密码不能为空');
}
$username = trim($params['username']);
$failKey = 'login_fail_' . md5($username);
$fails = (int) cache($failKey); // 读取失败次数,并强制转为整数
if ($fails >= 5) {
return $this->error('失败次数过多,请 15 分钟后再试', [], 429);
}
try {
$result = $authService->login(trim($params['username']), trim($params['password']));
cache($failKey, null);
return $this->success('登录成功', $result);
} catch (\Exception $e) {
cache($failKey, $fails + 1, 900); // 失败次数存活时间15分钟之后自动清零
return $this->error($e->getMessage());
}
}
/*
退出登录(已完成)
*/
public function logout()
{
Session::clear();
return $this->success('退出成功');
}
}