改进 通知
ToDo: 将 WorkOrder 的通知分离
This commit is contained in:
parent
642d4ba65c
commit
736638aadf
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Models\Module;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
@ -15,20 +16,25 @@ class Users extends Event implements ShouldBroadcastNow
|
||||
|
||||
public User $user;
|
||||
public string $type = 'ping';
|
||||
public string|array $data;
|
||||
public array $data;
|
||||
public null|Module $module;
|
||||
|
||||
public Carbon $sent_at;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(User $user, $type, string|array $data)
|
||||
public function __construct(User $user, $type, array $data)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->type = $type;
|
||||
$this->data = $data;
|
||||
|
||||
$this->sent_at = Carbon::now();
|
||||
|
||||
if (Auth::guard('module')->check()) {
|
||||
$this->module = Auth::guard('module')->user();
|
||||
} else {
|
||||
|
@ -54,14 +54,6 @@ public function handle(): void
|
||||
$this->workOrder->update([
|
||||
'status' => 'error'
|
||||
]);
|
||||
|
||||
} else {
|
||||
if ($this->type == 'delete') {
|
||||
broadcast(new Users($this->workOrder->user, 'work-order.deleted', $this->workOrder));
|
||||
} else {
|
||||
broadcast(new Users($this->workOrder->user, 'work-order.updated', $this->workOrder));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,65 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class Common extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public string $title;
|
||||
public string $content;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(string $title, string $content)
|
||||
{
|
||||
$this->title = $title;
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function via(): array
|
||||
{
|
||||
return ['mail'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
*
|
||||
* @return MailMessage
|
||||
*/
|
||||
public function toMail(): MailMessage
|
||||
{
|
||||
return (new MailMessage)->subject($this->title)->markdown('mail.common', [
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
41
app/Notifications/CommonChannel.php
Normal file
41
app/Notifications/CommonChannel.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Events\Users;
|
||||
use App\Models\User;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class CommonChannel extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Send the given notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @param Notification $notification
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function send(mixed $notifiable, Notification $notification): void
|
||||
{
|
||||
$data = $notification->toArray($notifiable);
|
||||
|
||||
if (!$data) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user_id = $notifiable->user_id ?? $notifiable->id;
|
||||
|
||||
$user = User::find($user_id);
|
||||
|
||||
if (!in_array($data['type'] ?? '', ['info', 'success', 'warning', 'error'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
broadcast(new Users($user, $data['type'], $data));
|
||||
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@
|
||||
|
||||
use App\Models\WorkOrder\WorkOrder as WorkOrderModel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Messages\BroadcastMessage;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
@ -28,11 +27,13 @@ public function __construct(WorkOrderModel $work_order)
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
*
|
||||
* @param WorkOrderModel $workOrder
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function via(WorkOrderModel $workOrder): array
|
||||
{
|
||||
$methods = [WeComChannel::class, 'broadcast'];
|
||||
$methods = [WeComChannel::class, CommonChannel::class];
|
||||
|
||||
if (in_array($workOrder->status, ['processing', 'replied'])) {
|
||||
$methods[] = 'mail';
|
||||
@ -61,18 +62,21 @@ public function toMail(WorkOrderModel $workOrder): MailMessage
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
*
|
||||
* @param WorkOrderModel $workOrder
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
public function toArray(WorkOrderModel $workOrder): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
$array = $workOrder->toArray();
|
||||
$array['type'] = 'info';
|
||||
$array['title'] = '工单: ' . $workOrder->title . ' 状态更新。';
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public function toWeCom(WorkOrderModel $workOrder): false|array
|
||||
{
|
||||
|
||||
$workOrder->load(['module', 'user']);
|
||||
|
||||
$module = $workOrder->module;
|
||||
@ -97,9 +101,9 @@ public function toWeCom(WorkOrderModel $workOrder): false|array
|
||||
];
|
||||
}
|
||||
|
||||
public function toBroadcast(WorkOrderModel $workOrder): BroadcastMessage
|
||||
{
|
||||
return new BroadcastMessage($workOrder->toArray());
|
||||
}
|
||||
// public function toBroadcast(WorkOrderModel $workOrder): BroadcastMessage
|
||||
// {
|
||||
// return new BroadcastMessage($workOrder->toArray());
|
||||
// }
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user