SwooleTaskService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-15
  7. *
  8. *
  9. */
  10. namespace crmeb\services;
  11. use Swoole\Server;
  12. use think\facade\Log;
  13. /**
  14. * Class SwooleTaskService
  15. * @package crmeb\services
  16. * @author xaboy
  17. * @day 2020-04-15
  18. */
  19. class SwooleTaskService
  20. {
  21. /**
  22. * @var string
  23. */
  24. protected $type;
  25. /**
  26. * @var array
  27. */
  28. protected $data;
  29. /**
  30. * @var Server
  31. */
  32. protected $server;
  33. /**
  34. * @var
  35. */
  36. protected $callback;
  37. /**
  38. * SwooleTaskService constructor.
  39. * @param string $type
  40. */
  41. public function __construct(string $type)
  42. {
  43. $this->type = $type;
  44. // 只有在Swoole扩展已加载且swoole.server服务存在时才获取server实例
  45. if (extension_loaded('swoole') && app()->has('swoole.server')) {
  46. $this->server = app('swoole.server');
  47. } else {
  48. $this->server = null;
  49. }
  50. }
  51. /**
  52. * @param array $data
  53. * @return $this
  54. * @author xaboy
  55. * @day 2020-04-15
  56. */
  57. public function setData(array $data)
  58. {
  59. $this->data = $data;
  60. return $this;
  61. }
  62. /**
  63. * @param int $workId
  64. * @return mixed
  65. * @author xaboy
  66. * @day 2020-04-15
  67. */
  68. public function push(int $workId = -1)
  69. {
  70. try {
  71. // 只有在有Swoole服务器实例时才发送任务
  72. if ($this->server instanceof \Swoole\Server) {
  73. return $this->server->task(['type' => $this->type, 'data' => $this->data], $workId);
  74. } else {
  75. // 非Swoole环境下记录日志但不抛出异常
  76. Log::info('Swoole服务器不可用,任务未发送: ' . $this->type);
  77. return false;
  78. }
  79. } catch (\Exception $e) {
  80. Log::info('发送 Task 失败' . $e->getMessage());
  81. }
  82. }
  83. /**
  84. * @param string $type
  85. * @return static
  86. * @author xaboy
  87. * @day 2020/5/25
  88. */
  89. public static function create(string $type)
  90. {
  91. return new static($type);
  92. }
  93. public static function user($uid, $data)
  94. {
  95. $type = 'user';
  96. return self::create('message')->setData(compact('type', 'uid', 'data'))->push();
  97. }
  98. public static function admin(string $msgType, array $msgData, $uid = [])
  99. {
  100. return self::create('admin')->setData([
  101. 'uid' => $uid,
  102. 'data' => [
  103. 'type' => $msgType,
  104. 'data' => $msgData
  105. ]
  106. ])->push();
  107. }
  108. public static function merchant(string $msgType, array $msgData, int $merId, $uid = [])
  109. {
  110. return self::create('merchant')->setData([
  111. 'uid' => $uid,
  112. 'mer_id' => $merId,
  113. 'data' => [
  114. 'type' => $msgType,
  115. 'data' => $msgData
  116. ]
  117. ])->push();
  118. }
  119. /**
  120. * @param $merId
  121. * @param array $result
  122. * @return mixed
  123. * @author xaboy
  124. * @day 2020/5/25
  125. */
  126. public static function log($merId, array $result)
  127. {
  128. return self::create('log')->setData(compact('merId', 'result'))->push();
  129. }
  130. /**
  131. * @param int $uid
  132. * @param int $type_id
  133. * @param string $type
  134. * @param string $content
  135. * @return mixed
  136. * @author xaboy
  137. * @day 2020/6/25
  138. */
  139. public static function visit(int $uid, int $type_id, string $type, string $content = '')
  140. {
  141. return self::create('visit')->setData(compact('uid', 'type', 'type_id', 'content'))->push();
  142. }
  143. public static function __callStatic($name, $arguments)
  144. {
  145. return self::create($name)->setData($arguments[0])->push();
  146. }
  147. }