UserTimeout.php 5.3 KB

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