Files
zyt/server/app/adminapi/service/AdminTokenService.php
2026-11-12 14:27:49 +08:00

178 lines
7.3 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\service;
use app\common\cache\AdminTokenCache;
use app\common\enum\AdminTerminalEnum;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminSession;
use think\facade\Config;
/**
* 管理员token
* Class AdminTokenService
* @package app\adminapi\service
*/
class AdminTokenService
{
public static function expireDuration($terminal): int
{
return (int) Config::get((int) $terminal === AdminTerminalEnum::BCR
? 'project.admin_token.bcr_expire_duration' : 'project.admin_token.expire_duration',
(int) $terminal === AdminTerminalEnum::BCR ? 86400 * 180 : 3600 * 8);
}
public static function renewalWindow($terminal): int
{
return (int) Config::get((int) $terminal === AdminTerminalEnum::BCR
? 'project.admin_token.bcr_be_expire_duration' : 'project.admin_token.be_expire_duration',
(int) $terminal === AdminTerminalEnum::BCR ? 86400 * 150 : 3600);
}
/** 与改密同一事务调用;只撤销 BCR,会话有效性由 DB 决定,旧缓存不能续活。 */
public static function revokeBcrSessions(int $adminId): void
{
AdminSession::where('admin_id', $adminId)->where('terminal', AdminTerminalEnum::BCR)
->update(['expire_time' => time() - 1, 'update_time' => time()]);
}
/**
* @notes 设置或更新管理员token
* @param $adminId //管理员id
* @param $terminal //多终端名称
* @param $multipointLogin //是否支持多处登录
* @return false|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 令狐冲
* @date 2021/7/2 20:25
*/
public static function setToken($adminId, $terminal, $multipointLogin = 1)
{
$time = time();
$adminSession = AdminSession::where([['admin_id', '=', $adminId], ['terminal', '=', $terminal]])->find();
//获取token延长过期的时间
$expireTime = $time + self::expireDuration($terminal);
$adminTokenCache = new AdminTokenCache();
//token处理
if ($adminSession) {
if ($adminSession->expire_time < $time || $multipointLogin === 0
|| ((int) $terminal === AdminTerminalEnum::BCR && (int) $adminSession->expire_time === $time)) {
//清空缓存
$adminTokenCache->deleteAdminInfo($adminSession->token);
//如果token过期或账号设置不支持多处登录,更新token
$adminSession->token = create_token($adminId);
}
$adminSession->expire_time = $expireTime;
$adminSession->update_time = $time;
$adminSession->save();
} else {
//找不到在该终端的token记录,创建token记录
$adminSession = AdminSession::create([
'admin_id' => $adminId,
'terminal' => $terminal,
'token' => create_token($adminId),
'expire_time' => $expireTime
]);
}
return $adminTokenCache->setAdminInfo($adminSession->token);
}
/**
* @notes 延长token过期时间
* @param $token
* @return array|false|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 令狐冲
* @date 2021/7/5 14:25
*/
public static function overtimeToken($token)
{
$time = time();
$adminSession = AdminSession::where('token', '=', $token)->findOrEmpty();
if ($adminSession->isEmpty()) {
return false;
}
if ((int) $adminSession->terminal === AdminTerminalEnum::BCR) {
$previousExpiry = (int) $adminSession->expire_time;
if ($previousExpiry <= $time || !Admin::where('id', $adminSession->admin_id)
->where('disable', 0)->field('id')->find()) {
return false;
}
// 比较读取时的有效期和 token;注销、改密或同端重登后不能把旧会话续活。
// CAS 失败也可能是另一请求先续期,交给 DB 有效性校验区分。
AdminSession::where('id', $adminSession->id)->where('token', $token)
->where('expire_time', $previousExpiry)->where('expire_time', '>', $time)
->update(['expire_time' => $time + self::expireDuration($adminSession->terminal), 'update_time' => $time]);
return (new AdminTokenCache())->setAdminInfo($token);
}
//延长token过期时间
$adminSession->expire_time = $time + self::expireDuration($adminSession->terminal);
$adminSession->update_time = $time;
$adminSession->save();
return (new AdminTokenCache())->setAdminInfo($adminSession->token);
}
/**
* @notes 设置token为过期
* @param $token
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 令狐冲
* @date 2021/7/5 14:31
*/
public static function expireToken($token)
{
$adminSession = AdminSession::where('token', '=', $token)
->with('admin')
->findOrEmpty();
if ($adminSession->isEmpty()) {
return false;
}
// 其他终端保留多处登录的原有退出语义。
// BCR 必须注销;同账号多台 BCR 复用一个 token 时,这些设备会一起失效。
if ((int) $adminSession->terminal !== AdminTerminalEnum::BCR
&& $adminSession->admin->multipoint_login === 1) {
return false;
}
$time = time();
if ((int) $adminSession->terminal === AdminTerminalEnum::BCR) {
// 只注销调用者持有的 token,不能误伤并发重登后替换到同一行的新 token。
AdminSession::where('id', $adminSession->id)->where('token', $token)
->update(['expire_time' => $time - 1, 'update_time' => $time]);
} else {
$adminSession->expire_time = $time;
$adminSession->update_time = $time;
$adminSession->save();
}
return (new AdminTokenCache())->deleteAdminInfo($token);
}
}