logistics_sy/app/common/cache/UserTokenCache.php

104 lines
3.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\common\cache;
use app\common\model\user\User;
use app\common\model\user\UserSession;
class UserTokenCache extends BaseCache
{
private $prefix = 'token_user_';
/**
* @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 2022/9/16 10:11
*/
public function getUserInfo($token)
{
//直接从缓存获取
$userInfo = $this->get($this->prefix . $token);
if ($userInfo) {
return $userInfo;
}
//从数据获取信息被设置缓存(可能后台清除缓存)
$userInfo = $this->setUserInfo($token);
if ($userInfo) {
return $userInfo;
}
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 2022/9/16 10:11
*/
public function setUserInfo($token)
{
$userSession = UserSession::where([['token', '=', $token], ['expire_time', '>', time()]])->find();
if (empty($userSession)) {
return [];
}
$user = User::where('id', '=', $userSession->user_id)
->find();
$userInfo = [
'user_id' => $user->id,
'nickname' => $user->nickname,
'token' => $token,
'sn' => $user->sn,
'mobile' => $user->mobile,
'avatar' => $user->avatar,
'terminal' => $userSession->terminal,
'expire_time' => $userSession->expire_time,
];
$ttl = new \DateTime(Date('Y-m-d H:i:s', $userSession->expire_time));
$this->set($this->prefix . $token, $userInfo, $ttl);
return $this->getUserInfo($token);
}
/**
* @notes 删除缓存
* @param $token
* @return bool
* @author 段誉
* @date 2022/9/16 10:13
*/
public function deleteUserInfo($token)
{
return $this->delete($this->prefix . $token);
}
}