Service.php 12 KB

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