107 lines
3.5 KiB
PHP
Executable File
107 lines
3.5 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeshop100%开源免费商用商城系统
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||
// | github下载:https://github.com/likeshop-github
|
||
// | 访问官网:https://www.likeshop.cn
|
||
// | 访问社区:https://home.likeshop.cn
|
||
// | 访问手册:http://doc.likeshop.cn
|
||
// | 微信公众号:likeshop技术社区
|
||
// | likeshop团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeshopTeam
|
||
// +----------------------------------------------------------------------
|
||
|
||
|
||
namespace app\common\cache;
|
||
|
||
|
||
use app\common\model\shop\ShopUser;
|
||
use app\common\model\shop\ShopUserSession;
|
||
use app\common\model\shop\Shop;
|
||
|
||
class ShopUserTokenCache extends BaseCache
|
||
{
|
||
|
||
private $prefix = 'token_shop_user_';
|
||
|
||
/**
|
||
* @notes 通过token获取缓存用户信息
|
||
* @param $token
|
||
* @return false|mixed
|
||
* @author 令狐冲
|
||
* @date 2021/6/30 16:57
|
||
*/
|
||
public function getShopUserInfo($token)
|
||
{
|
||
//直接从缓存获取
|
||
$shopUserInfo = $this->get($this->prefix . $token);
|
||
if ($shopUserInfo) {
|
||
return $shopUserInfo;
|
||
}
|
||
|
||
//从数据获取信息被设置缓存(可能后台清除缓存)
|
||
$shophUserInfo = $this->setShopUserInfo($token);
|
||
if ($shophUserInfo) {
|
||
return $shophUserInfo;
|
||
}
|
||
|
||
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 setShopUserInfo($token)
|
||
{
|
||
$shopUserSession = ShopUserSession::where([['token', '=', $token], ['expire_time', '>', time()]])->find();
|
||
if (empty($shopUserSession)) {
|
||
return [];
|
||
}
|
||
$shopUser = ShopUser::where('id', '=', $shopUserSession->shop_user_id)
|
||
->find();
|
||
$shop = Shop::where(['shop_user_id'=>$shopUserSession->shop_user_id])
|
||
->order('id desc')
|
||
->field('id,audit_status')
|
||
->findOrEmpty();
|
||
$shopUserInfo = [
|
||
'shop_user_id' => $shopUser->id,
|
||
'shop_id'=> $shop['id'],
|
||
'token' => $token,
|
||
'sn' => $shopUser->sn,
|
||
'account' => $shopUser->account,
|
||
'audit_status' => $shop['audit_status'],
|
||
'avatar' => $shopUser->avatar,
|
||
'terminal' => $shopUserSession->terminal,
|
||
'expire_time' => $shopUserSession->expire_time,
|
||
];
|
||
$this->set($this->prefix . $token, $shopUserInfo, new \DateTime(Date('Y-m-d H:i:s', $shopUserSession->expire_time)));
|
||
return $this->getShopUserInfo($token);
|
||
}
|
||
|
||
/**
|
||
* @notes 删除缓存
|
||
* @param $token
|
||
* @return bool
|
||
* @author 令狐冲
|
||
* @date 2021/7/3 16:57
|
||
*/
|
||
public function deleteShopUserInfo($token)
|
||
{
|
||
return $this->delete($this->prefix . $token);
|
||
}
|
||
|
||
|
||
} |