KefuTokenCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Kefu;
  21. use app\common\model\KefuSession;
  22. /**
  23. * 客服token缓存
  24. * Class KefuTokenCache
  25. * @package app\common\cache
  26. */
  27. class KefuTokenCache extends BaseCache
  28. {
  29. private $prefix = 'token_kefu_';
  30. /**
  31. * @notes 通过token获取缓存管理员信息
  32. * @param $token
  33. * @return array|false|mixed
  34. * @author 段誉
  35. * @date 2022/3/9 18:57
  36. */
  37. public function getKefuInfo($token)
  38. {
  39. //直接从缓存获取
  40. $info = $this->get($this->prefix . $token);
  41. if ($info) {
  42. return $info;
  43. }
  44. //从数据获取信息被设置缓存(可能后台清除缓存)
  45. $info = $this->setKefuInfo($token);
  46. if ($info) {
  47. return $info;
  48. }
  49. return false;
  50. }
  51. /**
  52. * @notes 通过有效token设置管理信息缓存
  53. * @param $token
  54. * @return array|false|mixed
  55. * @throws \Exception
  56. * @author 段誉
  57. * @date 2022/3/9 18:57
  58. */
  59. public function setKefuInfo($token)
  60. {
  61. $kefuSession = KefuSession::where([['token', '=', $token], ['expire_time', '>', time()]])->findOrEmpty();
  62. if ($kefuSession->isEmpty()) {
  63. return [];
  64. }
  65. $kefu = Kefu::with('admin')
  66. ->where('id', '=', $kefuSession->kefu_id)
  67. ->findOrEmpty();
  68. $kefuInfo = [
  69. 'id' => $kefu['id'],
  70. 'admin_id' => $kefu['admin_id'],
  71. 'nickname' => $kefu['nickname'],
  72. 'account' => $kefu['admin']['account'] ?? '',
  73. 'token' => $token,
  74. 'terminal' => $kefuSession['terminal'],
  75. 'expire_time' => $kefuSession['expire_time'],
  76. ];
  77. $this->set($this->prefix . $token, $kefuInfo, new \DateTime(Date('Y-m-d H:i:s', $kefuSession->expire_time)));
  78. return $this->getKefuInfo($token);
  79. }
  80. /**
  81. * @notes 删除缓存
  82. * @param $token
  83. * @return bool
  84. * @author 段誉
  85. * @date 2022/3/9 18:57
  86. */
  87. public function deleteKefuInfo($token)
  88. {
  89. return $this->delete($this->prefix . $token);
  90. }
  91. }