Service.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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\SaasOrder;
  9. use app\model\saas\SaasOrderAddress;
  10. use app\model\saas\SaasOrderDelay;
  11. use app\model\saas\SaasStore;
  12. use app\model\system\SystemUser;
  13. use app\service\saas\ChatStoreService;
  14. use DI\Attribute\Inject;
  15. use LinFly\Annotation\Attributes\Route\Controller;
  16. use LinFly\Annotation\Attributes\Route\GetMapping;
  17. use LinFly\Annotation\Attributes\Route\Middleware;
  18. use LinFly\Annotation\Attributes\Route\PostMapping;
  19. use support\Request;
  20. use support\Response;
  21. use Webman\Push\Api;
  22. use Webman\RedisQueue\Redis;
  23. use function DI\get;
  24. #[Controller("/dy/service"),Middleware(AuthMiddleware::class)]
  25. class Service extends Base
  26. {
  27. #[Inject]
  28. protected ChatStoreService $service;
  29. /**
  30. * 获取客服列表-已聊过的
  31. * @param Request $request
  32. * @return Response
  33. */
  34. #[GetMapping("list")]
  35. public function getServiceList(Request $request): Response
  36. {
  37. try {
  38. $param = $request->all();
  39. if (!empty($param['size'])) {
  40. $param['pageSize'] = $param['size'];
  41. }
  42. $param['openid'] = $request->user['openid'];
  43. $list = $this->service->setModel()->getList($param,null,true,['last','avatar'],['last' => function($data,$resp) use($param){
  44. $last = (new SaasChatMsg)->where(['poi_id' => $resp['poi_id'],'openid' => $param['openid'],'is_user' => 0])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
  45. if ($last->isEmpty()) {
  46. return ['type' => "text","content"=>"-",'num' => 0,"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  47. }
  48. return ["content"=> str_cut_ellipsis($last['content'],12),'num' => 0,"create_at"=> formatTime($last['create_at']),'type' => $last['type']];
  49. },'avatar' => function(){
  50. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  51. }]);
  52. return successTrans("success.data",pageFormat($list));
  53. } catch (\Throwable $throwable) {
  54. return error($throwable->getMessage());
  55. }
  56. }
  57. /**
  58. * 分配客服
  59. * @param Request $request
  60. * @return Response
  61. */
  62. #[PostMapping("shareout")]
  63. public function shareUser2Mer(Request $request): Response
  64. {
  65. try {
  66. $param = $request->all();
  67. $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
  68. if ($store->isEmpty()) return error("店铺不存在");
  69. $chatStore = (new SaasChatStore)->where(['poi_id'=> $store['poi_id'],"openid" => $request->user['openid']])->findOrEmpty();
  70. $userAvatar = $request->user['avatar'];
  71. // 获取最新的10条聊天记录
  72. $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id'],'is_user' => 0])->append(['avatar','time'])->withAttr(['avatar' => function($data,$resp) use($userAvatar){
  73. if ($resp['source'] == 1) { // 用户
  74. return $userAvatar??'';
  75. } else {
  76. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  77. }
  78. },'time' => function($resp,$data){
  79. return strtotime($data['create_at']) * 1000;
  80. }])->order("id","desc")->paginate([
  81. "list_rows" => 10,
  82. "page" => 1
  83. ]);
  84. // 获取在线客服
  85. $service = (new SystemUser)->where("store_id",$param['poi'])->where("type",3)->where("is_line",1)->findOrEmpty();
  86. if ($service->isEmpty()) return $this->encode("ok",['store' => $store,'code' => 3,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => 0]); // 无客服在线
  87. if ($chatStore->isEmpty()) {
  88. $chatStore->insertGetId([
  89. "poi_id" => $store['poi_id'],
  90. "poi_name" => $store['poi_name'],
  91. "openid" => $request->user['openid'],
  92. "nickname" => $request->user['nickname'],
  93. "service_id" => $service['id'],
  94. "order" => $param['order']??'',
  95. "goods" => $param['goods']??'',
  96. "last_at" => getDateFull()
  97. ]);
  98. } else {
  99. $chatStore->service_id = $service['id'];
  100. $chatStore->order = $param['order']??'';
  101. $chatStore->nickname = $request->user['nickname'];
  102. $chatStore->goods = $param['goods']??'';
  103. $chatStore->last_at = getDateFull();
  104. $chatStore->save();
  105. }
  106. return $this->encode("ok",['store' => $store,'code' => 1,'sendId' => $request->user['openid'],'msg' => pageFormatMsg($msg),"serviceId" => $service['id'],'avatar' => $userAvatar]); // 客服在线
  107. } catch (\Throwable $throwable) {
  108. echo $throwable->getMessage()."\n";
  109. echo $throwable->getFile()."\n";
  110. echo $throwable->getLine()."\n";
  111. return error($throwable->getMessage());
  112. }
  113. }
  114. #[GetMapping("msg")]
  115. public function getMessageList(Request $request): Response
  116. {
  117. try {
  118. $param = $this->_valid([
  119. "poi.require" => trans("empty.require"),
  120. "page.default" => 1,
  121. "size.default" => 10
  122. ],$request->method());
  123. if (!is_array($param)) return error($param);
  124. $userAvatar = $request->user['avatar'];
  125. $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $param['poi'],'is_user' => 0])->append(['avatar','time'])->withAttr(['avatar' => function($data,$resp) use($userAvatar){
  126. if ($resp['source'] == 1) {
  127. return $userAvatar??'';
  128. } else {
  129. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  130. }
  131. },'time' => function($resp,$data){
  132. return strtotime($data['create_at']) * 1000;
  133. }])->order("id","desc")->paginate([
  134. "list_rows" => $param['size']??10,
  135. "page" => $param['page']??1,
  136. ]);
  137. return $this->encode("ok",pageFormat($msg));
  138. } catch (\Throwable $throwable) {
  139. return error($throwable->getMessage());
  140. }
  141. }
  142. /**
  143. * 发送消息
  144. * @param Request $request
  145. * @return Response
  146. */
  147. #[PostMapping("send")]
  148. public function sendMsg(Request $request): Response
  149. {
  150. try {
  151. $param = $this->_valid([
  152. "content.require" => trans("empty.require"),
  153. "groupId.require" => trans("empty.require"),
  154. "type.require" => trans("empty.require"),
  155. "sendId.require" => trans("empty.require"),
  156. ],"post");
  157. if (!is_array($param)) return error($param);
  158. (new SaasChatMsg)->insertGetId([
  159. "source" => 1,
  160. "openid" => $request->user['openid'],
  161. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  162. "type" => $param['type'],
  163. "msgId" => time(),
  164. "poi_id" => $param['groupId'],
  165. "service_id" => $param['sendId'],
  166. ]);
  167. $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => $param['sendId']])->with(['user' => function($query){
  168. $query->field("openid,nickname,avatar,mobile,create_at");
  169. }])->append(['last','avatar'])->withAttr(['last' => function($data,$resp) use($param){
  170. $last = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['sendId'],"openid" => $resp['openid']])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
  171. if ($last->isEmpty()) {
  172. return ['type' => "text","content"=>"无",'num' => 0,'time' => time(),"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  173. }
  174. $total = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['sendId'],"openid" => $resp['openid'],'is_read' => 0])->count();
  175. return ["content"=> str_cut_ellipsis($last['content']),'num' => ($total),"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
  176. },'avatar' => function(){
  177. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  178. }])->findOrEmpty();
  179. if (!$chatStore->isEmpty())
  180. {
  181. $chatStore->save(['last_at' => getDateFull()]);
  182. }
  183. $chatStore['last_at'] = getDateFull();
  184. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  185. $api->trigger("service-{$param['sendId']}","message",[
  186. "type" => $param['type'],
  187. "time" => time() * 1000,
  188. "msgId" => time(),
  189. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  190. "source" => 1,
  191. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  192. "poi_id" => $param['groupId'],
  193. "openid" => $request->user['openid'],
  194. "service_id" => $param['sendId'],
  195. "create_at" => formatTime(time()),
  196. "last_at" => getDateFull(),
  197. "user" => [
  198. "nickname" => $request->user['nickname'],
  199. "avatar" => $request->user['avatar']
  200. ],
  201. "item" => $chatStore->toArray()
  202. ]);
  203. return successTrans("success.data");
  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/img")]
  215. public function sendImgMsg(Request $request): Response
  216. {
  217. try {
  218. $resp = UploadExtend::uploadFile();
  219. if (!isset($resp[0]['url'])) return error("发送失败");
  220. $param = $this->_valid([
  221. "groupId.require" => trans("empty.require"),
  222. "type.require" => trans("empty.require"),
  223. "sendId.require" => trans("empty.require"),
  224. ],"post");
  225. if (!is_array($param)) return error($param);
  226. $param['content'] = $resp[0]['url'];
  227. $state = (new SaasChatMsg)->insertGetId([
  228. "source" => 1,
  229. "openid" => $request->user['openid'],
  230. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  231. "type" => $param['type'],
  232. "msgId" => time(),
  233. "poi_id" => $param['groupId'],
  234. "service_id" => $param['sendId'],
  235. ]);
  236. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  237. $api->trigger("service-{$param['sendId']}","message",[
  238. "type" => $param['type'],
  239. "time" => time() * 1000,
  240. "msgId" => time(),
  241. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  242. "source" => 1,
  243. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  244. "poi_id" => $param['groupId'],
  245. "openid" => $request->user['openid'],
  246. "service_id" => $param['sendId'],
  247. "create_at" => formatTime(time()),
  248. "user" => [
  249. "nickname" => $request->user['nickname']
  250. ]
  251. ]);
  252. return successTrans("success.data",['path' => $param['content']],200);
  253. } catch (\Throwable $throwable) {
  254. echo $throwable->getMessage()."\n";
  255. return error($throwable->getMessage());
  256. }
  257. }
  258. /**
  259. * 收集完善用户收货地址
  260. * @param Request $request
  261. * @return Response
  262. */
  263. #[PostMapping("send/address")]
  264. public function setOrderAddress(Request $request): Response
  265. {
  266. try {
  267. $param = $this->_valid([
  268. "city.require" => trans("empty.require"),
  269. "mobile.require" => trans("empty.require"),
  270. "mobile.mobile" => trans("error.mobile"),
  271. "address.require" => trans("empty.require"),
  272. "nickname.require" => trans("empty.require"),
  273. "order.require" => trans("empty.require"),
  274. ],$request->method());
  275. if (!is_array($param)) return error($param);
  276. echo getDateFull()."===地址信息\n";
  277. print_r($param);
  278. $cityJson = json_decode($param['city'],true);
  279. $cityCode = [];
  280. $city_text = "";
  281. foreach ($cityJson as $k => $v){
  282. $cityCode[$k] = $v['value'];
  283. $city_text .= $v['text'];
  284. }
  285. $param['city_text'] = $city_text;
  286. $param['city_code'] = json_encode($cityCode);
  287. $orderData = json_decode($param['order'],true);
  288. $order = (new SaasOrder)->where("out_order_no",$orderData["order"])->with(['poi'])->findOrEmpty();
  289. if ($order->isEmpty()) return error("关联订单错误");
  290. if ($order['openid'] <> $request->user['openid']) return error("关联订单错误");
  291. if ($order['status'] <> 1) return error("关联订单错误");
  292. $startTime = $order['poi']['order_start']??'09:00';
  293. $endTime = $order['poi']['order_end']??'22:30';
  294. $startTimeStamp = strtotime("Y-m-d {$startTime}:00");
  295. $endTimeStamp = strtotime("Y-m-d {$endTime}:00");
  296. $address = (new SaasOrderAddress)->where(['openid' => $request->user['openid'],'order_sn' => $orderData['order']])->findOrEmpty();
  297. if (!$address->isEmpty()) return error("请勿重复提交");
  298. $address->insertGetId([
  299. "openid" => $request->user['openid'],
  300. "order_sn" => $orderData['order'],
  301. "city_code" => $param['city_code'],
  302. "city_text" => $param['city_text'],
  303. "mobile" => $param['mobile'],
  304. "address" => $param['address'],
  305. "nickname" => $param['nickname'],
  306. ]);
  307. $order->status = 2;
  308. $order->express_status = 1;
  309. $order->save();
  310. // 判断是否在核销范围
  311. // if (time() > $startTimeStamp && time() < $endTimeStamp) { // 自动核销,订单状态改成待发货
  312. // Redis::send("order-done",['order' => $orderData['order']]);
  313. // } else { // 写入待核销定时任务队列中
  314. // (new SaasOrderDelay)->insertGetId([
  315. // "order_sn" => $orderData['order'],
  316. // ]);
  317. // }
  318. return success("提交成功");
  319. } catch (\Throwable $throwable) {
  320. echo $throwable->getMessage()."\n";
  321. return error($throwable->getMessage());
  322. }
  323. }
  324. }