SwooleTaskService.php 3.6 KB

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