SwooleTaskListen.php 5.7 KB

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