初始版本

This commit is contained in:
贾祥聪
2025-08-19 14:16:51 +08:00
commit f937a1f9b9
4373 changed files with 359728 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
<?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;
/**
* //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
* Class AdminAccountSafeCache
* @package app\common\cache
*/
class AdminAccountSafeCache extends BaseCache
{
private $key;//缓存次数名称
public $minute = 15;//缓存设置为15分钟即密码错误次数达到锁定15分钟
public $count = 15; //设置连续输错次数即15分钟内连续输错误15次后锁定
public function __construct()
{
parent::__construct();
$ip = \request()->ip();
$this->key = $this->tagName . $ip;
}
/**
* @notes 记录登录错误次数
* @author 令狐冲
* @date 2021/6/30 01:51
*/
public function record()
{
if ($this->get($this->key)) {
//缓存存在,记录错误次数
$this->inc($this->key, 1);
} else {
//缓存不存在,第一次设置缓存
$this->set($this->key, 1, $this->minute * 60);
}
}
/**
* @notes 判断是否安全
* @return bool
* @author 令狐冲
* @date 2021/6/30 01:53
*/
public function isSafe()
{
$count = $this->get($this->key);
if ($count >= $this->count) {
return false;
}
return true;
}
/**
* @notes 删除该ip记录错误次数
* @author 令狐冲
* @date 2021/6/30 01:55
*/
public function relieve()
{
$this->delete($this->key);
}
}

122
server/app/common/cache/AdminAuthCache.php vendored Executable file
View File

@@ -0,0 +1,122 @@
<?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\adminapi\logic\auth\AuthLogic;
/**
* 管理员权限缓存
* Class AdminAuthCache
* @package app\common\cache
*/
class AdminAuthCache extends BaseCache
{
private $prefix = 'admin_auth_';
private $authConfigList = [];
private $cacheMd5Key = ''; //权限文件MD5的key
private $cacheAllKey = ''; //全部权限的key
private $cacheUrlKey = ''; //管理员的url缓存key
private $authMd5 = ''; //权限文件MD5的值
private $adminId = '';
public function __construct($adminId = '')
{
parent::__construct();
$this->adminId = $adminId;
// 全部权限
$this->authConfigList = AuthLogic::getAllAuth();
// 当前权限配置文件的md5
$this->authMd5 = md5(json_encode($this->authConfigList));
$this->cacheMd5Key = $this->prefix . 'md5';
$this->cacheAllKey = $this->prefix . 'all';
$this->cacheUrlKey = $this->prefix . 'url_' . $this->adminId;
$cacheAuthMd5 = $this->get($this->cacheMd5Key);
$cacheAuth = $this->get($this->cacheAllKey);
//权限配置和缓存权限对比,不一样说明权限配置文件已修改,清理缓存
if ($this->authMd5 !== $cacheAuthMd5 || empty($cacheAuth)) {
$this->deleteTag();
}
}
/**
* @notes 获取管理权限uri
* @param $adminId
* @return array|mixed
* @author 令狐冲
* @date 2021/8/19 15:27
*/
public function getAdminUri()
{
//从缓存获取,直接返回
$urisAuth = $this->get($this->cacheUrlKey);
if ($urisAuth) {
return $urisAuth;
}
//获取角色关联的菜单id(菜单或权限)
$urisAuth = AuthLogic::getAuthByAdminId($this->adminId);
if (empty($urisAuth)) {
return [];
}
$this->set($this->cacheUrlKey, $urisAuth, 3600);
//保存到缓存并读取返回
return $urisAuth;
}
/**
* @notes 获取全部权限uri
* @return array|mixed
* @author cjhao
* @date 2021/9/13 11:41
*/
public function getAllUri()
{
$cacheAuth = $this->get($this->cacheAllKey);
if ($cacheAuth) {
return $cacheAuth;
}
// 获取全部权限
$authList = AuthLogic::getAllAuth();
//保存到缓存并读取返回
$this->set($this->cacheMd5Key, $this->authMd5);
$this->set($this->cacheAllKey, $authList);
return $authList;
}
/**
* @notes 清理管理员缓存
* @return bool
* @author cjhao
* @date 2021/10/13 18:47
*/
public function clearAuthCache()
{
$this->clear($this->cacheUrlKey);
return true;
}
}

110
server/app/common/cache/AdminTokenCache.php vendored Executable file
View File

