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.

187 lines
6.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use App\Models\BirthdayMessage;
use App\Models\Config;
use App\Models\EmailRecordUser;
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()
{
// 匹配今天生日格式YYYY-MM-DD 或 MM-DD
$today = date('m-d');
$users = User::where('is_schoolmate', 1)
->where(function ($query) use ($today) {
$query->where('birthday', 'like', '%-' . $today)
->orWhere('birthday', 'like', $today . '%');
})
->get();
$birthdayCount = $users->count();
// 发送通知给用户
$smsSign = Config::getValueByKey('sms_sign') ?: '';
$userSmsSuccessCount = 0;
$userSmsFailCount = 0;
$userEmailSuccessCount = 0;
$userEmailFailCount = 0;
foreach ($users as $user) {
$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}) 发送短信失败");
}
}
}
// 发送邮件
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());
}
} else {
$this->warn("未找到可用的邮件模板,跳过向用户 {$username}({$user->email}) 发送邮件");
}
}
}
// 输出短信发送统计
if ($userSmsSuccessCount > 0) {
$this->info("共向 {$userSmsSuccessCount} 位用户发送生日祝福短信成功");
}
if ($userSmsFailCount > 0) {
$this->error("{$userSmsFailCount} 位用户短信发送失败");
}
// 输出邮件发送统计
if ($userEmailSuccessCount > 0) {
$this->info("共向 {$userEmailSuccessCount} 位用户发送生日祝福邮件成功");
}
if ($userEmailFailCount > 0) {
$this->error("{$userEmailFailCount} 位用户邮件发送失败");
}
// 如果有生日用户,给管理员发送短信
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} 位校友生日");
}
}