增加 工单回复通知
This commit is contained in:
parent
e1f5337fe3
commit
60ba123739
@ -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] : [];
|
||||
}
|
||||
}
|
||||
|
84
app/Notifications/WorkOrder/Reply.php
Normal file
84
app/Notifications/WorkOrder/Reply.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\WorkOrder;
|
||||
|
||||
use App\Models\WorkOrder\Reply as ReplyModel;
|
||||
use App\Models\WorkOrder\WorkOrder as WorkOrderModel;
|
||||
use App\Notifications\Channels\WebChannel;
|
||||
use App\Notifications\Channels\WeComChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class Reply extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public ReplyModel $reply_model;
|
||||
|
||||
protected WorkOrderModel $work_order;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(ReplyModel $reply)
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user