KefuTokenService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\kefuapi\service;
  20. use app\common\cache\KefuTokenCache;
  21. use app\common\model\KefuSession;
  22. use think\facade\Config;
  23. /**
  24. * 客服token
  25. * Class KefuTokenService
  26. * @package app\kefuapi\service
  27. */
  28. class KefuTokenService
  29. {
  30. /**
  31. * @notes 设置或更新token
  32. * @param $kefuId
  33. * @param $terminal
  34. * @return array|false|mixed
  35. * @author 段誉
  36. * @date 2022/3/9 18:56
  37. */
  38. public static function setToken($kefuId, $terminal)
  39. {
  40. $time = time();
  41. $kefuSession = KefuSession::where([['kefu_id', '=', $kefuId], ['terminal', '=', $terminal]])->findOrEmpty();
  42. //获取token延长过期的时间
  43. $tokenCache = new KefuTokenCache();
  44. $expireTime = $time + Config::get('project.kefu_token.expire_duration');
  45. $token = create_token($kefuId);
  46. //token处理
  47. if ($kefuSession->isEmpty()) {
  48. //找不到在该终端的token记录,创建token记录
  49. KefuSession::create([
  50. 'kefu_id' => $kefuId,
  51. 'terminal' => $terminal,
  52. 'token' => $token,
  53. 'expire_time' => $expireTime
  54. ]);
  55. } else {
  56. // 清空缓存
  57. $tokenCache->deleteKefuInfo($kefuSession->token);
  58. // 更新token
  59. KefuSession::update([
  60. 'id' => $kefuSession['id'],
  61. 'token' => $token,
  62. 'expire_time' => $expireTime,
  63. 'update_time' => $time,
  64. ]);
  65. }
  66. return $tokenCache->setKefuInfo($token);
  67. }
  68. /**
  69. * @notes 延长token过期时间
  70. * @param $token
  71. * @return array|false|mixed
  72. * @author 段誉
  73. * @date 2022/3/9 18:56
  74. */
  75. public static function overtimeToken($token)
  76. {
  77. $time = time();
  78. $kefuSession = KefuSession::where('token', '=', $token)->findOrEmpty();
  79. if ($kefuSession->isEmpty()) {
  80. return false;
  81. }
  82. //延长token过期时间
  83. $kefuSession->expire_time = $time + Config::get('project.kefu_token.expire_duration');
  84. $kefuSession->update_time = $time;
  85. $kefuSession->save();
  86. return (new KefuTokenCache())->setKefuInfo($token);
  87. }
  88. /**
  89. * @notes 设置token为过期
  90. * @param $token
  91. * @return bool
  92. * @author 段誉
  93. * @date 2022/3/9 18:57
  94. */
  95. public static function expireToken($token)
  96. {
  97. $kefuSession = KefuSession::where('token', '=', $token)->findOrEmpty();
  98. if ($kefuSession->isEmpty()) {
  99. return false;
  100. }
  101. $time = time();
  102. $kefuSession->expire_time = $time;
  103. $kefuSession->update_time = $time;
  104. $kefuSession->save();
  105. return (new KefuTokenCache())->deleteKefuInfo($token);
  106. }
  107. }