改进 事件和通知系统

This commit is contained in:
iVampireSP.com 2023-01-10 20:43:54 +08:00
parent 1b48ded856
commit f8671069e4
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
10 changed files with 2238 additions and 368 deletions

View File

@ -4,7 +4,7 @@
use App\Http\Controllers\Admin\NotificationController; use App\Http\Controllers\Admin\NotificationController;
use App\Models\User; use App\Models\User;
use App\Notifications\CommonNotification; use App\Notifications\Common;
use GeneaLabs\LaravelModelCaching\CachedBuilder; use GeneaLabs\LaravelModelCaching\CachedBuilder;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -48,7 +48,7 @@ public function handle(): void
// chunk // chunk
$users->chunk(100, function ($users) { $users->chunk(100, function ($users) {
foreach ($users as $user) { foreach ($users as $user) {
$user->notify(new CommonNotification($this->title, $this->content)); $user->notify(new Common($this->title, $this->content));
} }
}); });
} }

View File

@ -7,7 +7,7 @@
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
class CommonNotification extends Notification implements ShouldQueue class Common extends Notification implements ShouldQueue
{ {
use Queueable; use Queueable;

View File

@ -3,61 +3,48 @@
namespace App\Notifications; namespace App\Notifications;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class WeComChannel extends Notification class WeComChannel extends Notification
{ {
use Queueable; use Queueable;
/** /**
* Create a new notification instance. * Send the given notification.
*
* @param mixed $notifiable
* @param Notification $notification
* *
* @return void * @return void
*/ */
public function __construct() public function send(mixed $notifiable, Notification $notification): void
{ {
// $data = $notification->toWeCom($notifiable);
}
/** if (!$data) {
* Get the notification's delivery channels. return;
* }
* @param mixed $notifiable
*
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/** $view = $data['view'];
* Get the mail representation of the notification. $key = $data['wecom_key'] ?? null;
*
* @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!');
}
/** if (!$key) {
* Get the array representation of the notification. $key = config('settings.wecom.robot_hook.default');
* }
* @param mixed $notifiable
* $resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $key, [
* @return array 'msgtype' => 'markdown',
*/ 'markdown' => [
public function toArray($notifiable) 'content' => view($view, [
{ 'data' => $data['data'],
return [ ])->render(),
// ],
]; ]);
if (!$resp->successful()) {
Log::error('企业微信机器人发送失败', $data['data']);
}
} }
} }

View File

@ -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,
]);
}
}
}

View File

@ -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)
{
//
}
}

View File

@ -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
{
//
}
}

View File

@ -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
{
//
}
}

View File

@ -12,7 +12,7 @@ class BroadcastServiceProvider extends ServiceProvider
* *
* @return void * @return void
*/ */
public function boot() public function boot(): void
{ {
Broadcast::routes([ Broadcast::routes([
'middleware' => ['auth:sanctum', 'auth'], 'middleware' => ['auth:sanctum', 'auth'],

View File

@ -2,12 +2,6 @@
namespace App\Providers; 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\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@ -30,13 +24,8 @@ class EventServiceProvider extends ServiceProvider
* *
* @return void * @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 * @return bool
*/ */
public function shouldDiscoverEvents() public function shouldDiscoverEvents(): bool
{ {
return false; return false;
} }

2208
yarn.lock

File diff suppressed because it is too large Load Diff