改进 事件和通知系统
This commit is contained in:
parent
1b48ded856
commit
f8671069e4
@ -4,7 +4,7 @@
|
||||
|
||||
use App\Http\Controllers\Admin\NotificationController;
|
||||
use App\Models\User;
|
||||
use App\Notifications\CommonNotification;
|
||||
use App\Notifications\Common;
|
||||
use GeneaLabs\LaravelModelCaching\CachedBuilder;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
@ -48,7 +48,7 @@ public function handle(): void
|
||||
// chunk
|
||||
$users->chunk(100, function ($users) {
|
||||
foreach ($users as $user) {
|
||||
$user->notify(new CommonNotification($this->title, $this->content));
|
||||
$user->notify(new Common($this->title, $this->content));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class CommonNotification extends Notification implements ShouldQueue
|
||||
class Common extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
@ -3,61 +3,48 @@
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WeComChannel extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
* Send the given notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @param Notification $notification
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
public function send(mixed $notifiable, Notification $notification): void
|
||||
{
|
||||
//
|
||||
}
|
||||
$data = $notification->toWeCom($notifiable);
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
{
|
||||
return ['mail'];
|
||||
}
|
||||
if (!$data) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return MailMessage
|
||||
*/
|
||||
public function toMail($notifiable)
|
||||
{
|
||||
return (new MailMessage)
|
||||
->line('The introduction to the notification.')
|
||||
->action('Notification Action', url('/'))
|
||||
->line('Thank you for using our application!');
|
||||
}
|
||||
$view = $data['view'];
|
||||
$key = $data['wecom_key'] ?? null;
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($notifiable)
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
if (!$key) {
|
||||
$key = config('settings.wecom.robot_hook.default');
|
||||
}
|
||||
|
||||
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $key, [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => [
|
||||
'content' => view($view, [
|
||||
'data' => $data['data'],
|
||||
])->render(),
|
||||
],
|
||||
]);
|
||||
|
||||
if (!$resp->successful()) {
|
||||
Log::error('企业微信机器人发送失败', $data['data']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,111 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\WorkOrder\Reply;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WorkOrderNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
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($notifiable)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function toGroup($notifiable)
|
||||
{
|
||||
$workOrder = $notifiable;
|
||||
|
||||
$reply = [];
|
||||
|
||||
if ($notifiable instanceof WorkOrder) {
|
||||
|
||||
$view = 'notifications.work_order.created';
|
||||
|
||||
$workOrder->load(['module', 'user']);
|
||||
|
||||
$module = $workOrder->module;
|
||||
|
||||
// if (!$workOrder->notify) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
} else if ($notifiable instanceof Reply) {
|
||||
|
||||
$view = 'notifications.work_order.reply';
|
||||
|
||||
|
||||
$workOrder->load(['workOrder', 'user']);
|
||||
$workOrder->workOrder->load('module');
|
||||
|
||||
$reply = $workOrder;
|
||||
$workOrder = $workOrder->workOrder;
|
||||
|
||||
if (!$workOrder->notify) {
|
||||
return;
|
||||
}
|
||||
|
||||
$module = $workOrder->module;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
// 取消隐藏字段
|
||||
$module->makeVisible(['wecom_key']);
|
||||
|
||||
if ($module->wecom_key == null) {
|
||||
$wecom_key = config('settings.wecom.robot_hook.default');
|
||||
} else {
|
||||
$wecom_key = $module->wecom_key;
|
||||
}
|
||||
|
||||
// 隐藏字段
|
||||
$module->makeHidden(['wecom_key']);
|
||||
|
||||
|
||||
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => [
|
||||
'content' => view($view, [
|
||||
'workOrder' => $workOrder,
|
||||
'user' => $workOrder->user,
|
||||
'reply' => $reply,
|
||||
'module' => $module,
|
||||
])->render(),
|
||||
],
|
||||
]);
|
||||
|
||||
if (!$resp->successful()) {
|
||||
Log::error('企业微信机器人发送失败', [
|
||||
'resp' => $resp->json(),
|
||||
'workOrder' => $workOrder,
|
||||
'reply' => $reply,
|
||||
'module' => $module,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Balance;
|
||||
use App\Notifications\UserBalanceNotification;
|
||||
|
||||
class BalanceObserve
|
||||
{
|
||||
/**
|
||||
* Handle the Balance "created" event.
|
||||
*
|
||||
* @param Balance $balance
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function created(Balance $balance)
|
||||
{
|
||||
//
|
||||
return (new UserBalanceNotification())
|
||||
->toGroup($balance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "updated" event.
|
||||
*
|
||||
* @param Balance $balance
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function updated(Balance $balance)
|
||||
{
|
||||
//
|
||||
return (new UserBalanceNotification())
|
||||
->toGroup($balance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "deleted" event.
|
||||
*
|
||||
* @param Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Balance $balance)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "restored" event.
|
||||
*
|
||||
* @param Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Balance $balance)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "force deleted" event.
|
||||
*
|
||||
* @param Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(Balance $balance)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers\WorkOrder;
|
||||
|
||||
use App\Models\WorkOrder\Reply;
|
||||
use App\Notifications\WorkOrderNotification;
|
||||
|
||||
class ReplyObserver
|
||||
{
|
||||
/**
|
||||
* Handle the Reply "created" event.
|
||||
*
|
||||
* @param Reply $reply
|
||||
*
|
||||
* @return void|null
|
||||
*/
|
||||
public function created(Reply $reply)
|
||||
{
|
||||
//
|
||||
return (new WorkOrderNotification())
|
||||
->toGroup($reply);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "updated" event.
|
||||
*
|
||||
* @param Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Reply $reply): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "deleted" event.
|
||||
*
|
||||
* @param Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Reply $reply): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "restored" event.
|
||||
*
|
||||
* @param Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Reply $reply): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers\WorkOrder;
|
||||
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use App\Notifications\WorkOrderNotification;
|
||||
|
||||
class WorkOrderObserver
|
||||
{
|
||||
/**
|
||||
* Handle the WorkOrder "created" event.
|
||||
*
|
||||
* @param WorkOrder $workOrder
|
||||
*
|
||||
* @return void|null
|
||||
*/
|
||||
public function created(WorkOrder $workOrder)
|
||||
{
|
||||
(new WorkOrderNotification())
|
||||
->toGroup($workOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "updated" event.
|
||||
*
|
||||
* @param WorkOrder $workOrder
|
||||
*
|
||||
* @return void|null
|
||||
*/
|
||||
public function updated(WorkOrder $workOrder)
|
||||
{
|
||||
(new WorkOrderNotification())
|
||||
->toGroup($workOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "deleted" event.
|
||||
*
|
||||
* @param WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(WorkOrder $workOrder): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "restored" event.
|
||||
*
|
||||
* @param WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restored(WorkOrder $workOrder): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
}
|
@ -12,7 +12,7 @@ class BroadcastServiceProvider extends ServiceProvider
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
public function boot(): void
|
||||
{
|
||||
Broadcast::routes([
|
||||
'middleware' => ['auth:sanctum', 'auth'],
|
||||
|
@ -2,12 +2,6 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Balance;
|
||||
use App\Models\WorkOrder\Reply;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use App\Observers\BalanceObserve;
|
||||
use App\Observers\WorkOrder\ReplyObserver;
|
||||
use App\Observers\WorkOrder\WorkOrderObserver;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
@ -30,13 +24,8 @@ class EventServiceProvider extends ServiceProvider
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
|
||||
WorkOrder::observe(WorkOrderObserver::class);
|
||||
Reply::observe(ReplyObserver::class);
|
||||
Balance::observe(BalanceObserve::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,7 +33,7 @@ public function boot()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldDiscoverEvents()
|
||||
public function shouldDiscoverEvents(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user