| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasChatMsg;
- use app\model\saas\SaasChatStore;
- use app\model\saas\SaasStore;
- use app\model\system\SystemUser;
- use app\service\saas\ChatStoreService;
- use DI\Attribute\Inject;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use LinFly\Annotation\Attributes\Route\Middleware;
- use LinFly\Annotation\Attributes\Route\PostMapping;
- use support\Request;
- use support\Response;
- use Webman\Push\Api;
- #[Controller("/dy/service"),Middleware(AuthMiddleware::class)]
- class Service extends Base
- {
- #[Inject]
- protected ChatStoreService $service;
- /**
- * 获取客服列表-已聊过的
- * @param Request $request
- * @return Response
- */
- #[GetMapping("list")]
- public function getServiceList(Request $request): Response
- {
- try {
- $param = $request->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();
- // 获取在线客服
- $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]); // 无客服在线
- if ($chatStore->isEmpty()) {
- $chatStore->insertGetId([
- "poi_id" => $store['poi_id'],
- "poi_name" => $store['poi_name'],
- "openid" => $request->user['openid'],
- "service_id" => $service['id']
- ]);
- }
- // 获取最新的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
- ]);
- 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()),
- ]);
- return successTrans("success.data");
- } catch (\Throwable $throwable) {
- echo $throwable->getMessage()."\n";
- return error($throwable->getMessage());
- }
- }
- }
|