diff --git a/app/Events/Users.php b/app/Events/Users.php index ad61aa3..3d0464e 100644 --- a/app/Events/Users.php +++ b/app/Events/Users.php @@ -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 { diff --git a/app/Jobs/Module/WorkOrder/WorkOrder.php b/app/Jobs/Module/WorkOrder/WorkOrder.php index 35f8794..55f7ff1 100644 --- a/app/Jobs/Module/WorkOrder/WorkOrder.php +++ b/app/Jobs/Module/WorkOrder/WorkOrder.php @@ -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)); - } } - } } diff --git a/app/Notifications/Common.php b/app/Notifications/Common.php deleted file mode 100644 index 1127c02..0000000 --- a/app/Notifications/Common.php +++ /dev/null @@ -1,65 +0,0 @@ -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 [ - // - ]; - } -} diff --git a/app/Notifications/CommonChannel.php b/app/Notifications/CommonChannel.php new file mode 100644 index 0000000..6542ad7 --- /dev/null +++ b/app/Notifications/CommonChannel.php @@ -0,0 +1,41 @@ +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)); + + } +} diff --git a/app/Notifications/WorkOrder.php b/app/Notifications/WorkOrder.php index 85ddfde..7fa643d 100644 --- a/app/Notifications/WorkOrder.php +++ b/app/Notifications/WorkOrder.php @@ -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()); + // } }