|
|
@@ -0,0 +1,107 @@
|
|
|
+<?php
|
|
|
+namespace app\admin\command;
|
|
|
+
|
|
|
+use think\console\Command;
|
|
|
+use think\console\Input;
|
|
|
+use think\console\Output;
|
|
|
+use app\admin\model\User;
|
|
|
+use app\admin\model\Config;
|
|
|
+use app\admin\model\KefuTime;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Worker 命令行类
|
|
|
+ */
|
|
|
+class UserTimeout extends Command
|
|
|
+{
|
|
|
+ public function configure()
|
|
|
+ {
|
|
|
+ $this->setName('user:timeout')
|
|
|
+ ->setDescription('用户会话检测');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function execute(Input $input, Output $output)
|
|
|
+ {
|
|
|
+ $config = Config::whereIn('field',['kefu_timeout_reminder','kefu_timeout_warning','user_timeouted_warning', 'user_timeout_warning','user_inline_finished'])->column('val', 'field');
|
|
|
+
|
|
|
+ $user_timeout_warning = 0;
|
|
|
+ if (isset($config['user_timeout_warning']) && $config['user_timeout_warning'] > 0) {
|
|
|
+ $user_timeout_warning = $config['user_timeout_warning'];
|
|
|
+ //用户等待即将超时预警
|
|
|
+ $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 1)
|
|
|
+ ->where('service_start', '<=', time() - $user_timeout_warning)
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ $this->sendMessage($list, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($config['user_timeouted_warning']) && $config['user_timeouted_warning'] > 0) {
|
|
|
+ $user_timeouted_warning = $config['user_timeouted_warning'];
|
|
|
+ //用户等待已超时预警
|
|
|
+ $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 1)
|
|
|
+ ->where('service_start', '<=', time() - $user_timeouted_warning)
|
|
|
+ ->where(function($query) use ($user_timeout_warning){
|
|
|
+ if ($user_timeout_warning > 0) {
|
|
|
+ $query->where('service_start', '>', time() - $user_timeout_warning);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ $this->sendMessage($list, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ $kefu_timeout_warning = 0;
|
|
|
+ if (isset($config['kefu_timeout_warning']) && $config['kefu_timeout_warning'] > 0) {
|
|
|
+ $kefu_timeout_warning = $config['kefu_timeout_warning'];
|
|
|
+ //用户服务中-即将超时预警时间
|
|
|
+ $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 2)
|
|
|
+ ->where('service_start', '<=', time() - $kefu_timeout_warning)
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ $this->sendMessage($list, 3);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($config['kefu_timeout_reminder']) && $config['kefu_timeout_reminder'] > 0) {
|
|
|
+ $timeout_reminder = $config['kefu_timeout_reminder'];
|
|
|
+ //用户服务中-已超时提醒
|
|
|
+ $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 2)
|
|
|
+ ->where('service_start', '<=', time() - $timeout_reminder)
|
|
|
+ ->where(function($query) use ($kefu_timeout_warning){
|
|
|
+ if ($kefu_timeout_warning > 0) {
|
|
|
+ $query->where('service_start', '>', time() - $kefu_timeout_warning);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ $this->sendMessage($list, 4);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($config['user_inline_finished']) && $config['user_inline_finished'] > 0) {
|
|
|
+ $user_inline_finished = $config['user_inline_finished'] * 60;
|
|
|
+ //会员离线后自动结束会话时间
|
|
|
+ $list = User::where('is_online', 0)->whereIn('service_status', [0,1,2])
|
|
|
+ ->where('offline_time', '<=', time() - $user_inline_finished)
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ foreach($list as $user) {
|
|
|
+ User::where('id', $user['user_id'])->update(['service_status' => 3, 'service_start' => time()]);
|
|
|
+ KefuTime::endData($user['cs_uid'], 3); //结束接线时间
|
|
|
+ //通知客服已结束
|
|
|
+ wsSendMsg($user['cs_uid'],'handleChat',['user_id'=>$user['user_id']]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送提醒通知
|
|
|
+ */
|
|
|
+ public function sendMessage($list, $type)
|
|
|
+ {
|
|
|
+ foreach($list as $user) {
|
|
|
+ //通知客服已结束
|
|
|
+ wsSendMsg($user['cs_uid'],'timeout',['user_id'=>$user['user_id'], 'type'=>$type]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|