multi-store/app/common/cache/StaffTokenCache.php
2024-06-03 15:48:47 +08:00

91 lines
2.5 KiB
PHP

<?php
namespace app\common\cache;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminSession;
use app\common\model\auth\SystemRole;
use app\common\model\BaseModel;
use app\common\model\system_store\SystemStoreStaff;
use app\common\model\system_store\SystemStoreStaffSession;
use support\Cache;
use think\facade\Db;
class StaffTokenCache extends BaseCache
{
private $prefix = 'token_staff_';
/**
* @notes 通过token获取缓存管理员信息
* @param $token
* @return false|mixed
* @author 令狐冲
* @date 2021/6/30 16:57
*/
public function getAdminInfo($token)
{
//直接从缓存获取
$adminInfo = Cache::get($this->prefix . $token);
if ($adminInfo) {
return $adminInfo;
}
//从数据获取信息被设置缓存(可能后台清除缓存)
$adminInfo = $this->setAdminInfo($token);
if ($adminInfo) {
return $adminInfo;
}
return false;
}
/**
* @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 12:12
*/
public function setAdminInfo($token)
{
$adminSession = SystemStoreStaffSession::where([['token', '=', $token], ['expire_time', '>', time()]])
->find();
if (empty($adminSession)) {
return [];
}
$admin = SystemStoreStaff::where('id', '=', $adminSession->staff_id)
->append(['role_id'])
->find();
$adminInfo = [
'admin_id' => $admin->id,
'name' => $admin->staff_name,
'role_name' => $admin->is_manager == 1? '店长' : '店员',
'account' => $admin->account,
'store_id' => $admin->store_id,
'is_manager' => $admin->is_manager,
'is_admin' => $admin->is_admin,
'token' => $token,
'expire_time' => $adminSession->expire_time,
];
Cache::set($this->prefix . $token, $adminInfo);
return $this->getAdminInfo($token);
}
/**
* @notes 删除缓存
* @param $token
* @return bool
* @author 令狐冲
* @date 2021/7/3 16:57
*/
public function deleteAdminInfo($token)
{
return Cache::delete($this->prefix . $token);
}
}