Service.php 11 KB

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