Service.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\saas\SaasChatMsg;
  6. use app\model\saas\SaasChatStore;
  7. use app\model\saas\SaasStore;
  8. use app\model\system\SystemUser;
  9. use app\service\saas\ChatStoreService;
  10. use DI\Attribute\Inject;
  11. use LinFly\Annotation\Attributes\Route\Controller;
  12. use LinFly\Annotation\Attributes\Route\GetMapping;
  13. use LinFly\Annotation\Attributes\Route\Middleware;
  14. use LinFly\Annotation\Attributes\Route\PostMapping;
  15. use support\Request;
  16. use support\Response;
  17. #[Controller("/dy/service"),Middleware(AuthMiddleware::class)]
  18. class Service extends Base
  19. {
  20. #[Inject]
  21. protected ChatStoreService $service;
  22. /**
  23. * 获取客服列表-已聊过的
  24. * @param Request $request
  25. * @return Response
  26. */
  27. #[GetMapping("list")]
  28. public function getServiceList(Request $request): Response
  29. {
  30. try {
  31. $param = $request->all();
  32. if (!empty($param['size'])) {
  33. $param['pageSize'] = $param['size'];
  34. }
  35. $param['openid'] = $request->user['openid'];
  36. $list = $this->service->setModel()->getList($param,null,true,['last','avatar'],['last' => function($data,$resp) use($param){
  37. $last = (new SaasChatMsg)->where("poi_id",$resp['poi_id'])->where("openid",$param['openid'])->order("create_at desc")->field("content,create_at")->findOrEmpty();
  38. if ($last->isEmpty()) {
  39. return ["content"=>"无",'num' => 0,"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  40. }
  41. return ["content"=> $last['content'],'num' => 0,"create_at"=> formatTime($last['create_at'])];
  42. },'avatar' => function(){
  43. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  44. }]);
  45. return successTrans("success.data",pageFormat($list));
  46. } catch (\Throwable $throwable) {
  47. return error($throwable->getMessage());
  48. }
  49. }
  50. /**
  51. * 分配客服
  52. * @param Request $request
  53. * @return Response
  54. */
  55. #[PostMapping("shareout")]
  56. public function shareUser2Mer(Request $request): Response
  57. {
  58. try {
  59. $param = $request->all();
  60. $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
  61. if ($store->isEmpty()) return error("店铺不存在");
  62. $chatStore = (new SaasChatStore)->where(['poi_id'=> $store['poi_id'],"openid" => $request->user['openid']])->findOrEmpty();
  63. if ($chatStore->isEmpty()) {
  64. $chatStore->insertGetId([
  65. "poi_id" => $store['poi_id'],
  66. "poi_name" => $store['poi_name'],
  67. "openid" => $request->user['openid']
  68. ]);
  69. }
  70. // 获取在线客服
  71. $service = (new SystemUser)->where("store_id",$param['poi'])->where("type",3)->where("is_line",1)->findOrEmpty();
  72. if ($service->isEmpty()) return $this->encode("ok",['store' => $store,'code' => 3]); // 无客服在线
  73. return $this->encode("ok",['store' => $store,'code' => 1]); // 客服在线
  74. } catch (\Throwable $throwable) {
  75. return error($throwable->getMessage());
  76. }
  77. }
  78. }