SwooleTaskListen.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace ln\listens;
  3. use app\common\repositories\store\service\StoreServiceLogRepository;
  4. use app\common\repositories\system\admin\AdminLogRepository;
  5. use app\common\repositories\user\UserRepository;
  6. use app\common\repositories\user\UserVisitRepository;
  7. use app\webscoket\Manager;
  8. use ln\interfaces\ListenerInterface;
  9. use ln\jobs\SendNewsJob;
  10. use Swoole\Server;
  11. use Swoole\Server\Task;
  12. use think\facade\Queue;
  13. class SwooleTaskListen implements ListenerInterface
  14. {
  15. /**
  16. * @var Task
  17. */
  18. protected $task;
  19. public function handle($task): void
  20. {
  21. request()->clearCache();
  22. $this->task = $task;
  23. if (method_exists($this, $task->data['type']))
  24. $this->{$task->data['type']}($task->data['data']);
  25. }
  26. public function message(array $data)
  27. {
  28. $server = app()->make(Server::class);
  29. $storeServiceLogRepository = app()->make(StoreServiceLogRepository::class);
  30. $uid = is_array($data['uid']) ? $data['uid'] : [$data['uid']];
  31. $except = $data['except'] ?? [];
  32. if (!count($uid) && $data['type'] != 'user') {
  33. $fds = $data['type'] == 'mer' ? Manager::merFd($data['mer_id'] ?? 0) : Manager::userFd(0);
  34. foreach ($fds as $fd) {
  35. if (!in_array($fd, $except) && $server->isEstablished($fd) && $server->exist($fd))
  36. $server->push((int)$fd, json_encode($data['data']));
  37. }
  38. } else {
  39. foreach ($uid as $id) {
  40. $fds = Manager::userFd(array_search($data['type'], Manager::USER_TYPE), $id);
  41. foreach ($fds as $fd) {
  42. if (!in_array($fd, $except) && $server->isEstablished($fd) && $server->exist($fd) && ($data['from'] !== 'user' || $storeServiceLogRepository->getChat('fd_' . $fd, true) == $data['data']['data']['mer_id']))
  43. $server->push((int)$fd, json_encode($data['data']));
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * //TODO 用户给客服发送消息
  50. *
  51. * @param array $data
  52. * @author xaboy
  53. * @day 2020/6/15
  54. */
  55. public function chatToService(array $data)
  56. {
  57. $serviceLogRepository = app()->make(StoreServiceLogRepository::class);
  58. if ($serviceLogRepository->getChat($data['uid'] . $data['data']['mer_id'], true) == $data['data']['uid']) {
  59. $this->message([
  60. 'uid' => $data['uid'],
  61. 'type' => 'user',
  62. 'from' => 'user',
  63. 'data' => ['type' => 'chat', 'data' => $data['data']],
  64. 'except' => $data['except'] ?? []
  65. ]);
  66. $serviceLogRepository->serviceRead($data['data']['mer_id'], $data['data']['uid'], $data['data']['service_id']);
  67. } else {
  68. //TODO 客服消息提醒
  69. Queue::push(SendNewsJob::class, [
  70. $data['uid'],
  71. [
  72. 'title' => '收到用户【' . app()->make(UserRepository::class)->getUsername($data['data']['uid']) . '】的咨询消息,请及时查看',
  73. 'description' => $data['data'],
  74. 'url' => rtrim(systemConfig('site_url'), '/') . '/pages/chat/customer_list/chat?userId=' . $data['data']['uid'] . '&mer_id=' . $data['data']['mer_id'],
  75. 'image' => rtrim(systemConfig('site_url'), '/') . '/static/service_wechat_msg.jpg'
  76. ]
  77. ]);
  78. }
  79. }
  80. /**
  81. * //TODO 客服给用户发送消息
  82. * @param array $data
  83. * @author xaboy
  84. * @day 2020/6/15
  85. */
  86. public function chatToUser(array $data)
  87. {
  88. $serviceLogRepository = app()->make(StoreServiceLogRepository::class);
  89. if ($serviceLogRepository->getChat($data['uid']) == $data['data']['mer_id']) {
  90. $this->message([
  91. 'uid' => $data['uid'],
  92. 'type' => 'user',
  93. 'from' => 'service',
  94. 'data' => ['type' => 'chat', 'data' => $data['data']],
  95. 'except' => $data['except'] ?? []
  96. ]);
  97. $serviceLogRepository->userRead($data['data']['mer_id'], $data['data']['uid']);
  98. } else {
  99. //TODO 用户消息提醒
  100. Queue::push(SendNewsJob::class, [
  101. $data['uid'],
  102. [
  103. 'title' => '您收到新的消息,请及时查看',
  104. 'description' => $data['data'],
  105. 'url' => rtrim(systemConfig('site_url'), '/') . '/pages/chat/customer_list/chat?mer_id=' . $data['data']['mer_id'],
  106. 'image' => rtrim(systemConfig('site_url'), '/') . '/static/service_wechat_msg.jpg'
  107. ]
  108. ]);
  109. }
  110. }
  111. public function admin(array $data)
  112. {
  113. $this->message([
  114. 'uid' => $data['uid'] ?? [],
  115. 'type' => 'admin',
  116. 'data' => $data['data']
  117. ]
  118. );
  119. }
  120. public function merchant(array $data)
  121. {
  122. $this->message([
  123. 'uid' => $data['uid'] ?? [],
  124. 'mer_id' => $data['mer_id'],
  125. 'type' => 'mer',
  126. 'data' => $data['data']
  127. ]
  128. );
  129. }
  130. public function visit(array $data)
  131. {
  132. /** @var UserVisitRepository $make */
  133. $make = app()->make(UserVisitRepository::class);
  134. $make->create($data);
  135. }
  136. public function log(array $data)
  137. {
  138. app()->make(AdminLogRepository::class)->create($data['merId'], $data['result']);
  139. }
  140. }