改进 通知
This commit is contained in:
parent
736638aadf
commit
2c275c08cf
@ -7,8 +7,10 @@
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Broadcasting\PrivateChannel;
|
use Illuminate\Broadcasting\PrivateChannel;
|
||||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class Users extends Event implements ShouldBroadcastNow
|
class Users extends Event implements ShouldBroadcastNow
|
||||||
{
|
{
|
||||||
@ -16,8 +18,9 @@ class Users extends Event implements ShouldBroadcastNow
|
|||||||
|
|
||||||
public User $user;
|
public User $user;
|
||||||
public string $type = 'ping';
|
public string $type = 'ping';
|
||||||
public array $data;
|
public array|Model $data;
|
||||||
public null|Module $module;
|
public null|Module $module = null;
|
||||||
|
public string $event = 'messages';
|
||||||
|
|
||||||
public Carbon $sent_at;
|
public Carbon $sent_at;
|
||||||
|
|
||||||
@ -27,18 +30,45 @@ class Users extends Event implements ShouldBroadcastNow
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(User $user, $type, array $data)
|
public function __construct(User|int $user, $type, array|Model $data)
|
||||||
{
|
{
|
||||||
$this->user = $user;
|
// init vars
|
||||||
$this->type = $type;
|
|
||||||
$this->data = $data;
|
|
||||||
|
|
||||||
$this->sent_at = Carbon::now();
|
$this->sent_at = Carbon::now();
|
||||||
|
|
||||||
|
// if user is int
|
||||||
|
if (is_int($user)) {
|
||||||
|
$user = User::find($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->user = $user;
|
||||||
|
|
||||||
|
$this->type = $type;
|
||||||
|
|
||||||
|
if ($data instanceof Model) {
|
||||||
|
$this->data = $data->toArray();
|
||||||
|
} else {
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// check if module
|
||||||
if (Auth::guard('module')->check()) {
|
if (Auth::guard('module')->check()) {
|
||||||
$this->module = Auth::guard('module')->user();
|
$this->module = Auth::guard('module')->user();
|
||||||
} else {
|
|
||||||
$this->module = null;
|
if (isset($this->data['event'])) {
|
||||||
|
$this->event = $this->module->id . '.' . $this->data['event'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// log
|
||||||
|
if (config('app.env') != 'production') {
|
||||||
|
Log::debug('Users Event', [
|
||||||
|
'user' => $this->user->id,
|
||||||
|
'type' => $this->type,
|
||||||
|
'data' => $this->data,
|
||||||
|
'module' => $this->module,
|
||||||
|
'event' => $this->event,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +79,6 @@ public function broadcastOn(): PrivateChannel
|
|||||||
|
|
||||||
public function broadcastAs(): string
|
public function broadcastAs(): string
|
||||||
{
|
{
|
||||||
return 'common';
|
return 'messages';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Modules;
|
namespace App\Http\Controllers\Modules;
|
||||||
|
|
||||||
use App\Events\Users;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Host;
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Notifications\WebNotification;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
@ -15,7 +14,7 @@ public function broadcast_to_user(Request $request, User $user): JsonResponse
|
|||||||
{
|
{
|
||||||
$this->validate($request, $this->rules());
|
$this->validate($request, $this->rules());
|
||||||
|
|
||||||
broadcast(new Users($user, $request->input('type'), $request->all()));
|
$user->notify(new WebNotification($request->all(), $request->input('type')));
|
||||||
|
|
||||||
return $this->created("message sent.");
|
return $this->created("message sent.");
|
||||||
}
|
}
|
||||||
@ -25,7 +24,7 @@ private function rules(): array
|
|||||||
return [
|
return [
|
||||||
'message' => 'required|string|max:255',
|
'message' => 'required|string|max:255',
|
||||||
'type' => 'required|in:info,error,warning,success',
|
'type' => 'required|in:info,error,warning,success',
|
||||||
'data' => 'nullable|json',
|
'event' => 'nullable|alpha',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use App\Events\Users;
|
use App\Events\Users;
|
||||||
use App\Jobs\Module\HostJob;
|
use App\Jobs\Module\HostJob;
|
||||||
|
use App\Notifications\WebNotification;
|
||||||
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@ -51,10 +52,13 @@ protected static function boot()
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
static::created(function ($model) {
|
static::created(function (self $model) {
|
||||||
broadcast(new Users($model->user_id, 'hosts.created', $model));
|
// broadcast(new Users($model->user, 'success', $model));
|
||||||
|
$model->user->notify(new WebNotification($model, 'hosts.created'));
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
static::updating(function ($model) {
|
static::updating(function ($model) {
|
||||||
if ($model->isDirty('status')) {
|
if ($model->isDirty('status')) {
|
||||||
if ($model->status == 'suspended') {
|
if ($model->status == 'suspended') {
|
||||||
|
@ -80,4 +80,10 @@ public function scopeBirthday()
|
|||||||
return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month)
|
return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month)
|
||||||
->whereDay('birthday_at', now()->day)->whereNull('banned_at');
|
->whereDay('birthday_at', now()->day)->whereNull('banned_at');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function selectPublic(): User
|
||||||
|
{
|
||||||
|
// 过滤掉私有字段
|
||||||
|
return $this->select(['id', 'name', 'email_md5', 'created_at']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Notifications\WorkOrder as WorkOrderNotification;
|
use App\Notifications\WorkOrder as WorkOrderNotification;
|
||||||
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
||||||
use Illuminate\Broadcasting\PrivateChannel;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
@ -149,9 +148,4 @@ public function routeNotificationForMail(WorkOrderNotification $work_order): arr
|
|||||||
|
|
||||||
return [$user->email => $user->name];
|
return [$user->email => $user->name];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function receivesBroadcastNotificationsOn(): string
|
|
||||||
{
|
|
||||||
return new PrivateChannel('users.' . $this->user_id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Notifications\Notification;
|
use Illuminate\Notifications\Notification;
|
||||||
|
|
||||||
class CommonChannel extends Notification
|
class WebChannel extends Notification
|
||||||
{
|
{
|
||||||
use Queueable;
|
use Queueable;
|
||||||
|
|
||||||
@ -32,10 +32,9 @@ public function send(mixed $notifiable, Notification $notification): void
|
|||||||
$user = User::find($user_id);
|
$user = User::find($user_id);
|
||||||
|
|
||||||
if (!in_array($data['type'] ?? '', ['info', 'success', 'warning', 'error'])) {
|
if (!in_array($data['type'] ?? '', ['info', 'success', 'warning', 'error'])) {
|
||||||
return;
|
$data['type'] = 'info';
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast(new Users($user, $data['type'], $data));
|
broadcast(new Users($user, $data['type'], $data));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
51
app/Notifications/WebNotification.php
Normal file
51
app/Notifications/WebNotification.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Notifications\Notification;
|
||||||
|
|
||||||
|
class WebNotification extends Notification
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
|
||||||
|
public array|Model $message = [];
|
||||||
|
public string $type = 'info';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new notification instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(array|Model $message, string $type)
|
||||||
|
{
|
||||||
|
if ($message instanceof Model) {
|
||||||
|
$message = $message->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->message = $message;
|
||||||
|
|
||||||
|
$this->type = $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the notification's delivery channels.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function via(): array
|
||||||
|
{
|
||||||
|
return [WebChannel::class];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the array representation of the notification.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
}
|
@ -33,7 +33,7 @@ public function __construct(WorkOrderModel $work_order)
|
|||||||
*/
|
*/
|
||||||
public function via(WorkOrderModel $workOrder): array
|
public function via(WorkOrderModel $workOrder): array
|
||||||
{
|
{
|
||||||
$methods = [WeComChannel::class, CommonChannel::class];
|
$methods = [WeComChannel::class, WebChannel::class];
|
||||||
|
|
||||||
if (in_array($workOrder->status, ['processing', 'replied'])) {
|
if (in_array($workOrder->status, ['processing', 'replied'])) {
|
||||||
$methods[] = 'mail';
|
$methods[] = 'mail';
|
||||||
@ -69,8 +69,12 @@ public function toMail(WorkOrderModel $workOrder): MailMessage
|
|||||||
public function toArray(WorkOrderModel $workOrder): array
|
public function toArray(WorkOrderModel $workOrder): array
|
||||||
{
|
{
|
||||||
$array = $workOrder->toArray();
|
$array = $workOrder->toArray();
|
||||||
$array['type'] = 'info';
|
|
||||||
$array['title'] = '工单: ' . $workOrder->title . ' 状态更新。';
|
$array['event'] = 'WorkOrder.updated';
|
||||||
|
|
||||||
|
if ($workOrder->status === 'replied') {
|
||||||
|
$array['event'] = 'WorkOrder.replied';
|
||||||
|
}
|
||||||
|
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// use App\Models\Task;
|
||||||
use Illuminate\Support\Facades\Broadcast;
|
use Illuminate\Support\Facades\Broadcast;
|
||||||
|
|
||||||
Broadcast::channel('users.{userId}', function ($user, $userId) {
|
Broadcast::channel('users.{userId}', function ($user, $userId) {
|
||||||
return (int)$user->id === (int)$userId;
|
return (int)$user->id === (int)$userId;
|
||||||
});
|
});
|
||||||
|
|
||||||
Broadcast::channel('tasks.{userId}', function ($user, $userId) {
|
// Broadcast::channel('tasks.{task}', function ($user, Task $task) {
|
||||||
return (int)$user->id === (int)$userId;
|
// return (int)$user->id === (int)$task->user_id;
|
||||||
});
|
// });
|
||||||
|
|
||||||
Broadcast::channel('work-orders.{uuid}', function () {
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
Broadcast::channel('servers', function () {
|
Broadcast::channel('servers', function () {
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user