更新
This commit is contained in:
@@ -17,7 +17,9 @@ namespace app\adminapi\service;
|
||||
|
||||
use app\common\cache\AdminTokenCache;
|
||||
use app\common\model\auth\AdminSession;
|
||||
use app\common\model\auth\Admin;
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 管理员token
|
||||
@@ -40,6 +42,9 @@ class AdminTokenService
|
||||
*/
|
||||
public static function setToken($adminId, $terminal, $multipointLogin = 1)
|
||||
{
|
||||
return Db::transaction(static function () use ($adminId, $terminal, $multipointLogin) {
|
||||
$admin = Admin::where('id', $adminId)->lock(true)->findOrEmpty();
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0) { return false; }
|
||||
$time = time();
|
||||
$adminSession = AdminSession::where([['admin_id', '=', $adminId], ['terminal', '=', $terminal]])->find();
|
||||
|
||||
@@ -70,6 +75,7 @@ class AdminTokenService
|
||||
}
|
||||
|
||||
return $adminTokenCache->setAdminInfo($adminSession->token);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,8 +90,13 @@ class AdminTokenService
|
||||
*/
|
||||
public static function overtimeToken($token)
|
||||
{
|
||||
return Db::transaction(static function () use ($token) {
|
||||
$candidate = AdminSession::where('token', $token)->findOrEmpty();
|
||||
if ($candidate->isEmpty()) { return false; }
|
||||
$admin = Admin::where('id', (int) $candidate['admin_id'])->lock(true)->findOrEmpty();
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0) { return false; }
|
||||
$time = time();
|
||||
$adminSession = AdminSession::where('token', '=', $token)->findOrEmpty();
|
||||
$adminSession = AdminSession::where('token', '=', $token)->where('expire_time', '>', $time)->lock(true)->findOrEmpty();
|
||||
if ($adminSession->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
@@ -93,7 +104,16 @@ class AdminTokenService
|
||||
$adminSession->expire_time = $time + Config::get('project.admin_token.expire_duration');
|
||||
$adminSession->update_time = $time;
|
||||
$adminSession->save();
|
||||
return (new AdminTokenCache())->setAdminInfo($adminSession->token);
|
||||
$cache = new AdminTokenCache();
|
||||
$cached = $cache->get('token_admin_' . $token);
|
||||
if ($cached) {
|
||||
// 续期不得把登录IP改绑到当前请求IP。
|
||||
$cached['expire_time'] = $adminSession->expire_time;
|
||||
$cache->set('token_admin_' . $token, $cached, new \DateTime(date('Y-m-d H:i:s', $adminSession->expire_time)));
|
||||
return $cached;
|
||||
}
|
||||
return $cache->setAdminInfo($adminSession->token);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,25 +128,16 @@ class AdminTokenService
|
||||
*/
|
||||
public static function expireToken($token)
|
||||
{
|
||||
$adminSession = AdminSession::where('token', '=', $token)
|
||||
->with('admin')
|
||||
->findOrEmpty();
|
||||
|
||||
if ($adminSession->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//当支持多处登录的时候,服务端不注销
|
||||
if ($adminSession->admin->multipoint_login === 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$time = time();
|
||||
$adminSession->expire_time = $time;
|
||||
$adminSession->update_time = $time;
|
||||
$adminSession->save();
|
||||
|
||||
return (new AdminTokenCache())->deleteAdminInfo($token);
|
||||
$changed = Db::transaction(static function () use ($token): bool {
|
||||
$candidate = AdminSession::where('token', $token)->findOrEmpty();
|
||||
if ($candidate->isEmpty()) { return false; }
|
||||
$admin = Admin::where('id', (int) $candidate['admin_id'])->lock(true)->findOrEmpty();
|
||||
// 保留原退出登录在多点登录模式下不注销的语义。
|
||||
if (!$admin->isEmpty() && (int) $admin['multipoint_login'] === 1) { return false; }
|
||||
return AdminSession::where('token', $token)->update(['expire_time' => time() - 1, 'update_time' => time()]) > 0;
|
||||
});
|
||||
if ($changed) { (new AdminTokenCache())->deleteAdminInfo($token); }
|
||||
return $changed;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user