UserTimeout.php 7.5 KB

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