hu hao 4 жил өмнө
parent
commit
a5ff915c70

+ 47 - 0
app/Events/GitPushedEvent.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Events;
+
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class GitPushedEvent
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $email;
+    /**
+     * [
+     *  'title'=>''
+     *  'description'='信息'
+     * ]
+     */
+    public $load;
+
+    /**
+     * GitPushedEvent constructor.
+     * @param $email
+     * @param $load
+     */
+    public function __construct($email, $load)
+    {
+        $this->email = $email;
+        $this->load = $load;
+    }
+
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new PrivateChannel('channel-name');
+    }
+}

+ 0 - 1
app/Events/SendEmailEvent.php

@@ -14,7 +14,6 @@ class SendEmailEvent
 {
     use Dispatchable, InteractsWithSockets, SerializesModels;
 
-
     /**
      * SendEmailEvent constructor.
      */

+ 20 - 0
app/Http/Controllers/api/email/SendEmailsController.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Http\Controllers\api\email;
+
+use App\Events\GitPushedEvent;
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+
+class SendEmailsController extends Controller
+{
+    public function send(Request $request)
+    {
+        $email=$request['email'];
+        $load=[
+            'title'=>$request['title'],
+            'description'=>$request['description'],
+        ];
+        event(new GitPushedEvent($email,$load));
+    }
+}

+ 47 - 0
app/Listeners/GitPushedSendEmailListener.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\GitPushedEvent;
+
+use App\MailEvent;
+use App\Notifications\SendEmailNotification;
+use App\User;
+
+class GitPushedSendEmailListener
+{
+    /**
+     * Create the event listener.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Handle the event.
+     *
+     * @param GitPushedEvent $event
+     * @return void
+     */
+    public function handle(GitPushedEvent $event)
+    {
+        $emailEvent = MailEvent::query()
+            ->where('event_name', GitPushedEvent::class)
+            ->where('is_active', true)
+            ->firstOrCreate([
+                'name' => 'gitPushEvent',
+                'event_name' => GitPushedEvent::class,
+            ]);
+        if ($emailEvent) {
+            $email = $event->email;
+            $load = $event->load; //array
+            $user = User::query()->where('email',$email)->first();
+            $template = json_encode($load);
+            $user->notify(new SendEmailNotification($template, 'emails.gitPush'));
+        }
+
+    }
+}

+ 1 - 1
app/Listeners/SendEmailListener.php

@@ -42,7 +42,7 @@ class SendEmailListener
             /** @var MailEvent $emailEvent */
             $users = $this->getUsers($emailEvent);
             foreach ($users as $user) {//给这些用户发送通知
-                $user->notify(new SendEmailNotification($emailEvent->template));//邮件模板由数据库中定制的模板为准
+                $user->notify(new SendEmailNotification($emailEvent->template,'emails.test'));//邮件模板由数据库中定制的模板为准
             }
         }
     }

+ 10 - 5
app/Notifications/SendEmailNotification.php

@@ -13,20 +13,24 @@ class SendEmailNotification extends Notification implements ShouldQueue
 
     public $objToJson;
 
+    public $view;
+
     /**
      * SendEmailNotification constructor.
      * @param $objToJson
+     * @param $view
      */
-    public function __construct($objToJson)
+    public function __construct($objToJson, $view)
     {
         $this->objToJson = $objToJson;
+        $this->view = $view;
     }
 
 
     /**
      * Get the notification's delivery channels.
      *
-     * @param  mixed  $notifiable
+     * @param mixed $notifiable
      * @return array
      */
     public function via($notifiable): array
@@ -37,21 +41,22 @@ class SendEmailNotification extends Notification implements ShouldQueue
     /**
      * Get the mail representation of the notification.
      *
-     * @param  mixed  $notifiable
+     * @param mixed $notifiable
      * @return MailMessage
      */
     public function toMail($notifiable): MailMessage
     {
         $template = json_decode($this->objToJson);
+        $view = $this->view ? $this->view : 'emails.test';
         return (new MailMessage)->view(
-            'emails.test', ['objToJson' => $template]
+            $view, ['objToJson' => $template]
         )->subject($template->title);
     }
 
     /**
      * Get the array representation of the notification.
      *
-     * @param  mixed  $notifiable
+     * @param mixed $notifiable
      * @return array
      */
     public function toArray($notifiable)

+ 5 - 0
app/Providers/EventServiceProvider.php

@@ -35,6 +35,11 @@ class EventServiceProvider extends ServiceProvider
         'App\Events\SendEmailEvent' => [
             'App\Listeners\SendEmailListener'
         ],
+
+        'App\Events\GitPushedEvent' => [
+            'App\Listeners\GitPushedSendEmailListener'
+        ],
+
         'App\Events\AddOrUpdateOrderIssues' => [//问题件新增或更新
             'App\Listeners\AddOrUpdateOrderIssuesListener',//将对应的order_packages的数据的异常装变更
         ],

+ 5 - 0
app/Providers/RouteServiceProvider.php

@@ -76,6 +76,11 @@ class RouteServiceProvider extends ServiceProvider
              ->namespace($this->namespace)
              ->group(base_path('routes/apiLocal.php'));
 
+        Route::prefix('api/email/send')
+            ->middleware('api')
+            ->namespace('App\Http\Controllers\api\email')
+            ->group(base_path('routes/api/email/send.php'));
+
         Route::prefix('api/thirdPart/flux')
             ->middleware('api')
             ->namespace('App\Http\Controllers\api\thirdPart\flux')

+ 2 - 0
resources/views/emails/gitPush.blade.php

@@ -0,0 +1,2 @@
+{{  $objToJson->title }}
+{{  $objToJson->description }}

+ 8 - 0
routes/api/email/send.php

@@ -0,0 +1,8 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+
+/*
+文件地址前缀:/api/email/send
+*/
+Route::middleware(['throttle:1,1'])->post('gitPushSendEmail', "SendEmailsController@send");