|
|
@@ -0,0 +1,218 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\controller\v2;
|
|
|
+
|
|
|
+use app\extra\basic\Base;
|
|
|
+use app\middleware\AuthMiddleware;
|
|
|
+use app\model\saas\SaasChatMsg;
|
|
|
+use app\model\saas\SaasService;
|
|
|
+use app\model\saas\SaasStore;
|
|
|
+use app\model\saas\SaasUserOpen;
|
|
|
+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;
|
|
|
+
|
|
|
+
|
|
|
+#[Controller("/v2/chat"),Middleware(AuthMiddleware::class)]
|
|
|
+class Chat 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'],'openid' => $param['openid'],'is_user' => 0])->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"=> str_cut_ellipsis($last['content'],12),'num' => 0,"create_at"=> formatTime($last['create_at']),'type' => $last['type']];
|
|
|
+ },'avatar' => function(){
|
|
|
+ return sConf("service.logo");
|
|
|
+ }]);
|
|
|
+ $user = (new SaasUserOpen)->where(['openid' => $param['openid']])->findOrEmpty();
|
|
|
+ if ($user->isEmpty()) return error("数据错误");
|
|
|
+ $userState = 1;
|
|
|
+ if (empty($user['avatar'])) {
|
|
|
+ $userState = 0;
|
|
|
+ }
|
|
|
+ return successTrans("success.data",['data' => pageFormat($list),'user' => $userState]);
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取基础客服消息
|
|
|
+ */
|
|
|
+ #[GetMapping("service")]
|
|
|
+ public function getChatStore(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $this->_valid([
|
|
|
+ "poi.require" => "参数错误",
|
|
|
+ "size.require" => "参数错误",
|
|
|
+ "page.require" => "参数错误",
|
|
|
+ ],$request->method());
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
+ $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
|
|
|
+ if ($store->isEmpty()) return error("店铺不存在");
|
|
|
+ $userAvatar = $request->user['avatar'];
|
|
|
+ // 获取最新的10条聊天记录
|
|
|
+ $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id']])
|
|
|
+ ->append(['time','userType','msgType'])
|
|
|
+ ->withAttr(['time' => function($resp,$data){
|
|
|
+ return strtotime($data['create_at']) * 1000;
|
|
|
+ },'userType' => function($resp,$data){
|
|
|
+ if ($data['source'] == 1) {
|
|
|
+ return "self";
|
|
|
+ } else {
|
|
|
+ return "friend";
|
|
|
+ }
|
|
|
+ },'msgType' => function($resp,$data){
|
|
|
+ return $data['type'];
|
|
|
+ }])
|
|
|
+ ->order("id","desc")
|
|
|
+ ->paginate([
|
|
|
+ "list_rows" => $param['size'],
|
|
|
+ "page" => 1
|
|
|
+ ]);
|
|
|
+ $serviceAvatar = sConf("service.logo");
|
|
|
+ $star = (new SaasService)->where("poi_id",$param['poi'])->with(['quick'])->findOrEmpty();
|
|
|
+ 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
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 历史聊天列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ #[GetMapping("msg")]
|
|
|
+ public function getMsgList(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $this->_valid([
|
|
|
+ "poi.require" => "参数错误",
|
|
|
+ "size.require" => "参数错误",
|
|
|
+ "page.require" => "参数错误",
|
|
|
+ ],$request->method());
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
+ $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
|
|
|
+ if ($store->isEmpty()) return error("店铺不存在");
|
|
|
+ $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id']])
|
|
|
+ ->append(['time','userType','msgType'])
|
|
|
+ ->withAttr(['time' => function($resp,$data){
|
|
|
+ return strtotime($data['create_at']) * 1000;
|
|
|
+ },'userType' => function($resp,$data){
|
|
|
+ if ($data['source'] == 1) {
|
|
|
+ return "self";
|
|
|
+ } else {
|
|
|
+ return "friend";
|
|
|
+ }
|
|
|
+ },'msgType' => function($resp,$data){
|
|
|
+ return $data['type'];
|
|
|
+ }])
|
|
|
+ ->order("id","desc")
|
|
|
+ ->paginate([
|
|
|
+ "list_rows" => $param['size'],
|
|
|
+ "page" => $param['page']
|
|
|
+ ]);
|
|
|
+ return success("ok",['data' => $msg]);
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转人工
|
|
|
+ * @return Response|void
|
|
|
+ */
|
|
|
+ public function checkServiceUser()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文字消息
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ #[PostMapping("text")]
|
|
|
+ public function sendTextMsg(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $this->_valid([
|
|
|
+ "content.require" => trans("empty.require"),
|
|
|
+ "groupId.require" => trans("empty.require"),
|
|
|
+ "type.require" => trans("empty.require")
|
|
|
+ ],"post");
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
+ (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" => '', // 客服ID
|
|
|
+ ]);
|
|
|
+ // 推送消息
|
|
|
+
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送图片消息
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ #[PostMapping("image")]
|
|
|
+ public function sendImageMsg(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|