@@ -0,0 +1,110 @@
<?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\auth\Admin;
use app\common\model\auth\AdminSession;
/**
* 管理员token缓存
* Class AdminTokenCache
* @package app\common\cache
*/
class AdminTokenCache extends BaseCache
{
private $prefix = 'token_admin_';
/**
* @notes 通过token获取缓存管理员信息
* @param $token
* @return false|mixed
* @author 令狐冲
* @date 2021/6/30 16:57
*/
public function getAdminInfo($token)
{
//直接从缓存获取
$adminInfo = $this->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 = AdminSession::where([['token', '=', $token], ['expire_time', '>', time()]])
->find();
if (empty($adminSession)) {
return [];
}
$admin = Admin::where('id', '=', $adminSession->admin_id)
->with('role')
->find();
$adminInfo = [
'admin_id' => $admin->id,
'root' => $admin->root,
'name' => $admin->name,
'account' => $admin->account,
'role_name' => $admin->role['name'] ?? '',
'role_id' => $admin->role_id,
'token' => $token,
'terminal' => $adminSession->terminal,
'expire_time' => $adminSession->expire_time,
];
$this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time)));
return $this->getAdminInfo($token);
}
/**
* @notes 删除缓存
* @param $token
* @return bool
* @author 令狐冲
* @date 2021/7/3 16:57
*/
public function deleteAdminInfo($token)
{
return $this->delete($this->prefix . $token);
}
}

73
server/app/common/cache/BaseCache.php vendored Executable file
View File

@@ -0,0 +1,73 @@
<?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
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace app\common\cache;
use think\App;
use think\Cache;
/**
* 缓存基础类,用于管理缓存
* Class BaseCache
* @package app\common\cache
*/
abstract class BaseCache extends Cache
{
/**
* 缓存标签
* @var string
*/
protected $tagName;
public function __construct()
{
parent::__construct(app());
$this->tagName = get_class($this);
}
/**
* @notes 重写父类set自动打上标签
* @param string $key
* @param mixed $value
* @param null $ttl
* @return bool
* @author 段誉
* @date 2021/12/27 14:16
*/
public function set($key, $value, $ttl = null): bool
{
return $this->store()->tag($this->tagName)->set($key, $value, $ttl);
}
/**
* @notes 清除缓存类所有缓存
* @return bool
* @author 段誉
* @date 2021/12/27 14:16
*/
public function deleteTag(): bool
{
return $this->tag($this->tagName)->clear();
}
}

View File

@@ -0,0 +1,85 @@
<?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;
/**
* //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
* Class CoachUserAccountSafeCache
* @package app\common\cache
*/
class CoachUserAccountSafeCache extends BaseCache
{
private $key;//缓存次数名称
public $minute = 15;//缓存设置为15分钟即密码错误次数达到锁定15分钟
public $count = 15; //设置连续输错次数即15分钟内连续输错误15次后锁定
public function __construct()
{
parent::__construct();
$ip = \request()->ip();
$this->key = $this->tagName . $ip;
}
/**
* @notes 记录登录错误次数
* @author 令狐冲
* @date 2021/6/30 01:51
*/
public function record()
{
if ($this->get($this->key)) {
//缓存存在,记录错误次数
$this->inc($this->key, 1);
} else {
//缓存不存在,第一次设置缓存
$this->set($this->key, 1, $this->minute * 60);
}
}
/**
* @notes 判断是否安全
* @return bool
* @author 令狐冲
* @date 2021/6/30 01:53
*/
public function isSafe()
{
$count = $this->get($this->key);
if ($count >= $this->count) {
return false;
}
return true;
}
/**
* @notes 删除该ip记录错误次数
* @author 令狐冲
* @date 2021/6/30 01:55
*/
public function relieve()
{
$this->delete($this->key);
}
}

View File

