diff --git a/app/Console/Commands/CheckBirthday.php b/app/Console/Commands/CheckBirthday.php index 265885b..0f8c4af 100755 --- a/app/Console/Commands/CheckBirthday.php +++ b/app/Console/Commands/CheckBirthday.php @@ -44,14 +44,13 @@ class CheckBirthday extends Command */ public function handle() { - // 匹配今天生日(格式:YYYY-MM-DD 或 MM-DD) - $today = date('m-d'); + // 仅完整生日(YYYY-MM-DD)参与提醒,避免 YYYY-MM 被自动补成 01 误判 $users = User::where('is_schoolmate', 1) - ->where(function ($query) use ($today) { - $query->where('birthday', 'like', '%-' . $today) - ->orWhere('birthday', 'like', $today . '%'); - }) + ->whereNotNull('birthday') ->get(); + $users = $users->filter(function ($user) { + return User::isBirthdayToday($user->birthday); + })->values(); $birthdayCount = $users->count(); diff --git a/app/Console/Commands/NormalizeUserBirthdayMonth.php b/app/Console/Commands/NormalizeUserBirthdayMonth.php new file mode 100644 index 0000000..9caa256 --- /dev/null +++ b/app/Console/Commands/NormalizeUserBirthdayMonth.php @@ -0,0 +1,78 @@ +option('dry-run'); + $query = User::query() + ->whereNotNull('birthday') + ->where('birthday', 'like', '____-__-01'); + + $total = (clone $query)->count(); + + if ($total === 0) { + $this->info('未找到需要修复的生日数据'); + return self::SUCCESS; + } + + $this->info(($dryRun ? '预览' : '开始修复') . " {$total} 条生日数据"); + + $updated = 0; + $previewed = 0; + + $query->orderBy('id')->chunkById(200, function ($users) use ($dryRun, &$updated, &$previewed) { + foreach ($users as $user) { + $normalizedBirthday = substr($user->birthday, 0, 7); + + if ($dryRun) { + if ($previewed < 20) { + $this->line("user_id={$user->id} {$user->birthday} => {$normalizedBirthday}"); + } + $previewed++; + continue; + } + + $user->birthday = $normalizedBirthday; + $user->save(); + $updated++; + } + }); + + if ($dryRun) { + if ($previewed > 20) { + $this->line('仅展示前 20 条预览结果'); + } + $this->info("预览完成,共 {$total} 条待修复"); + return self::SUCCESS; + } + + $this->info("修复完成,共更新 {$updated} 条生日数据"); + + return self::SUCCESS; + } +} diff --git a/app/Http/Controllers/Mobile/UserController.php b/app/Http/Controllers/Mobile/UserController.php index dbf3b0d..fd2fff0 100755 --- a/app/Http/Controllers/Mobile/UserController.php +++ b/app/Http/Controllers/Mobile/UserController.php @@ -279,7 +279,7 @@ class UserController extends CommonController })->where('id', $this->getUserId())->count(); // 是否生日 $is_birthday = 0; - if (isset($user->birthday) && date('m-d', strtotime($user->birthday)) == date('m-d')) { + if (User::isBirthdayToday($user->birthday ?? null)) { $is_birthday = 1; } return $this->success(compact('user', 'door_appointments', 'course_signs', 'enter_schoolmate', 'is_birthday')); diff --git a/app/Models/User.php b/app/Models/User.php index f0266a4..c58001a 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -125,6 +125,26 @@ class User extends Authenticatable implements Auditable 'is_schoolmate' => ['否', '是'], ]; + public static function hasCompleteBirthday(?string $birthday): bool + { + if (empty($birthday) || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthday)) { + return false; + } + + [$year, $month, $day] = array_map('intval', explode('-', $birthday)); + + return checkdate($month, $day, $year); + } + + public static function isBirthdayToday(?string $birthday): bool + { + if (!self::hasCompleteBirthday($birthday)) { + return false; + } + + return substr($birthday, 5, 5) === date('m-d'); + } + public function getMobileAttribute($value) { // 如果url中包含admin字符串,则所有的手机号显示中间4位星号代替 diff --git a/database/migrations/2026_04_02_094504_change_users_birthday_back_to_string.php b/database/migrations/2026_04_02_094504_change_users_birthday_back_to_string.php new file mode 100644 index 0000000..429bcfe --- /dev/null +++ b/database/migrations/2026_04_02_094504_change_users_birthday_back_to_string.php @@ -0,0 +1,31 @@ +string('birthday')->nullable()->comment('生日')->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->date('birthday')->nullable()->comment('生日')->change(); + }); + } +};