love_php/app/Console/Commands/SendActivitySmsBeforeBeginning.php

103 lines
3.3 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\ActivityMember;
use App\Models\Activity;
use App\Models\Message;
use App\Jobs\SendSmsBatch;
class SendActivitySmsBeforeBeginning extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:send_sms_to_activity_members';
/**
* The console command description.
*
* @var string
*/
protected $description = '活动开始前15分钟 发送短信通知';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
//每1分钟调一次接口
public function handle()
{
//获取活动开始前15分钟的活动
$now_date = date('Y-m-d H:i:s');
$send_time = date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($now_date)));
$activities = Activity::whereBetween('start_time',[$now_date,$send_time])->get();
if (!empty($activities)){//要发送通知的活动
foreach ($activities as $activity) {
$message = '【福恋】恭喜您,您参加的“'.$activity->theme.'”活动报名成功,请于'.$activity->start_time.'准时参与。如有问题请联系18922809346退订回N';
$members = ActivityMember::where('activity_id',$activity->id)->where('is_joined',1)->get();
$mobile = [];
foreach ($members as $member) {
$result = $this->isSendSms($activity,$member);
if ($result) { //没有发送过 就发
$mobile[] = $result;
}
}
if(!empty($mobile) && is_array($mobile)){
//批量发送
$array = [
'mobile' => $mobile,
'message' => $message
];
//添加记录
$this->addSmsRecord($mobile,$activity);
SendSmsBatch::dispatch($array)->onQueue('love');
}
}
}
}
//判断是否发送过该短信
public function isSendSms($activity,$member){
$code = '活动短信通知'.$activity->id;
$is_send = Message::where('phone',$member->mobile)->where('code',$code)->count();
if ($is_send) {
return false;
}else{
return $member->mobile;
}
}
//增加短信记录
public function addSmsRecord($mobile,$activity){
$code = '活动短信通知'.$activity->id;
$message = '【福恋】恭喜您,您参加的“'.$activity->theme.'”活动报名成功,请于'.$activity->start_time.'准时参与。如有问题请联系4000401707退订回N';
if(!empty($mobile) && is_array($mobile)){
foreach($mobile as $mob){
Message::create([
'phone'=>$mob,
'message'=>$message,
'code'=>$code,
'confirmed'=>1,
'ip' => request() ? request()->ip() : '127.0.0.1',
'is_click'=>0,
]);
}
}
}
}