all(); if (!empty($param['size'])) { $param['pageSize'] = $param['size']; } $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'])->where("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()))]; } return ["content"=> $last['content'],'num' => 0,"create_at"=> formatTime($last['create_at']),'type' => $last['type']]; },'avatar' => function(){ return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png"; }]); return successTrans("success.data",pageFormat($list)); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 分配客服 * @param Request $request * @return Response */ #[PostMapping("shareout")] public function shareUser2Mer(Request $request): Response { try { $param = $request->all(); $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty(); if ($store->isEmpty()) return error("店铺不存在"); $chatStore = (new SaasChatStore)->where(['poi_id'=> $store['poi_id'],"openid" => $request->user['openid']])->findOrEmpty(); // 获取最新的10条聊天记录 $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id']])->append(['avatar','time'])->withAttr(['avatar' => function(){ return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png"; },'time' => function($resp,$data){ return strtotime($data['create_at']) * 1000; }])->order("id","desc")->paginate([ "list_rows" => 10, "page" => 1 ]); // 获取在线客服 $service = (new SystemUser)->where("store_id",$param['poi'])->where("type",3)->where("is_line",1)->findOrEmpty(); if ($service->isEmpty()) return $this->encode("ok",['store' => $store,'code' => 3,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => 0]); // 无客服在线 if ($chatStore->isEmpty()) { $chatStore->insertGetId([ "poi_id" => $store['poi_id'], "poi_name" => $store['poi_name'], "openid" => $request->user['openid'], "service_id" => $service['id'] ]); } return $this->encode("ok",['store' => $store,'code' => 1,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => $service['id']]); // 客服在线 } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } #[GetMapping("msg")] public function getMessageList(Request $request): Response { try { $param = $this->_valid([ "poi.require" => trans("empty.require"), "page.default" => 1, "size.default" => 10 ],$request->method()); if (!is_array($param)) return error($param); $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $param['poi']])->append(['avatar','time'])->withAttr(['avatar' => function(){ return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png"; },'time' => function($resp,$data){ return strtotime($data['create_at']) * 1000; }])->order("id","desc")->paginate([ "list_rows" => $param['size']??10, "page" => $param['page']??1, ]); return $this->encode("ok",pageFormat($msg)); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 发送消息 * @param Request $request * @return Response */ #[PostMapping("send")] public function sendMsg(Request $request): Response { try { $param = $this->_valid([ "content.require" => trans("empty.require"), "groupId.require" => trans("empty.require"), "type.require" => trans("empty.require"), "sendId.require" => trans("empty.require"), ],"post"); if (!is_array($param)) return error($param); $state = (new SaasChatMsg)->insertGetId([ "source" => 1, "openid" => $request->user['openid'], "content" => is_array($param['content'])?json_encode($param['content']):$param['content'], "type" => $param['type'], "msgId" => time(), "poi_id" => $param['groupId'], "service_id" => $param['sendId'], ]); $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-{$param['sendId']}","message",[ "type" => $param['type'], "time" => time() * 1000, "msgId" => time(), "content" => is_array($param['content'])?json_encode($param['content']):$param['content'], "source" => 1, "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png", "poi_id" => $param['groupId'], "openid" => $request->user['openid'], "service_id" => $param['sendId'], "create_at" => formatTime(time()), "user" => [ "nickname" => $request->user['nickname'] ] ]); return successTrans("success.data"); } catch (\Throwable $throwable) { echo $throwable->getMessage()."\n"; return error($throwable->getMessage()); } } /** * 发送图片消息 * @param Request $request * @return Response */ #[PostMapping("send/img")] public function sendImgMsg(Request $request): Response { try { $resp = UploadExtend::uploadFile(); if (!isset($resp[0]['url'])) return error("发送失败"); $param = $this->_valid([ "groupId.require" => trans("empty.require"), "type.require" => trans("empty.require"), "sendId.require" => trans("empty.require"), ],"post"); if (!is_array($param)) return error($param); $param['content'] = $resp[0]['url']; $state = (new SaasChatMsg)->insertGetId([ "source" => 1, "openid" => $request->user['openid'], "content" => is_array($param['content'])?json_encode($param['content']):$param['content'], "type" => $param['type'], "msgId" => time(), "poi_id" => $param['groupId'], "service_id" => $param['sendId'], ]); $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-{$param['sendId']}","message",[ "type" => $param['type'], "time" => time() * 1000, "msgId" => time(), "content" => is_array($param['content'])?json_encode($param['content']):$param['content'], "source" => 1, "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png", "poi_id" => $param['groupId'], "openid" => $request->user['openid'], "service_id" => $param['sendId'], "create_at" => formatTime(time()), "user" => [ "nickname" => $request->user['nickname'] ] ]); return successTrans("success.data",['path' => $param['content']],200); } catch (\Throwable $throwable) { echo $throwable->getMessage()."\n"; return error($throwable->getMessage()); } } #[PostMapping("send/address")] public function setOrderAddress(Request $request): Response { try { $param = $request->all(); print_r($param); return success("提交成功"); } catch (\Throwable $throwable) { echo $throwable->getMessage()."\n"; return error($throwable->getMessage()); } } }