更新
This commit is contained in:
+143
@@ -0,0 +1,143 @@
|
||||
<?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\model\auth\AdminSession;
|
||||
use app\common\model\auth\Admin;
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 管理员token
|
||||
* Class AdminTokenService
|
||||
* @package app\adminapi\service
|
||||
*/
|
||||
class AdminTokenService
|
||||
{
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
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();
|
||||
|
||||
//获取token延长过期的时间
|
||||
$expireTime = $time + Config::get('project.admin_token.expire_duration');
|
||||
$adminTokenCache = new AdminTokenCache();
|
||||
|
||||
//token处理
|
||||
if ($adminSession) {
|
||||
if ($adminSession->expire_time < $time || $multipointLogin === 0) {
|
||||
//清空缓存
|
||||
$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)
|
||||
{
|
||||
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)->where('expire_time', '>', $time)->lock(true)->findOrEmpty();
|
||||
if ($adminSession->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
//延长token过期时间
|
||||
$adminSession->expire_time = $time + Config::get('project.admin_token.expire_duration');
|
||||
$adminSession->update_time = $time;
|
||||
$adminSession->save();
|
||||
$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);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
$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