Przeglądaj źródła

发送邮件测试完成

ANG YU 5 lat temu
rodzic
commit
942471cbe4

+ 33 - 8
app/Listeners/SendEmailListener.php

@@ -3,11 +3,15 @@
 namespace App\Listeners;
 
 use App\Events\SendEmailEvent;
+use App\MailEvent;
 use App\Notifications\SendEmailNotification;
-use App\User;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Queue\InteractsWithQueue;
+use Tightenco\Collect\Support\Collection;
 
+/**
+ * 邮件发送监听器
+ * Class SendEmailListener
+ * @package App\Listeners
+ */
 class SendEmailListener
 {
 
@@ -17,6 +21,7 @@ class SendEmailListener
      */
     public function __construct()
     {
+
     }
 
 
@@ -29,10 +34,30 @@ class SendEmailListener
     public function handle(SendEmailEvent $event)
     {
         //根据SendEmailEvent->className 从数据库中查找,判断状态是否开启
-        $user = User::query()->where('name', 'yang')->first();
-        $user->notify(new SendEmailNotification(json_encode([
-            'title' => '测试邮件发送',
-            'description' => "和哈哈哈哈",
-        ])));
+        $emailEvent = MailEvent::query()
+            ->where('event_name', SendEmailEvent::class)
+            ->where('is_active', true)
+            ->first();
+        if ($emailEvent) {
+            /** @var MailEvent $emailEvent */
+            $users = $this->getUsers($emailEvent);
+            foreach ($users as $user) {//给这些用户发送通知
+                $user->notify(new SendEmailNotification($emailEvent->template));//邮件模板由数据库中定制的模板为准
+            }
+        }
+    }
+
+    /**
+     * 根據事件关联的角色,查询到用户
+     * @param MailEvent $emailEvent
+     * @return \Illuminate\Support\Collection|Collection
+     */
+    private function getUsers(MailEvent $emailEvent)
+    {
+        $users = collect();
+        foreach ($emailEvent->roles as $role) {
+            $users = $users->merge($role->users);
+        }
+        return $users;
     }
 }

+ 6 - 2
app/MailEvent.php

@@ -5,6 +5,7 @@ namespace App;
 use Illuminate\Database\Eloquent\Model;
 
 use App\Traits\LogModelChanging;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 
 class MailEvent extends Model
 {
@@ -12,8 +13,11 @@ class MailEvent extends Model
 
     protected $fillable = ['name', 'event_name', 'remark', 'is_active', 'template'];
 
-    public function roles()
+    /**
+     * @return BelongsToMany
+     */
+    public function roles(): BelongsToMany
     {
-        $this->belongsToMany(Role::class);
+        return $this->belongsToMany(Role::class,'mail_event_role','mail_event_id','role_id');
     }
 }

+ 4 - 4
app/Notifications/SendEmailNotification.php

@@ -7,7 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Notifications\Messages\MailMessage;
 use Illuminate\Notifications\Notification;
 
-class SendEmailNotification extends Notification
+class SendEmailNotification extends Notification implements ShouldQueue
 {
     use Queueable;
 
@@ -29,7 +29,7 @@ class SendEmailNotification extends Notification
      * @param  mixed  $notifiable
      * @return array
      */
-    public function via($notifiable)
+    public function via($notifiable): array
     {
         return ['mail'];
     }
@@ -38,9 +38,9 @@ class SendEmailNotification extends Notification
      * Get the mail representation of the notification.
      *
      * @param  mixed  $notifiable
-     * @return \Illuminate\Notifications\Messages\MailMessage
+     * @return MailMessage
      */
-    public function toMail($notifiable)
+    public function toMail($notifiable): MailMessage
     {
         return (new MailMessage)->view(
             'emails.test', ['objToJson' => json_decode($this->objToJson)]

+ 13 - 17
database/migrations/2021_01_08_110607_add_default_email.php

@@ -8,6 +8,8 @@ use Illuminate\Support\Facades\Schema;
 
 class AddDefaultEmail extends Migration
 {
+    protected $mailEvent;
+
     /**
      * Run the migrations.
      *
@@ -15,22 +17,18 @@ class AddDefaultEmail extends Migration
      */
     public function up()
     {
-        Schema::table('mail_events', function (Blueprint $table) {
-            //
-            $template = json_encode([
+        //
+        $this->mailEvent = (new MailEvent([
+            'name' => '发送默认邮件',
+            'event_name' => SendEmailEvent::class,
+            'remark' => null,
+            'is_active' => true,
+            'template' => json_encode([
                 'title' => '邮件测试',
-                'description' =>'这是一封测试邮件,收到不用回复'
-            ]);
-
-            (new MailEvent([
-                'name' => '发送默认邮件',
-                'event_name' => SendEmailEvent::class,
-                'remark' => null,
-                'is_active' => true,
-                'template' => $template,
-            ]))->save();
+                'description' => '这是一封测试邮件,收到不用回复'
+            ]),
+        ]))->save();
 
-        });
     }
 
     /**
@@ -40,8 +38,6 @@ class AddDefaultEmail extends Migration
      */
     public function down()
     {
-        Schema::table('mail_events', function (Blueprint $table) {
-            MailEvent::query()->where('name', '发送默认邮件')->delete();
-        });
+        $this->mailEvent->delete();
     }
 }

+ 7 - 14
database/migrations/2021_01_08_112003_add_default_mail_event_role.php

@@ -2,6 +2,7 @@
 
 use App\Events\SendEmailEvent;
 use App\MailEvent;
+use App\Role;
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\DB;
@@ -9,6 +10,7 @@ use Illuminate\Support\Facades\Schema;
 
 class AddDefaultMailEventRole extends Migration
 {
+    protected $role;
     /**
      * Run the migrations.
      *
@@ -16,17 +18,10 @@ class AddDefaultMailEventRole extends Migration
      */
     public function up()
     {
-        Schema::table('mail_event_role', function (Blueprint $table) {
-            $role = (new \App\Role([
-                'name' => '发送邮件测试'
-            ]))->save();
-
-            $emailEvent = MailEvent::query()->where('name', '发送默认邮件')->first();
-            DB::table('mail_event_role')->insert([
-                'mail_event_id' => $emailEvent->id,
-                'role_id' => $role->id,
-            ]);
-        });
+        $this->role =  Role::query()->firstOrCreate(['name'=>'发送邮件测试']);
+        $emailEvent = MailEvent::query()->where('name', '发送默认邮件')->first();
+        $emailEvent->roles()->attach($this->role->id);
+        $this->role->users()->attach(\App\User::query()->where('name','yang')->first()->id);
     }
 
     /**
@@ -36,8 +31,6 @@ class AddDefaultMailEventRole extends Migration
      */
     public function down()
     {
-        Schema::table('mail_event_role', function (Blueprint $table) {
-            //
-        });
+        $this->role->delete();
     }
 }