love_php/app/Console/Commands/LiveRemind.php
2026-04-02 09:20:51 +08:00

76 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Live\Live;
use App\Models\Live\AnchorViewer;
use App\Jobs\SendTemplateMsg;
use App\Models\Live\Viewer;
use App\Models\Live\Anchor;
use Illuminate\Support\Facades\Redis;
class LiveRemind extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'liveRemind';
/**
* The console command description.
*
* @var string
*/
protected $description = '开播前一个小时通知已经关注的用户';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$Live = Live::where('status',0)->where('is_show',1)->get();
foreach ($Live as $key => $value) {
$time = strtotime($value->start_time);
$Remaining_time = $time-(time()+60*60);
if($Remaining_time<=0){
$key = 'liveRemind'.$value->id;
if(Redis::get($key)){
continue;
}else{
Redis::setex($key,100*60,1);
$teacher = Anchor::where('id', $value->anchor_id)->value('name');
$followers = AnchorViewer::where('anchor_id', $value->anchor_id)->where('focusing', 1)->pluck('viewer_id')->toArray();
$data['touser'] = Viewer::whereIn('id', $followers)->pluck('openid')->toArray();
$data['template_id'] = 'BinirgWI3EAYs9jindxPBwUm-rAceTu9T4H02m-sq5g';
$data['url'] = env('APP_URL').'/api/official/live/wechat/oauth?live_id='.$value->id;
$data['data'] = [
'first' => '您关注的老师即将在1小时后开始直播课。',
'keyword1' => $value->start_time,
'keyword2' => $teacher,
'reamrk' => '点击详情,即可观看预告',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
}
}
# code...
}
}
}