You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.5 KiB

10 months ago
<?php
namespace App\Console\Commands;
3 months ago
use App\Models\BirthdayMessage;
5 months ago
use App\Models\Config;
3 months ago
use App\Models\EmailRecordUser;
10 months ago
use App\Models\User;
use App\Repositories\MeetRepository;
use Illuminate\Console\Command;
class CheckBirthday extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check_birthday';
/**
* 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()
{
4 days ago
// 仅完整生日YYYY-MM-DD参与提醒避免 YYYY-MM 被自动补成 01 误判
5 months ago
$users = User::where('is_schoolmate', 1)
4 days ago
->whereNotNull('birthday')
5 months ago
->get();
4 days ago
$users = $users->filter(function ($user) {
return User::isBirthdayToday($user->birthday);
})->values();
5 months ago
$birthdayCount = $users->count();
// 发送通知给用户
3 months ago
$smsSign = Config::getValueByKey('sms_sign') ?: '';
3 months ago
$userSmsSuccessCount = 0;
$userSmsFailCount = 0;
$userEmailSuccessCount = 0;
$userEmailFailCount = 0;
3 months ago
10 months ago
foreach ($users as $user) {
3 months ago
$username = $user->username ?: '校友';
$hasMobile = !empty($user->mobile);
$hasEmail = !empty($user->email);
// 发送短信
if ($hasMobile) {
// 获取随机短信模板
$smsMessage = BirthdayMessage::getRandomSmsMessage();
if ($smsMessage) {
// 替换用户名占位符
$smsContent = str_replace('{username}', $username, $smsMessage);
// 添加短信签名
$smsContent = $smsSign . $smsContent;
// 直接发送短信
$result = ymSms($user->mobile, $smsContent);
if ($result) {
$userSmsSuccessCount++;
$this->info("已向用户 {$username}({$user->mobile}) 发送生日祝福短信");
} else {
$userSmsFailCount++;
$this->error("向用户 {$username}({$user->mobile}) 发送短信失败");
}
}
3 months ago
}
3 months ago
// 发送邮件
if ($hasEmail) {
// 获取随机邮件模板
$emailTemplate = BirthdayMessage::getRandomEmailMessage();
if ($emailTemplate) {
try {
// 准备变量数据
$varData = [
'username' => $username,
];
// 使用模板方法替换邮件标题和内容中的变量
$emailSubject = EmailRecordUser::template($emailTemplate['subject'], $varData);
$emailContent = EmailRecordUser::template($emailTemplate['content'], $varData);
// 发送邮件
EmailRecordUser::email($emailSubject, $emailContent, $user->email);
$userEmailSuccessCount++;
$this->info("已向用户 {$username}({$user->email}) 发送生日祝福邮件");
} catch (\Exception $e) {
$userEmailFailCount++;
$this->error("向用户 {$username}({$user->email}) 发送邮件失败: " . $e->getMessage());
}
3 months ago
} else {
3 months ago
$this->warn("未找到可用的邮件模板,跳过向用户 {$username}({$user->email}) 发送邮件");
3 months ago
}
}
}
3 months ago
// 输出短信发送统计
if ($userSmsSuccessCount > 0) {
$this->info("共向 {$userSmsSuccessCount} 位用户发送生日祝福短信成功");
}
if ($userSmsFailCount > 0) {
$this->error("共 {$userSmsFailCount} 位用户短信发送失败");
}
// 输出邮件发送统计
if ($userEmailSuccessCount > 0) {
$this->info("共向 {$userEmailSuccessCount} 位用户发送生日祝福邮件成功");
3 months ago
}
3 months ago
if ($userEmailFailCount > 0) {
$this->error("共 {$userEmailFailCount} 位用户邮件发送失败");
10 months ago
}
5 months ago
// 如果有生日用户,给管理员发送短信
if ($birthdayCount > 0) {
$adminMobiles = Config::getValueByKey('birthday_notice');
if ($adminMobiles) {
// 收集生日用户名字
$userNames = $users->pluck('username')->filter()->toArray();
// 构建短信内容(统一模板,显示所有名字)
$namesStr = implode('、', $userNames);
$content = "{$smsSign}今日有{$birthdayCount}位校友生日:{$namesStr},请及时关注。";
// 分割手机号(支持英文逗号分隔)
$mobileList = array_map('trim', explode(',', $adminMobiles));
$mobileList = array_filter($mobileList); // 过滤空值
$successCount = 0;
$failCount = 0;
foreach ($mobileList as $mobile) {
if (empty($mobile)) {
continue;
}
$result = ymSms($mobile, $content);
if ($result) {
$this->info("已向管理员 {$mobile} 发送生日提醒短信");
$successCount++;
} else {
$this->error("向管理员 {$mobile} 发送短信失败");
$failCount++;
}
}
if ($successCount > 0) {
$this->info("共向 {$successCount} 位管理员发送短信成功");
}
if ($failCount > 0) {
$this->error("共 {$failCount} 位管理员短信发送失败");
}
} else {
$this->warn("未配置 birthday_notice 管理员手机号,跳过短信发送");
}
} else {
$this->info("今日无校友生日");
}
return $this->info("检测完成,共发现 {$birthdayCount} 位校友生日");
10 months ago
}
}