SwooleTaskService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace ln\services;
  3. use Swoole\Server;
  4. use think\facade\Log;
  5. /**
  6. * Class SwooleTaskService
  7. * @package ln\services
  8. * @author xaboy
  9. * @day 2020-04-15
  10. */
  11. class SwooleTaskService
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $type;
  17. /**
  18. * @var array
  19. */
  20. protected $data;
  21. /**
  22. * @var Server
  23. */
  24. protected $server;
  25. /**
  26. * @var
  27. */
  28. protected $callback;
  29. /**
  30. * SwooleTaskService constructor.
  31. * @param string $type
  32. */
  33. public function __construct(string $type)
  34. {
  35. $this->type = $type;
  36. $this->server = app('swoole.server');
  37. }
  38. /**
  39. * @param array $data
  40. * @return $this
  41. * @author xaboy
  42. * @day 2020-04-15
  43. */
  44. public function setData(array $data)
  45. {
  46. $this->data = $data;
  47. return $this;
  48. }
  49. /**
  50. * @param int $workId
  51. * @return mixed
  52. * @author xaboy
  53. * @day 2020-04-15
  54. */
  55. public function push(int $workId = -1)
  56. {
  57. try {
  58. return $this->server->task(['type' => $this->type, 'data' => $this->data], $workId);
  59. } catch (\Exception $e) {
  60. Log::info('发送 Task 失败' . $e->getMessage());
  61. }
  62. }
  63. /**
  64. * @param string $type
  65. * @return static
  66. * @author xaboy
  67. * @day 2020/5/25
  68. */
  69. public static function create(string $type)
  70. {
  71. return new static($type);
  72. }
  73. public static function user($uid, $data)
  74. {
  75. $type = 'user';
  76. return self::create('message')->setData(compact('type', 'uid', 'data'))->push();
  77. }
  78. public static function admin(string $msgType, array $msgData, $uid = [])
  79. {
  80. return self::create('admin')->setData([
  81. 'uid' => $uid,
  82. 'data' => [
  83. 'type' => $msgType,
  84. 'data' => $msgData
  85. ]
  86. ])->push();
  87. }
  88. public static function merchant(string $msgType, array $msgData, int $merId, $uid = [])
  89. {
  90. return self::create('merchant')->setData([
  91. 'uid' => $uid,
  92. 'mer_id' => $merId,
  93. 'data' => [
  94. 'type' => $msgType,
  95. 'data' => $msgData
  96. ]
  97. ])->push();
  98. }
  99. /**
  100. * @param $merId
  101. * @param array $result
  102. * @return mixed
  103. * @author xaboy
  104. * @day 2020/5/25
  105. */
  106. public static function log($merId, array $result)
  107. {
  108. return self::create('log')->setData(compact('merId', 'result'))->push();
  109. }
  110. /**
  111. * @param int $uid
  112. * @param int $type_id
  113. * @param string $type
  114. * @param string $content
  115. * @return mixed
  116. * @author xaboy
  117. * @day 2020/6/25
  118. */
  119. public static function visit(int $uid, int $type_id, string $type, string $content = '')
  120. {
  121. return self::create('visit')->setData(compact('uid', 'type', 'type_id', 'content'))->push();
  122. }
  123. public static function __callStatic($name, $arguments)
  124. {
  125. return self::create($name)->setData($arguments[0])->push();
  126. }
  127. }