Task.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils;
  4. use think\facade\Db;
  5. class Task
  6. {
  7. /**
  8. * 发送任务
  9. * @param $uid
  10. * @param $type
  11. * @param $param
  12. */
  13. public static function send($uid, $type, $param)
  14. {
  15. $updatId = Db::name("task")->insertGetId([
  16. 'uid' => $uid,
  17. 'type' => $type,
  18. 'data' => json_encode($param),
  19. 'time' => time(),
  20. ]);
  21. //执行任务
  22. self::Task($type, ['taskId' => $updatId]);
  23. }
  24. /**
  25. * 执行任务数据
  26. * @param $type 任务类型
  27. * @param array $data 任务code
  28. * @return bool 任务数据
  29. */
  30. public static function Task($type, array $data = []) {
  31. $data['type'] = $type;
  32. $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
  33. $ret = @$client->connect('127.0.0.1', 9506);
  34. if (!$ret)
  35. return false;
  36. $client->send(serialize($data) . "\r\n");
  37. return true;
  38. try {
  39. }catch (\think\Exception $e) {
  40. return false;
  41. }
  42. }
  43. }