Service.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\UploadExtend;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasChatMsg;
  7. use app\model\saas\SaasChatStore;
  8. use app\model\saas\SaasStore;
  9. use app\model\system\SystemUser;
  10. use app\service\saas\ChatStoreService;
  11. use DI\Attribute\Inject;
  12. use LinFly\Annotation\Attributes\Route\Controller;
  13. use LinFly\Annotation\Attributes\Route\GetMapping;
  14. use LinFly\Annotation\Attributes\Route\Middleware;
  15. use LinFly\Annotation\Attributes\Route\PostMapping;
  16. use support\Request;
  17. use support\Response;
  18. use Webman\Push\Api;
  19. #[Controller("/dy/service"),Middleware(AuthMiddleware::class)]
  20. class Service extends Base
  21. {
  22. #[Inject]
  23. protected ChatStoreService $service;
  24. /**
  25. * 获取客服列表-已聊过的
  26. * @param Request $request
  27. * @return Response
  28. */
  29. #[GetMapping("list")]
  30. public function getServiceList(Request $request): Response
  31. {
  32. try {
  33. $param = $request->all();
  34. if (!empty($param['size'])) {
  35. $param['pageSize'] = $param['size'];
  36. }
  37. $param['openid'] = $request->user['openid'];
  38. $list = $this->service->setModel()->getList($param,null,true,['last','avatar'],['last' => function($data,$resp) use($param){
  39. $last = (new SaasChatMsg)->where("poi_id",$resp['poi_id'])->where("openid",$param['openid'])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
  40. if ($last->isEmpty()) {
  41. return ['type' => "text","content"=>"无",'num' => 0,"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  42. }
  43. return ["content"=> $last['content'],'num' => 0,"create_at"=> formatTime($last['create_at']),'type' => $last['type']];
  44. },'avatar' => function(){
  45. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  46. }]);
  47. return successTrans("success.data",pageFormat($list));
  48. } catch (\Throwable $throwable) {
  49. return error($throwable->getMessage());
  50. }
  51. }
  52. /**
  53. * 分配客服
  54. * @param Request $request
  55. * @return Response
  56. */
  57. #[PostMapping("shareout")]
  58. public function shareUser2Mer(Request $request): Response
  59. {
  60. try {
  61. $param = $request->all();
  62. $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
  63. if ($store->isEmpty()) return error("店铺不存在");
  64. $chatStore = (new SaasChatStore)->where(['poi_id'=> $store['poi_id'],"openid" => $request->user['openid']])->findOrEmpty();
  65. // 获取最新的10条聊天记录
  66. $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id']])->append(['avatar','time'])->withAttr(['avatar' => function(){
  67. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  68. },'time' => function($resp,$data){
  69. return strtotime($data['create_at']) * 1000;
  70. }])->order("id","desc")->paginate([
  71. "list_rows" => 10,
  72. "page" => 1
  73. ]);
  74. // 获取在线客服
  75. $service = (new SystemUser)->where("store_id",$param['poi'])->where("type",3)->where("is_line",1)->findOrEmpty();
  76. if ($service->isEmpty()) return $this->encode("ok",['store' => $store,'code' => 3,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => 0]); // 无客服在线
  77. if ($chatStore->isEmpty()) {
  78. $chatStore->insertGetId([
  79. "poi_id" => $store['poi_id'],
  80. "poi_name" => $store['poi_name'],
  81. "openid" => $request->user['openid'],
  82. "service_id" => $service['id']
  83. ]);
  84. }
  85. return $this->encode("ok",['store' => $store,'code' => 1,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => $service['id']]); // 客服在线
  86. } catch (\Throwable $throwable) {
  87. return error($throwable->getMessage());
  88. }
  89. }
  90. #[GetMapping("msg")]
  91. public function getMessageList(Request $request): Response
  92. {
  93. try {
  94. $param = $this->_valid([
  95. "poi.require" => trans("empty.require"),
  96. "page.default" => 1,
  97. "size.default" => 10
  98. ],$request->method());
  99. if (!is_array($param)) return error($param);
  100. $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $param['poi']])->append(['avatar','time'])->withAttr(['avatar' => function(){
  101. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  102. },'time' => function($resp,$data){
  103. return strtotime($data['create_at']) * 1000;
  104. }])->order("id","desc")->paginate([
  105. "list_rows" => $param['size']??10,
  106. "page" => $param['page']??1,
  107. ]);
  108. return $this->encode("ok",pageFormat($msg));
  109. } catch (\Throwable $throwable) {
  110. return error($throwable->getMessage());
  111. }
  112. }
  113. /**
  114. * 发送消息
  115. * @param Request $request
  116. * @return Response
  117. */
  118. #[PostMapping("send")]
  119. public function sendMsg(Request $request): Response
  120. {
  121. try {
  122. $param = $this->_valid([
  123. "content.require" => trans("empty.require"),
  124. "groupId.require" => trans("empty.require"),
  125. "type.require" => trans("empty.require"),
  126. "sendId.require" => trans("empty.require"),
  127. ],"post");
  128. if (!is_array($param)) return error($param);
  129. $state = (new SaasChatMsg)->insertGetId([
  130. "source" => 1,
  131. "openid" => $request->user['openid'],
  132. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  133. "type" => $param['type'],
  134. "msgId" => time(),
  135. "poi_id" => $param['groupId'],
  136. "service_id" => $param['sendId'],
  137. ]);
  138. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  139. $api->trigger("service-{$param['sendId']}","message",[
  140. "type" => $param['type'],
  141. "time" => time() * 1000,
  142. "msgId" => time(),
  143. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  144. "source" => 1,
  145. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  146. "poi_id" => $param['groupId'],
  147. "openid" => $request->user['openid'],
  148. "service_id" => $param['sendId'],
  149. "create_at" => formatTime(time()),
  150. "user" => [
  151. "nickname" => $request->user['nickname']
  152. ]
  153. ]);
  154. return successTrans("success.data");
  155. } catch (\Throwable $throwable) {
  156. echo $throwable->getMessage()."\n";
  157. return error($throwable->getMessage());
  158. }
  159. }
  160. /**
  161. * 发送图片消息
  162. * @param Request $request
  163. * @return Response
  164. */
  165. #[PostMapping("send/img")]
  166. public function sendImgMsg(Request $request): Response
  167. {
  168. try {
  169. $resp = UploadExtend::uploadFile();
  170. if (!isset($resp[0]['url'])) return error("发送失败");
  171. $param = $this->_valid([
  172. "groupId.require" => trans("empty.require"),
  173. "type.require" => trans("empty.require"),
  174. "sendId.require" => trans("empty.require"),
  175. ],"post");
  176. if (!is_array($param)) return error($param);
  177. $param['content'] = $resp[0]['url'];
  178. $state = (new SaasChatMsg)->insertGetId([
  179. "source" => 1,
  180. "openid" => $request->user['openid'],
  181. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  182. "type" => $param['type'],
  183. "msgId" => time(),
  184. "poi_id" => $param['groupId'],
  185. "service_id" => $param['sendId'],
  186. ]);
  187. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  188. $api->trigger("service-{$param['sendId']}","message",[
  189. "type" => $param['type'],
  190. "time" => time() * 1000,
  191. "msgId" => time(),
  192. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  193. "source" => 1,
  194. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  195. "poi_id" => $param['groupId'],
  196. "openid" => $request->user['openid'],
  197. "service_id" => $param['sendId'],
  198. "create_at" => formatTime(time()),
  199. "user" => [
  200. "nickname" => $request->user['nickname']
  201. ]
  202. ]);
  203. return successTrans("success.data",['path' => $param['content']],200);
  204. } catch (\Throwable $throwable) {
  205. echo $throwable->getMessage()."\n";
  206. return error($throwable->getMessage());
  207. }
  208. }
  209. /**
  210. * 收集完善用户收货地址
  211. * @param Request $request
  212. * @return Response
  213. */
  214. #[PostMapping("send/address")]
  215. public function setOrderAddress(Request $request): Response
  216. {
  217. try {
  218. $param = $request->all();
  219. print_r($param);
  220. return success("提交成功");
  221. } catch (\Throwable $throwable) {
  222. echo $throwable->getMessage()."\n";
  223. return error($throwable->getMessage());
  224. }
  225. }
  226. }