UserTimeout.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. use GatewayClient\Gateway;
  11. /**
  12. * Worker 命令行类
  13. */
  14. class UserTimeout extends Command
  15. {
  16. public function configure()
  17. {
  18. $this->setName('user:timeout')
  19. ->setDescription('用户会话检测');
  20. }
  21. public function execute(Input $input, Output $output)
  22. {
  23. //检测离线用户状态
  24. $this->checkOfflineUser();
  25. $config = Config::whereIn('field',['kefu_timeout_reminder','kefu_timeout_warning','user_timeouted_warning', 'user_timeout_warning','user_inline_finished'])->column('val','field');
  26. $user_timeout_warning = 0;
  27. if (isset($config['user_timeout_warning']) && $config['user_timeout_warning'] > 0) {
  28. $user_timeout_warning = $config['user_timeout_warning'];
  29. //用户等待即将超时预警
  30. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 1)
  31. ->where('service_start', '<=', time() - $user_timeout_warning)
  32. ->where('timeout_type', 0)
  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. ->where('timeout_type', '<',2)
  49. ->select()
  50. ->toArray();
  51. $this->sendMessage($list, 2);
  52. $output->writeln('用户等待已超时预警'.count($list));
  53. }
  54. $kefu_timeout_warning = 0;
  55. if (isset($config['kefu_timeout_warning']) && $config['kefu_timeout_warning'] > 0) {
  56. $kefu_timeout_warning = $config['kefu_timeout_warning'];
  57. //用户服务中-即将超时预警时间
  58. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 2)
  59. ->where('service_start', '<=', time() - $kefu_timeout_warning)
  60. ->where('timeout_type', '<',3)
  61. ->select()
  62. ->toArray();
  63. $this->sendMessage($list, 3);
  64. $output->writeln('用户服务中-即将超时预警'.count($list));
  65. }
  66. if (isset($config['kefu_timeout_reminder']) && $config['kefu_timeout_reminder'] > 0) {
  67. $timeout_reminder = $config['kefu_timeout_reminder'];
  68. //用户服务中-已超时提醒
  69. $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 2)
  70. ->where('service_start', '<=', time() - $timeout_reminder)
  71. ->where(function($query) use ($kefu_timeout_warning){
  72. if ($kefu_timeout_warning > 0) {
  73. $query->where('service_start', '>', time() - $kefu_timeout_warning);
  74. }
  75. })
  76. ->where('timeout_type', '<',4)
  77. ->select()
  78. ->toArray();
  79. $output->writeln('用户服务中-已超时提醒'.count($list));
  80. $this->sendMessage($list, 4);
  81. }
  82. if (isset($config['user_inline_finished']) && $config['user_inline_finished'] > 0) {
  83. $user_inline_finished = $config['user_inline_finished'] * 60;
  84. //会员离线后自动结束会话时间
  85. $list = User::where('role', 0)
  86. ->where('is_online', 0)
  87. ->whereIn('service_status', [0,1,2])
  88. ->where('offline_time', '<=', time() - $user_inline_finished)
  89. ->select()
  90. ->toArray();
  91. $cs_uid = getAutoCsUid();//获取机器人ID
  92. foreach($list as $user) {
  93. try {
  94. Db::startTrans();
  95. //用户转给机器人客服
  96. User::where('user_id', $user['user_id'])->update(['service_status' => 0, 'service_start' => 0, 'timeout_type' => 0, 'cs_uid'=>$cs_uid]);
  97. KefuTime::endData($user['uid'], 3, $user['cs_uid']); //结束接线时间
  98. Db::commit();
  99. //通知关闭聊天框
  100. wsSendMsg($user['cs_uid'],'closeChat',['user_id'=>$user['user_id']]);
  101. //通知客服已结束
  102. wsSendMsg($user['cs_uid'],'handleChat',['user_id'=>$user['user_id']]);
  103. } catch (\Exception $e) {
  104. Db::rollback();
  105. }
  106. }
  107. $output->writeln('会员离线后自动结束会话时间'.count($list));
  108. }
  109. if (isset($config['kefu_inline_finished']) && $config['kefu_inline_finished'] > 0) {
  110. $kefu_inline_finished = $config['kefu_inline_finished'] * 60;
  111. //客服离线后自动结束会话时间
  112. $list = User::where('role', 3)
  113. ->where('is_online', 0)
  114. ->where('is_finished', 0)
  115. ->where('offline_time', '<=', time() - $kefu_inline_finished)
  116. ->select()
  117. ->toArray();
  118. $cs_uid = getAutoCsUid();//获取机器人ID
  119. foreach($list as $user) {
  120. try {
  121. Db::startTrans();
  122. User::kefuOffline($user['user_id'], $user['uid']);
  123. Db::commit();
  124. } catch (\Exception $e) {
  125. Db::rollback();
  126. }
  127. }
  128. $output->writeln('客服离线后自动结束会话时间'.count($list));
  129. }
  130. }
  131. //检测离线用户,更新在线状态
  132. public function checkOfflineUser()
  133. {
  134. Gateway::$registerAddress = config('gateway.registerAddress');
  135. $onlineList=Gateway::getAllUidList();
  136. $userOnlineList = User::where('is_online', '>', 0)->column('user_id');
  137. foreach($userOnlineList as $user_id) {
  138. if(!isset($onlineList[$user_id])) {
  139. User::where('user_id', $user_id)->update(['is_online' => 0]);
  140. }
  141. }
  142. }
  143. /**
  144. * 发送提醒通知
  145. */
  146. public function sendMessage($list, $type)
  147. {
  148. foreach($list as $user) {
  149. //更新用户超时提醒的状态
  150. User::where('user_id', $user['user_id'])->update(['timeout_type' => $type]);
  151. //通知客服已结束
  152. wsSendMsg($user['cs_uid'],'timeout',['user_id'=>$user['user_id'], 'realname'=>$user['realname'], 'type'=>$type]);
  153. }
  154. }
  155. }