瀏覽代碼

发送邮件暂时提交

ANG YU 5 年之前
父節點
當前提交
883753bb8c

+ 35 - 0
app/Events/SendEmailEvent.php

@@ -0,0 +1,35 @@
+<?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 SendEmailEvent
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+
+    /**
+     * SendEmailEvent constructor.
+     */
+    public function __construct()
+    {
+    }
+
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new PrivateChannel('channel-name');
+    }
+}

+ 7 - 0
app/Http/Controllers/TestController.php

@@ -16,6 +16,7 @@ use App\Console\Commands\SyncWmsCommoditiesInformation;
 use App\Console\Commands\SyncWMSOrderTask;
 use App\Console\Commands\WasSyncWmsAsnInformation;
 use App\Events\CancelOrder;
+use App\Events\SendEmailEvent;
 use App\Http\Requests\ForeignHaiRobotic_taskUpdateRequest;
 use App\Http\Requests\TestAaRequest;
 use App\Imports\OrderTrackingImport;
@@ -24,6 +25,7 @@ use App\LaborReport;
 use App\Log;
 use App\Logistic;
 use App\Menu;
+use App\Notifications\SendEmailNotification;
 use App\OracleActAllocationDetails;
 use App\OracleBasCustomer;
 use App\OracleBasSKU;
@@ -1441,4 +1443,9 @@ where (commodities.owner_id,commodity_barcodes.code) in (select commodities.owne
         $service->clearCancelledOrderTask();
     }
 
+    public function sendEmail()
+    {
+        event(new SendEmailEvent());
+    }
+
 }

+ 38 - 0
app/Listeners/SendEmailListener.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\SendEmailEvent;
+use App\Notifications\SendEmailNotification;
+use App\User;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+
+class SendEmailListener
+{
+
+
+    /**
+     * SendEmailListener constructor.
+     */
+    public function __construct()
+    {
+    }
+
+
+    /**
+     * Handle the event.
+     *
+     * @param SendEmailEvent $event
+     * @return void
+     */
+    public function handle(SendEmailEvent $event)
+    {
+        //根据SendEmailEvent->className 从数据库中查找,判断状态是否开启
+        $user = User::query()->where('name', 'yang')->first();
+        $user->notify(new SendEmailNotification(json_encode([
+            'title' => '测试邮件发送',
+            'description' => "和哈哈哈哈",
+        ])));
+    }
+}

+ 19 - 0
app/MailEvent.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Traits\LogModelChanging;
+
+class MailEvent extends Model
+{
+    use LogModelChanging;
+
+    protected $fillable = ['name', 'event_name', 'remark', 'is_active', 'template'];
+
+    public function roles()
+    {
+        $this->belongsToMany(Role::class);
+    }
+}

+ 62 - 0
app/Notifications/SendEmailNotification.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Notifications;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Notifications\Messages\MailMessage;
+use Illuminate\Notifications\Notification;
+
+class SendEmailNotification extends Notification
+{
+    use Queueable;
+
+    public $objToJson;
+
+    /**
+     * SendEmailNotification constructor.
+     * @param $objToJson
+     */
+    public function __construct($objToJson)
+    {
+        $this->objToJson = $objToJson;
+    }
+
+
+    /**
+     * Get the notification's delivery channels.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function via($notifiable)
+    {
+        return ['mail'];
+    }
+
+    /**
+     * Get the mail representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return \Illuminate\Notifications\Messages\MailMessage
+     */
+    public function toMail($notifiable)
+    {
+        return (new MailMessage)->view(
+            'emails.test', ['objToJson' => json_decode($this->objToJson)]
+        );
+    }
+
+    /**
+     * Get the array representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function toArray($notifiable)
+    {
+        return [
+            //
+        ];
+    }
+}

+ 3 - 0
app/Providers/EventServiceProvider.php

@@ -35,6 +35,9 @@ class EventServiceProvider extends ServiceProvider
         ModelChangedEvent::class =>[
             ModelChangedListener::class
         ],
+        'App\Events\SendEmailEvent' => [
+            'App\Listeners\SendEmailListener'
+        ],
     ];
 
     /**

+ 35 - 0
database/migrations/2021_01_08_101028_create_notifications_table.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateNotificationsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('notifications', function (Blueprint $table) {
+            $table->uuid('id')->primary();
+            $table->string('type');
+            $table->morphs('notifiable');
+            $table->text('data');
+            $table->timestamp('read_at')->nullable();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('notifications');
+    }
+}

+ 36 - 0
database/migrations/2021_01_08_110021_create_mail_events_table.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateMailEventsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('mail_events', function (Blueprint $table) {
+            $table->id();
+            $table->string('name');
+            $table->string('event_name');
+            $table->string('remark')->nullable();
+            $table->boolean('is_active')->default(true);
+            $table->text('template')->nullable()->comment('邮件模板,json格式');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('mail_events');
+    }
+}

+ 33 - 0
database/migrations/2021_01_08_110329_create_mail_event_role_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateMailEventRoleTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('mail_event_role', function (Blueprint $table) {
+            $table->id();
+            $table->integer('mail_event_id');
+            $table->integer('role_id');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('mail_event_role');
+    }
+}

+ 47 - 0
database/migrations/2021_01_08_110607_add_default_email.php

@@ -0,0 +1,47 @@
+<?php
+
+use App\Events\SendEmailEvent;
+use App\MailEvent;
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddDefaultEmail extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('mail_events', function (Blueprint $table) {
+            //
+            $template = json_encode([
+                'title' => '邮件测试',
+                'description' =>'这是一封测试邮件,收到不用回复'
+            ]);
+
+            (new MailEvent([
+                'name' => '发送默认邮件',
+                'event_name' => SendEmailEvent::class,
+                'remark' => null,
+                'is_active' => true,
+                'template' => $template,
+            ]))->save();
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('mail_events', function (Blueprint $table) {
+            MailEvent::query()->where('name', '发送默认邮件')->delete();
+        });
+    }
+}

+ 43 - 0
database/migrations/2021_01_08_112003_add_default_mail_event_role.php

@@ -0,0 +1,43 @@
+<?php
+
+use App\Events\SendEmailEvent;
+use App\MailEvent;
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
+
+class AddDefaultMailEventRole extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    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,
+            ]);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('mail_event_role', function (Blueprint $table) {
+            //
+        });
+    }
+}

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

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