@@ -0,0 +1,108 @@
<?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\enum\coach\CoachEnum;
use app\common\model\coach\Coach;
use app\common\model\coach\CoachUser;
use app\common\model\coach\CoachUserSession;
use app\common\model\coach\ShopUserSession;
class CoachUserTokenCache extends BaseCache
{
private $prefix = 'token_coach_user_';
/**
* @notes 通过token获取缓存用户信息
* @param $token
* @return false|mixed
* @author 令狐冲
* @date 2021/6/30 16:57
*/
public function getCoachUserInfo($token)
{
//直接从缓存获取
$coachUserInfo = $this->get($this->prefix . $token);
if ($coachUserInfo) {
return $coachUserInfo;
}
//从数据获取信息被设置缓存(可能后台清除缓存)
$coachUserInfo = $this->setCoachUserInfo($token);
if ($coachUserInfo) {
return $coachUserInfo;
}
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 setCoachUserInfo($token)
{
$coachUserSession = CoachUserSession::where([['token', '=', $token], ['expire_time', '>', time()]])->find();
if (empty($coachUserSession)) {
return [];
}
$coachUser = CoachUser::where('id', '=', $coachUserSession->coach_user_id)
->find();
$coachInfo = Coach::where(['coach_user_id'=>$coachUserSession->coach_user_id])
->order('id desc')
->field('id,audit_status')
->findOrEmpty();
$coachUserInfo = [
'coach_user_id' => $coachUser->id,
'coach_id'=> $coachInfo['id'],
'token' => $token,
'sn' => $coachUser->sn,
'account' => $coachUser->account,
'avatar' => $coachUser->avatar,
'terminal' => $coachUserSession->terminal,
'audit_status' => $coachInfo['audit_status'],
'expire_time' => $coachUserSession->expire_time,
];
$this->set($this->prefix . $token, $coachUserInfo, new \DateTime(Date('Y-m-d H:i:s', $coachUserSession->expire_time)));
return $this->getCoachUserInfo($token);
}
/**
* @notes 删除缓存
* @param $token
* @return bool
* @author 令狐冲
* @date 2021/7/3 16:57
*/
public function deleteCoachUserInfo($token)
{
return $this->delete($this->prefix . $token);
}
}

View File

@@ -0,0 +1,85 @@
<?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;
/**
* //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
* Class ShopUserAccountSafeCache
* @package app\common\cache
*/
class ShopUserAccountSafeCache extends BaseCache
{
private $key;//缓存次数名称
public $minute = 15;//缓存设置为15分钟即密码错误次数达到锁定15分钟
public $count = 15; //设置连续输错次数即15分钟内连续输错误15次后锁定
public function __construct()
{
parent::__construct();
$ip = \request()->ip();
$this->key = $this->tagName . $ip;
}
/**
* @notes 记录登录错误次数
* @author 令狐冲
* @date 2021/6/30 01:51
*/
public function record()
{
if ($this->get($this->key)) {
//缓存存在,记录错误次数
$this->inc($this->key, 1);
} else {
//缓存不存在,第一次设置缓存
$this->set($this->key, 1, $this->minute * 60);
}
}
/**
* @notes 判断是否安全
* @return bool
* @author 令狐冲
* @date 2021/6/30 01:53
*/
public function isSafe()
{
$count = $this->get($this->key);
if ($count >= $this->count) {
return false;
}
return true;
}
/**
* @notes 删除该ip记录错误次数
* @author 令狐冲
* @date 2021/6/30 01:55
*/
public function relieve()
{
$this->delete($this->key);
}
}

107
server/app/common/cache/ShopUserTokenCache.php vendored Executable file
View File

@@ -0,0 +1,107 @@
<?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);
}
}

View File

@@ -0,0 +1,85 @@
<?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;
/**
* //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
* Class AdminAccountSafeCache
* @package app\common\cache
*/
class UserAccountSafeCache extends BaseCache
{
private $key;//缓存次数名称
public $minute = 15;//缓存设置为15分钟即密码错误次数达到锁定15分钟
public $count = 15; //设置连续输错次数即15分钟内连续输错误15次后锁定
public function __construct()
{
parent::__construct();
$ip = \request()->ip();
$this->key = $this->tagName . $ip;
}
/**
* @notes 记录登录错误次数
* @author 令狐冲
* @date 2021/6/30 01:51
*/
public function record()
{
if ($this->get($this->key)) {
//缓存存在,记录错误次数
$this->inc($this->key, 1);
} else {
//缓存不存在,第一次设置缓存
$this->set($this->key, 1, $this->minute * 60);
}
}
/**
* @notes 判断是否安全
* @return bool
* @author 令狐冲
* @date 2021/6/30 01:53
*/
public function isSafe()
{
$count = $this->get($this->key);
if ($count >= $this->count) {
return false;
}
return true;
}
/**
* @notes 删除该ip记录错误次数
* @author 令狐冲
* @date 2021/6/30 01:55
*/
public function relieve()
{
$this->delete($this->key);
}
}

104
server/app/common/cache/UserTokenCache.php vendored Executable file
View File

@@ -0,0 +1,104 @@
<?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\user\User;
use app\common\model\user\UserSession;
class UserTokenCache extends BaseCache
{
private $prefix = 'token_user_';
/**
* @notes 通过token获取缓存用户信息
* @param $token
* @return false|mixed
* @author 令狐冲
* @date 2021/6/30 16:57
*/
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 2021/7/5 12:12
*/
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,
];
$this->set($this->prefix . $token, $userInfo, new \DateTime(Date('Y-m-d H:i:s', $userSession->expire_time)));
return $this->getUserInfo($token);
}
/**
* @notes 删除缓存
* @param $token
* @return bool
* @author 令狐冲
* @date 2021/7/3 16:57
*/
public function deleteUserInfo($token)
{
return $this->delete($this->prefix . $token);
}
}