From e5fbf22bb8dbd72e6d43065cc084c6a642e35e9c Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 1 Mar 2023 23:00:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E7=94=9F=E6=97=A5?= =?UTF-8?q?=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../User/TodayIsUserBirthday.php | 71 +++++++++++++------ 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/app/Notifications/User/TodayIsUserBirthday.php b/app/Notifications/User/TodayIsUserBirthday.php index a8a9cb8..c15098a 100644 --- a/app/Notifications/User/TodayIsUserBirthday.php +++ b/app/Notifications/User/TodayIsUserBirthday.php @@ -2,6 +2,7 @@ namespace App\Notifications\User; +use App\Notifications\Channels\WebChannel; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -11,18 +12,49 @@ class TodayIsUserBirthday extends Notification implements ShouldQueue { use Queueable; + private string $subject = '生日快乐'; + + private string $greeting = '生日快乐🎂'; + /** * Get the notification's delivery channels. */ public function via(): array { - return ['mail']; + return [WebChannel::class, 'mail']; } /** * Get the mail representation of the notification. */ public function toMail(): MailMessage + { + $email = (new MailMessage) + ->subject($this->subject) + ->greeting($this->greeting); + + $lyrics = $this->lyrics(); + foreach ($lyrics as $lyric) { + $email->line($lyric); + } + + $email->line($this->suffixText()); + + return $email; + } + + /** + * Get the array representation of the notification. + */ + public function toArray(): array + { + return [ + 'title' => $this->greeting, + 'message' => $this->lyrics(true).$this->suffixText(), + ]; + } + + protected function lyrics(bool $to_string = false): array|string { $lyrics = [ [ @@ -50,38 +82,31 @@ public function toMail(): MailMessage [ '即使以心传心但也一定有着极限的。所以要好好地说出来。', ], - ]; - $lyric = $lyrics[array_rand($lyrics)]; + $random_lyrics = $lyrics[array_rand($lyrics)]; - $email = (new MailMessage) - ->subject('生日快乐') - ->greeting('生日快乐🎂'); + if ($to_string) { + $lyric_string = ''; + foreach ($random_lyrics as $lyric) { + $lyric_string .= $lyric.PHP_EOL.PHP_EOL; + } - foreach ($lyric as $line) { - $email->line($line); + return $lyric_string; } + return $random_lyrics; + } + + public function suffixText(): string + { $today = now()->format('Y-m-d'); - $text = <<line($text); - - return $email; - } - - /** - * Get the array representation of the notification. - */ - public function toArray(): array - { - return [ - // - ]; } }