| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://doc.hyperf.io
- * @contact group@hyperf.io
- * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
- */
- namespace App\Jobs;
- use App\Model\VideoModel;
- use App\Repositories\Service\CommonService;
- use Hyperf\AsyncQueue\Job;
- use Modules\Api\Services\VideoService;
- use Psr\SimpleCache\CacheInterface;
- class AddHomeJob extends Job
- {
- public $params;
- public function __construct($params)
- {
- // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
- $this->params = $params;
- }
- public function handle()
- {
- $video_ids = $this->params['video_ids'];
- try {
- $cache = make(CacheInterface::class);
- $video_ads = make(VideoService::class)->getAdList('video_home');
- for ($i=1; $i<= 20; $i++) {
- if ($cache->has('video:home:' . $i)) {
- $cache->delete('video:home:' . $i);
- }
- $cache->rememberForever(
- 'video:home:' . $i,
- function () use ($video_ads, $i, $video_ids) {
- //$start= ($i - 1) * 5;
- //$video_ids = array_slice($video_ids, $start, 5);
- $video_data = [];
- $result = VideoModel::query()
- ->select(['id','title', 'cover', 'smu', 'like', 'uid', 'from'])
- ->with(['category:type.id as type_id,type.title','user'])
- ->where('status', 1)
- ->whereIn('id', $video_ids)
- ->inRandomOrder()
- ->get()->toArray();
- foreach ($result as $k => $v) {
- // $result[$k]['smu'] = make(VideoService::class)
- // ->getPlayUrl($result[$k]['from'], $result[$k]['smu']);
- // $result[$k]['user']['avatar'] = make(CommonService::class)
- // ->getImageUrl($result[$k]['user']['avatar']);
- foreach ($result[$k]['category'] as $ck => $t) {
- unset($result[$k]['category'][$ck]['pivot']);
- }
- }
- foreach ($result as $k => $v) {
- $video_data[$k] = [
- 'type' => 'v',
- 'video' => $v,
- ];
- }
- if ($video_ads) {
- $video_ad = collect($video_ads)
- ->random(1)
- ->first();
- $video_data[count($video_data)] = [
- 'type' => 'a',
- 'ad' => $video_ad,
- ];
- }
- unset($result);
- return $video_data;
- }
- );
- }
- } catch (\Exception $exception) {
- logger('AddHomeJob', [$exception->getMessage()]);
- }
- }
- }
|