Service.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $userAvatar = $request->user['avatar'];
  66. // 获取最新的10条聊天记录
  67. $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id']])->append(['avatar','time'])->withAttr(['avatar' => function($data,$resp) use($userAvatar){
  68. if ($resp['source'] == 1) { // 用户
  69. return $userAvatar??'';
  70. } else {
  71. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  72. }
  73. },'time' => function($resp,$data){
  74. return strtotime($data['create_at']) * 1000;
  75. }])->order("id","desc")->paginate([
  76. "list_rows" => 10,
  77. "page" => 1
  78. ]);
  79. // 获取在线客服
  80. $service = (new SystemUser)->where("store_id",$param['poi'])->where("type",3)->where("is_line",1)->findOrEmpty();
  81. if ($service->isEmpty()) return $this->encode("ok",['store' => $store,'code' => 3,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => 0]); // 无客服在线
  82. if ($chatStore->isEmpty()) {
  83. $chatStore->insertGetId([
  84. "poi_id" => $store['poi_id'],
  85. "poi_name" => $store['poi_name'],
  86. "openid" => $request->user['openid'],
  87. "service_id" => $service['id'],
  88. "order" => $param['order']??'',
  89. "goods" => $param['goods']??''
  90. ]);
  91. } else {
  92. $chatStore->service_id = $service['id'];
  93. $chatStore->order = $param['order']??'';
  94. $chatStore->goods = $param['goods']??'';
  95. $chatStore->save();
  96. }
  97. return $this->encode("ok",['store' => $store,'code' => 1,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => $service['id'],'avatar' => $userAvatar]); // 客服在线
  98. } catch (\Throwable $throwable) {
  99. echo $throwable->getMessage()."\n";
  100. echo $throwable->getFile()."\n";
  101. echo $throwable->getLine()."\n";
  102. return error($throwable->getMessage());
  103. }
  104. }
  105. #[GetMapping("msg")]
  106. public function getMessageList(Request $request): Response
  107. {
  108. try {
  109. $param = $this->_valid([
  110. "poi.require" => trans("empty.require"),
  111. "page.default" => 1,
  112. "size.default" => 10
  113. ],$request->method());
  114. if (!is_array($param)) return error($param);
  115. $userAvatar = $request->user['avatar'];
  116. $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $param['poi']])->append(['avatar','time'])->withAttr(['avatar' => function($data,$resp) use($userAvatar){
  117. if ($resp['source'] == 1) {
  118. return $userAvatar??'';
  119. } else {
  120. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  121. }
  122. },'time' => function($resp,$data){
  123. return strtotime($data['create_at']) * 1000;
  124. }])->order("id","desc")->paginate([
  125. "list_rows" => $param['size']??10,
  126. "page" => $param['page']??1,
  127. ]);
  128. return $this->encode("ok",pageFormat($msg));
  129. } catch (\Throwable $throwable) {
  130. return error($throwable->getMessage());
  131. }
  132. }
  133. /**
  134. * 发送消息
  135. * @param Request $request
  136. * @return Response
  137. */
  138. #[PostMapping("send")]
  139. public function sendMsg(Request $request): Response
  140. {
  141. try {
  142. $param = $this->_valid([
  143. "content.require" => trans("empty.require"),
  144. "groupId.require" => trans("empty.require"),
  145. "type.require" => trans("empty.require"),
  146. "sendId.require" => trans("empty.require"),
  147. ],"post");
  148. if (!is_array($param)) return error($param);
  149. $state = (new SaasChatMsg)->insertGetId([
  150. "source" => 1,
  151. "openid" => $request->user['openid'],
  152. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  153. "type" => $param['type'],
  154. "msgId" => time(),
  155. "poi_id" => $param['groupId'],
  156. "service_id" => $param['sendId'],
  157. ]);
  158. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  159. $api->trigger("service-{$param['sendId']}","message",[
  160. "type" => $param['type'],
  161. "time" => time() * 1000,
  162. "msgId" => time(),
  163. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  164. "source" => 1,
  165. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  166. "poi_id" => $param['groupId'],
  167. "openid" => $request->user['openid'],
  168. "service_id" => $param['sendId'],
  169. "create_at" => formatTime(time()),
  170. "user" => [
  171. "nickname" => $request->user['nickname'],
  172. "avatar" => $request->user['avatar']
  173. ]
  174. ]);
  175. return successTrans("success.data");
  176. } catch (\Throwable $throwable) {
  177. echo $throwable->getMessage()."\n";
  178. return error($throwable->getMessage());
  179. }
  180. }
  181. /**
  182. * 发送图片消息
  183. * @param Request $request
  184. * @return Response
  185. */
  186. #[PostMapping("send/img")]
  187. public function sendImgMsg(Request $request): Response
  188. {
  189. try {
  190. $resp = UploadExtend::uploadFile();
  191. if (!isset($resp[0]['url'])) return error("发送失败");
  192. $param = $this->_valid([
  193. "groupId.require" => trans("empty.require"),
  194. "type.require" => trans("empty.require"),
  195. "sendId.require" => trans("empty.require"),
  196. ],"post");
  197. if (!is_array($param)) return error($param);
  198. $param['content'] = $resp[0]['url'];
  199. $state = (new SaasChatMsg)->insertGetId([
  200. "source" => 1,
  201. "openid" => $request->user['openid'],
  202. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  203. "type" => $param['type'],
  204. "msgId" => time(),
  205. "poi_id" => $param['groupId'],
  206. "service_id" => $param['sendId'],
  207. ]);
  208. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  209. $api->trigger("service-{$param['sendId']}","message",[
  210. "type" => $param['type'],
  211. "time" => time() * 1000,
  212. "msgId" => time(),
  213. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  214. "source" => 1,
  215. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  216. "poi_id" => $param['groupId'],
  217. "openid" => $request->user['openid'],
  218. "service_id" => $param['sendId'],
  219. "create_at" => formatTime(time()),
  220. "user" => [
  221. "nickname" => $request->user['nickname']
  222. ]
  223. ]);
  224. return successTrans("success.data",['path' => $param['content']],200);
  225. } catch (\Throwable $throwable) {
  226. echo $throwable->getMessage()."\n";
  227. return error($throwable->getMessage());
  228. }
  229. }
  230. /**
  231. * 收集完善用户收货地址
  232. * @param Request $request
  233. * @return Response
  234. */
  235. #[PostMapping("send/address")]
  236. public function setOrderAddress(Request $request): Response
  237. {
  238. try {
  239. $param = $request->all();
  240. print_r($param);
  241. return success("提交成功");
  242. } catch (\Throwable $throwable) {
  243. echo $throwable->getMessage()."\n";
  244. return error($throwable->getMessage());
  245. }
  246. }
  247. }