AddHomeJob.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://doc.hyperf.io
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Jobs;
  12. use App\Model\VideoModel;
  13. use App\Repositories\Service\CommonService;
  14. use Hyperf\AsyncQueue\Job;
  15. use Modules\Api\Services\VideoService;
  16. use Psr\SimpleCache\CacheInterface;
  17. class AddHomeJob extends Job
  18. {
  19. public $params;
  20. public function __construct($params)
  21. {
  22. // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
  23. $this->params = $params;
  24. }
  25. public function handle()
  26. {
  27. $video_ids = $this->params['video_ids'];
  28. try {
  29. $cache = make(CacheInterface::class);
  30. $video_ads = make(VideoService::class)->getAdList('video_home');
  31. for ($i=1; $i<= 20; $i++) {
  32. if ($cache->has('video:home:' . $i)) {
  33. $cache->delete('video:home:' . $i);
  34. }
  35. $cache->rememberForever(
  36. 'video:home:' . $i,
  37. function () use ($video_ads, $i, $video_ids) {
  38. //$start= ($i - 1) * 5;
  39. //$video_ids = array_slice($video_ids, $start, 5);
  40. $video_data = [];
  41. $result = VideoModel::query()
  42. ->select(['id','title', 'cover', 'smu', 'like', 'uid', 'from'])
  43. ->with(['category:type.id as type_id,type.title','user'])
  44. ->where('status', 1)
  45. ->whereIn('id', $video_ids)
  46. ->inRandomOrder()
  47. ->get()->toArray();
  48. foreach ($result as $k => $v) {
  49. // $result[$k]['smu'] = make(VideoService::class)
  50. // ->getPlayUrl($result[$k]['from'], $result[$k]['smu']);
  51. // $result[$k]['user']['avatar'] = make(CommonService::class)
  52. // ->getImageUrl($result[$k]['user']['avatar']);
  53. foreach ($result[$k]['category'] as $ck => $t) {
  54. unset($result[$k]['category'][$ck]['pivot']);
  55. }
  56. }
  57. foreach ($result as $k => $v) {
  58. $video_data[$k] = [
  59. 'type' => 'v',
  60. 'video' => $v,
  61. ];
  62. }
  63. if ($video_ads) {
  64. $video_ad = collect($video_ads)
  65. ->random(1)
  66. ->first();
  67. $video_data[count($video_data)] = [
  68. 'type' => 'a',
  69. 'ad' => $video_ad,
  70. ];
  71. }
  72. unset($result);
  73. return $video_data;
  74. }
  75. );
  76. }
  77. } catch (\Exception $exception) {
  78. logger('AddHomeJob', [$exception->getMessage()]);
  79. }
  80. }
  81. }