SportOdds.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sport as SportModel;
  5. class SportOdds extends Command
  6. {
  7. /**
  8. * 命令名称和签名
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'sport:odds {is_roll=0}';
  13. protected $is_roll = 0;
  14. /**
  15. * 命令描述
  16. *
  17. * @var string
  18. */
  19. protected $description = '赔率(滚球5秒更新一次,普通的3小时更新一次)';
  20. /**
  21. * 执行命令
  22. *
  23. * @return int
  24. */
  25. public function handle()
  26. {
  27. $this->is_roll = $this->argument('is_roll');
  28. if ($this->is_roll == 1) {
  29. $this->info('滚球开始执行赔率任务...');
  30. $this->sportOddsData($this->is_roll);
  31. } else {
  32. $this->info('普通开始执行赔率任务...');
  33. $this->sportOddsData($this->is_roll);
  34. }
  35. $this->info('结束执行赔率任务');
  36. }
  37. public function sportOddsData($is_roll)
  38. {
  39. try {
  40. $host = env('SPORT_HOST');
  41. $where['status'] = 1;
  42. $where['is_roll'] = $is_roll;
  43. //滚球(滚球是开赛后随时可以买,更新数据)
  44. if ($is_roll == 1) {
  45. $where['state'] = 1; //比赛状态:0未开始1进行中2已完场3延期4取消
  46. $url = "{$host}/api/sport/oddsLive";
  47. } else {
  48. //普通球赛是开赛前购买,更新数据
  49. $where['state'] = 0;
  50. $url = "{$host}/api/sport/odds";
  51. }
  52. $list = SportModel::where($where)->get()->toArray();
  53. foreach ($list as $item) {
  54. $url .= "?data_id={$item['data_id']}";
  55. $result = file_get_contents($url);
  56. $result = $result ? json_decode($result, true) : [];
  57. }
  58. } catch (\Exception $e) {
  59. $this->error($e->getMessage());
  60. }
  61. return true;
  62. }
  63. }