2019-12-02 23:04:43 +08:00
|
|
|
<?php
|
2019-12-12 18:52:24 +08:00
|
|
|
namespace catchAdmin\user;
|
2019-12-02 23:04:43 +08:00
|
|
|
|
2019-12-12 09:13:44 +08:00
|
|
|
use catchAdmin\user\model\Users;
|
2019-12-12 18:52:24 +08:00
|
|
|
use catcher\exceptions\FailedException;
|
|
|
|
use catcher\exceptions\LoginFailedException;
|
2019-12-02 23:04:43 +08:00
|
|
|
use think\facade\Session;
|
|
|
|
|
|
|
|
class Auth
|
|
|
|
{
|
2019-12-12 18:52:24 +08:00
|
|
|
protected const USER_KEY = 'admin_user';
|
2019-12-02 23:04:43 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 登陆
|
|
|
|
*
|
|
|
|
* @time 2019年11月28日
|
|
|
|
* @param $params
|
|
|
|
* @throws LoginFailedException
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-12-12 18:52:24 +08:00
|
|
|
public static function login($params)
|
2019-12-02 23:04:43 +08:00
|
|
|
{
|
2019-12-12 18:52:24 +08:00
|
|
|
$user = Users::where('email', $params['email'])->find();
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:13:44 +08:00
|
|
|
if (!password_verify($params['password'], $user->password)) {
|
2019-12-02 23:04:43 +08:00
|
|
|
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:13:44 +08:00
|
|
|
if ($user->status == Users::DISABLE) {
|
|
|
|
throw new LoginFailedException('该用户已被禁用');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 记录用户登录
|
2019-12-14 22:59:38 +08:00
|
|
|
$user->last_login_ip = request()->ip();
|
2019-12-12 09:13:44 +08:00
|
|
|
$user->last_login_time = time();
|
|
|
|
$user->save();
|
|
|
|
|
2019-12-12 18:52:24 +08:00
|
|
|
Session::set(self::getLoginUserKey(), $user);
|
2019-12-02 23:04:43 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 退出登陆
|
|
|
|
*
|
|
|
|
* @time 2019年11月28日
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-12-12 18:52:24 +08:00
|
|
|
public static function logout(): bool
|
2019-12-02 23:04:43 +08:00
|
|
|
{
|
2019-12-12 18:52:24 +08:00
|
|
|
Session::delete(self::getLoginUserKey());
|
2019-12-02 23:04:43 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-12 18:52:24 +08:00
|
|
|
|
|
|
|
public static function user()
|
|
|
|
{
|
|
|
|
return Session::get(self::getLoginUserKey(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function getLoginUserKey(): string
|
|
|
|
{
|
|
|
|
return md5(self::USER_KEY);
|
|
|
|
}
|
2019-12-02 23:04:43 +08:00
|
|
|
}
|