增加 WorkOrder WeCom 通知

This commit is contained in:
iVampireSP.com 2023-01-10 20:46:53 +08:00
parent 88af12e386
commit 3b73ee2efe
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 98 additions and 2 deletions

View File

@ -7,6 +7,7 @@
use App\Models\Host;
use App\Models\Module;
use App\Models\User;
use App\Notifications\WorkOrder as WorkOrderNotification;
use Eloquent;
use GeneaLabs\LaravelModelCaching\CachedBuilder;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
@ -14,6 +15,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
@ -71,7 +73,7 @@
*/
class WorkOrder extends Model
{
use Cachable;
use Cachable, Notifiable;
protected $table = 'work_orders';
@ -130,8 +132,10 @@ protected static function boot()
});
// updated
static::updated(function ($model) {
static::updated(function (self $model) {
dispatch(new WorkOrderJob($model, 'put'));
$model->notify(new WorkOrderNotification($model));
});
}

View File

@ -0,0 +1,92 @@
<?php
namespace App\Notifications;
use App\Models\WorkOrder\WorkOrder as WorkOrderModel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class WorkOrder extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return array
*/
public function via(mixed $notifiable): array
{
return [WeComChannel::class];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return MailMessage
*/
public function toMail(mixed $notifiable): MailMessage
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
*
* @return array
*/
public function toArray(mixed $notifiable): array
{
return [
//
];
}
public function toWeCom(WorkOrderModel $workOrder): false|array
{
$workOrder->load(['module', 'user']);
$module = $workOrder->module;
if ($workOrder->notify === 0) {
return false;
}
// 取消隐藏字段
$module->makeVisible(['wecom_key']);
if ($module->wecom_key == null) {
$wecom_key = config('settings.wecom.robot_hook.default');
} else {
$wecom_key = $module->wecom_key;
}
return [
'key' => $wecom_key,
'view' => 'notifications.work_order',
'data' => $workOrder
];
}
}