UserTimeout.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\admin\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use app\admin\model\User;
  7. use app\admin\model\Config;
  8. use app\admin\model\KefuTime;
  9. /**
  10. * Worker 命令行类
  11. */
  12. class UserTimeout extends Command
  13. {
  14. public function configure()
  15. {
  16. $this->setName('user:timeout')
  17. ->setDescription('用户会话检测');
  18. }
  19. public function execute(Input $input, Output $output)
  20. {
  21. $config = Config::whereIn('field',['kefu_timeout_reminder','kefu_timeout_warning','user_timeouted_warning', 'user_timeout_warning','user_inline_finished'])->column('val', 'field');
  22. $user_timeout_warning = 0;
  23. if (isset($config['user_timeout_warning']) && $config['user_timeout_warning'] > 0) {
  24. $user_timeout_warning = $config['user_timeout_warning'];
  25. //用户等待即将超时预警
  26. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 1)
  27. ->where('service_start', '<=', time() - $user_timeout_warning)
  28. ->select()
  29. ->toArray();
  30. $this->sendMessage($list, 1);
  31. }
  32. if (isset($config['user_timeouted_warning']) && $config['user_timeouted_warning'] > 0) {
  33. $user_timeouted_warning = $config['user_timeouted_warning'];
  34. //用户等待已超时预警
  35. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 1)
  36. ->where('service_start', '<=', time() - $user_timeouted_warning)
  37. ->where(function($query) use ($user_timeout_warning){
  38. if ($user_timeout_warning > 0) {
  39. $query->where('service_start', '>', time() - $user_timeout_warning);
  40. }
  41. })
  42. ->select()
  43. ->toArray();
  44. $this->sendMessage($list, 2);
  45. }
  46. $kefu_timeout_warning = 0;
  47. if (isset($config['kefu_timeout_warning']) && $config['kefu_timeout_warning'] > 0) {
  48. $kefu_timeout_warning = $config['kefu_timeout_warning'];
  49. //用户服务中-即将超时预警时间
  50. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 2)
  51. ->where('service_start', '<=', time() - $kefu_timeout_warning)
  52. ->select()
  53. ->toArray();
  54. $this->sendMessage($list, 3);
  55. }
  56. if (isset($config['kefu_timeout_reminder']) && $config['kefu_timeout_reminder'] > 0) {
  57. $timeout_reminder = $config['kefu_timeout_reminder'];
  58. //用户服务中-已超时提醒
  59. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 2)
  60. ->where('service_start', '<=', time() - $timeout_reminder)
  61. ->where(function($query) use ($kefu_timeout_warning){
  62. if ($kefu_timeout_warning > 0) {
  63. $query->where('service_start', '>', time() - $kefu_timeout_warning);
  64. }
  65. })
  66. ->select()
  67. ->toArray();
  68. $this->sendMessage($list, 4);
  69. }
  70. if (isset($config['user_inline_finished']) && $config['user_inline_finished'] > 0) {
  71. $user_inline_finished = $config['user_inline_finished'] * 60;
  72. //会员离线后自动结束会话时间
  73. $list = User::where('is_online', 0)->whereIn('service_status', [0,1,2])
  74. ->where('offline_time', '<=', time() - $user_inline_finished)
  75. ->select()
  76. ->toArray();
  77. foreach($list as $user) {
  78. User::where('id', $user['user_id'])->update(['service_status' => 3, 'service_start' => time()]);
  79. KefuTime::endData($user['cs_uid'], 3); //结束接线时间
  80. //通知客服已结束
  81. wsSendMsg($user['cs_uid'],'handleChat',['user_id'=>$user['user_id']]);
  82. }
  83. }
  84. }
  85. /**
  86. * 发送提醒通知
  87. */
  88. public function sendMessage($list, $type)
  89. {
  90. foreach($list as $user) {
  91. //通知客服已结束
  92. wsSendMsg($user['cs_uid'],'timeout',['user_id'=>$user['user_id'], 'type'=>$type]);
  93. }
  94. }
  95. }