123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace ln\services;
- use Swoole\Server;
- use think\facade\Log;
- /**
- * Class SwooleTaskService
- * @package ln\services
- * @author xaboy
- * @day 2020-04-15
- */
- class SwooleTaskService
- {
- /**
- * @var string
- */
- protected $type;
- /**
- * @var array
- */
- protected $data;
- /**
- * @var Server
- */
- protected $server;
- /**
- * @var
- */
- protected $callback;
- /**
- * SwooleTaskService constructor.
- * @param string $type
- */
- public function __construct(string $type)
- {
- $this->type = $type;
- $this->server = app('swoole.server');
- }
- /**
- * @param array $data
- * @return $this
- * @author xaboy
- * @day 2020-04-15
- */
- public function setData(array $data)
- {
- $this->data = $data;
- return $this;
- }
- /**
- * @param int $workId
- * @return mixed
- * @author xaboy
- * @day 2020-04-15
- */
- public function push(int $workId = -1)
- {
- try {
- return $this->server->task(['type' => $this->type, 'data' => $this->data], $workId);
- } catch (\Exception $e) {
- Log::info('发送 Task 失败' . $e->getMessage());
- }
- }
- /**
- * @param string $type
- * @return static
- * @author xaboy
- * @day 2020/5/25
- */
- public static function create(string $type)
- {
- return new static($type);
- }
- public static function user($uid, $data)
- {
- $type = 'user';
- return self::create('message')->setData(compact('type', 'uid', 'data'))->push();
- }
- public static function admin(string $msgType, array $msgData, $uid = [])
- {
- return self::create('admin')->setData([
- 'uid' => $uid,
- 'data' => [
- 'type' => $msgType,
- 'data' => $msgData
- ]
- ])->push();
- }
- public static function merchant(string $msgType, array $msgData, int $merId, $uid = [])
- {
- return self::create('merchant')->setData([
- 'uid' => $uid,
- 'mer_id' => $merId,
- 'data' => [
- 'type' => $msgType,
- 'data' => $msgData
- ]
- ])->push();
- }
- /**
- * @param $merId
- * @param array $result
- * @return mixed
- * @author xaboy
- * @day 2020/5/25
- */
- public static function log($merId, array $result)
- {
- return self::create('log')->setData(compact('merId', 'result'))->push();
- }
- /**
- * @param int $uid
- * @param int $type_id
- * @param string $type
- * @param string $content
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public static function visit(int $uid, int $type_id, string $type, string $content = '')
- {
- return self::create('visit')->setData(compact('uid', 'type', 'type_id', 'content'))->push();
- }
- public static function __callStatic($name, $arguments)
- {
- return self::create($name)->setData($arguments[0])->push();
- }
- }
|