master
cody 3 weeks ago
parent dd18a91847
commit 91748327fb

@ -2,6 +2,7 @@
namespace App\Console\Commands;
use App\Models\Config;
use App\Models\User;
use App\Notifications\AuditNotify;
use App\Notifications\BirthdayNotify;
@ -44,12 +45,69 @@ class CheckBirthday extends Command
*/
public function handle()
{
$day = date('Y-m');
$users = User::where('is_schoolmate', 1)->where('birthday', 'like', '%' . $day . '%')->get();
// 匹配今天生日格式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();
// 发送通知给用户
foreach ($users as $user) {
Notification::send($user, new BirthdayNotify(['user_id' => $user->id]));
}
return $this->info('更新完成');
// 如果有生日用户,给管理员发送短信
if ($birthdayCount > 0) {
$adminMobiles = Config::getValueByKey('birthday_notice');
if ($adminMobiles) {
$smsSign = Config::getValueByKey('sms_sign') ?: '';
// 收集生日用户名字
$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} 位校友生日");
}

Loading…
Cancel
Save