AdminTokenCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\cache;
  20. use app\common\model\Admin;
  21. use app\common\model\AdminSession;
  22. class AdminTokenCache extends BaseCache
  23. {
  24. private $prefix = 'token_admin_';
  25. /**
  26. * @notes 通过token获取缓存管理员信息
  27. * @param $token
  28. * @return false|mixed
  29. * @author 令狐冲
  30. * @date 2021/6/30 16:57
  31. */
  32. public function getAdminInfo($token)
  33. {
  34. //直接从缓存获取
  35. $adminInfo = $this->get($this->prefix . $token);
  36. if ($adminInfo) {
  37. return $adminInfo;
  38. }
  39. //从数据获取信息被设置缓存(可能后台清除缓存)
  40. $adminInfo = $this->setAdminInfo($token);
  41. if ($adminInfo) {
  42. return $adminInfo;
  43. }
  44. return false;
  45. }
  46. /**
  47. * @notes 通过有效token设置管理信息缓存
  48. * @param $token
  49. * @return array|false|mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author 令狐冲
  54. * @date 2021/7/5 12:12
  55. */
  56. public function setAdminInfo($token)
  57. {
  58. $adminSession = AdminSession::where([['token', '=', $token], ['expire_time', '>', time()]])
  59. ->find();
  60. if (empty($adminSession)) {
  61. return [];
  62. }
  63. $admin = Admin::where('id', '=', $adminSession->admin_id)
  64. ->with('role')
  65. ->find();
  66. $adminInfo = [
  67. 'admin_id' => $admin->id,
  68. 'root' => $admin->root,
  69. 'name' => $admin->name,
  70. 'account' => $admin->account,
  71. 'role_name' => $admin->role['name'] ?? '',
  72. 'role_id' => $admin->role_id,
  73. 'token' => $token,
  74. 'terminal' => $adminSession->terminal,
  75. 'expire_time' => $adminSession->expire_time,
  76. ];
  77. $this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time)));
  78. return $this->getAdminInfo($token);
  79. }
  80. /**
  81. * @notes 删除缓存
  82. * @param $token
  83. * @return bool
  84. * @author 令狐冲
  85. * @date 2021/7/3 16:57
  86. */
  87. public function deleteAdminInfo($token)
  88. {
  89. return $this->delete($this->prefix . $token);
  90. }
  91. }