diff --git a/app/Models/WorkOrder/WorkOrder.php b/app/Models/WorkOrder/WorkOrder.php index b71bbd9..8f9b641 100644 --- a/app/Models/WorkOrder/WorkOrder.php +++ b/app/Models/WorkOrder/WorkOrder.php @@ -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)); }); } diff --git a/app/Notifications/WorkOrder.php b/app/Notifications/WorkOrder.php new file mode 100644 index 0000000..114e04f --- /dev/null +++ b/app/Notifications/WorkOrder.php @@ -0,0 +1,92 @@ +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 + ]; + } + +}