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