Zory 1 mese fa
parent
commit
8abc597ef9

+ 1 - 1
app/command/UserOff.php

@@ -28,7 +28,7 @@ class UserOff extends Command
                 }
                 $endTime = time();
                 foreach ($resp as $item) {
-                    if (strtotime("+10 min",strtotime($item['last_active_at'])) < $endTime) {
+                    if (strtotime("+5 min",strtotime($item['last_active_at'])) < $endTime) {
                         $output->writeln("客服下线=={$item['truename']}==".getDateFull());
                         (new SystemUser)->where('id',$item['id'])->update(['is_line' => 0]);
                     } else {

+ 96 - 0
app/controller/service/Chat.php

@@ -175,6 +175,102 @@ class Chat extends Base
     }
 
 
+    #[GetMapping('line')]
+    public function getUserLine(Request $request): Response
+    {
+        try {
+            $user = (new SystemUser)->where("id",$request->user['id'])->findOrEmpty();
+            if ($user->isEmpty()) return error("数据不存在");
+            return success("ok",['line' => $user['is_line']]);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+
+    #[PostMapping('line/set')]
+    public function setUserLine(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "status.require"  => trans("empty.require"),
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            $user = (new SystemUser)->where("id",$request->user['id'])->findOrEmpty();
+            if ($user->isEmpty()) return error("数据不存在");
+            $user->is_line = $param['status'];
+            $state = $user->save();
+            if (!$state) return error('操作失败');
+            return success("ok",['line' => $user['is_line']]);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+    #[GetMapping('search')]
+    public function searchChat(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "key.require"       => trans("empty.require"),
+                "type.require"      => trans("empty.require"),
+                "page.default"      => 1,
+                "size.default"      => 10
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            if ($param['type'] == 'user') {
+                $data = (new SaasChatStore)->where(['service_id' => $request->user['id']])->where("nickname","like","%{$param['key']}%")->with(['user' => function ($query) {
+                    $query->field("openid,nickname,avatar");
+                }])->paginate([
+                    "list_rows" => $param['size'],
+                    "page"      => $param['page']
+                ]);
+            } else {
+                $data = (new SaasChatMsg)->where(['service_id' => $request->user['id']])->where("content","like","%{$param['key']}%")->with(['user' => function ($query) {
+                    $query->field("openid,nickname,avatar");
+                }])->paginate([
+                    "list_rows" => $param['size'],
+                    "page"      => $param['page']
+                ]);
+            }
+            return success('ok',$data->toArray());
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+    #[PostMapping('check')]
+    public function searchUserCheck(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "openid.require"        => trans("empty.require"),
+                "poi_id.require"        => trans("empty.require"),
+                "service_id.require"    => trans("empty.require"),
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            $data = $this->service->setModel()->getList($param,['user' => function($query){
+                $query->field("openid,nickname,avatar,mobile,create_at");
+            }],false,['avatar','last','lastTime'],['avatar' => function(){
+                return sConf("service.logo");
+            },'last' => function ($data,$next) use ($param) {
+                $map = ['poi_id' => $next['poi_id'], 'service_id' => $param['service_id'], 'openid' => $next['openid']];
+                $unreadSql = Db::table('saas_chat_msg')
+                    ->where($map)
+                    ->where('is_read', 0)
+                    ->field('COUNT(*)')
+                    ->buildSql();
+                return (new SaasChatMsg)->where($map)->field(['LEFT(content, 12) as content', 'create_at', 'type', Db::raw($unreadSql . ' AS num')])->order('create_at',"desc")->find();
+            },'lastTime' => function ($data,$next) {
+                return formatTime($next['last_at']);
+            }]);
+            return success('ok',$data);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+
     #[GetMapping('ts')]
     public function testData()
     {

+ 7 - 7
app/controller/service/Heart.php

@@ -21,13 +21,13 @@ class Heart extends Base
     public function setHeartBeat(Request $request): Response
     {
         try {
-            $user = (new SystemUser)->where("id",$request->user['id'])->findOrEmpty();
-            if ($user->isEmpty()) return errorTrans("empty.data");
-            if ($user['is_line'] == 0) {
-                $user->is_line = 1;
-            }
-            $user->last_active_at = getDateFull();
-            $user->save();
+//            $user = (new SystemUser)->where("id",$request->user['id'])->findOrEmpty();
+//            if ($user->isEmpty()) return errorTrans("empty.data");
+//            if ($user['is_line'] == 0) {
+//                $user->is_line = 1;
+//            }
+//            $user->last_active_at = getDateFull();
+//            $user->save();
             return success("ok");
         } catch (\Throwable $throwable) {
             return error($throwable->getMessage());

+ 53 - 11
app/controller/v2/Chat.php

@@ -10,6 +10,7 @@ use app\model\saas\SaasChatStore;
 use app\model\saas\SaasService;
 use app\model\saas\SaasStore;
 use app\model\saas\SaasUserOpen;
+use app\model\system\SystemUser;
 use app\service\saas\ChatStoreService;
 use DI\Attribute\Inject;
 use LinFly\Annotation\Attributes\Route\Controller;
@@ -44,7 +45,7 @@ class Chat extends Base
             }
             $param['openid'] = $request->user['openid'];
             $list = $this->service->setModel()->getList($param,null,true,['last','avatar'],['last' => function($data,$resp) use($param){
-                $last = (new SaasChatMsg)->where(['poi_id' => $resp['poi_id'],'openid' => $param['openid'],'is_user' => 0])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
+                $last = (new SaasChatMsg)->where(['poi_id' => $resp['poi_id'],'openid' => $param['openid']])->order("create_at","desc")->field("content,create_at,type")->findOrEmpty();
                 if ($last->isEmpty()) {
                     return ['type' => "text","content"=>"-",'num' => 0,"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
                 }
@@ -75,6 +76,8 @@ class Chat extends Base
                 "poi.require"   => "参数错误",
                 "size.require"  => "参数错误",
                 "page.require"  => "参数错误",
+                "goods.default"  => "",
+                "order.default"  => "",
             ],$request->method());
             if (!is_array($param)) return error($param);
             $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
@@ -101,6 +104,43 @@ class Chat extends Base
                 ]);
             $serviceAvatar = sConf("service.logo");
             $star = (new SaasService)->where("poi_id",$param['poi'])->with(['quick'])->findOrEmpty();
+            // 获取在线客服
+            $service = (new SystemUser)->where(['store_id' => $param['poi'],"type" => 3,"is_line" => 1])->order("last_msg_at","asc")->findOrEmpty();
+            print_r($service->toArray());
+            if ($service->isEmpty()) { // 无客服在线
+                return success("ok",[
+                    "avatar"    => $userAvatar,
+                    "userId"    => md5($request->user['openid']),
+                    "msg"       => $msg,
+                    "service"   => $serviceAvatar,
+                    "store"     => [
+                        "shopName" => $store['poi_name'],
+                        "shopId"   => $store['poi_id'],
+                    ],
+                    "quick"     => $star,
+                    "chat_id"   => 0, // 客服ID
+                ]);
+            }
+            $chatStore = (new SaasChatStore)->where(['poi_id'=> $store['poi_id'],"openid" => $request->user['openid']])->findOrEmpty();
+            if ($chatStore->isEmpty()) {
+                $chatStore->insertGetId([
+                    "poi_id"    => $store['poi_id'],
+                    "poi_name"  => $store['poi_name'],
+                    "openid"    => $request->user['openid'],
+                    "nickname"  => $request->user['nickname'],
+                    "service_id"    => $service['id'],
+                    "order"     => $param['order']??'',
+                    "goods"     => $param['goods']??'',
+                    "last_at"   => getDateFull()
+                ]);
+            } else {
+                $chatStore->service_id = $service['id'];
+                $chatStore->order = $param['order']??'';
+                $chatStore->nickname = $request->user['nickname'];
+                $chatStore->goods = $param['goods']??'';
+                $chatStore->last_at = getDateFull();
+                $chatStore->save();
+            }
             return success("ok",[
                 "avatar"    => $userAvatar,
                 "userId"    => md5($request->user['openid']),
@@ -110,7 +150,8 @@ class Chat extends Base
                     "shopName" => $store['poi_name'],
                     "shopId"   => $store['poi_id'],
                 ],
-                "quick"     => $star
+                "quick"     => $star,
+                "chat_id"   => $service['id'], // 客服ID
             ]);
         } catch (\Throwable $throwable) {
             return error($throwable->getMessage());
@@ -183,6 +224,7 @@ class Chat extends Base
             $param = $this->_valid([
                 "content.require"   => trans("empty.require"),
                 "groupId.require"   => trans("empty.require"),
+                "chatId.require"    => trans("empty.require"),
                 "type.require"      => trans("empty.require")
             ],"post");
             if (!is_array($param)) return error($param);
@@ -193,14 +235,14 @@ class Chat extends Base
                 "type"      => $param['type'],
                 "msgId"     => time(),
                 "poi_id"    => $param['groupId'],
-                "service_id" => '105', // 客服ID
+                "service_id" => $param['chatId'], // 客服ID
             ]);
-            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => 105])->with(['user' => function($query){
+            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => $param['chatId']])->with(['user' => function($query){
                 $query->field("openid,nickname,avatar,mobile,create_at");
             }])->append(['avatar'])->withAttr(['avatar' => function () {
                 return sConf("service.logo");
             }])->findOrEmpty();
-            $total = (new SaasChatMsg)->where(["poi_id" => $chatStore['poi_id'],"service_id" => 105,"openid" => $chatStore['openid'],'is_read' => 0])->count();
+            $total = (new SaasChatMsg)->where(["poi_id" => $chatStore['poi_id'],"service_id" => $param['chatId'],"openid" => $chatStore['openid'],'is_read' => 0])->count();
             if (!$chatStore->isEmpty()) {
                 $chatStore->last_at = getDateFull();
                 $chatStore->save();
@@ -213,7 +255,7 @@ class Chat extends Base
             }
             // 推送消息
             $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
-            $api->trigger("service-105","message",[
+            $api->trigger("service-{$param['chatId']}","message",[
                 "type"      => $param['type'],
                 "content"   => is_array($param['content'])?json_encode($param['content']):$param['content'],
                 "openid"    => $request->user['openid'],
@@ -244,7 +286,7 @@ class Chat extends Base
             $param = $this->_valid([
                 "groupId.require"   => trans("empty.require"),
                 "type.require"      => trans("empty.require"),
-                "sendId.require"    => trans("empty.require"),
+                "chatId.require"    => trans("empty.require"),
             ],"post");
             if (!is_array($param)) return error($param);
             $resp = UploadExtend::uploadFile();
@@ -257,14 +299,14 @@ class Chat extends Base
                 "type"      => $param['type'],
                 "msgId"     => time(),
                 "poi_id"    => $param['groupId'],
-                "service_id" => '105', // 客服ID
+                "service_id" => $param['chatId'], // 客服ID
             ]);
-            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => 105])->with(['user' => function($query){
+            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => $param['chatId']])->with(['user' => function($query){
                 $query->field("openid,nickname,avatar,mobile,create_at");
             }])->append(['avatar'])->withAttr(['avatar' => function () {
                 return sConf("service.logo");
             }])->findOrEmpty();
-            $total = (new SaasChatMsg)->where(["poi_id" => $chatStore['poi_id'],"service_id" => 105,"openid" => $chatStore['openid'],'is_read' => 0])->count();
+            $total = (new SaasChatMsg)->where(["poi_id" => $chatStore['poi_id'],"service_id" => $param['chatId'],"openid" => $chatStore['openid'],'is_read' => 0])->count();
             if (!$chatStore->isEmpty()) {
                 $chatStore->last_at = getDateFull();
                 $chatStore->save();
@@ -277,7 +319,7 @@ class Chat extends Base
             }
             // 推送消息
             $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
-            $api->trigger("service-105","message",[
+            $api->trigger("service-{$param['chatId']}","message",[
                 "type"      => $param['type'],
                 "content"   => is_array($param['content'])?json_encode($param['content']):$param['content'],
                 "openid"    => $request->user['openid'],