SwooleTaskService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $this->server = app('swoole.server');
  45. }
  46. /**
  47. * @param array $data
  48. * @return $this
  49. * @author xaboy
  50. * @day 2020-04-15
  51. */
  52. public function setData(array $data)
  53. {
  54. $this->data = $data;
  55. return $this;
  56. }
  57. /**
  58. * @param int $workId
  59. * @return mixed
  60. * @author xaboy
  61. * @day 2020-04-15
  62. */
  63. public function push(int $workId = -1)
  64. {
  65. try {
  66. return $this->server->task(['type' => $this->type, 'data' => $this->data], $workId);
  67. } catch (\Exception $e) {
  68. Log::info('发送 Task 失败' . $e->getMessage());
  69. }
  70. }
  71. /**
  72. * @param string $type
  73. * @return static
  74. * @author xaboy
  75. * @day 2020/5/25
  76. */
  77. public static function create(string $type)
  78. {
  79. return new static($type);
  80. }
  81. public static function user($uid, $data)
  82. {
  83. $type = 'user';
  84. return self::create('message')->setData(compact('type', 'uid', 'data'))->push();
  85. }
  86. public static function admin(string $msgType, array $msgData, $uid = [])
  87. {
  88. return self::create('admin')->setData([
  89. 'uid' => $uid,
  90. 'data' => [
  91. 'type' => $msgType,
  92. 'data' => $msgData
  93. ]
  94. ])->push();
  95. }
  96. public static function merchant(string $msgType, array $msgData, int $merId, $uid = [])
  97. {
  98. return self::create('merchant')->setData([
  99. 'uid' => $uid,
  100. 'mer_id' => $merId,
  101. 'data' => [
  102. 'type' => $msgType,
  103. 'data' => $msgData
  104. ]
  105. ])->push();
  106. }
  107. /**
  108. * @param $merId
  109. * @param array $result
  110. * @return mixed
  111. * @author xaboy
  112. * @day 2020/5/25
  113. */
  114. public static function log($merId, array $result)
  115. {
  116. return self::create('log')->setData(compact('merId', 'result'))->push();
  117. }
  118. /**
  119. * @param int $uid
  120. * @param int $type_id
  121. * @param string $type
  122. * @param string $content
  123. * @return mixed
  124. * @author xaboy
  125. * @day 2020/6/25
  126. */
  127. public static function visit(int $uid, int $type_id, string $type, string $content = '')
  128. {
  129. return self::create('visit')->setData(compact('uid', 'type', 'type_id', 'content'))->push();
  130. }
  131. public static function __callStatic($name, $arguments)
  132. {
  133. return self::create($name)->setData($arguments[0])->push();
  134. }
  135. }