Lae/app/Notifications/WorkOrder/WorkOrder.php

102 lines
2.5 KiB
PHP
Raw Normal View History

2023-01-10 12:46:53 +00:00
<?php
2023-01-13 14:11:56 +00:00
namespace App\Notifications\WorkOrder;
2023-01-10 12:46:53 +00:00
use App\Models\WorkOrder\WorkOrder as WorkOrderModel;
2023-01-13 14:11:56 +00:00
use App\Notifications\Channels\WebChannel;
use App\Notifications\Channels\WeComChannel;
2023-01-10 12:46:53 +00:00
use Illuminate\Bus\Queueable;
2023-01-17 13:02:04 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
2023-01-10 12:46:53 +00:00
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
2023-01-17 13:02:04 +00:00
class WorkOrder extends Notification implements ShouldQueue
2023-01-10 12:46:53 +00:00
{
use Queueable;
2023-01-10 13:42:27 +00:00
public WorkOrderModel $work_order;
2023-01-10 12:46:53 +00:00
/**
* Create a new notification instance.
*
* @return void
*/
2023-01-10 13:42:27 +00:00
public function __construct(WorkOrderModel $work_order)
2023-01-10 12:46:53 +00:00
{
2023-01-10 13:42:27 +00:00
$this->work_order = $work_order;
2023-01-10 12:46:53 +00:00
}
/**
* Get the notification's delivery channels.
*
*
2023-02-07 09:04:11 +00:00
* @param WorkOrderModel $workOrder
2023-01-10 12:46:53 +00:00
* @return array
*/
2023-01-10 14:47:55 +00:00
public function via(WorkOrderModel $workOrder): array
2023-01-10 12:46:53 +00:00
{
2023-01-11 12:19:05 +00:00
$methods = [WeComChannel::class, WebChannel::class];
2023-01-10 14:47:55 +00:00
if (in_array($workOrder->status, ['processing', 'replied'])) {
$methods[] = 'mail';
}
return $methods;
2023-01-10 12:46:53 +00:00
}
/**
* Get the mail representation of the notification.
*
*
2023-02-07 09:04:11 +00:00
* @param WorkOrderModel $workOrder
2023-01-10 12:46:53 +00:00
* @return MailMessage
*/
2023-01-10 14:47:55 +00:00
public function toMail(WorkOrderModel $workOrder): MailMessage
2023-01-10 12:46:53 +00:00
{
return (new MailMessage)
2023-02-07 09:04:11 +00:00
->subject('工单: '.$workOrder->title.' 状态更新。')
2023-01-10 14:47:55 +00:00
->line('我们查阅了您的工单并做出了相应处理。')
->line('请前往我们的仪表盘继续跟进问题。');
2023-01-10 12:46:53 +00:00
}
/**
* Get the array representation of the notification.
*
*
2023-02-07 09:04:11 +00:00
* @param WorkOrderModel $workOrder
2023-01-10 12:46:53 +00:00
* @return array
*/
public function toArray(WorkOrderModel $workOrder): array
2023-01-10 12:46:53 +00:00
{
$array = $workOrder->toArray();
2023-01-11 12:19:05 +00:00
2023-02-07 09:04:11 +00:00
$array['event'] = 'work-order.'.$workOrder->status;
return $array;
2023-01-10 12:46:53 +00:00
}
public function toWeCom(WorkOrderModel $workOrder): false|array
{
$workOrder->load(['module', 'user']);
2023-02-12 16:45:57 +00:00
$wecom_key = config('settings.wecom.robot_hook.default');
if ($workOrder->module) {
$module = $workOrder->module;
2023-01-10 12:46:53 +00:00
2023-02-12 16:45:57 +00:00
$wecom_key = $module->makeVisible(['wecom_key'])->wecom_key ?? $wecom_key;
2023-01-10 12:46:53 +00:00
}
return [
'key' => $wecom_key,
'view' => 'notifications.work_order',
2023-01-30 16:14:07 +00:00
'data' => $workOrder,
2023-01-10 12:46:53 +00:00
];
}
// public function toBroadcast(WorkOrderModel $workOrder): BroadcastMessage
// {
// return new BroadcastMessage($workOrder->toArray());
// }
2023-01-10 12:46:53 +00:00
}