UserTimeout.php 5.2 KB

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