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])); } // 如果有生日用户,给管理员发送短信 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} 位校友生日"); } }