multi-store/app/store/service/AdminTokenService.php

111 lines
3.5 KiB
PHP
Raw Normal View History

2024-06-01 16:07:53 +08:00
<?php
namespace app\store\service;
2024-06-03 15:48:47 +08:00
use app\common\cache\StaffTokenCache;
use app\common\model\system_store\SystemStoreStaffSession;
2024-06-01 16:07:53 +08:00
use Webman\Config;
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)
{
$time = time();
2024-06-03 15:48:47 +08:00
$adminSession = SystemStoreStaffSession::where([['staff_id', '=', $adminId], ['terminal', '=', $terminal]])->find();
2024-06-01 16:07:53 +08:00
//获取token延长过期的时间
$expireTime = $time + Config::get('project.admin_token.expire_duration');
2024-06-03 16:14:59 +08:00
$staffTokenCache = new StaffTokenCache();
2024-06-01 16:07:53 +08:00
//token处理
if ($adminSession) {
if ($adminSession->expire_time < $time || $multipointLogin === 0) {
//清空缓存
2024-06-03 16:14:59 +08:00
$staffTokenCache->deleteAdminInfo($adminSession->token);
2024-06-01 16:07:53 +08:00
//如果token过期或账号设置不支持多处登录更新token
$adminSession->token = create_token($adminId);
}
$adminSession->expire_time = $expireTime;
$adminSession->update_time = $time;
$adminSession->save();
} else {
//找不到在该终端的token记录创建token记录
2024-06-03 15:48:47 +08:00
$adminSession = SystemStoreStaffSession::create([
'staff_id' => $adminId,
2024-06-01 16:07:53 +08:00
'terminal' => $terminal,
'token' => create_token($adminId),
'expire_time' => $expireTime
]);
}
2024-06-03 16:14:59 +08:00
return $staffTokenCache->setAdminInfo($adminSession->token);
2024-06-01 16:07:53 +08:00
}
/**
* @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();
2024-06-03 16:14:59 +08:00
$adminSession = SystemStoreStaffSession::where('token', '=', $token)->findOrEmpty();
2024-06-01 16:07:53 +08:00
if ($adminSession->isEmpty()) {
return false;
}
//延长token过期时间
$adminSession->expire_time = $time + Config::get('project.admin_token.expire_duration');
$adminSession->update_time = $time;
$adminSession->save();
2024-06-03 16:14:59 +08:00
return (new StaffTokenCache())->setAdminInfo($adminSession->token);
2024-06-01 16:07:53 +08:00
}
/**
* @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)
{
2024-06-03 15:48:47 +08:00
$adminSession = SystemStoreStaffSession::where('token', '=', $token)
2024-06-01 16:07:53 +08:00
->findOrEmpty();
if ($adminSession->isEmpty()) {
return false;
}
$time = time();
$adminSession->expire_time = $time;
$adminSession->update_time = $time;
$adminSession->save();
2024-06-03 15:48:47 +08:00
return (new StaffTokenCache())->deleteAdminInfo($token);
2024-06-01 16:07:53 +08:00
}
}