From 60ba123739a1069d206007d50928d4fd4650de27 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 1 Mar 2023 22:30:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E5=B7=A5=E5=8D=95?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/WorkOrder/Reply.php | 14 ++++- app/Notifications/WorkOrder/Reply.php | 84 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 app/Notifications/WorkOrder/Reply.php diff --git a/app/Models/WorkOrder/Reply.php b/app/Models/WorkOrder/Reply.php index 8242143..6c01346 100644 --- a/app/Models/WorkOrder/Reply.php +++ b/app/Models/WorkOrder/Reply.php @@ -5,13 +5,15 @@ use App\Exceptions\CommonException; use App\Models\Module; use App\Models\User; +use App\Notifications\WorkOrder\Reply as ReplyNotification; use GeneaLabs\LaravelModelCaching\Traits\Cachable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Notifications\Notifiable; class Reply extends Model { - use Cachable; + use Cachable, Notifiable; protected $table = 'work_order_replies'; @@ -77,6 +79,9 @@ protected static function booted() $model->workOrder->save(); } + // notify + $model->notify(new ReplyNotification($model)); + // dispatch dispatch(new \App\Jobs\WorkOrder\Reply($model, 'post')); dispatch(new \App\Jobs\WorkOrder\WorkOrder($model->workOrder, 'put')); @@ -118,4 +123,11 @@ public function safeDelete(): void { dispatch(new \App\Jobs\WorkOrder\Reply($this, 'delete')); } + + public function routeNotificationForMail(): array + { + $user = $this->workOrder->user; + + return $user ? [$user->email => $user->name] : []; + } } diff --git a/app/Notifications/WorkOrder/Reply.php b/app/Notifications/WorkOrder/Reply.php new file mode 100644 index 0000000..b2e9553 --- /dev/null +++ b/app/Notifications/WorkOrder/Reply.php @@ -0,0 +1,84 @@ +reply_model = $reply; + $this->work_order = $reply->workOrder; + } + + /** + * Get the notification's delivery channels. + */ + public function via(): array + { + $channels = [WeComChannel::class, WebChannel::class]; + + if ($this->work_order->status === 'replied') { + $channels[] = 'mail'; + } + + return $channels; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(ReplyModel $reply): MailMessage + { + $url = URL::format(config('settings.dashboard.base_url'), config('settings.dashboard.work_order_path').'/'.$this->work_order->uuid); + + return (new MailMessage) + ->subject('工单: '.$this->work_order->title.' 需要您处理。') + ->line('我们的回复: ') + ->line($reply->content) + ->action('查看工单', $url); + } + + /** + * Get the array representation of the notification. + */ + public function toArray(ReplyModel $reply): array + { + $array = $reply->toArray(); + + $array['event'] = 'work-order.reply.created'; + + return $array; + } + + public function toWeCom(ReplyModel $reply): false|array + { + $this->work_order->load(['module', 'user']); + + return [ + 'key' => $this->work_order->wecom_key, + 'view' => 'notifications.work_order.reply', + 'data' => $reply, + ]; + } +}