Service.php 11 KB

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